From 891e99ede0ef916b04b1770fef783b7cc48a2732 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Wed, 1 Oct 2025 15:17:12 +0700 Subject: [PATCH] fix(sdk): wasm is not initialized for some methods --- packages/js-evo-sdk/src/dpns/facade.ts | 9 ++- packages/js-evo-sdk/src/tokens/facade.ts | 3 +- packages/js-evo-sdk/src/wallet/functions.ts | 66 ++++++++++++------- packages/js-evo-sdk/src/wasm.ts | 1 + .../tests/functional/tokens.spec.mjs | 4 +- .../tests/functional/wallet.spec.mjs | 24 +++---- .../tests/unit/facades/dpns.spec.mjs | 12 ++-- .../tests/unit/facades/tokens.spec.mjs | 4 +- 8 files changed, 75 insertions(+), 48 deletions(-) diff --git a/packages/js-evo-sdk/src/dpns/facade.ts b/packages/js-evo-sdk/src/dpns/facade.ts index a4bcf83038b..166efc7d145 100644 --- a/packages/js-evo-sdk/src/dpns/facade.ts +++ b/packages/js-evo-sdk/src/dpns/facade.ts @@ -8,15 +8,18 @@ export class DpnsFacade { this.sdk = sdk; } - convertToHomographSafe(input: string): string { + async convertToHomographSafe(input: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.dpnsConvertToHomographSafe(input); } - isValidUsername(label: string): boolean { + async isValidUsername(label: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.dpnsIsValidUsername(label); } - isContestedUsername(label: string): boolean { + async isContestedUsername(label: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.dpnsIsContestedUsername(label); } diff --git a/packages/js-evo-sdk/src/tokens/facade.ts b/packages/js-evo-sdk/src/tokens/facade.ts index 48152f05276..1cecb8b1f5b 100644 --- a/packages/js-evo-sdk/src/tokens/facade.ts +++ b/packages/js-evo-sdk/src/tokens/facade.ts @@ -9,7 +9,8 @@ export class TokensFacade { this.sdk = sdk; } - calculateId(contractId: string, tokenPosition: number): string { + async calculateId(contractId: string, tokenPosition: number): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.calculateTokenIdFromContract(contractId, tokenPosition); } diff --git a/packages/js-evo-sdk/src/wallet/functions.ts b/packages/js-evo-sdk/src/wallet/functions.ts index 935cba0bc66..40359c60a6d 100644 --- a/packages/js-evo-sdk/src/wallet/functions.ts +++ b/packages/js-evo-sdk/src/wallet/functions.ts @@ -1,31 +1,38 @@ import * as wasm from '../wasm.js'; export namespace wallet { - export function generateMnemonic(wordCount?: number, languageCode?: string): string { + export async function generateMnemonic(wordCount?: number, languageCode?: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.generateMnemonic(wordCount ?? null, languageCode ?? null); } - export function validateMnemonic(mnemonic: string, languageCode?: string): boolean { + export async function validateMnemonic(mnemonic: string, languageCode?: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.validateMnemonic(mnemonic, languageCode ?? null); } - export function mnemonicToSeed(mnemonic: string, passphrase?: string): Uint8Array { + export async function mnemonicToSeed(mnemonic: string, passphrase?: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.mnemonicToSeed(mnemonic, passphrase ?? null); } - export function deriveKeyFromSeedPhrase(mnemonic: string, passphrase: string | null | undefined, network: string): any { + export async function deriveKeyFromSeedPhrase(mnemonic: string, passphrase: string | null | undefined, network: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.deriveKeyFromSeedPhrase(mnemonic, passphrase ?? null, network); } - export function deriveKeyFromSeedWithPath(mnemonic: string, passphrase: string | null | undefined, path: string, network: string): any { + export async function deriveKeyFromSeedWithPath(mnemonic: string, passphrase: string | null | undefined, path: string, network: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.deriveKeyFromSeedWithPath(mnemonic, passphrase ?? null, path, network); } - export function deriveKeyFromSeedWithExtendedPath(mnemonic: string, passphrase: string | null | undefined, path: string, network: string): any { + export async function deriveKeyFromSeedWithExtendedPath(mnemonic: string, passphrase: string | null | undefined, path: string, network: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.deriveKeyFromSeedWithExtendedPath(mnemonic, passphrase ?? null, path, network); } - export function deriveDashpayContactKey(mnemonic: string, passphrase: string | null | undefined, senderIdentityId: string, receiverIdentityId: string, account: number, addressIndex: number, network: string): any { + export async function deriveDashpayContactKey(mnemonic: string, passphrase: string | null | undefined, senderIdentityId: string, receiverIdentityId: string, account: number, addressIndex: number, network: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.deriveDashpayContactKey( mnemonic, passphrase ?? null, @@ -37,63 +44,78 @@ export namespace wallet { ); } - export function derivationPathBip44Mainnet(account: number, change: number, index: number): any { + export async function derivationPathBip44Mainnet(account: number, change: number, index: number): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.derivationPathBip44Mainnet(account, change, index); } - export function derivationPathBip44Testnet(account: number, change: number, index: number): any { + export async function derivationPathBip44Testnet(account: number, change: number, index: number): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.derivationPathBip44Testnet(account, change, index); } - export function derivationPathDip9Mainnet(featureType: number, account: number, index: number): any { + export async function derivationPathDip9Mainnet(featureType: number, account: number, index: number): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.derivationPathDip9Mainnet(featureType, account, index); } - export function derivationPathDip9Testnet(featureType: number, account: number, index: number): any { + export async function derivationPathDip9Testnet(featureType: number, account: number, index: number): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.derivationPathDip9Testnet(featureType, account, index); } - export function derivationPathDip13Mainnet(account: number): any { + export async function derivationPathDip13Mainnet(account: number): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.derivationPathDip13Mainnet(account); } - export function derivationPathDip13Testnet(account: number): any { + export async function derivationPathDip13Testnet(account: number): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.derivationPathDip13Testnet(account); } - export function deriveChildPublicKey(xpub: string, index: number, hardened: boolean): string { + export async function deriveChildPublicKey(xpub: string, index: number, hardened: boolean): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.deriveChildPublicKey(xpub, index, hardened); } - export function xprvToXpub(xprv: string): string { + export async function xprvToXpub(xprv: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.xprvToXpub(xprv); } - export function generateKeyPair(network: string): any { + export async function generateKeyPair(network: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.generateKeyPair(network); } - export function generateKeyPairs(network: string, count: number): any[] { + export async function generateKeyPairs(network: string, count: number): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.generateKeyPairs(network, count); } - export function keyPairFromWif(privateKeyWif: string): any { + export async function keyPairFromWif(privateKeyWif: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.keyPairFromWif(privateKeyWif); } - export function keyPairFromHex(privateKeyHex: string, network: string): any { + export async function keyPairFromHex(privateKeyHex: string, network: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.keyPairFromHex(privateKeyHex, network); } - export function pubkeyToAddress(pubkeyHex: string, network: string): string { + export async function pubkeyToAddress(pubkeyHex: string, network: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.pubkeyToAddress(pubkeyHex, network); } - export function validateAddress(address: string, network: string): boolean { + export async function validateAddress(address: string, network: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.validateAddress(address, network); } - export function signMessage(message: string, privateKeyWif: string): string { + export async function signMessage(message: string, privateKeyWif: string): Promise { + await wasm.ensureInitialized(); return wasm.WasmSdk.signMessage(message, privateKeyWif); } } diff --git a/packages/js-evo-sdk/src/wasm.ts b/packages/js-evo-sdk/src/wasm.ts index f8d6272de67..f2f0fbb62d6 100644 --- a/packages/js-evo-sdk/src/wasm.ts +++ b/packages/js-evo-sdk/src/wasm.ts @@ -13,3 +13,4 @@ export async function ensureInitialized(): Promise { // Re-export all wasm SDK symbols for convenience export * from '@dashevo/wasm-sdk/compressed'; export { default } from '@dashevo/wasm-sdk/compressed'; +export type { DataContract } from '@dashevo/wasm-sdk/compressed'; diff --git a/packages/js-evo-sdk/tests/functional/tokens.spec.mjs b/packages/js-evo-sdk/tests/functional/tokens.spec.mjs index 68ae4e952dd..88b499330bd 100644 --- a/packages/js-evo-sdk/tests/functional/tokens.spec.mjs +++ b/packages/js-evo-sdk/tests/functional/tokens.spec.mjs @@ -10,8 +10,8 @@ describe('Tokens', function tokensSuite() { await sdk.connect(); }); - it('calculateId() derives token ID from contract', () => { - const id = sdk.tokens.calculateId(TEST_IDS.tokenContractId, 0); + it('calculateId() derives token ID from contract', async () => { + const id = await sdk.tokens.calculateId(TEST_IDS.tokenContractId, 0); expect(id).to.equal(TEST_IDS.tokenId); }); diff --git a/packages/js-evo-sdk/tests/functional/wallet.spec.mjs b/packages/js-evo-sdk/tests/functional/wallet.spec.mjs index 3d9a5fcb180..776b9712ad7 100644 --- a/packages/js-evo-sdk/tests/functional/wallet.spec.mjs +++ b/packages/js-evo-sdk/tests/functional/wallet.spec.mjs @@ -1,25 +1,25 @@ import { wallet } from '../../dist/evo-sdk.module.js'; describe('wallet helpers', () => { - it('generateMnemonic() returns phrase and validateMnemonic() succeeds', () => { - const mnemonic = wallet.generateMnemonic(12, 'en'); + it('generateMnemonic() returns phrase and validateMnemonic() succeeds', async () => { + const mnemonic = await wallet.generateMnemonic(12, 'en'); expect(mnemonic).to.be.a('string'); - expect(wallet.validateMnemonic(mnemonic, 'en')).to.equal(true); + expect(await wallet.validateMnemonic(mnemonic, 'en')).to.equal(true); }); - it('mnemonicToSeed() returns Uint8Array and derive functions respond', () => { - const mnemonic = wallet.generateMnemonic(); - const seed = wallet.mnemonicToSeed(mnemonic); + it('mnemonicToSeed() returns Uint8Array and derive functions respond', async () => { + const mnemonic = await wallet.generateMnemonic(); + const seed = await wallet.mnemonicToSeed(mnemonic); expect(seed).to.be.instanceOf(Uint8Array); - expect(wallet.deriveKeyFromSeedPhrase(mnemonic, null, 'testnet')).to.exist(); - expect(wallet.deriveKeyFromSeedWithPath(mnemonic, null, "m/44'/5'/0'", 'testnet')).to.exist(); - expect(wallet.deriveKeyFromSeedWithExtendedPath(mnemonic, null, "m/15'/0'", 'testnet')).to.exist(); + expect(await wallet.deriveKeyFromSeedPhrase(mnemonic, null, 'testnet')).to.exist(); + expect(await wallet.deriveKeyFromSeedWithPath(mnemonic, null, "m/44'/5'/0'", 'testnet')).to.exist(); + expect(await wallet.deriveKeyFromSeedWithExtendedPath(mnemonic, null, "m/15'/0'", 'testnet')).to.exist(); }); - it('key utilities return expected shapes', () => { - const kp = wallet.generateKeyPair('testnet'); + it('key utilities return expected shapes', async () => { + const kp = await wallet.generateKeyPair('testnet'); expect(kp).to.be.an('object'); - const kps = wallet.generateKeyPairs('testnet', 2); + const kps = await wallet.generateKeyPairs('testnet', 2); expect(kps).to.be.an('array'); }); }); diff --git a/packages/js-evo-sdk/tests/unit/facades/dpns.spec.mjs b/packages/js-evo-sdk/tests/unit/facades/dpns.spec.mjs index 4b375bdca1a..a0b2e0da145 100644 --- a/packages/js-evo-sdk/tests/unit/facades/dpns.spec.mjs +++ b/packages/js-evo-sdk/tests/unit/facades/dpns.spec.mjs @@ -22,13 +22,13 @@ describe('DPNSFacade', () => { this.sinon.stub(wasmSdk, 'getDpnsUsernameByNameWithProofInfo').resolves({}); }); - it('convertToHomographSafe/isValidUsername/isContestedUsername use class statics', () => { - const out1 = wasmSDKPackage.WasmSdk.dpnsConvertToHomographSafe('abc'); - const out2 = wasmSDKPackage.WasmSdk.dpnsIsValidUsername('abc'); - const out3 = wasmSDKPackage.WasmSdk.dpnsIsContestedUsername('abc'); + it('convertToHomographSafe/isValidUsername/isContestedUsername await wasm statics', async () => { + const out1 = await client.dpns.convertToHomographSafe('abc'); + const out2 = await client.dpns.isValidUsername('abc'); + const out3 = await client.dpns.isContestedUsername('abc'); expect(out1).to.be.ok(); - expect(typeof out2).to.not.equal('undefined'); - expect(typeof out3).to.not.equal('undefined'); + expect(out2).to.be.a('boolean'); + expect(out3).to.be.a('boolean'); }); it('name resolution and registration forward correctly', async () => { diff --git a/packages/js-evo-sdk/tests/unit/facades/tokens.spec.mjs b/packages/js-evo-sdk/tests/unit/facades/tokens.spec.mjs index 0b535d243b7..0d3303a7a41 100644 --- a/packages/js-evo-sdk/tests/unit/facades/tokens.spec.mjs +++ b/packages/js-evo-sdk/tests/unit/facades/tokens.spec.mjs @@ -45,8 +45,8 @@ describe('TokensFacade', () => { this.sinon.stub(wasmSdk, 'tokenConfigUpdate').resolves('ok'); }); - it('calculateId() uses wasm static helper', () => { - const out = client.tokens.calculateId('Hqyu8WcRwXCTwbNxdga4CN5gsVEGc67wng4TFzceyLUv', 0); + it('calculateId() uses wasm static helper', async () => { + const out = await client.tokens.calculateId('Hqyu8WcRwXCTwbNxdga4CN5gsVEGc67wng4TFzceyLUv', 0); expect(out).to.equal('BpJvvpPiR2obh7ueZixjtYXsmWQdgJhiZtQJWjD7Ruus'); });