From dbcdbc4650d4bd7f3ef98c464e4026d238d75724 Mon Sep 17 00:00:00 2001 From: Steve Myers Date: Sat, 5 Nov 2022 11:59:10 -0500 Subject: [PATCH] Fix wasm compiler descriptor translate_pk --- src/wasm.rs | 51 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/src/wasm.rs b/src/wasm.rs index 08f674b..0a9e7ad 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -8,8 +8,10 @@ use bitcoin::*; use bdk::blockchain::AnyBlockchain; use bdk::database::AnyDatabase; +use bdk::miniscript::{MiniscriptKey, Translator}; use js_sys::Promise; use regex::Regex; +use std::collections::HashMap; use std::error::Error; use std::ops::Deref; use std::path::PathBuf; @@ -123,6 +125,38 @@ impl WasmWallet { } } +#[cfg(feature = "compiler")] +struct AliasMap { + inner: HashMap, +} + +#[cfg(feature = "compiler")] +impl Translator for AliasMap { + // Provides the translation public keys P -> Q + fn pk(&mut self, pk: &String) -> Result { + self.inner + .get(pk) + .map(|a| a.into_key()) + .ok_or(bdk::Error::Generic("Couldn't map alias".to_string())) // Dummy Err + } + + fn sha256(&mut self, sha256: &String) -> Result { + Ok(sha256.to_string()) + } + + fn hash256(&mut self, hash256: &String) -> Result { + Ok(hash256.to_string()) + } + + fn ripemd160(&mut self, ripemd160: &String) -> Result { + Ok(ripemd160.to_string()) + } + + fn hash160(&mut self, hash160: &String) -> Result { + Ok(hash160.to_string()) + } +} + #[wasm_bindgen] #[cfg(feature = "compiler")] pub fn compile(policy: String, aliases: String, script_type: String) -> Result { @@ -133,10 +167,7 @@ pub fn compile(policy: String, aliases: String, script_type: String) -> Result Result> { use std::collections::HashMap; let aliases: HashMap = serde_json::from_str(&aliases)?; - let aliases: HashMap = aliases - .into_iter() - .map(|(k, v)| (k, v.into_key())) - .collect(); + let mut aliases = AliasMap { inner: aliases }; let policy = Concrete::::from_str(&policy)?; @@ -147,10 +178,8 @@ pub fn compile(policy: String, aliases: String, script_type: String) -> Result return Err(Box::::from("InvalidScriptType")), }; - let descriptor: Result, bdk::Error> = descriptor.translate_pk( - |key| Ok(aliases.get(key).unwrap_or(key).into()), - |key| Ok(aliases.get(key).unwrap_or(key).into()), - ); + let descriptor: Result, bdk::Error> = + descriptor.translate_pk(&mut aliases); let descriptor = descriptor?; Ok(descriptor.to_string().into()) @@ -172,10 +201,10 @@ enum Alias { #[cfg(feature = "compiler")] impl Alias { - fn into_key(self) -> String { + fn into_key(&self) -> String { match self { Alias::GenWif => { - let generated: GeneratedKey = + let generated: GeneratedKey = GeneratableDefaultOptions::generate_default().unwrap(); let mut key = generated.into_key(); @@ -194,7 +223,7 @@ impl Alias { format!("{}{}", xprv, path) } - Alias::Existing { extra } => extra, + Alias::Existing { extra } => extra.to_string(), } } }