|
1 | 1 | // SPDX-License-Identifier: CC0-1.0 |
2 | 2 |
|
3 | 3 | use core::convert::TryInto; |
4 | | -use core::fmt; |
5 | 4 | use core::str::FromStr; |
| 5 | +use core::{fmt, hash}; |
6 | 6 | #[cfg(feature = "std")] |
7 | 7 | use std::error; |
8 | 8 |
|
@@ -212,6 +212,35 @@ impl fmt::Display for Wildcard { |
212 | 212 | } |
213 | 213 | } |
214 | 214 |
|
| 215 | +impl fmt::Display for SinglePub { |
| 216 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 217 | + maybe_fmt_master_id(f, &self.origin)?; |
| 218 | + match self.key { |
| 219 | + SinglePubKey::FullKey(full_key) => full_key.fmt(f), |
| 220 | + SinglePubKey::XOnly(x_only_key) => x_only_key.fmt(f), |
| 221 | + }?; |
| 222 | + Ok(()) |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +impl MiniscriptKey for SinglePub { |
| 227 | + type Sha256 = sha256::Hash; |
| 228 | + type Hash256 = hash256::Hash; |
| 229 | + type Ripemd160 = ripemd160::Hash; |
| 230 | + type Hash160 = hash160::Hash; |
| 231 | + |
| 232 | + fn is_x_only_key(&self) -> bool { matches!(self.key, SinglePubKey::XOnly(_)) } |
| 233 | + |
| 234 | + fn num_der_paths(&self) -> usize { 0 } |
| 235 | + |
| 236 | + fn is_uncompressed(&self) -> bool { |
| 237 | + match self.key { |
| 238 | + SinglePubKey::FullKey(ref key) => key.is_uncompressed(), |
| 239 | + _ => false, |
| 240 | + } |
| 241 | + } |
| 242 | +} |
| 243 | + |
215 | 244 | impl SinglePriv { |
216 | 245 | /// Returns the public key of this key. |
217 | 246 | fn to_public<C: Signing>(&self, secp: &Secp256k1<C>) -> SinglePub { |
@@ -269,6 +298,30 @@ impl DescriptorXKey<bip32::Xpriv> { |
269 | 298 | } |
270 | 299 | } |
271 | 300 |
|
| 301 | +impl<K: InnerXKey> fmt::Display for DescriptorXKey<K> { |
| 302 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 303 | + maybe_fmt_master_id(f, &self.origin)?; |
| 304 | + self.xkey.fmt(f)?; |
| 305 | + fmt_derivation_path(f, &self.derivation_path)?; |
| 306 | + self.wildcard.fmt(f)?; |
| 307 | + Ok(()) |
| 308 | + } |
| 309 | +} |
| 310 | + |
| 311 | +impl<K> MiniscriptKey for DescriptorXKey<K> |
| 312 | +where |
| 313 | + K: InnerXKey + Clone + Ord + Eq + hash::Hash + fmt::Debug, |
| 314 | +{ |
| 315 | + type Sha256 = sha256::Hash; |
| 316 | + type Hash256 = hash256::Hash; |
| 317 | + type Ripemd160 = ripemd160::Hash; |
| 318 | + type Hash160 = hash160::Hash; |
| 319 | + |
| 320 | + fn is_x_only_key(&self) -> bool { false } |
| 321 | + |
| 322 | + fn num_der_paths(&self) -> usize { 1 } |
| 323 | +} |
| 324 | + |
272 | 325 | impl DescriptorMultiXKey<bip32::Xpriv> { |
273 | 326 | /// Returns the public version of this multi-key, applying all the hardened derivation steps that |
274 | 327 | /// are shared among all derivation paths before turning it into a public key. |
@@ -350,6 +403,30 @@ impl DescriptorMultiXKey<bip32::Xpriv> { |
350 | 403 | } |
351 | 404 | } |
352 | 405 |
|
| 406 | +impl<K: InnerXKey> fmt::Display for DescriptorMultiXKey<K> { |
| 407 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 408 | + maybe_fmt_master_id(f, &self.origin)?; |
| 409 | + self.xkey.fmt(f)?; |
| 410 | + fmt_derivation_paths(f, self.derivation_paths.paths())?; |
| 411 | + self.wildcard.fmt(f)?; |
| 412 | + Ok(()) |
| 413 | + } |
| 414 | +} |
| 415 | + |
| 416 | +impl<K> MiniscriptKey for DescriptorMultiXKey<K> |
| 417 | +where |
| 418 | + K: InnerXKey + Clone + Ord + Eq + hash::Hash + fmt::Debug, |
| 419 | +{ |
| 420 | + type Sha256 = sha256::Hash; |
| 421 | + type Hash256 = hash256::Hash; |
| 422 | + type Ripemd160 = ripemd160::Hash; |
| 423 | + type Hash160 = hash160::Hash; |
| 424 | + |
| 425 | + fn is_x_only_key(&self) -> bool { false } |
| 426 | + |
| 427 | + fn num_der_paths(&self) -> usize { self.derivation_paths.paths().len() } |
| 428 | +} |
| 429 | + |
353 | 430 | /// Kinds of malformed key data |
354 | 431 | #[derive(Debug, PartialEq, Eq, Clone)] |
355 | 432 | #[non_exhaustive] |
@@ -531,28 +608,9 @@ impl From<bip32::Error> for XKeyParseError { |
531 | 608 | impl fmt::Display for DescriptorPublicKey { |
532 | 609 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
533 | 610 | match *self { |
534 | | - Self::Single(ref pk) => { |
535 | | - maybe_fmt_master_id(f, &pk.origin)?; |
536 | | - match pk.key { |
537 | | - SinglePubKey::FullKey(full_key) => full_key.fmt(f), |
538 | | - SinglePubKey::XOnly(x_only_key) => x_only_key.fmt(f), |
539 | | - }?; |
540 | | - Ok(()) |
541 | | - } |
542 | | - Self::XPub(ref xpub) => { |
543 | | - maybe_fmt_master_id(f, &xpub.origin)?; |
544 | | - xpub.xkey.fmt(f)?; |
545 | | - fmt_derivation_path(f, &xpub.derivation_path)?; |
546 | | - xpub.wildcard.fmt(f)?; |
547 | | - Ok(()) |
548 | | - } |
549 | | - Self::MultiXPub(ref xpub) => { |
550 | | - maybe_fmt_master_id(f, &xpub.origin)?; |
551 | | - xpub.xkey.fmt(f)?; |
552 | | - fmt_derivation_paths(f, xpub.derivation_paths.paths())?; |
553 | | - xpub.wildcard.fmt(f)?; |
554 | | - Ok(()) |
555 | | - } |
| 611 | + Self::Single(ref pk) => pk.fmt(f), |
| 612 | + Self::XPub(ref xpub) => xpub.fmt(f), |
| 613 | + Self::MultiXPub(ref xpub) => xpub.fmt(f), |
556 | 614 | } |
557 | 615 | } |
558 | 616 | } |
@@ -1292,25 +1350,23 @@ impl MiniscriptKey for DescriptorPublicKey { |
1292 | 1350 |
|
1293 | 1351 | fn is_uncompressed(&self) -> bool { |
1294 | 1352 | match self { |
1295 | | - DescriptorPublicKey::Single(SinglePub { |
1296 | | - key: SinglePubKey::FullKey(ref key), .. |
1297 | | - }) => key.is_uncompressed(), |
| 1353 | + DescriptorPublicKey::Single(key) => key.is_uncompressed(), |
1298 | 1354 | _ => false, |
1299 | 1355 | } |
1300 | 1356 | } |
1301 | 1357 |
|
1302 | 1358 | fn is_x_only_key(&self) -> bool { |
1303 | | - matches!( |
1304 | | - self, |
1305 | | - DescriptorPublicKey::Single(SinglePub { key: SinglePubKey::XOnly(ref _key), .. }) |
1306 | | - ) |
| 1359 | + match self { |
| 1360 | + DescriptorPublicKey::Single(single_pub) => single_pub.is_x_only_key(), |
| 1361 | + _ => false, |
| 1362 | + } |
1307 | 1363 | } |
1308 | 1364 |
|
1309 | 1365 | fn num_der_paths(&self) -> usize { |
1310 | 1366 | match self { |
1311 | | - DescriptorPublicKey::Single(_) => 0, |
1312 | | - DescriptorPublicKey::XPub(_) => 1, |
1313 | | - DescriptorPublicKey::MultiXPub(xpub) => xpub.derivation_paths.paths().len(), |
| 1367 | + DescriptorPublicKey::Single(single) => single.num_der_paths(), |
| 1368 | + DescriptorPublicKey::XPub(xpub) => xpub.num_der_paths(), |
| 1369 | + DescriptorPublicKey::MultiXPub(xpub) => xpub.num_der_paths(), |
1314 | 1370 | } |
1315 | 1371 | } |
1316 | 1372 | } |
|
0 commit comments