|
1 | 1 | use crate::error::WasmUtxoError; |
| 2 | +use crate::wasm::try_from_js_value::get_field; |
2 | 3 | use crate::wasm::try_into_js_value::TryIntoJsValue; |
3 | 4 | use miniscript::bitcoin::{PublicKey, XOnlyPublicKey}; |
| 5 | +use miniscript::miniscript::analyzable::ExtParams; |
4 | 6 | use miniscript::{bitcoin, Legacy, Miniscript, Segwitv0, Tap}; |
5 | 7 | use std::fmt; |
6 | 8 | use std::str::FromStr; |
@@ -86,6 +88,85 @@ impl WrapMiniscript { |
86 | 88 | _ => Err(WasmUtxoError::new("Invalid context type")), |
87 | 89 | } |
88 | 90 | } |
| 91 | + |
| 92 | + #[wasm_bindgen(js_name = fromStringExt, skip_typescript)] |
| 93 | + pub fn from_string_ext( |
| 94 | + script: &str, |
| 95 | + context_type: &str, |
| 96 | + ext_params_config: JsValue, |
| 97 | + ) -> Result<WrapMiniscript, WasmUtxoError> { |
| 98 | + let params = build_ext_params(&ext_params_config)?; |
| 99 | + match context_type { |
| 100 | + "tap" => Ok(WrapMiniscript::from( |
| 101 | + Miniscript::<XOnlyPublicKey, Tap>::from_str_ext(script, ¶ms) |
| 102 | + .map_err(WasmUtxoError::from)?, |
| 103 | + )), |
| 104 | + "segwitv0" => Ok(WrapMiniscript::from( |
| 105 | + Miniscript::<PublicKey, Segwitv0>::from_str_ext(script, ¶ms) |
| 106 | + .map_err(WasmUtxoError::from)?, |
| 107 | + )), |
| 108 | + "legacy" => Ok(WrapMiniscript::from( |
| 109 | + Miniscript::<PublicKey, Legacy>::from_str_ext(script, ¶ms) |
| 110 | + .map_err(WasmUtxoError::from)?, |
| 111 | + )), |
| 112 | + _ => Err(WasmUtxoError::new("Invalid context type")), |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + #[wasm_bindgen(js_name = fromBitcoinScriptExt, skip_typescript)] |
| 117 | + pub fn from_bitcoin_script_ext( |
| 118 | + script: &[u8], |
| 119 | + context_type: &str, |
| 120 | + ext_params_config: JsValue, |
| 121 | + ) -> Result<WrapMiniscript, WasmUtxoError> { |
| 122 | + let params = build_ext_params(&ext_params_config)?; |
| 123 | + let script = bitcoin::Script::from_bytes(script); |
| 124 | + match context_type { |
| 125 | + "tap" => Ok(WrapMiniscript::from( |
| 126 | + Miniscript::<XOnlyPublicKey, Tap>::decode_with_ext(script, ¶ms) |
| 127 | + .map_err(WasmUtxoError::from)?, |
| 128 | + )), |
| 129 | + "segwitv0" => Ok(WrapMiniscript::from( |
| 130 | + Miniscript::<PublicKey, Segwitv0>::decode_with_ext(script, ¶ms) |
| 131 | + .map_err(WasmUtxoError::from)?, |
| 132 | + )), |
| 133 | + "legacy" => Ok(WrapMiniscript::from( |
| 134 | + Miniscript::<PublicKey, Legacy>::decode_with_ext(script, ¶ms) |
| 135 | + .map_err(WasmUtxoError::from)?, |
| 136 | + )), |
| 137 | + _ => Err(WasmUtxoError::new("Invalid context type")), |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +fn build_ext_params(config: &JsValue) -> Result<ExtParams, WasmUtxoError> { |
| 143 | + let flag = |key| -> Result<bool, WasmUtxoError> { |
| 144 | + if config.is_undefined() || config.is_null() { |
| 145 | + return Ok(false); |
| 146 | + } |
| 147 | + Ok(get_field::<Option<bool>>(config, key)?.unwrap_or(false)) |
| 148 | + }; |
| 149 | + |
| 150 | + let mut params = ExtParams::sane().drop(); |
| 151 | + if flag("topUnsafe")? { |
| 152 | + params = params.top_unsafe(); |
| 153 | + } |
| 154 | + if flag("resourceLimitations")? { |
| 155 | + params = params.exceed_resource_limitations(); |
| 156 | + } |
| 157 | + if flag("timelockMixing")? { |
| 158 | + params = params.timelock_mixing(); |
| 159 | + } |
| 160 | + if flag("malleability")? { |
| 161 | + params = params.malleability(); |
| 162 | + } |
| 163 | + if flag("repeatedPk")? { |
| 164 | + params = params.repeated_pk(); |
| 165 | + } |
| 166 | + if flag("rawPkh")? { |
| 167 | + params = params.raw_pkh(); |
| 168 | + } |
| 169 | + Ok(params) |
89 | 170 | } |
90 | 171 |
|
91 | 172 | impl From<Miniscript<XOnlyPublicKey, Tap>> for WrapMiniscript { |
|
0 commit comments