Skip to content

Commit c474524

Browse files
committed
fix(sdk-lib-mpc): use @bitgo/wasm-mps/web in browser for EdDSA DKG
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
1 parent f3cc4eb commit c474524

4 files changed

Lines changed: 24 additions & 7 deletions

File tree

modules/sdk-lib-mpc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
]
3737
},
3838
"dependencies": {
39-
"@bitgo/wasm-mps": "1.7.0",
39+
"@bitgo/wasm-mps": "1.8.1",
4040
"@noble/curves": "1.8.1",
4141
"@silencelaboratories/dkls-wasm-ll-node": "1.2.0-pre.4",
4242
"@silencelaboratories/dkls-wasm-ll-web": "1.2.0-pre.4",

modules/sdk-lib-mpc/src/tss/eddsa-mps/dkg.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { encode } from 'cbor-x';
33
import crypto from 'crypto';
44
import { DeserializedMessage, DeserializedMessages, DkgState, EddsaReducedKeyShare } from './types';
55

6-
type WasmMps = typeof import('@bitgo/wasm-mps');
6+
type NodeWasmer = typeof import('@bitgo/wasm-mps');
7+
type WebWasmer = typeof import('@bitgo/wasm-mps/web');
8+
type WasmMps = NodeWasmer | WebWasmer;
79

810
/**
911
* EdDSA Distributed Key Generation (DKG) implementation using @bitgo/wasm-mps.
@@ -53,7 +55,21 @@ export class DKG {
5355

5456
private async loadWasmMps(): Promise<void> {
5557
if (!this.wasmMps) {
56-
this.wasmMps = await import('@bitgo/wasm-mps');
58+
if (
59+
typeof window !== 'undefined' &&
60+
/* checks for electron processes */
61+
!window.process &&
62+
!window.process?.['type']
63+
) {
64+
// Browser: web build has explicit init() — guaranteed ready after await
65+
// eslint-disable-next-line import/no-internal-modules -- @bitgo/wasm-mps exposes environment-specific subpath exports.
66+
const webWasm = await import('@bitgo/wasm-mps/web');
67+
await webWasm.default();
68+
this.wasmMps = webWasm;
69+
} else {
70+
// Node.js: dynamic import() rewritten to require() by tsc → CJS build → readFileSync
71+
this.wasmMps = await import('@bitgo/wasm-mps');
72+
}
5773
}
5874
}
5975

webpack/bitgojs.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
'@bitgo/wasm-ton': path.resolve('../../node_modules/@bitgo/wasm-ton/dist/esm/js/index.js'),
2323
'@bitgo/wasm-utxo': path.resolve('../../node_modules/@bitgo/wasm-utxo/dist/esm/js/index.js'),
2424
'@bitgo/wasm-solana': path.resolve('../../node_modules/@bitgo/wasm-solana/dist/esm/js/index.js'),
25+
'@bitgo/wasm-mps/web': path.resolve('../../node_modules/@bitgo/wasm-mps/dist/web/js/wasm/wasm_mps.js'),
2526
'@bitgo/wasm-mps': path.resolve('../../node_modules/@bitgo/wasm-mps/dist/esm/js/wasm/wasm_mps.js'),
2627
'@bitgo/utxo-ord': path.resolve('../utxo-ord/dist/esm/index.js'),
2728
},

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,10 +1015,10 @@
10151015
resolved "https://registry.npmjs.org/@bitgo/wasm-dot/-/wasm-dot-1.7.0.tgz"
10161016
integrity sha512-KoXavJvyDHlEN+sWcigbgxYJtdFaU7gS0EkYQbNH4npVjNlzo6rL6gwjyWbyOy7oEs65DhpJ9vY5kRbE/bKiTQ==
10171017

1018-
"@bitgo/wasm-mps@1.7.0":
1019-
version "1.7.0"
1020-
resolved "https://registry.npmjs.org/@bitgo/wasm-mps/-/wasm-mps-1.7.0.tgz#e7ebca1afd2df757e69c5cdac702d6a06156867c"
1021-
integrity sha512-SNO7as4UvnE2ptDXp1oUXjABA8Y3/71lgVpAQyAGSfSaURjz4rG19+JZR54GBRIaA6hvUPr029b4gFyqoZPcgg==
1018+
"@bitgo/wasm-mps@1.8.1":
1019+
version "1.8.1"
1020+
resolved "https://registry.npmjs.org/@bitgo/wasm-mps/-/wasm-mps-1.8.1.tgz#946673f5845696cdcf744f8122fd1fc2be3edce1"
1021+
integrity sha512-CV8EXYc1BGYtXdCRDxJ5h04nj/LpMgu3VlkfowlodI6UKcj1zotAvk4OMIdgiPPbKVr1l+xibHDXZYx/uf3rnw==
10221022

10231023
"@bitgo/wasm-solana@^2.6.0":
10241024
version "2.6.0"

0 commit comments

Comments
 (0)