|
| 1 | +//! Support for multi-signature keys |
| 2 | +use std; |
| 3 | +use std::str::FromStr; |
| 4 | +use std::{error, fmt}; |
| 5 | + |
| 6 | +use crate::expression::Tree; |
| 7 | +use crate::MiniscriptKey; |
| 8 | + |
| 9 | +#[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq, Hash)] |
| 10 | +/// Enum for representing keys in miniscript |
| 11 | +pub enum KeyExpr<Pk: MiniscriptKey> { |
| 12 | + /// Single-key (e.g pk(a), here 'a' is a single key) |
| 13 | + SingleKey(Pk), |
| 14 | + |
| 15 | + /// Collection of keys in used for multi-signature |
| 16 | + MuSig(Vec<KeyExpr<Pk>>), |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug, PartialEq)] |
| 20 | +/// Errors related to KeyExpr |
| 21 | +pub enum KeyExprError { |
| 22 | + /// Single key is empty string |
| 23 | + SingleKeyEmptyError, |
| 24 | + |
| 25 | + /// Parsing error for single key |
| 26 | + SingleKeyParseError, |
| 27 | +} |
| 28 | + |
| 29 | +impl fmt::Display for KeyExprError { |
| 30 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 31 | + match *self { |
| 32 | + KeyExprError::SingleKeyEmptyError => f.write_str("str for single key was empty"), |
| 33 | + KeyExprError::SingleKeyParseError => f.write_str("not able to parse the single key"), |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl error::Error for KeyExprError {} |
| 39 | + |
| 40 | +impl<Pk: MiniscriptKey + FromStr> FromStr for KeyExpr<Pk> { |
| 41 | + type Err = KeyExprError; |
| 42 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 43 | + let (key_tree, _) = Tree::from_slice(s).unwrap(); |
| 44 | + fn expr_from_tree<Pk: MiniscriptKey + FromStr>( |
| 45 | + tree: &Tree, |
| 46 | + ) -> Result<KeyExpr<Pk>, KeyExprError> { |
| 47 | + if tree.name == "musig" { |
| 48 | + let mut key_expr_vect = vec![]; |
| 49 | + for sub_tree in tree.args.iter() { |
| 50 | + let temp_res = expr_from_tree(sub_tree)?; |
| 51 | + key_expr_vect.push(temp_res); |
| 52 | + } |
| 53 | + Ok(KeyExpr::MuSig(key_expr_vect)) |
| 54 | + } else { |
| 55 | + if tree.name != "" { |
| 56 | + let single_key = match Pk::from_str(tree.name) { |
| 57 | + Ok(x) => x, |
| 58 | + Err(_) => { |
| 59 | + return Err(KeyExprError::SingleKeyParseError); |
| 60 | + } |
| 61 | + }; |
| 62 | + Ok(KeyExpr::SingleKey(single_key)) |
| 63 | + } else { |
| 64 | + Err(KeyExprError::SingleKeyEmptyError) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + expr_from_tree(&key_tree) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl<Pk: MiniscriptKey> fmt::Display for KeyExpr<Pk> { |
| 73 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 74 | + match *self { |
| 75 | + KeyExpr::SingleKey(ref pk) => write!(f, "{}", pk), |
| 76 | + KeyExpr::MuSig(ref my_vec) => { |
| 77 | + write!(f, "musig(")?; |
| 78 | + let len = my_vec.len(); |
| 79 | + for (index, k) in my_vec.iter().enumerate() { |
| 80 | + if index == len - 1 { |
| 81 | + write!(f, "{}", k)?; |
| 82 | + } else { |
| 83 | + write!(f, "{},", k)?; |
| 84 | + } |
| 85 | + } |
| 86 | + f.write_str(")") |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | +#[cfg(test)] |
| 92 | +mod tests { |
| 93 | + use super::*; |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn test_one() { |
| 97 | + let dummy_key = "musig(A,B,musig(C,musig(D,E)))"; |
| 98 | + let pk = KeyExpr::<String>::from_str(dummy_key).unwrap(); |
| 99 | + println!("{}", pk); |
| 100 | + assert_eq!(dummy_key, format!("{}", pk)) |
| 101 | + } |
| 102 | +} |
0 commit comments