Skip to content

Commit 110f75e

Browse files
committed
chore: some fixes
1 parent a26b97d commit 110f75e

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

packages/js-evo-sdk/src/wallet/functions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as wasm from '../wasm.js';
22

33
export namespace wallet {
4-
export async function generateMnemonic(wordCount?: number, languageCode?: string): Promise<string> {
4+
export async function generateMnemonic(params?: wasm.GenerateMnemonicParams): Promise<string> {
55
await wasm.ensureInitialized();
6-
return wasm.WasmSdk.generateMnemonic(wordCount ?? null, languageCode ?? null);
6+
return wasm.WasmSdk.generateMnemonic(params ?? null);
77
}
88

99
export async function validateMnemonic(mnemonic: string, languageCode?: string): Promise<boolean> {

packages/js-evo-sdk/tests/functional/wallet.spec.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { wallet } from '../../dist/evo-sdk.module.js';
22

33
describe('wallet helpers', () => {
44
it('generateMnemonic() returns phrase and validateMnemonic() succeeds', async () => {
5-
const mnemonic = await wallet.generateMnemonic(12, 'en');
5+
const mnemonic = await wallet.generateMnemonic({ wordCount: 12, languageCode: 'en' });
66
expect(mnemonic).to.be.a('string');
77
expect(await wallet.validateMnemonic(mnemonic, 'en')).to.equal(true);
88
});
@@ -12,8 +12,12 @@ describe('wallet helpers', () => {
1212
const seed = await wallet.mnemonicToSeed(mnemonic);
1313
expect(seed).to.be.instanceOf(Uint8Array);
1414
expect(await wallet.deriveKeyFromSeedPhrase({ mnemonic, passphrase: null, network: 'testnet' })).to.exist();
15-
expect(await wallet.deriveKeyFromSeedWithPath({ mnemonic, passphrase: null, path: "m/44'/5'/0'", network: 'testnet' })).to.exist();
16-
expect(await wallet.deriveKeyFromSeedWithExtendedPath({ mnemonic, passphrase: null, path: "m/15'/0'", network: 'testnet' })).to.exist();
15+
expect(await wallet.deriveKeyFromSeedWithPath({
16+
mnemonic, passphrase: null, path: "m/44'/5'/0'", network: 'testnet',
17+
})).to.exist();
18+
expect(await wallet.deriveKeyFromSeedWithExtendedPath({
19+
mnemonic, passphrase: null, path: "m/15'/0'", network: 'testnet',
20+
})).to.exist();
1721
});
1822

1923
it('key utilities return expected shapes', async () => {

packages/wasm-sdk/src/wallet/extended_derivation.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,11 @@ impl WasmSdk {
274274
"Options object is required",
275275
"deriveDashpayContactKey options",
276276
)?;
277-
let sender_id_formatted = {
277+
let sender_id_hex = {
278278
let id: dash_sdk::dpp::prelude::Identifier = sender_identity_id.into();
279279
hex::encode(id.as_bytes())
280280
};
281-
let receiver_id_formatted = {
281+
let receiver_id_hex = {
282282
let id: dash_sdk::dpp::prelude::Identifier = receiver_identity_id.into();
283283
hex::encode(id.as_bytes())
284284
};
@@ -294,16 +294,16 @@ impl WasmSdk {
294294
// m / 9' / coin_type' / 15' / account' / sender_id / receiver_id / index
295295
let path = format!(
296296
"m/9'/{}'/{}'/{}'/0x{}/0x{}/{}",
297-
coin_type, 15, account, sender_id_formatted, receiver_id_formatted, address_index
297+
coin_type, 15, account, sender_id_hex, receiver_id_hex, address_index
298298
);
299299
debug!(target : "wasm_sdk", path = % path, "DashPay contact path");
300300

301301
// Reuse common derivation logic
302302
let common = derive_common_from_mnemonic(&mnemonic, passphrase, &network, &path)?;
303303
Ok(DashpayContactKeyInfoWasm::from_common(
304304
common,
305-
sender_id_formatted,
306-
receiver_id_formatted,
305+
sender_id_hex,
306+
receiver_id_hex,
307307
account,
308308
address_index,
309309
))

packages/wasm-sdk/src/wallet/key_derivation.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Implements BIP32, BIP39, and BIP44 standards for hierarchical deterministic key derivation
44
55
use crate::error::WasmSdkError;
6-
use crate::queries::utils::deserialize_required_query;
6+
use crate::queries::utils::{deserialize_query_with_default, deserialize_required_query};
77
use crate::sdk::WasmSdk;
88
use bip39::{Language, Mnemonic};
99
use dash_sdk::dpp::dashcore;
@@ -18,6 +18,19 @@ use std::str::FromStr;
1818
use 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)]
2235
const DERIVE_FROM_SEED_PHRASE_OPTS_TS: &'static str = r#"
2336
export 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")]
5375
struct 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

packages/wasm-sdk/tests/unit/key-generation.spec.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ describe('Keys and mnemonics', () => {
99

1010
describe('mnemonic', () => {
1111
it('generates 12 and 24 words and validates', () => {
12-
const m12 = sdk.WasmSdk.generateMnemonic(12);
12+
const m12 = sdk.WasmSdk.generateMnemonic({ wordCount: 12 });
1313
expect(m12.split(' ').length).to.equal(12);
1414
expect(sdk.WasmSdk.validateMnemonic(m12)).to.equal(true);
1515

16-
const m24 = sdk.WasmSdk.generateMnemonic(24);
16+
const m24 = sdk.WasmSdk.generateMnemonic({ wordCount: 24 });
1717
expect(m24.split(' ').length).to.equal(24);
1818
expect(sdk.WasmSdk.validateMnemonic(m24)).to.equal(true);
1919
});
2020

2121
it('supports language wordlists', () => {
2222
const langs = ['en', 'es', 'fr', 'it', 'ja', 'ko', 'pt', 'cs'];
2323
for (const lang of langs) {
24-
const m = sdk.WasmSdk.generateMnemonic(12, lang);
24+
const m = sdk.WasmSdk.generateMnemonic({ wordCount: 12, languageCode: lang });
2525
expect(sdk.WasmSdk.validateMnemonic(m, lang)).to.equal(true);
2626
}
2727
});

0 commit comments

Comments
 (0)