Skip to content

Commit

Permalink
Merge pull request #45 from wizardsardine/refac-ledger-policy-extract
Browse files Browse the repository at this point in the history
Change ledger extract policy helper for clarity
  • Loading branch information
edouardparis authored Sep 4, 2023
2 parents ef6c190 + 9f651c9 commit 417e047
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "async-hwi"
version = "0.0.10"
version = "0.0.11"
edition = "2018"
description = "Async hardware wallet interface"
authors = ["Edouard Paris <[email protected]>"]
Expand Down
25 changes: 11 additions & 14 deletions src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,18 @@ impl<T: Transport + Sync + Send> HWI for Ledger<T> {
pub fn extract_keys_and_template(policy: &str) -> Result<(String, Vec<WalletPubKey>), HWIError> {
let re = Regex::new(r"((\[.+?\])?[xyYzZtuUvV]pub[1-9A-HJ-NP-Za-km-z]{79,108})").unwrap();
let mut descriptor_template = policy.to_string();
let mut pubkeys: Vec<WalletPubKey> = Vec::new();
let mut pubkeys_str: Vec<&str> = Vec::new();
for capture in re.find_iter(policy) {
let pubkey =
WalletPubKey::from_str(capture.as_str()).map_err(|_| HWIError::UnsupportedInput)?;
let pubkey_index = if !pubkeys.contains(&pubkey) {
pubkeys.push(pubkey);
pubkeys.len() - 1
} else {
pubkeys
.iter()
.position(|p| p == &pubkey)
.expect("Just checked it's in.")
};
descriptor_template =
descriptor_template.replace(capture.as_str(), &format!("@{}", pubkey_index));
if !pubkeys_str.contains(&capture.as_str()) {
pubkeys_str.push(capture.as_str());
}
}

let mut pubkeys: Vec<WalletPubKey> = Vec::new();
for (i, key_str) in pubkeys_str.iter().enumerate() {
descriptor_template = descriptor_template.replace(key_str, &format!("@{}", i));
let pubkey = WalletPubKey::from_str(key_str).map_err(|_| HWIError::UnsupportedInput)?;
pubkeys.push(pubkey);
}

// Do not include the hash in the descriptor template.
Expand Down

0 comments on commit 417e047

Please sign in to comment.