@bubolabs/wallet-wasm 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js ADDED
@@ -0,0 +1,748 @@
1
+ import * as coreWasm from "./pkg/bubo_wallet_wasm.js";
2
+ import * as cardanoWasm from "./pkg-cardano/bubo_wallet_wasm.js";
3
+
4
+ let initialized = false;
5
+
6
+ export class WalletWasmError extends Error {
7
+ constructor(payload) {
8
+ super(payload.message);
9
+ this.name = "WalletWasmError";
10
+ this.code = payload.code;
11
+ this.details = payload.details ?? null;
12
+ }
13
+ }
14
+
15
+ export async function init() {
16
+ callWasm(() => coreWasm.init());
17
+ callWasm(() => cardanoWasm.init());
18
+ initialized = true;
19
+ }
20
+
21
+ export function listSupportedChains() {
22
+ ensureInitialized();
23
+ return mergeByChainId([
24
+ ...parseJsonResult(callWasm(() => coreWasm.listSupportedChains())),
25
+ ...parseJsonResult(callWasm(() => cardanoWasm.listSupportedChains())),
26
+ ]);
27
+ }
28
+
29
+ export function listChainExtensions() {
30
+ ensureInitialized();
31
+ return mergeByChainId([
32
+ ...parseJsonResult(callWasm(() => coreWasm.listChainExtensions())),
33
+ ...parseJsonResult(callWasm(() => cardanoWasm.listChainExtensions())),
34
+ ]);
35
+ }
36
+
37
+ export function invokeChainExtension(chainId, method, payload = {}) {
38
+ ensureInitialized();
39
+ const module = wasmForChain(chainId);
40
+ return callWasm(() => module.invokeChainExtension(chainId, method, toJson(payload)));
41
+ }
42
+
43
+ export function deriveAddress(request) {
44
+ ensureInitialized();
45
+ const module = wasmForRequest(request);
46
+ return callWasm(() => module.deriveAddress(toJson(request)));
47
+ }
48
+
49
+ export function deriveWallet(request) {
50
+ ensureInitialized();
51
+ const module = wasmForRequest(request);
52
+ return parseJsonResult(callWasm(() => module.deriveWallet(toJson(request))));
53
+ }
54
+
55
+ export function deriveAddressFromPrivateKey(request) {
56
+ ensureInitialized();
57
+ const module = wasmForRequest(request);
58
+ return callWasm(() => module.deriveAddressFromPrivateKey(toJson(request)));
59
+ }
60
+
61
+ export function deriveWalletFromPrivateKey(request) {
62
+ ensureInitialized();
63
+ const module = wasmForRequest(request);
64
+ return parseJsonResult(callWasm(() => module.deriveWalletFromPrivateKey(toJson(request))));
65
+ }
66
+
67
+ export function buildAndSign(request) {
68
+ ensureInitialized();
69
+ const module = wasmForRequest(request);
70
+ return callWasm(() => module.buildAndSign(toJson(request)));
71
+ }
72
+
73
+ export function buildAndSignFromPrivateKey(request) {
74
+ ensureInitialized();
75
+ const module = wasmForRequest(request);
76
+ return callWasm(() => module.buildAndSignFromPrivateKey(toJson(request)));
77
+ }
78
+
79
+ export function signMessage(request) {
80
+ ensureInitialized();
81
+ const module = wasmForRequest(request);
82
+ return callWasm(() => module.signMessage(toJson(request)));
83
+ }
84
+
85
+ export function signMessageFromPrivateKey(request) {
86
+ ensureInitialized();
87
+ const module = wasmForRequest(request);
88
+ return callWasm(() => module.signMessageFromPrivateKey(toJson(request)));
89
+ }
90
+
91
+ export function signBytes(request) {
92
+ ensureInitialized();
93
+ const module = wasmForRequest(request);
94
+ return callWasm(() => module.signBytes(toJson(request)));
95
+ }
96
+
97
+ export function signBytesFromPrivateKey(request) {
98
+ ensureInitialized();
99
+ const module = wasmForRequest(request);
100
+ return callWasm(() => module.signBytesFromPrivateKey(toJson(request)));
101
+ }
102
+
103
+ export function signTypedData(request) {
104
+ ensureInitialized();
105
+ const module = wasmForRequest(request);
106
+ return callWasm(() => module.signTypedData(toJson(request)));
107
+ }
108
+
109
+ export function signTypedDataFromPrivateKey(request) {
110
+ ensureInitialized();
111
+ const module = wasmForRequest(request);
112
+ return callWasm(() => module.signTypedDataFromPrivateKey(toJson(request)));
113
+ }
114
+
115
+ export const eth = {
116
+ ...deriveMethods("eth"),
117
+ ...buildAndSignMethods("eth"),
118
+ ...signMessageMethods("eth"),
119
+ ...signTypedDataMethods("eth"),
120
+ buildTx(payload) {
121
+ return invokeChainExtension("eth", "eth.buildTx", payload);
122
+ },
123
+ signTx(payload) {
124
+ return invokeChainExtension("eth", "eth.signTx", payload);
125
+ },
126
+ decodeSignedTx(payload) {
127
+ return parseJsonResult(invokeChainExtension("eth", "eth.decodeSignedTx", payload));
128
+ },
129
+ ecrecover(payload) {
130
+ return parseJsonResult(invokeChainExtension("eth", "eth.ecrecover", payload));
131
+ },
132
+ recoverTypedDataSigner(payload) {
133
+ return parseJsonResult(
134
+ invokeChainExtension("eth", "eth.recoverTypedDataSigner", payload)
135
+ );
136
+ },
137
+ verifyMessageSignature(payload) {
138
+ return parseJsonResult(
139
+ invokeChainExtension("eth", "eth.verifyMessageSignature", payload)
140
+ );
141
+ },
142
+ verifyTypedDataSignature(payload) {
143
+ return parseJsonResult(
144
+ invokeChainExtension("eth", "eth.verifyTypedDataSignature", payload)
145
+ );
146
+ },
147
+ buildEip1271IsValidSignatureCall(payload) {
148
+ return parseJsonResult(
149
+ invokeChainExtension("eth", "eth.buildEip1271IsValidSignatureCall", payload)
150
+ );
151
+ },
152
+ verifyEip1271IsValidSignatureResult(payload) {
153
+ return parseJsonResult(
154
+ invokeChainExtension("eth", "eth.verifyEip1271IsValidSignatureResult", payload)
155
+ );
156
+ },
157
+ computeUserOperationHash(payload) {
158
+ return parseJsonResult(
159
+ invokeChainExtension("eth", "eth.computeUserOperationHash", payload)
160
+ );
161
+ },
162
+ signUserOperation(payload) {
163
+ return parseJsonResult(invokeChainExtension("eth", "eth.signUserOperation", payload));
164
+ },
165
+ recoverUserOperationSigner(payload) {
166
+ return parseJsonResult(
167
+ invokeChainExtension("eth", "eth.recoverUserOperationSigner", payload)
168
+ );
169
+ },
170
+ };
171
+
172
+ export const btc = {
173
+ ...deriveMethods("btc"),
174
+ ...buildAndSignMethods("btc"),
175
+ ...signMessageMethods("btc"),
176
+ ...signBytesMethods("btc"),
177
+ deriveAccountXpub(payload) {
178
+ return parseJsonResult(invokeChainExtension("btc", "btc.deriveAccountXpub", payload));
179
+ },
180
+ deriveAddressFromXpub(payload) {
181
+ return invokeChainExtension("btc", "btc.deriveAddressFromXpub", payload);
182
+ },
183
+ buildTx(payload) {
184
+ return parseJsonResult(invokeChainExtension("btc", "btc.buildTx", payload));
185
+ },
186
+ signTx(payload) {
187
+ return parseJsonResult(invokeChainExtension("btc", "btc.signTx", payload));
188
+ },
189
+ buildPsbt(payload) {
190
+ return parseJsonResult(invokeChainExtension("btc", "btc.buildPsbt", payload));
191
+ },
192
+ signPsbt(payload) {
193
+ return parseJsonResult(invokeChainExtension("btc", "btc.signPsbt", payload));
194
+ },
195
+ signPsbtWithSigners(payload) {
196
+ return parseJsonResult(invokeChainExtension("btc", "btc.signPsbtWithSigners", payload));
197
+ },
198
+ partialSignPsbt(payload) {
199
+ return parseJsonResult(invokeChainExtension("btc", "btc.partialSignPsbt", payload));
200
+ },
201
+ verifyPsbtFinalized(payload) {
202
+ return parseJsonResult(invokeChainExtension("btc", "btc.verifyPsbtFinalized", payload));
203
+ },
204
+ inspectPsbt(payload) {
205
+ return parseJsonResult(invokeChainExtension("btc", "btc.inspectPsbt", payload));
206
+ },
207
+ decodeSignedTx(payload) {
208
+ return parseJsonResult(invokeChainExtension("btc", "btc.decodeSignedTx", payload));
209
+ },
210
+ verifyMessage(payload) {
211
+ return parseJsonResult(invokeChainExtension("btc", "btc.verifyMessage", payload));
212
+ },
213
+ frostTrustedDealerKeygen(payload) {
214
+ return parseJsonResult(
215
+ invokeChainExtension("btc", "btc.frostTrustedDealerKeygen", payload)
216
+ );
217
+ },
218
+ frostDkgRound1(payload) {
219
+ return parseJsonResult(invokeChainExtension("btc", "btc.frostDkgRound1", payload));
220
+ },
221
+ frostDkgRound2(payload) {
222
+ return parseJsonResult(invokeChainExtension("btc", "btc.frostDkgRound2", payload));
223
+ },
224
+ frostDkgFinalize(payload) {
225
+ return parseJsonResult(invokeChainExtension("btc", "btc.frostDkgFinalize", payload));
226
+ },
227
+ frostRound1Commit(payload) {
228
+ return parseJsonResult(invokeChainExtension("btc", "btc.frostRound1Commit", payload));
229
+ },
230
+ frostBuildSigningPackage(payload) {
231
+ return parseJsonResult(
232
+ invokeChainExtension("btc", "btc.frostBuildSigningPackage", payload)
233
+ );
234
+ },
235
+ frostRound2SignShare(payload) {
236
+ return parseJsonResult(invokeChainExtension("btc", "btc.frostRound2SignShare", payload));
237
+ },
238
+ frostAggregateSignature(payload) {
239
+ return parseJsonResult(
240
+ invokeChainExtension("btc", "btc.frostAggregateSignature", payload)
241
+ );
242
+ },
243
+ frostTaprootComputeTweak(payload) {
244
+ return parseJsonResult(
245
+ invokeChainExtension("btc", "btc.frostTaprootComputeTweak", payload)
246
+ );
247
+ },
248
+ frostVerifySignatureShare(payload) {
249
+ return parseJsonResult(
250
+ invokeChainExtension("btc", "btc.frostVerifySignatureShare", payload)
251
+ );
252
+ },
253
+ frostBuildTaprootPsbtSigningInputs(payload) {
254
+ return parseJsonResult(
255
+ invokeChainExtension("btc", "btc.frostBuildTaprootPsbtSigningInputs", payload)
256
+ );
257
+ },
258
+ frostFinalizeTaprootPsbt(payload) {
259
+ return parseJsonResult(
260
+ invokeChainExtension("btc", "btc.frostFinalizeTaprootPsbt", payload)
261
+ );
262
+ },
263
+ frostNonceSessionScope(payload) {
264
+ return parseJsonResult(invokeChainExtension("btc", "btc.frostNonceSessionScope", payload));
265
+ },
266
+ frostResetNonceGuard(payload) {
267
+ return parseJsonResult(invokeChainExtension("btc", "btc.frostResetNonceGuard", payload));
268
+ },
269
+ };
270
+
271
+ export const solana = {
272
+ ...deriveMethods("solana"),
273
+ ...buildAndSignMethods("solana"),
274
+ ...signMessageMethods("solana"),
275
+ ...signBytesMethods("solana"),
276
+ buildTx(payload) {
277
+ return invokeChainExtension("solana", "solana.buildTx", payload);
278
+ },
279
+ signTx(payload) {
280
+ return invokeChainExtension("solana", "solana.signTx", payload);
281
+ },
282
+ inspectSignedTx(payload) {
283
+ return parseJsonResult(invokeChainExtension("solana", "solana.inspectSignedTx", payload));
284
+ },
285
+ decodeSignedTx(payload) {
286
+ return parseJsonResult(invokeChainExtension("solana", "solana.decodeSignedTx", payload));
287
+ },
288
+ verifyMessageSignature(payload) {
289
+ return parseJsonResult(
290
+ invokeChainExtension("solana", "solana.verifyMessageSignature", payload)
291
+ );
292
+ },
293
+ verifyBytesSignature(payload) {
294
+ return parseJsonResult(
295
+ invokeChainExtension("solana", "solana.verifyBytesSignature", payload)
296
+ );
297
+ },
298
+ };
299
+
300
+ export const ton = {
301
+ deriveAddress(request) {
302
+ return invokeChainExtension("ton", "ton.deriveAddress", inferTonMnemonicType(request));
303
+ },
304
+ deriveWallet(request) {
305
+ return parseJsonResult(
306
+ invokeChainExtension("ton", "ton.deriveWallet", inferTonMnemonicType(request))
307
+ );
308
+ },
309
+ deriveAddressFromPrivateKey(request) {
310
+ return invokeChainExtension(
311
+ "ton",
312
+ "ton.deriveAddressFromPrivateKey",
313
+ withTonKeyEncoding(request)
314
+ );
315
+ },
316
+ deriveWalletFromPrivateKey(request) {
317
+ return parseJsonResult(
318
+ invokeChainExtension(
319
+ "ton",
320
+ "ton.deriveWalletFromPrivateKey",
321
+ withTonKeyEncoding(request)
322
+ )
323
+ );
324
+ },
325
+ buildAndSign(request) {
326
+ const payload = parsePayloadObject(request.txPayload);
327
+ const built = this.buildTx(payload);
328
+ const signed = this.signTx({
329
+ ...pickTonSignerFields(request),
330
+ unsignedTx: built.unsignedTx,
331
+ unsignedTxEncoding: built.unsignedTxEncoding,
332
+ });
333
+ return base64ToBytes(signed.signedTx);
334
+ },
335
+ buildAndSignFromPrivateKey(request) {
336
+ const payload = parsePayloadObject(request.txPayload);
337
+ const built = this.buildTx(payload);
338
+ const signed = this.signTx({
339
+ privateKey: request.privateKey,
340
+ keyEncoding: request.privateKeyEncoding,
341
+ unsignedTx: built.unsignedTx,
342
+ unsignedTxEncoding: built.unsignedTxEncoding,
343
+ });
344
+ return base64ToBytes(signed.signedTx);
345
+ },
346
+ buildWalletStateInit(payload) {
347
+ return parseJsonResult(
348
+ invokeChainExtension("ton", "ton.buildWalletStateInit", inferTonMnemonicType(payload))
349
+ );
350
+ },
351
+ buildWalletStateInitFromPrivateKey(payload) {
352
+ return parseJsonResult(
353
+ invokeChainExtension(
354
+ "ton",
355
+ "ton.buildWalletStateInitFromPrivateKey",
356
+ withTonKeyEncoding(payload)
357
+ )
358
+ );
359
+ },
360
+ buildTx(payload) {
361
+ return parseJsonResult(invokeChainExtension("ton", "ton.buildTx", payload));
362
+ },
363
+ signTx(payload) {
364
+ return parseJsonResult(
365
+ invokeChainExtension("ton", "ton.signTx", inferTonMnemonicType(withTonKeyEncoding(payload)))
366
+ );
367
+ },
368
+ inspectSignedTx(payload) {
369
+ return parseJsonResult(invokeChainExtension("ton", "ton.inspectSignedTx", payload));
370
+ },
371
+ decodeSignedTx(payload) {
372
+ return parseJsonResult(invokeChainExtension("ton", "ton.decodeSignedTx", payload));
373
+ },
374
+ };
375
+
376
+ export const tron = {
377
+ ...deriveMethods("tron"),
378
+ ...buildAndSignMethods("tron"),
379
+ ...signMessageMethods("tron"),
380
+ ...signBytesMethods("tron"),
381
+ buildTx(payload) {
382
+ return parseJsonResult(invokeChainExtension("tron", "tron.buildTx", payload));
383
+ },
384
+ signTx(payload) {
385
+ return parseJsonResult(invokeChainExtension("tron", "tron.signTx", payload));
386
+ },
387
+ inspectSignedTx(payload) {
388
+ return parseJsonResult(invokeChainExtension("tron", "tron.inspectSignedTx", payload));
389
+ },
390
+ decodeSignedTx(payload) {
391
+ return parseJsonResult(invokeChainExtension("tron", "tron.decodeSignedTx", payload));
392
+ },
393
+ };
394
+
395
+ export const near = {
396
+ ...deriveMethods("near"),
397
+ ...buildAndSignMethods("near"),
398
+ ...signMessageMethods("near"),
399
+ ...signBytesMethods("near"),
400
+ ...jsonTxMethods("near"),
401
+ };
402
+
403
+ export const cosmos = {
404
+ ...deriveMethods("cosmos"),
405
+ ...buildAndSignMethods("cosmos"),
406
+ ...signMessageMethods("cosmos"),
407
+ ...signBytesMethods("cosmos"),
408
+ ...jsonTxMethods("cosmos"),
409
+ };
410
+
411
+ export const cardano = {
412
+ ...deriveMethods("cardano"),
413
+ ...buildAndSignMethods("cardano"),
414
+ ...signMessageMethods("cardano"),
415
+ ...signBytesMethods("cardano"),
416
+ ...jsonTxMethods("cardano"),
417
+ };
418
+
419
+ export const aptos = {
420
+ ...deriveMethods("aptos"),
421
+ ...buildAndSignMethods("aptos"),
422
+ ...signMessageMethods("aptos"),
423
+ ...signBytesMethods("aptos"),
424
+ ...jsonTxMethods("aptos"),
425
+ };
426
+
427
+ export const polkadot = {
428
+ ...deriveMethods("polkadot"),
429
+ ...buildAndSignMethods("polkadot"),
430
+ ...signMessageMethods("polkadot"),
431
+ ...signBytesMethods("polkadot"),
432
+ ...jsonTxMethods("polkadot"),
433
+ };
434
+
435
+ export const dogecoin = {
436
+ deriveAddress(request) {
437
+ return invokeChainExtension("dogecoin", "dogecoin.deriveAddress", request);
438
+ },
439
+ deriveWallet(request) {
440
+ return parseJsonResult(invokeChainExtension("dogecoin", "dogecoin.deriveWallet", request));
441
+ },
442
+ deriveAddressFromPrivateKey(request) {
443
+ return invokeChainExtension(
444
+ "dogecoin",
445
+ "dogecoin.deriveAddressFromPrivateKey",
446
+ withKeyEncodingAlias(request)
447
+ );
448
+ },
449
+ deriveWalletFromPrivateKey(request) {
450
+ return parseJsonResult(
451
+ invokeChainExtension(
452
+ "dogecoin",
453
+ "dogecoin.deriveWalletFromPrivateKey",
454
+ withKeyEncodingAlias(request)
455
+ )
456
+ );
457
+ },
458
+ ...buildAndSignMethods("dogecoin"),
459
+ ...signMessageMethods("dogecoin"),
460
+ ...signBytesMethods("dogecoin"),
461
+ ...jsonTxMethods("dogecoin"),
462
+ };
463
+
464
+ export const starknet = {
465
+ ...deriveMethods("starknet"),
466
+ ...buildAndSignMethods("starknet"),
467
+ ...signMessageMethods("starknet"),
468
+ ...signBytesMethods("starknet"),
469
+ ...jsonTxMethods("starknet"),
470
+ computeAccountAddress(payload) {
471
+ return parseJsonResult(
472
+ invokeChainExtension("starknet", "starknet.computeAccountAddress", payload)
473
+ );
474
+ },
475
+ };
476
+
477
+ export const stellar = {
478
+ ...deriveMethods("stellar"),
479
+ ...buildAndSignMethods("stellar"),
480
+ ...signMessageMethods("stellar"),
481
+ ...signBytesMethods("stellar"),
482
+ ...jsonTxMethods("stellar"),
483
+ };
484
+
485
+ export const sui = {
486
+ ...deriveMethods("sui"),
487
+ ...buildAndSignMethods("sui"),
488
+ ...signMessageMethods("sui"),
489
+ ...signBytesMethods("sui"),
490
+ ...jsonTxMethods("sui"),
491
+ };
492
+
493
+ export const xrpl = {
494
+ ...deriveMethods("xrpl"),
495
+ ...buildAndSignMethods("xrpl"),
496
+ ...jsonTxMethods("xrpl"),
497
+ };
498
+
499
+ function inferTonMnemonicType(request = {}) {
500
+ if (
501
+ request &&
502
+ typeof request === "object" &&
503
+ typeof request.mnemonic === "string" &&
504
+ request.mnemonicType == null &&
505
+ request.mnemonic_type == null
506
+ ) {
507
+ const wordCount = request.mnemonic.trim().split(/\s+/).filter(Boolean).length;
508
+ if ([12, 15, 18, 21].includes(wordCount)) {
509
+ return {
510
+ ...request,
511
+ mnemonicType: "bip39",
512
+ };
513
+ }
514
+ }
515
+ return request;
516
+ }
517
+
518
+ function withTonKeyEncoding(request = {}) {
519
+ if (
520
+ request &&
521
+ typeof request === "object" &&
522
+ request.privateKeyEncoding != null &&
523
+ request.keyEncoding == null
524
+ ) {
525
+ const { privateKeyEncoding, ...rest } = request;
526
+ return {
527
+ ...rest,
528
+ keyEncoding: privateKeyEncoding,
529
+ };
530
+ }
531
+ return request;
532
+ }
533
+
534
+ function withKeyEncodingAlias(request = {}) {
535
+ if (
536
+ request &&
537
+ typeof request === "object" &&
538
+ request.privateKeyEncoding != null &&
539
+ request.keyEncoding == null
540
+ ) {
541
+ const { privateKeyEncoding, ...rest } = request;
542
+ return {
543
+ ...rest,
544
+ keyEncoding: privateKeyEncoding,
545
+ };
546
+ }
547
+ return request;
548
+ }
549
+
550
+ function pickTonSignerFields(request = {}) {
551
+ return inferTonMnemonicType({
552
+ mnemonic: request.mnemonic,
553
+ mnemonicType: request.mnemonicType,
554
+ derivationPath: request.derivationPath,
555
+ account: request.account,
556
+ index: request.index,
557
+ passphrase: request.passphrase,
558
+ });
559
+ }
560
+
561
+ function parsePayloadObject(value) {
562
+ if (typeof value !== "string") {
563
+ return value ?? {};
564
+ }
565
+ try {
566
+ return JSON.parse(value);
567
+ } catch (error) {
568
+ throw new WalletWasmError({
569
+ code: "SERIALIZATION_FAILED",
570
+ message: "failed to parse TON txPayload JSON",
571
+ details: error instanceof Error ? error.message : String(error),
572
+ });
573
+ }
574
+ }
575
+
576
+ function base64ToBytes(value) {
577
+ const binary =
578
+ typeof atob === "function"
579
+ ? atob(value)
580
+ : Buffer.from(value, "base64").toString("binary");
581
+ const bytes = new Uint8Array(binary.length);
582
+ for (let index = 0; index < binary.length; index += 1) {
583
+ bytes[index] = binary.charCodeAt(index);
584
+ }
585
+ return bytes;
586
+ }
587
+
588
+ function deriveMethods(chainId) {
589
+ return {
590
+ deriveAddress(request) {
591
+ return deriveAddress(withChain(chainId, request));
592
+ },
593
+ deriveWallet(request) {
594
+ return deriveWallet(withChain(chainId, request));
595
+ },
596
+ deriveAddressFromPrivateKey(request) {
597
+ return deriveAddressFromPrivateKey(withChain(chainId, request));
598
+ },
599
+ deriveWalletFromPrivateKey(request) {
600
+ return deriveWalletFromPrivateKey(withChain(chainId, request));
601
+ },
602
+ };
603
+ }
604
+
605
+ function buildAndSignMethods(chainId) {
606
+ return {
607
+ buildAndSign(request) {
608
+ return buildAndSign(withChain(chainId, request));
609
+ },
610
+ buildAndSignFromPrivateKey(request) {
611
+ return buildAndSignFromPrivateKey(withChain(chainId, request));
612
+ },
613
+ };
614
+ }
615
+
616
+ function signMessageMethods(chainId) {
617
+ return {
618
+ signMessage(request) {
619
+ return signMessage(withChain(chainId, request));
620
+ },
621
+ signMessageFromPrivateKey(request) {
622
+ return signMessageFromPrivateKey(withChain(chainId, request));
623
+ },
624
+ };
625
+ }
626
+
627
+ function signBytesMethods(chainId) {
628
+ return {
629
+ signBytes(request) {
630
+ return signBytes(withChain(chainId, request));
631
+ },
632
+ signBytesFromPrivateKey(request) {
633
+ return signBytesFromPrivateKey(withChain(chainId, request));
634
+ },
635
+ };
636
+ }
637
+
638
+ function signTypedDataMethods(chainId) {
639
+ return {
640
+ signTypedData(request) {
641
+ return signTypedData(withChain(chainId, request));
642
+ },
643
+ signTypedDataFromPrivateKey(request) {
644
+ return signTypedDataFromPrivateKey(withChain(chainId, request));
645
+ },
646
+ };
647
+ }
648
+
649
+ function jsonTxMethods(chainId) {
650
+ return {
651
+ buildTx(payload) {
652
+ return parseJsonResult(invokeChainExtension(chainId, `${chainId}.buildTx`, payload));
653
+ },
654
+ signTx(payload) {
655
+ return parseJsonResult(invokeChainExtension(chainId, `${chainId}.signTx`, payload));
656
+ },
657
+ inspectSignedTx(payload) {
658
+ return parseJsonResult(
659
+ invokeChainExtension(chainId, `${chainId}.inspectSignedTx`, payload)
660
+ );
661
+ },
662
+ decodeSignedTx(payload) {
663
+ return parseJsonResult(
664
+ invokeChainExtension(chainId, `${chainId}.decodeSignedTx`, payload)
665
+ );
666
+ },
667
+ };
668
+ }
669
+
670
+ function wasmForRequest(request = {}) {
671
+ return wasmForChain(request.chainId);
672
+ }
673
+
674
+ function wasmForChain(chainId) {
675
+ return chainId === "cardano" ? cardanoWasm : coreWasm;
676
+ }
677
+
678
+ function mergeByChainId(items) {
679
+ return Array.from(new Map(items.map((item) => [item.chainId, item])).values()).sort((a, b) =>
680
+ a.chainId.localeCompare(b.chainId)
681
+ );
682
+ }
683
+
684
+ function ensureInitialized() {
685
+ if (!initialized) {
686
+ throw new WalletWasmError({
687
+ code: "NOT_INITIALIZED",
688
+ message: "wallet WASM module is not initialized; call init() first",
689
+ details: null,
690
+ });
691
+ }
692
+ }
693
+
694
+ function callWasm(fn) {
695
+ try {
696
+ return fn();
697
+ } catch (error) {
698
+ throw normalizeError(error);
699
+ }
700
+ }
701
+
702
+ function normalizeError(error) {
703
+ if (error instanceof WalletWasmError) {
704
+ return error;
705
+ }
706
+ const raw =
707
+ typeof error === "string"
708
+ ? error
709
+ : typeof error?.message === "string"
710
+ ? error.message
711
+ : String(error);
712
+ try {
713
+ const parsed = JSON.parse(raw);
714
+ if (parsed && typeof parsed.code === "string" && typeof parsed.message === "string") {
715
+ return new WalletWasmError(parsed);
716
+ }
717
+ } catch {
718
+ // Fall through to a stable wrapper for non-JSON wasm-bindgen errors.
719
+ }
720
+ return new WalletWasmError({
721
+ code: "INTERNAL",
722
+ message: raw,
723
+ details: null,
724
+ });
725
+ }
726
+
727
+ function parseJsonResult(value) {
728
+ try {
729
+ return JSON.parse(value);
730
+ } catch (error) {
731
+ throw new WalletWasmError({
732
+ code: "SERIALIZATION_FAILED",
733
+ message: "failed to parse WASM SDK JSON result",
734
+ details: error instanceof Error ? error.message : String(error),
735
+ });
736
+ }
737
+ }
738
+
739
+ function toJson(value) {
740
+ return typeof value === "string" ? value : JSON.stringify(value ?? {});
741
+ }
742
+
743
+ function withChain(chainId, request = {}) {
744
+ return {
745
+ ...request,
746
+ chainId,
747
+ };
748
+ }