1- use dpp:: dashcore:: PrivateKey ;
21use dpp:: dashcore:: hashes:: hex:: FromHex ;
32use dpp:: dashcore:: key:: Secp256k1 ;
43use dpp:: dashcore:: secp256k1:: Message ;
54use dpp:: dashcore:: secp256k1:: hashes:: hex:: { Case , DisplayHex } ;
65use dpp:: dashcore:: signer:: { CompactSignature , double_sha} ;
6+ use dpp:: dashcore:: { Network , PrivateKey , base58} ;
7+ use js_sys:: Uint8Array ;
78use pshenmic_dpp_enums:: network:: NetworkWASM ;
89use pshenmic_dpp_public_key:: PublicKeyWASM ;
10+ use pshenmic_dpp_utils:: { IntoWasm , get_class_type} ;
911use wasm_bindgen:: JsValue ;
1012use wasm_bindgen:: prelude:: wasm_bindgen;
1113
12- #[ derive( Debug ) ]
14+ #[ derive( Debug , Clone ) ]
1315#[ wasm_bindgen( js_name = "PrivateKeyWASM" ) ]
1416pub struct PrivateKeyWASM ( PrivateKey ) ;
1517
@@ -25,6 +27,11 @@ impl PrivateKeyWASM {
2527 "PrivateKeyWASM" . to_string ( )
2628 }
2729
30+ #[ wasm_bindgen( constructor) ]
31+ pub fn new ( value : & JsValue , js_network : & JsValue ) -> Result < PrivateKeyWASM , JsValue > {
32+ PrivateKeyWASM :: from_js_value ( value, js_network)
33+ }
34+
2835 #[ wasm_bindgen( js_name = "fromWIF" ) ]
2936 pub fn from_wif ( wif : & str ) -> Result < Self , JsValue > {
3037 let pk = PrivateKey :: from_wif ( wif) . map_err ( |err| JsValue :: from_str ( & * err. to_string ( ) ) ) ;
@@ -69,6 +76,11 @@ impl PrivateKeyWASM {
6976
7077#[ wasm_bindgen]
7178impl PrivateKeyWASM {
79+ #[ wasm_bindgen( js_name = "getNetwork" ) ]
80+ pub fn get_network ( & self ) -> String {
81+ NetworkWASM :: from ( self . 0 . network ) . into ( )
82+ }
83+
7284 #[ wasm_bindgen( js_name = "WIF" ) ]
7385 pub fn get_wif ( & self ) -> String {
7486 self . 0 . to_wif ( )
@@ -114,3 +126,57 @@ impl PrivateKeyWASM {
114126 Ok ( signature. to_vec ( ) )
115127 }
116128}
129+
130+ impl PrivateKeyWASM {
131+ pub fn network ( & self ) -> Network {
132+ self . 0 . network
133+ }
134+
135+ pub fn from_js_value ( value : & JsValue , js_network : & JsValue ) -> Result < Self , JsValue > {
136+ match value. is_string ( ) {
137+ true => {
138+ let str = value
139+ . as_string ( )
140+ . ok_or ( JsValue :: from_str ( "Invalid string" ) ) ?;
141+
142+ if str. len ( ) == 64 {
143+ // raw hex
144+ if js_network. is_undefined ( ) {
145+ Err ( JsValue :: from_str (
146+ "You must specify a network when pass private key hex as arguments" ,
147+ ) )
148+ } else {
149+ PrivateKeyWASM :: from_hex ( & str, js_network)
150+ }
151+ } else {
152+ // base58 check
153+ let key_base58 = base58:: decode_check ( & str) . map_err ( |err| {
154+ JsValue :: from ( format ! ( "Private Key error read wif ({})" , err) )
155+ } ) ?;
156+
157+ if key_base58. clone ( ) . len ( ) == 33 || key_base58. clone ( ) . len ( ) == 34 {
158+ PrivateKeyWASM :: from_wif ( & str)
159+ } else {
160+ Err ( JsValue :: from ( format ! (
161+ "Private key decoded wif must be 38 byte length ({})" ,
162+ key_base58. clone( ) . len( )
163+ ) ) )
164+ }
165+ }
166+ }
167+ false => match value. is_object ( ) || value. is_array ( ) {
168+ true => {
169+ if get_class_type ( & value) == Ok ( "PrivateKeyWASM" . to_string ( ) ) {
170+ Ok ( value. to_wasm :: < PrivateKeyWASM > ( "PrivateKeyWASM" ) ?. clone ( ) )
171+ } else {
172+ let uint8_array = Uint8Array :: from ( value. clone ( ) ) ;
173+ let bytes = uint8_array. to_vec ( ) ;
174+
175+ PrivateKeyWASM :: from_bytes ( bytes, js_network)
176+ }
177+ }
178+ false => Err ( JsValue :: from ( "Cannot parse private key" ) ) ?,
179+ } ,
180+ }
181+ }
182+ }
0 commit comments