@@ -17,18 +17,23 @@ pub struct CashAddr {
1717 pub script_hash : u32 ,
1818}
1919
20- pub struct Network {
20+ /// This maps to the structure of `utxolib.Network` so that we can use it for address encoding/decoding.
21+ /// We also use it to infer the output script support for the network.
22+ ///
23+ /// We cannot precisely map it to the proper `networks::Network` enum because certain
24+ /// different networks are structurally identical (all bitcoin testnets).
25+ pub struct UtxolibNetwork {
2126 pub pub_key_hash : u32 ,
2227 pub script_hash : u32 ,
2328 pub cash_addr : Option < CashAddr > ,
2429 pub bech32 : Option < String > ,
2530}
2631
27- impl Network {
28- /// Parse a Network object from a JavaScript value
32+ impl UtxolibNetwork {
33+ /// Parse a UtxolibNetwork object from a JavaScript value
2934 pub fn from_js_value ( js_network : & JsValue ) -> Result < Self > {
3035 use crate :: try_from_js_value:: TryFromJsValue ;
31- Network :: try_from_js_value ( js_network)
36+ UtxolibNetwork :: try_from_js_value ( js_network)
3237 . map_err ( |e| AddressError :: InvalidAddress ( e. to_string ( ) ) )
3338 }
3439 pub fn output_script_support ( & self ) -> OutputScriptSupport {
@@ -46,10 +51,10 @@ impl Network {
4651 }
4752}
4853
49- /// Convert output script to address string using a utxolib Network object
54+ /// Convert output script to address string using a utxolib UtxolibNetwork object
5055pub fn from_output_script_with_network (
5156 script : & Script ,
52- network : & Network ,
57+ network : & UtxolibNetwork ,
5358 format : AddressFormat ,
5459) -> Result < String > {
5560 network. output_script_support ( ) . assert_support ( script) ?;
@@ -94,8 +99,8 @@ pub fn from_output_script_with_network(
9499 }
95100}
96101
97- /// Convert address string to output script using a utxolib Network object
98- pub fn to_output_script_with_network ( address : & str , network : & Network ) -> Result < ScriptBuf > {
102+ /// Convert address string to output script using a utxolib UtxolibNetwork object
103+ pub fn to_output_script_with_network ( address : & str , network : & UtxolibNetwork ) -> Result < ScriptBuf > {
99104 use crate :: address:: AddressCodec ;
100105 use crate :: bitcoin:: hashes:: Hash ;
101106 use crate :: bitcoin:: { PubkeyHash , ScriptHash } ;
@@ -148,16 +153,16 @@ impl UtxolibCompatNamespace {
148153 ///
149154 /// # Arguments
150155 /// * `script` - The output script as a byte array
151- /// * `network` - The utxolib Network object from JavaScript
156+ /// * `network` - The UtxolibNetwork object from JavaScript
152157 /// * `format` - Optional address format: "default" or "cashaddr" (only applicable for Bitcoin Cash and eCash)
153158 #[ wasm_bindgen]
154159 pub fn from_output_script (
155160 script : & [ u8 ] ,
156161 network : JsValue ,
157162 format : Option < String > ,
158163 ) -> std:: result:: Result < String , JsValue > {
159- let network =
160- Network :: from_js_value ( & network ) . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?;
164+ let network = UtxolibNetwork :: from_js_value ( & network )
165+ . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?;
161166
162167 let script_obj = Script :: from_bytes ( script) ;
163168
@@ -173,16 +178,16 @@ impl UtxolibCompatNamespace {
173178 ///
174179 /// # Arguments
175180 /// * `address` - The address string
176- /// * `network` - The utxolib Network object from JavaScript
181+ /// * `network` - The UtxolibNetwork object from JavaScript
177182 /// * `format` - Optional address format (currently unused for decoding as all formats are accepted)
178183 #[ wasm_bindgen]
179184 pub fn to_output_script (
180185 address : & str ,
181186 network : JsValue ,
182187 format : Option < String > ,
183188 ) -> std:: result:: Result < Vec < u8 > , JsValue > {
184- let network =
185- Network :: from_js_value ( & network ) . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?;
189+ let network = UtxolibNetwork :: from_js_value ( & network )
190+ . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?;
186191
187192 // Validate format parameter even though we don't use it for decoding
188193 if let Some ( fmt) = format {
0 commit comments