|
| 1 | +//! Support for multi-signature keys |
| 2 | +use core::fmt; |
| 3 | +use core::str::FromStr; |
| 4 | + |
| 5 | +use crate::expression::{FromTree, Tree}; |
| 6 | +use crate::prelude::*; |
| 7 | +use crate::{Error, MiniscriptKey}; |
| 8 | + |
| 9 | +#[derive(Clone, PartialEq, PartialOrd, Ord, Eq, Hash)] |
| 10 | +/// Enum for representing musig 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 musig-signature |
| 16 | + MuSig(Vec<KeyExpr<Pk>>), |
| 17 | +} |
| 18 | + |
| 19 | +impl<Pk: MiniscriptKey + FromStr> FromTree for KeyExpr<Pk> { |
| 20 | + fn from_tree(tree: &Tree) -> Result<KeyExpr<Pk>, Error> { |
| 21 | + if tree.name == "musig" { |
| 22 | + let mut key_expr_vec = vec![]; |
| 23 | + for sub_tree in tree.args.iter() { |
| 24 | + let temp_res = KeyExpr::<Pk>::from_tree(sub_tree)?; |
| 25 | + key_expr_vec.push(temp_res); |
| 26 | + } |
| 27 | + Ok(KeyExpr::MuSig(key_expr_vec)) |
| 28 | + } else { |
| 29 | + let single_key = Pk::from_str(tree.name).map_err(|_| Error::SingleKeyParseError)?; |
| 30 | + Ok(KeyExpr::SingleKey(single_key)) |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl<Pk: MiniscriptKey + FromStr> FromStr for KeyExpr<Pk> { |
| 36 | + type Err = Error; |
| 37 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 38 | + let (key_tree, _) = Tree::from_slice(s).unwrap(); |
| 39 | + FromTree::from_tree(&key_tree) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl<Pk: MiniscriptKey> fmt::Debug for KeyExpr<Pk> { |
| 44 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 45 | + match *self { |
| 46 | + KeyExpr::SingleKey(ref pk) => write!(f, "{:?}", pk), |
| 47 | + KeyExpr::MuSig(ref my_vec) => { |
| 48 | + write!(f, "musig(")?; |
| 49 | + let len = my_vec.len(); |
| 50 | + for (index, k) in my_vec.iter().enumerate() { |
| 51 | + if index == len - 1 { |
| 52 | + write!(f, "{:?}", k)?; |
| 53 | + } else { |
| 54 | + write!(f, "{:?}", k)?; |
| 55 | + } |
| 56 | + } |
| 57 | + f.write_str(")") |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl<Pk: MiniscriptKey> fmt::Display for KeyExpr<Pk> { |
| 64 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 65 | + match *self { |
| 66 | + KeyExpr::SingleKey(ref pk) => write!(f, "{}", pk), |
| 67 | + KeyExpr::MuSig(ref my_vec) => { |
| 68 | + write!(f, "musig(")?; |
| 69 | + let len = my_vec.len(); |
| 70 | + for (index, k) in my_vec.iter().enumerate() { |
| 71 | + if index == len - 1 { |
| 72 | + write!(f, "{}", k)?; |
| 73 | + } else { |
| 74 | + write!(f, "{},", k)?; |
| 75 | + } |
| 76 | + } |
| 77 | + f.write_str(")") |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +#[cfg(test)] |
| 83 | +mod tests { |
| 84 | + use super::*; |
| 85 | + |
| 86 | + fn test_one(musig_key: &str) { |
| 87 | + let pk = KeyExpr::<String>::from_str(musig_key).unwrap(); |
| 88 | + println!("{}", pk); |
| 89 | + dbg!(&pk); |
| 90 | + assert_eq!(musig_key, format!("{}", pk)) |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn test_from_str_and_fmt() { |
| 95 | + test_one("musig(A,B,musig(C,musig(D,E)))"); |
| 96 | + test_one("musig(A)"); |
| 97 | + test_one("A"); |
| 98 | + test_one("musig(,,)"); |
| 99 | + } |
| 100 | +} |
0 commit comments