Skip to content

Commit b21fb4b

Browse files
committed
Fix wasm compiler descriptor translate_pk
1 parent b1b2a2a commit b21fb4b

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

src/wasm.rs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ use bitcoin::*;
88

99
use bdk::blockchain::AnyBlockchain;
1010
use bdk::database::AnyDatabase;
11+
use bdk::miniscript::{MiniscriptKey, Translator};
1112
use js_sys::Promise;
1213
use regex::Regex;
14+
use std::collections::HashMap;
1315
use std::error::Error;
1416
use std::ops::Deref;
1517
use std::path::PathBuf;
@@ -123,6 +125,38 @@ impl WasmWallet {
123125
}
124126
}
125127

128+
#[cfg(feature = "compiler")]
129+
struct AliasMap {
130+
inner: HashMap<String, Alias>,
131+
}
132+
133+
#[cfg(feature = "compiler")]
134+
impl Translator<String, String, bdk::Error> for AliasMap {
135+
// Provides the translation public keys P -> Q
136+
fn pk(&mut self, pk: &String) -> Result<String, bdk::Error> {
137+
self.inner
138+
.get(pk)
139+
.map(|a| a.into_key())
140+
.ok_or(bdk::Error::Generic("Couldn't map alias".to_string())) // Dummy Err
141+
}
142+
143+
fn sha256(&mut self, sha256: &String) -> Result<String, bdk::Error> {
144+
Ok(sha256.to_string())
145+
}
146+
147+
fn hash256(&mut self, hash256: &String) -> Result<String, bdk::Error> {
148+
Ok(hash256.to_string())
149+
}
150+
151+
fn ripemd160(&mut self, ripemd160: &String) -> Result<String, bdk::Error> {
152+
Ok(ripemd160.to_string())
153+
}
154+
155+
fn hash160(&mut self, hash160: &String) -> Result<String, bdk::Error> {
156+
Ok(hash160.to_string())
157+
}
158+
}
159+
126160
#[wasm_bindgen]
127161
#[cfg(feature = "compiler")]
128162
pub fn compile(policy: String, aliases: String, script_type: String) -> Result<JsValue, String> {
@@ -133,10 +167,7 @@ pub fn compile(policy: String, aliases: String, script_type: String) -> Result<J
133167
) -> Result<String, Box<dyn Error>> {
134168
use std::collections::HashMap;
135169
let aliases: HashMap<String, Alias> = serde_json::from_str(&aliases)?;
136-
let aliases: HashMap<String, String> = aliases
137-
.into_iter()
138-
.map(|(k, v)| (k, v.into_key()))
139-
.collect();
170+
let mut aliases = AliasMap { inner: aliases };
140171

141172
let policy = Concrete::<String>::from_str(&policy)?;
142173

@@ -147,10 +178,8 @@ pub fn compile(policy: String, aliases: String, script_type: String) -> Result<J
147178
_ => return Err(Box::<dyn Error>::from("InvalidScriptType")),
148179
};
149180

150-
let descriptor: Result<Descriptor<String>, bdk::Error> = descriptor.translate_pk(
151-
|key| Ok(aliases.get(key).unwrap_or(key).into()),
152-
|key| Ok(aliases.get(key).unwrap_or(key).into()),
153-
);
181+
let descriptor: Result<Descriptor<String>, bdk::Error> =
182+
descriptor.translate_pk(&mut aliases);
154183
let descriptor = descriptor?;
155184

156185
Ok(descriptor.to_string().into())
@@ -172,10 +201,10 @@ enum Alias {
172201

173202
#[cfg(feature = "compiler")]
174203
impl Alias {
175-
fn into_key(self) -> String {
204+
fn into_key(&self) -> String {
176205
match self {
177206
Alias::GenWif => {
178-
let generated: GeneratedKey<bitcoin::PrivateKey, miniscript::Legacy> =
207+
let generated: GeneratedKey<PrivateKey, miniscript::Legacy> =
179208
GeneratableDefaultOptions::generate_default().unwrap();
180209

181210
let mut key = generated.into_key();
@@ -194,7 +223,7 @@ impl Alias {
194223

195224
format!("{}{}", xprv, path)
196225
}
197-
Alias::Existing { extra } => extra,
226+
Alias::Existing { extra } => extra.to_string(),
198227
}
199228
}
200229
}

0 commit comments

Comments
 (0)