Skip to content

Commit 32441bf

Browse files
authored
fix(sdk): wasm is not initialized for some methods (#2792)
1 parent aaea36f commit 32441bf

File tree

8 files changed

+75
-48
lines changed

8 files changed

+75
-48
lines changed

packages/js-evo-sdk/src/dpns/facade.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ export class DpnsFacade {
88
this.sdk = sdk;
99
}
1010

11-
convertToHomographSafe(input: string): string {
11+
async convertToHomographSafe(input: string): Promise<string> {
12+
await wasm.ensureInitialized();
1213
return wasm.WasmSdk.dpnsConvertToHomographSafe(input);
1314
}
1415

15-
isValidUsername(label: string): boolean {
16+
async isValidUsername(label: string): Promise<boolean> {
17+
await wasm.ensureInitialized();
1618
return wasm.WasmSdk.dpnsIsValidUsername(label);
1719
}
1820

19-
isContestedUsername(label: string): boolean {
21+
async isContestedUsername(label: string): Promise<boolean> {
22+
await wasm.ensureInitialized();
2023
return wasm.WasmSdk.dpnsIsContestedUsername(label);
2124
}
2225

packages/js-evo-sdk/src/tokens/facade.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export class TokensFacade {
99
this.sdk = sdk;
1010
}
1111

12-
calculateId(contractId: string, tokenPosition: number): string {
12+
async calculateId(contractId: string, tokenPosition: number): Promise<string> {
13+
await wasm.ensureInitialized();
1314
return wasm.WasmSdk.calculateTokenIdFromContract(contractId, tokenPosition);
1415
}
1516

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

33
export namespace wallet {
4-
export function generateMnemonic(wordCount?: number, languageCode?: string): string {
4+
export async function generateMnemonic(wordCount?: number, languageCode?: string): Promise<string> {
5+
await wasm.ensureInitialized();
56
return wasm.WasmSdk.generateMnemonic(wordCount ?? null, languageCode ?? null);
67
}
78

8-
export function validateMnemonic(mnemonic: string, languageCode?: string): boolean {
9+
export async function validateMnemonic(mnemonic: string, languageCode?: string): Promise<boolean> {
10+
await wasm.ensureInitialized();
911
return wasm.WasmSdk.validateMnemonic(mnemonic, languageCode ?? null);
1012
}
1113

12-
export function mnemonicToSeed(mnemonic: string, passphrase?: string): Uint8Array {
14+
export async function mnemonicToSeed(mnemonic: string, passphrase?: string): Promise<Uint8Array> {
15+
await wasm.ensureInitialized();
1316
return wasm.WasmSdk.mnemonicToSeed(mnemonic, passphrase ?? null);
1417
}
1518

16-
export function deriveKeyFromSeedPhrase(mnemonic: string, passphrase: string | null | undefined, network: string): any {
19+
export async function deriveKeyFromSeedPhrase(mnemonic: string, passphrase: string | null | undefined, network: string): Promise<any> {
20+
await wasm.ensureInitialized();
1721
return wasm.WasmSdk.deriveKeyFromSeedPhrase(mnemonic, passphrase ?? null, network);
1822
}
1923

20-
export function deriveKeyFromSeedWithPath(mnemonic: string, passphrase: string | null | undefined, path: string, network: string): any {
24+
export async function deriveKeyFromSeedWithPath(mnemonic: string, passphrase: string | null | undefined, path: string, network: string): Promise<any> {
25+
await wasm.ensureInitialized();
2126
return wasm.WasmSdk.deriveKeyFromSeedWithPath(mnemonic, passphrase ?? null, path, network);
2227
}
2328

24-
export function deriveKeyFromSeedWithExtendedPath(mnemonic: string, passphrase: string | null | undefined, path: string, network: string): any {
29+
export async function deriveKeyFromSeedWithExtendedPath(mnemonic: string, passphrase: string | null | undefined, path: string, network: string): Promise<any> {
30+
await wasm.ensureInitialized();
2531
return wasm.WasmSdk.deriveKeyFromSeedWithExtendedPath(mnemonic, passphrase ?? null, path, network);
2632
}
2733

28-
export function deriveDashpayContactKey(mnemonic: string, passphrase: string | null | undefined, senderIdentityId: string, receiverIdentityId: string, account: number, addressIndex: number, network: string): any {
34+
export async function deriveDashpayContactKey(mnemonic: string, passphrase: string | null | undefined, senderIdentityId: string, receiverIdentityId: string, account: number, addressIndex: number, network: string): Promise<any> {
35+
await wasm.ensureInitialized();
2936
return wasm.WasmSdk.deriveDashpayContactKey(
3037
mnemonic,
3138
passphrase ?? null,
@@ -37,63 +44,78 @@ export namespace wallet {
3744
);
3845
}
3946

40-
export function derivationPathBip44Mainnet(account: number, change: number, index: number): any {
47+
export async function derivationPathBip44Mainnet(account: number, change: number, index: number): Promise<any> {
48+
await wasm.ensureInitialized();
4149
return wasm.WasmSdk.derivationPathBip44Mainnet(account, change, index);
4250
}
4351

44-
export function derivationPathBip44Testnet(account: number, change: number, index: number): any {
52+
export async function derivationPathBip44Testnet(account: number, change: number, index: number): Promise<any> {
53+
await wasm.ensureInitialized();
4554
return wasm.WasmSdk.derivationPathBip44Testnet(account, change, index);
4655
}
4756

48-
export function derivationPathDip9Mainnet(featureType: number, account: number, index: number): any {
57+
export async function derivationPathDip9Mainnet(featureType: number, account: number, index: number): Promise<any> {
58+
await wasm.ensureInitialized();
4959
return wasm.WasmSdk.derivationPathDip9Mainnet(featureType, account, index);
5060
}
5161

52-
export function derivationPathDip9Testnet(featureType: number, account: number, index: number): any {
62+
export async function derivationPathDip9Testnet(featureType: number, account: number, index: number): Promise<any> {
63+
await wasm.ensureInitialized();
5364
return wasm.WasmSdk.derivationPathDip9Testnet(featureType, account, index);
5465
}
5566

56-
export function derivationPathDip13Mainnet(account: number): any {
67+
export async function derivationPathDip13Mainnet(account: number): Promise<any> {
68+
await wasm.ensureInitialized();
5769
return wasm.WasmSdk.derivationPathDip13Mainnet(account);
5870
}
5971

60-
export function derivationPathDip13Testnet(account: number): any {
72+
export async function derivationPathDip13Testnet(account: number): Promise<any> {
73+
await wasm.ensureInitialized();
6174
return wasm.WasmSdk.derivationPathDip13Testnet(account);
6275
}
6376

64-
export function deriveChildPublicKey(xpub: string, index: number, hardened: boolean): string {
77+
export async function deriveChildPublicKey(xpub: string, index: number, hardened: boolean): Promise<string> {
78+
await wasm.ensureInitialized();
6579
return wasm.WasmSdk.deriveChildPublicKey(xpub, index, hardened);
6680
}
6781

68-
export function xprvToXpub(xprv: string): string {
82+
export async function xprvToXpub(xprv: string): Promise<string> {
83+
await wasm.ensureInitialized();
6984
return wasm.WasmSdk.xprvToXpub(xprv);
7085
}
7186

72-
export function generateKeyPair(network: string): any {
87+
export async function generateKeyPair(network: string): Promise<any> {
88+
await wasm.ensureInitialized();
7389
return wasm.WasmSdk.generateKeyPair(network);
7490
}
7591

76-
export function generateKeyPairs(network: string, count: number): any[] {
92+
export async function generateKeyPairs(network: string, count: number): Promise<any[]> {
93+
await wasm.ensureInitialized();
7794
return wasm.WasmSdk.generateKeyPairs(network, count);
7895
}
7996

80-
export function keyPairFromWif(privateKeyWif: string): any {
97+
export async function keyPairFromWif(privateKeyWif: string): Promise<any> {
98+
await wasm.ensureInitialized();
8199
return wasm.WasmSdk.keyPairFromWif(privateKeyWif);
82100
}
83101

84-
export function keyPairFromHex(privateKeyHex: string, network: string): any {
102+
export async function keyPairFromHex(privateKeyHex: string, network: string): Promise<any> {
103+
await wasm.ensureInitialized();
85104
return wasm.WasmSdk.keyPairFromHex(privateKeyHex, network);
86105
}
87106

88-
export function pubkeyToAddress(pubkeyHex: string, network: string): string {
107+
export async function pubkeyToAddress(pubkeyHex: string, network: string): Promise<string> {
108+
await wasm.ensureInitialized();
89109
return wasm.WasmSdk.pubkeyToAddress(pubkeyHex, network);
90110
}
91111

92-
export function validateAddress(address: string, network: string): boolean {
112+
export async function validateAddress(address: string, network: string): Promise<boolean> {
113+
await wasm.ensureInitialized();
93114
return wasm.WasmSdk.validateAddress(address, network);
94115
}
95116

96-
export function signMessage(message: string, privateKeyWif: string): string {
117+
export async function signMessage(message: string, privateKeyWif: string): Promise<string> {
118+
await wasm.ensureInitialized();
97119
return wasm.WasmSdk.signMessage(message, privateKeyWif);
98120
}
99121
}

packages/js-evo-sdk/src/wasm.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export async function ensureInitialized(): Promise<void> {
1313
// Re-export all wasm SDK symbols for convenience
1414
export * from '@dashevo/wasm-sdk/compressed';
1515
export { default } from '@dashevo/wasm-sdk/compressed';
16+
export type { DataContract } from '@dashevo/wasm-sdk/compressed';

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ describe('Tokens', function tokensSuite() {
1010
await sdk.connect();
1111
});
1212

13-
it('calculateId() derives token ID from contract', () => {
14-
const id = sdk.tokens.calculateId(TEST_IDS.tokenContractId, 0);
13+
it('calculateId() derives token ID from contract', async () => {
14+
const id = await sdk.tokens.calculateId(TEST_IDS.tokenContractId, 0);
1515
expect(id).to.equal(TEST_IDS.tokenId);
1616
});
1717

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

33
describe('wallet helpers', () => {
4-
it('generateMnemonic() returns phrase and validateMnemonic() succeeds', () => {
5-
const mnemonic = wallet.generateMnemonic(12, 'en');
4+
it('generateMnemonic() returns phrase and validateMnemonic() succeeds', async () => {
5+
const mnemonic = await wallet.generateMnemonic(12, 'en');
66
expect(mnemonic).to.be.a('string');
7-
expect(wallet.validateMnemonic(mnemonic, 'en')).to.equal(true);
7+
expect(await wallet.validateMnemonic(mnemonic, 'en')).to.equal(true);
88
});
99

10-
it('mnemonicToSeed() returns Uint8Array and derive functions respond', () => {
11-
const mnemonic = wallet.generateMnemonic();
12-
const seed = wallet.mnemonicToSeed(mnemonic);
10+
it('mnemonicToSeed() returns Uint8Array and derive functions respond', async () => {
11+
const mnemonic = await wallet.generateMnemonic();
12+
const seed = await wallet.mnemonicToSeed(mnemonic);
1313
expect(seed).to.be.instanceOf(Uint8Array);
14-
expect(wallet.deriveKeyFromSeedPhrase(mnemonic, null, 'testnet')).to.exist();
15-
expect(wallet.deriveKeyFromSeedWithPath(mnemonic, null, "m/44'/5'/0'", 'testnet')).to.exist();
16-
expect(wallet.deriveKeyFromSeedWithExtendedPath(mnemonic, null, "m/15'/0'", 'testnet')).to.exist();
14+
expect(await wallet.deriveKeyFromSeedPhrase(mnemonic, null, 'testnet')).to.exist();
15+
expect(await wallet.deriveKeyFromSeedWithPath(mnemonic, null, "m/44'/5'/0'", 'testnet')).to.exist();
16+
expect(await wallet.deriveKeyFromSeedWithExtendedPath(mnemonic, null, "m/15'/0'", 'testnet')).to.exist();
1717
});
1818

19-
it('key utilities return expected shapes', () => {
20-
const kp = wallet.generateKeyPair('testnet');
19+
it('key utilities return expected shapes', async () => {
20+
const kp = await wallet.generateKeyPair('testnet');
2121
expect(kp).to.be.an('object');
22-
const kps = wallet.generateKeyPairs('testnet', 2);
22+
const kps = await wallet.generateKeyPairs('testnet', 2);
2323
expect(kps).to.be.an('array');
2424
});
2525
});

packages/js-evo-sdk/tests/unit/facades/dpns.spec.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ describe('DPNSFacade', () => {
2222
this.sinon.stub(wasmSdk, 'getDpnsUsernameByNameWithProofInfo').resolves({});
2323
});
2424

25-
it('convertToHomographSafe/isValidUsername/isContestedUsername use class statics', () => {
26-
const out1 = wasmSDKPackage.WasmSdk.dpnsConvertToHomographSafe('abc');
27-
const out2 = wasmSDKPackage.WasmSdk.dpnsIsValidUsername('abc');
28-
const out3 = wasmSDKPackage.WasmSdk.dpnsIsContestedUsername('abc');
25+
it('convertToHomographSafe/isValidUsername/isContestedUsername await wasm statics', async () => {
26+
const out1 = await client.dpns.convertToHomographSafe('abc');
27+
const out2 = await client.dpns.isValidUsername('abc');
28+
const out3 = await client.dpns.isContestedUsername('abc');
2929
expect(out1).to.be.ok();
30-
expect(typeof out2).to.not.equal('undefined');
31-
expect(typeof out3).to.not.equal('undefined');
30+
expect(out2).to.be.a('boolean');
31+
expect(out3).to.be.a('boolean');
3232
});
3333

3434
it('name resolution and registration forward correctly', async () => {

packages/js-evo-sdk/tests/unit/facades/tokens.spec.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ describe('TokensFacade', () => {
4545
this.sinon.stub(wasmSdk, 'tokenConfigUpdate').resolves('ok');
4646
});
4747

48-
it('calculateId() uses wasm static helper', () => {
49-
const out = client.tokens.calculateId('Hqyu8WcRwXCTwbNxdga4CN5gsVEGc67wng4TFzceyLUv', 0);
48+
it('calculateId() uses wasm static helper', async () => {
49+
const out = await client.tokens.calculateId('Hqyu8WcRwXCTwbNxdga4CN5gsVEGc67wng4TFzceyLUv', 0);
5050
expect(out).to.equal('BpJvvpPiR2obh7ueZixjtYXsmWQdgJhiZtQJWjD7Ruus');
5151
});
5252

0 commit comments

Comments
 (0)