|
| 1 | +// SPDX-License-Identifier: CC0-1.0 |
| 2 | + |
| 3 | +use core::fmt::{self, Display, Write}; |
| 4 | +use core::str::FromStr; |
| 5 | + |
| 6 | +use super::{DerivPaths, DescriptorKeyParseError, Wildcard}; |
| 7 | +use crate::descriptor::key::{fmt_derivation_paths, parse_xkey_deriv}; |
| 8 | +use crate::descriptor::WalletPolicyError; |
| 9 | +use crate::{BTreeSet, MiniscriptKey, String}; |
| 10 | + |
| 11 | +const RECEIVE_CHANGE_SHORTHAND: &str = "**"; |
| 12 | +const RECEIVE_CHANGE_PATH: &str = "<0;1>/*"; |
| 13 | + |
| 14 | +/// A key expression type based off of the description of KEY and KP in BIP-388. |
| 15 | +/// Used as a `Pk` in `Descriptor<Pk>` |
| 16 | +#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] |
| 17 | +pub struct KeyExpression { |
| 18 | + /// The numeric part of key index (KI) |
| 19 | + pub index: KeyIndex, |
| 20 | + /// The derivation paths of this key |
| 21 | + pub derivation_paths: DerivPaths, |
| 22 | + /// The wildcard value |
| 23 | + pub wildcard: Wildcard, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Debug, Clone, Copy, Hash, PartialOrd, Ord, PartialEq, Eq)] |
| 27 | +pub struct KeyIndex(pub u32); |
| 28 | + |
| 29 | +impl KeyExpression { |
| 30 | + pub fn is_disjoint(&self, other: &KeyExpression) -> bool { |
| 31 | + let lhs: BTreeSet<_> = self |
| 32 | + .derivation_paths |
| 33 | + .paths() |
| 34 | + .iter() |
| 35 | + .flat_map(|p| p.into_iter().copied()) |
| 36 | + .collect(); |
| 37 | + |
| 38 | + !other |
| 39 | + .derivation_paths |
| 40 | + .paths() |
| 41 | + .iter() |
| 42 | + .flat_map(|p| p.into_iter()) |
| 43 | + .any(|cn| lhs.contains(cn)) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl TryFrom<&str> for KeyExpression { |
| 48 | + type Error = DescriptorKeyParseError; |
| 49 | + |
| 50 | + fn try_from(s: &str) -> Result<Self, Self::Error> { |
| 51 | + let path = match s.split_once('/') { |
| 52 | + Some((_placeholder, path)) => path, |
| 53 | + None => return Err(WalletPolicyError::KeyExpressionParseMustHaveDerivPath.into()), |
| 54 | + }; |
| 55 | + if path != RECEIVE_CHANGE_SHORTHAND && !valid_unhardened_derivation_path(path) { |
| 56 | + return Err(WalletPolicyError::KeyExpressionParseInvalidDerivPath.into()); |
| 57 | + } |
| 58 | + let (ki, derivation_paths, wildcard) = |
| 59 | + parse_xkey_deriv(&s.replace(RECEIVE_CHANGE_SHORTHAND, RECEIVE_CHANGE_PATH))?; |
| 60 | + Ok(KeyExpression { |
| 61 | + index: ki, |
| 62 | + derivation_paths: DerivPaths::new(derivation_paths) |
| 63 | + .ok_or(WalletPolicyError::KeyExpressionParseMustHaveDerivPath)?, |
| 64 | + wildcard, |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// Returns true if `path` is a string of the form /<NUM;NUM>/*, for two distinct |
| 70 | +// decimal numbers NUM representing unhardened derivations |
| 71 | +// NOTE: the prefix '/' should be stripped in the caller |
| 72 | +fn valid_unhardened_derivation_path(path: &str) -> bool { |
| 73 | + let (left, right) = match path.split_once(';') { |
| 74 | + Some(pair) => pair, |
| 75 | + None => return false, |
| 76 | + }; |
| 77 | + let left_num = match left.strip_prefix("<") { |
| 78 | + Some(num) => num, |
| 79 | + None => return false, |
| 80 | + }; |
| 81 | + let right_num = match right.strip_suffix(">/*") { |
| 82 | + Some(num) => num, |
| 83 | + None => return false, |
| 84 | + }; |
| 85 | + matches!( |
| 86 | + (left_num.parse::<u32>(), right_num.parse::<u32>()), |
| 87 | + (Ok(a), Ok(b)) if a < b |
| 88 | + ) |
| 89 | +} |
| 90 | + |
| 91 | +impl FromStr for KeyExpression { |
| 92 | + type Err = DescriptorKeyParseError; |
| 93 | + |
| 94 | + fn from_str(s: &str) -> Result<Self, Self::Err> { s.try_into() } |
| 95 | +} |
| 96 | + |
| 97 | +impl Display for KeyExpression { |
| 98 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 99 | + self.index.fmt(f)?; |
| 100 | + let mut path = String::new(); |
| 101 | + fmt_derivation_paths(&mut path, self.derivation_paths.paths())?; |
| 102 | + write!(&mut path, "{}", self.wildcard)?; |
| 103 | + write!(f, "{}", path.replace(RECEIVE_CHANGE_PATH, RECEIVE_CHANGE_SHORTHAND)) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl MiniscriptKey for KeyExpression { |
| 108 | + type Sha256 = String; |
| 109 | + type Hash256 = String; |
| 110 | + type Ripemd160 = String; |
| 111 | + type Hash160 = String; |
| 112 | + |
| 113 | + fn is_x_only_key(&self) -> bool { false } |
| 114 | + fn num_der_paths(&self) -> usize { self.derivation_paths.paths().len() } |
| 115 | +} |
| 116 | + |
| 117 | +impl FromStr for KeyIndex { |
| 118 | + type Err = WalletPolicyError; |
| 119 | + |
| 120 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 121 | + let mut chars = s.chars(); |
| 122 | + match chars.next() { |
| 123 | + Some('@') => { |
| 124 | + let index_str = chars.take_while(char::is_ascii_digit).collect::<String>(); |
| 125 | + let index = index_str |
| 126 | + .parse() |
| 127 | + .map_err(|_| WalletPolicyError::KeyIndexParseInvalidIndex(index_str))?; |
| 128 | + Ok(KeyIndex(index)) |
| 129 | + } |
| 130 | + Some(ch) => Err(WalletPolicyError::KeyIndexParseExpectedAtSign(ch)), |
| 131 | + None => Err(WalletPolicyError::KeyIndexParseInvalidIndex(s.into())), |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl Display for KeyIndex { |
| 137 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "@{}", self.0) } |
| 138 | +} |
| 139 | + |
| 140 | +#[cfg(test)] |
| 141 | +mod tests { |
| 142 | + use super::*; |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn can_test_disjoin_deriv_paths() { |
| 146 | + assert!(!KeyExpression::from_str("@0/<0;1>/*") |
| 147 | + .unwrap() |
| 148 | + .is_disjoint(&KeyExpression::from_str("@0/<1;2>/*").unwrap())); |
| 149 | + } |
| 150 | +} |
0 commit comments