33//! Implements BIP32, BIP39, and BIP44 standards for hierarchical deterministic key derivation
44
55use crate :: error:: WasmSdkError ;
6- use crate :: queries:: utils:: deserialize_required_query;
6+ use crate :: queries:: utils:: { deserialize_query_with_default , deserialize_required_query} ;
77use crate :: sdk:: WasmSdk ;
88use bip39:: { Language , Mnemonic } ;
99use dash_sdk:: dpp:: dashcore;
@@ -18,6 +18,19 @@ use std::str::FromStr;
1818use wasm_bindgen:: prelude:: * ;
1919
2020// TypeScript option bags (module scope) for wallet derivation helpers
21+ #[ wasm_bindgen( typescript_custom_section) ]
22+ const GENERATE_MNEMONIC_PARAMS_TS : & ' static str = r#"
23+ export interface GenerateMnemonicParams {
24+ wordCount?: number;
25+ languageCode?: string;
26+ }
27+ "# ;
28+ #[ wasm_bindgen]
29+ extern "C" {
30+ #[ wasm_bindgen( typescript_type = "GenerateMnemonicParams" ) ]
31+ pub type GenerateMnemonicParamsJs ;
32+ }
33+
2134#[ wasm_bindgen( typescript_custom_section) ]
2235const DERIVE_FROM_SEED_PHRASE_OPTS_TS : & ' static str = r#"
2336export interface DeriveKeyFromSeedPhraseParams {
@@ -48,6 +61,15 @@ extern "C" {
4861}
4962
5063// Inputs parsed from options (module scope)
64+ #[ derive( Default , serde:: Deserialize ) ]
65+ #[ serde( rename_all = "camelCase" ) ]
66+ struct GenerateMnemonicInput {
67+ #[ serde( default ) ]
68+ word_count : Option < u32 > ,
69+ #[ serde( default ) ]
70+ language_code : Option < String > ,
71+ }
72+
5173#[ derive( serde:: Deserialize ) ]
5274#[ serde( rename_all = "camelCase" ) ]
5375struct DeriveFromSeedPhraseInput {
@@ -197,9 +219,16 @@ impl WasmSdk {
197219 /// Generate a new mnemonic phrase
198220 #[ wasm_bindgen( js_name = "generateMnemonic" ) ]
199221 pub fn generate_mnemonic (
200- #[ wasm_bindgen( js_name = "wordCount" ) ] word_count : Option < u32 > ,
201- #[ wasm_bindgen( js_name = "languageCode" ) ] language_code : Option < String > ,
222+ params : Option < GenerateMnemonicParamsJs > ,
202223 ) -> Result < String , WasmSdkError > {
224+ let GenerateMnemonicInput {
225+ word_count,
226+ language_code,
227+ } = deserialize_query_with_default (
228+ params,
229+ "generateMnemonic options" ,
230+ ) ?;
231+
203232 let words = word_count. unwrap_or ( 12 ) ;
204233
205234 // Validate word count and calculate entropy bytes
@@ -295,7 +324,7 @@ impl WasmSdk {
295324 /// Derive a key from mnemonic phrase using BIP39/BIP44
296325 #[ wasm_bindgen( js_name = "deriveKeyFromSeedPhrase" ) ]
297326 pub fn derive_key_from_seed_phrase (
298- # [ wasm_bindgen ( unchecked_param_type = "DeriveKeyFromSeedPhraseParams" ) ] params : JsValue ,
327+ params : DeriveKeyFromSeedPhraseParamsJs ,
299328 ) -> Result < JsValue , WasmSdkError > {
300329 let DeriveFromSeedPhraseInput {
301330 mnemonic,
@@ -359,7 +388,7 @@ impl WasmSdk {
359388 /// Derive a key from seed phrase with arbitrary path
360389 #[ wasm_bindgen( js_name = "deriveKeyFromSeedWithPath" ) ]
361390 pub fn derive_key_from_seed_with_path (
362- # [ wasm_bindgen ( unchecked_param_type = "DeriveKeyFromSeedWithPathParams" ) ] params : JsValue ,
391+ params : DeriveKeyFromSeedWithPathParamsJs ,
363392 ) -> Result < JsValue , WasmSdkError > {
364393 use dash_sdk:: dpp:: key_wallet:: { DerivationPath , ExtendedPrivKey } ;
365394
0 commit comments