From c474524fc4cc5f2903ccd84a4249bca4e3a4a45d Mon Sep 17 00:00:00 2001 From: Marzooqa Kather Date: Tue, 28 Apr 2026 10:35:08 +0000 Subject: [PATCH] fix(sdk-lib-mpc): use @bitgo/wasm-mps/web in browser for EdDSA DKG MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In browsers (webpack), importing @bitgo/wasm-mps resolves to the --target bundler ESM build which has a race condition between webpack's async WASM instantiation and __wbg_set_wasm being called. This causes wasm.ed25519_dkg_round0_process to be undefined when initDkg() runs. Mirror the ECDSA pattern in ecdsa-dkls/dkg.ts: detect browser via `typeof window !== 'undefined'` (excluding Electron via window.process checks), then use @bitgo/wasm-mps/web which exposes an explicit init() function — guaranteed ready after await. Adds a local .d.ts shim for @bitgo/wasm-mps/web until WCI-250 publishes the ./web subpath export with its own types. Ticket: WCI-251 --- modules/sdk-lib-mpc/package.json | 2 +- modules/sdk-lib-mpc/src/tss/eddsa-mps/dkg.ts | 20 ++++++++++++++++++-- webpack/bitgojs.config.js | 1 + yarn.lock | 8 ++++---- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/modules/sdk-lib-mpc/package.json b/modules/sdk-lib-mpc/package.json index 6bf73ee07b..5fd84ba37d 100644 --- a/modules/sdk-lib-mpc/package.json +++ b/modules/sdk-lib-mpc/package.json @@ -36,7 +36,7 @@ ] }, "dependencies": { - "@bitgo/wasm-mps": "1.7.0", + "@bitgo/wasm-mps": "1.8.1", "@noble/curves": "1.8.1", "@silencelaboratories/dkls-wasm-ll-node": "1.2.0-pre.4", "@silencelaboratories/dkls-wasm-ll-web": "1.2.0-pre.4", diff --git a/modules/sdk-lib-mpc/src/tss/eddsa-mps/dkg.ts b/modules/sdk-lib-mpc/src/tss/eddsa-mps/dkg.ts index 6845f78a6e..d59e047235 100644 --- a/modules/sdk-lib-mpc/src/tss/eddsa-mps/dkg.ts +++ b/modules/sdk-lib-mpc/src/tss/eddsa-mps/dkg.ts @@ -3,7 +3,9 @@ import { encode } from 'cbor-x'; import crypto from 'crypto'; import { DeserializedMessage, DeserializedMessages, DkgState, EddsaReducedKeyShare } from './types'; -type WasmMps = typeof import('@bitgo/wasm-mps'); +type NodeWasmer = typeof import('@bitgo/wasm-mps'); +type WebWasmer = typeof import('@bitgo/wasm-mps/web'); +type WasmMps = NodeWasmer | WebWasmer; /** * EdDSA Distributed Key Generation (DKG) implementation using @bitgo/wasm-mps. @@ -53,7 +55,21 @@ export class DKG { private async loadWasmMps(): Promise { if (!this.wasmMps) { - this.wasmMps = await import('@bitgo/wasm-mps'); + if ( + typeof window !== 'undefined' && + /* checks for electron processes */ + !window.process && + !window.process?.['type'] + ) { + // Browser: web build has explicit init() — guaranteed ready after await + // eslint-disable-next-line import/no-internal-modules -- @bitgo/wasm-mps exposes environment-specific subpath exports. + const webWasm = await import('@bitgo/wasm-mps/web'); + await webWasm.default(); + this.wasmMps = webWasm; + } else { + // Node.js: dynamic import() rewritten to require() by tsc → CJS build → readFileSync + this.wasmMps = await import('@bitgo/wasm-mps'); + } } } diff --git a/webpack/bitgojs.config.js b/webpack/bitgojs.config.js index 94fbfec112..b40af33464 100644 --- a/webpack/bitgojs.config.js +++ b/webpack/bitgojs.config.js @@ -22,6 +22,7 @@ module.exports = { '@bitgo/wasm-ton': path.resolve('../../node_modules/@bitgo/wasm-ton/dist/esm/js/index.js'), '@bitgo/wasm-utxo': path.resolve('../../node_modules/@bitgo/wasm-utxo/dist/esm/js/index.js'), '@bitgo/wasm-solana': path.resolve('../../node_modules/@bitgo/wasm-solana/dist/esm/js/index.js'), + '@bitgo/wasm-mps/web': path.resolve('../../node_modules/@bitgo/wasm-mps/dist/web/js/wasm/wasm_mps.js'), '@bitgo/wasm-mps': path.resolve('../../node_modules/@bitgo/wasm-mps/dist/esm/js/wasm/wasm_mps.js'), '@bitgo/utxo-ord': path.resolve('../utxo-ord/dist/esm/index.js'), }, diff --git a/yarn.lock b/yarn.lock index b7906a70c8..797232913e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1015,10 +1015,10 @@ resolved "https://registry.npmjs.org/@bitgo/wasm-dot/-/wasm-dot-1.7.0.tgz" integrity sha512-KoXavJvyDHlEN+sWcigbgxYJtdFaU7gS0EkYQbNH4npVjNlzo6rL6gwjyWbyOy7oEs65DhpJ9vY5kRbE/bKiTQ== -"@bitgo/wasm-mps@1.7.0": - version "1.7.0" - resolved "https://registry.npmjs.org/@bitgo/wasm-mps/-/wasm-mps-1.7.0.tgz#e7ebca1afd2df757e69c5cdac702d6a06156867c" - integrity sha512-SNO7as4UvnE2ptDXp1oUXjABA8Y3/71lgVpAQyAGSfSaURjz4rG19+JZR54GBRIaA6hvUPr029b4gFyqoZPcgg== +"@bitgo/wasm-mps@1.8.1": + version "1.8.1" + resolved "https://registry.npmjs.org/@bitgo/wasm-mps/-/wasm-mps-1.8.1.tgz#946673f5845696cdcf744f8122fd1fc2be3edce1" + integrity sha512-CV8EXYc1BGYtXdCRDxJ5h04nj/LpMgu3VlkfowlodI6UKcj1zotAvk4OMIdgiPPbKVr1l+xibHDXZYx/uf3rnw== "@bitgo/wasm-solana@^2.6.0": version "2.6.0"