-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d589fe9
commit 62f896d
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
//! Support for multi-signature keys | ||
use std::str::FromStr; | ||
use std::{error, fmt}; | ||
|
||
use crate::expression::Tree; | ||
use crate::MiniscriptKey; | ||
|
||
#[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq, Hash)] | ||
/// Enum for representing keys in miniscript | ||
pub enum KeyExpr<Pk: MiniscriptKey> { | ||
/// Single-key (e.g pk(a), here 'a' is a single key) | ||
SingleKey(Pk), | ||
|
||
/// Collection of keys in used for multi-signature | ||
MuSig(Vec<KeyExpr<Pk>>), | ||
} | ||
|
||
#[derive(Debug, PartialEq)] | ||
/// Errors related to KeyExpr | ||
pub enum KeyExprError { | ||
/// Single key is empty string | ||
SingleKeyEmptyError, | ||
|
||
/// Parsing error for single key | ||
SingleKeyParseError, | ||
} | ||
|
||
impl fmt::Display for KeyExprError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match *self { | ||
KeyExprError::SingleKeyEmptyError => f.write_str("str for single key was empty"), | ||
KeyExprError::SingleKeyParseError => f.write_str("not able to parse the single key"), | ||
} | ||
} | ||
} | ||
|
||
impl error::Error for KeyExprError {} | ||
|
||
impl<Pk: MiniscriptKey + FromStr> FromStr for KeyExpr<Pk> { | ||
type Err = KeyExprError; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let (key_tree, _) = Tree::from_slice(s).unwrap(); | ||
fn expr_from_tree<Pk: MiniscriptKey + FromStr>( | ||
tree: &Tree, | ||
) -> Result<KeyExpr<Pk>, KeyExprError> { | ||
if tree.name == "musig" { | ||
let mut key_expr_vect = vec![]; | ||
for sub_tree in tree.args.iter() { | ||
let temp_res = expr_from_tree(sub_tree)?; | ||
key_expr_vect.push(temp_res); | ||
} | ||
Ok(KeyExpr::MuSig(key_expr_vect)) | ||
} else { | ||
if tree.name != "" { | ||
let single_key = match Pk::from_str(tree.name) { | ||
Ok(x) => x, | ||
Err(_) => { | ||
return Err(KeyExprError::SingleKeyParseError); | ||
} | ||
}; | ||
Ok(KeyExpr::SingleKey(single_key)) | ||
} else { | ||
Err(KeyExprError::SingleKeyEmptyError) | ||
} | ||
} | ||
} | ||
expr_from_tree(&key_tree) | ||
} | ||
} | ||
|
||
impl<Pk: MiniscriptKey> fmt::Display for KeyExpr<Pk> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match *self { | ||
KeyExpr::SingleKey(ref pk) => write!(f, "{}", pk), | ||
KeyExpr::MuSig(ref my_vec) => { | ||
write!(f, "musig(")?; | ||
let len = my_vec.len(); | ||
for (index, k) in my_vec.iter().enumerate() { | ||
if index == len - 1 { | ||
write!(f, "{}", k)?; | ||
} else { | ||
write!(f, "{},", k)?; | ||
} | ||
} | ||
f.write_str(")") | ||
} | ||
} | ||
} | ||
} | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_one() { | ||
let dummy_key = "musig(A,B,musig(C,musig(D,E)))"; | ||
let pk = KeyExpr::<String>::from_str(dummy_key).unwrap(); | ||
println!("{}", pk); | ||
assert_eq!(dummy_key, format!("{}", pk)) | ||
} | ||
} |