@getpara/server-sdk 1.4.4 → 1.5.1

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.
@@ -28,6 +28,26 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  mod
29
29
  ));
30
30
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
31
+ var __async = (__this, __arguments, generator) => {
32
+ return new Promise((resolve, reject) => {
33
+ var fulfilled = (value) => {
34
+ try {
35
+ step(generator.next(value));
36
+ } catch (e) {
37
+ reject(e);
38
+ }
39
+ };
40
+ var rejected = (value) => {
41
+ try {
42
+ step(generator.throw(value));
43
+ } catch (e) {
44
+ reject(e);
45
+ }
46
+ };
47
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
48
+ step((generator = generator.apply(__this, __arguments)).next());
49
+ });
50
+ };
31
51
 
32
52
  // src/wasm/wasm_exec.js
33
53
  var require_wasm_exec = __commonJS({
@@ -510,71 +530,73 @@ var require_wasm_exec = __commonJS({
510
530
  }
511
531
  };
512
532
  }
513
- async run(instance) {
514
- if (!(instance instanceof WebAssembly.Instance)) {
515
- throw new Error("Go.run: WebAssembly.Instance expected");
516
- }
517
- this._inst = instance;
518
- this.mem = new DataView(this._inst.exports.mem.buffer);
519
- this._values = [
520
- // JS values that Go currently has references to, indexed by reference id
521
- NaN,
522
- 0,
523
- null,
524
- true,
525
- false,
526
- globalThis,
527
- this
528
- ];
529
- this._goRefCounts = new Array(this._values.length).fill(Infinity);
530
- this._ids = /* @__PURE__ */ new Map([
531
- // mapping from JS values to reference ids
532
- [0, 1],
533
- [null, 2],
534
- [true, 3],
535
- [false, 4],
536
- [globalThis, 5],
537
- [this, 6]
538
- ]);
539
- this._idPool = [];
540
- this.exited = false;
541
- let offset = 4096;
542
- const strPtr = (str) => {
543
- const ptr = offset;
544
- const bytes = encoder.encode(str + "\0");
545
- new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
546
- offset += bytes.length;
547
- if (offset % 8 !== 0) {
548
- offset += 8 - offset % 8;
533
+ run(instance) {
534
+ return __async(this, null, function* () {
535
+ if (!(instance instanceof WebAssembly.Instance)) {
536
+ throw new Error("Go.run: WebAssembly.Instance expected");
549
537
  }
550
- return ptr;
551
- };
552
- const argc = this.argv.length;
553
- const argvPtrs = [];
554
- this.argv.forEach((arg) => {
555
- argvPtrs.push(strPtr(arg));
556
- });
557
- argvPtrs.push(0);
558
- const keys = Object.keys(this.env).sort();
559
- keys.forEach((key) => {
560
- argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
561
- });
562
- argvPtrs.push(0);
563
- const argv = offset;
564
- argvPtrs.forEach((ptr) => {
565
- this.mem.setUint32(offset, ptr, true);
566
- this.mem.setUint32(offset + 4, 0, true);
567
- offset += 8;
538
+ this._inst = instance;
539
+ this.mem = new DataView(this._inst.exports.mem.buffer);
540
+ this._values = [
541
+ // JS values that Go currently has references to, indexed by reference id
542
+ NaN,
543
+ 0,
544
+ null,
545
+ true,
546
+ false,
547
+ globalThis,
548
+ this
549
+ ];
550
+ this._goRefCounts = new Array(this._values.length).fill(Infinity);
551
+ this._ids = /* @__PURE__ */ new Map([
552
+ // mapping from JS values to reference ids
553
+ [0, 1],
554
+ [null, 2],
555
+ [true, 3],
556
+ [false, 4],
557
+ [globalThis, 5],
558
+ [this, 6]
559
+ ]);
560
+ this._idPool = [];
561
+ this.exited = false;
562
+ let offset = 4096;
563
+ const strPtr = (str) => {
564
+ const ptr = offset;
565
+ const bytes = encoder.encode(str + "\0");
566
+ new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
567
+ offset += bytes.length;
568
+ if (offset % 8 !== 0) {
569
+ offset += 8 - offset % 8;
570
+ }
571
+ return ptr;
572
+ };
573
+ const argc = this.argv.length;
574
+ const argvPtrs = [];
575
+ this.argv.forEach((arg) => {
576
+ argvPtrs.push(strPtr(arg));
577
+ });
578
+ argvPtrs.push(0);
579
+ const keys = Object.keys(this.env).sort();
580
+ keys.forEach((key) => {
581
+ argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
582
+ });
583
+ argvPtrs.push(0);
584
+ const argv = offset;
585
+ argvPtrs.forEach((ptr) => {
586
+ this.mem.setUint32(offset, ptr, true);
587
+ this.mem.setUint32(offset + 4, 0, true);
588
+ offset += 8;
589
+ });
590
+ const wasmMinDataAddr = 4096 + 8192;
591
+ if (offset >= wasmMinDataAddr) {
592
+ throw new Error("total length of command line and environment variables exceeds limit");
593
+ }
594
+ this._inst.exports.run(argc, argv);
595
+ if (this.exited) {
596
+ this._resolveExitPromise();
597
+ }
598
+ yield this._exitPromise;
568
599
  });
569
- const wasmMinDataAddr = 4096 + 8192;
570
- if (offset >= wasmMinDataAddr) {
571
- throw new Error("total length of command line and environment variables exceeds limit");
572
- }
573
- this._inst.exports.run(argc, argv);
574
- if (this.exited) {
575
- this._resolveExitPromise();
576
- }
577
- await this._exitPromise;
578
600
  }
579
601
  _resume() {
580
602
  if (this.exited) {
@@ -615,392 +637,426 @@ var import_core_sdk2 = require("@getpara/core-sdk");
615
637
  var import_core_sdk = require("@getpara/core-sdk");
616
638
  var configCGGMPBase = (serverUrl, walletId, id) => `{"ServerUrl":"${serverUrl}", "WalletId": "${walletId}", "Id":"${id}", "Ids":["USER","CAPSULE"], "Threshold":1}`;
617
639
  var configDKLSBase = (walletId, id, disableWebSockets) => `{"walletId": "${walletId}", "id":"${id}", "otherId":"CAPSULE", "isReceiver": false, "disableWebSockets": ${disableWebSockets}}`;
618
- async function keygenRequest(ctx, userId, walletId, protocolId) {
619
- const { data } = await ctx.mpcComputationClient.post("/wallets", {
620
- userId,
621
- walletId,
622
- protocolId
623
- });
624
- return data;
625
- }
626
- async function signMessageRequest(ctx, userId, walletId, protocolId, message, signer) {
627
- const { data } = await ctx.mpcComputationClient.post(`/wallets/${walletId}/messages/sign`, {
628
- userId,
629
- protocolId,
630
- message,
631
- signer
640
+ function keygenRequest(ctx, userId, walletId, protocolId) {
641
+ return __async(this, null, function* () {
642
+ const { data } = yield ctx.mpcComputationClient.post("/wallets", {
643
+ userId,
644
+ walletId,
645
+ protocolId
646
+ });
647
+ return data;
632
648
  });
633
- return data;
634
649
  }
635
- async function sendTransactionRequest(ctx, userId, walletId, protocolId, transaction, signer, chainId) {
636
- const { data } = await ctx.mpcComputationClient.post(`/wallets/${walletId}/transactions/send`, {
637
- userId,
638
- protocolId,
639
- transaction,
640
- signer,
641
- chainId
650
+ function signMessageRequest(ctx, userId, walletId, protocolId, message, signer) {
651
+ return __async(this, null, function* () {
652
+ const { data } = yield ctx.mpcComputationClient.post(`/wallets/${walletId}/messages/sign`, {
653
+ userId,
654
+ protocolId,
655
+ message,
656
+ signer
657
+ });
658
+ return data;
642
659
  });
643
- return data;
644
660
  }
645
- async function ed25519Keygen(ctx, userId) {
646
- const { walletId, protocolId } = await ctx.client.createWallet(userId, {
647
- scheme: import_core_sdk.WalletScheme.ED25519,
648
- type: import_core_sdk.WalletType.SOLANA
661
+ function sendTransactionRequest(ctx, userId, walletId, protocolId, transaction, signer, chainId) {
662
+ return __async(this, null, function* () {
663
+ const { data } = yield ctx.mpcComputationClient.post(`/wallets/${walletId}/transactions/send`, {
664
+ userId,
665
+ protocolId,
666
+ transaction,
667
+ signer,
668
+ chainId
669
+ });
670
+ return data;
649
671
  });
650
- const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
651
- try {
652
- const newSigner = await new Promise(
653
- (resolve, reject) => global.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
654
- if (err) {
655
- reject(err);
656
- }
657
- resolve(result);
658
- })
659
- );
660
- return { signer: newSigner, walletId };
661
- } catch (e) {
662
- throw new Error(`error creating account of type SOLANA with userId ${userId} and walletId ${walletId}`);
663
- }
664
672
  }
665
- async function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType) {
666
- const { walletId, protocolId } = await ctx.client.createPregenWallet({
667
- pregenIdentifier,
668
- pregenIdentifierType,
669
- scheme: import_core_sdk.WalletScheme.ED25519,
670
- type: import_core_sdk.WalletType.SOLANA
673
+ function ed25519Keygen(ctx, userId) {
674
+ return __async(this, null, function* () {
675
+ const { walletId, protocolId } = yield ctx.client.createWallet(userId, {
676
+ scheme: import_core_sdk.WalletScheme.ED25519,
677
+ type: import_core_sdk.WalletType.SOLANA
678
+ });
679
+ const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
680
+ try {
681
+ const newSigner = yield new Promise(
682
+ (resolve, reject) => global.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
683
+ if (err) {
684
+ reject(err);
685
+ }
686
+ resolve(result);
687
+ })
688
+ );
689
+ return { signer: newSigner, walletId };
690
+ } catch (e) {
691
+ throw new Error(`error creating account of type SOLANA with userId ${userId} and walletId ${walletId}`);
692
+ }
671
693
  });
672
- const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
673
- try {
674
- const newSigner = await new Promise(
675
- (resolve, reject) => global.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
676
- if (err) {
677
- reject(err);
678
- }
679
- resolve(result);
680
- })
681
- );
682
- return { signer: newSigner, walletId };
683
- } catch (e) {
684
- throw new Error(`error creating account of type SOLANA with walletId ${walletId}`);
685
- }
686
- }
687
- async function ed25519Sign(ctx, share, userId, walletId, base64Bytes) {
688
- const { protocolId } = await ctx.client.preSignMessage(userId, walletId, base64Bytes, import_core_sdk.WalletScheme.ED25519);
689
- try {
690
- const base64Sig = await new Promise(
691
- (resolve, reject) => global.ed25519Sign(share, protocolId, base64Bytes, (err, result) => {
692
- if (err) {
693
- reject(err);
694
- }
695
- resolve(result);
696
- })
697
- );
698
- return { signature: base64Sig };
699
- } catch (e) {
700
- throw new Error(`error signing for account of type SOLANA with userId ${userId} and walletId ${walletId}`);
701
- }
702
694
  }
703
- async function keygen(ctx, userId, type, secretKey) {
704
- const { walletId, protocolId } = await ctx.client.createWallet(userId, {
705
- useTwoSigners: true,
706
- scheme: ctx.useDKLS ? import_core_sdk.WalletScheme.DKLS : import_core_sdk.WalletScheme.CGGMP,
707
- type,
708
- cosmosPrefix: type === import_core_sdk.WalletType.COSMOS ? ctx.cosmosPrefix : void 0
709
- });
710
- if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
711
- return {
712
- signer: (await keygenRequest(ctx, userId, walletId, protocolId)).signer,
713
- walletId
714
- };
715
- }
716
- const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
717
- const signerConfigUser = ctx.useDKLS ? configDKLSBase(walletId, "USER", ctx.disableWebSockets) : configCGGMPBase(serverUrl, walletId, "USER");
718
- const createAccountFn = ctx.useDKLS ? global.dklsCreateAccount : global.createAccountV2;
719
- try {
720
- const newSigner = await new Promise(
721
- (resolve, reject) => createAccountFn(
722
- signerConfigUser,
723
- serverUrl,
724
- protocolId,
725
- secretKey,
726
- () => {
727
- },
728
- // no-op for deprecated callback to update progress percentage
729
- (err, result) => {
695
+ function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType) {
696
+ return __async(this, null, function* () {
697
+ const { walletId, protocolId } = yield ctx.client.createPregenWallet({
698
+ pregenIdentifier,
699
+ pregenIdentifierType,
700
+ scheme: import_core_sdk.WalletScheme.ED25519,
701
+ type: import_core_sdk.WalletType.SOLANA
702
+ });
703
+ const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
704
+ try {
705
+ const newSigner = yield new Promise(
706
+ (resolve, reject) => global.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
730
707
  if (err) {
731
708
  reject(err);
732
709
  }
733
710
  resolve(result);
734
- }
735
- )
736
- );
737
- return { signer: newSigner, walletId };
738
- } catch (e) {
739
- throw new Error(`error creating account of type ${type} with userId ${userId} and walletId ${walletId}`);
740
- }
741
- }
742
- async function preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey) {
743
- const { walletId, protocolId } = await ctx.client.createPregenWallet({
744
- pregenIdentifier,
745
- pregenIdentifierType,
746
- type,
747
- cosmosPrefix: type === import_core_sdk.WalletType.COSMOS ? ctx.cosmosPrefix : void 0
711
+ })
712
+ );
713
+ return { signer: newSigner, walletId };
714
+ } catch (e) {
715
+ throw new Error(`error creating account of type SOLANA with walletId ${walletId}`);
716
+ }
748
717
  });
749
- if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
750
- return {
751
- signer: (await keygenRequest(ctx, partnerId, walletId, protocolId)).signer,
752
- walletId
753
- };
754
- }
755
- const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
756
- const signerConfigUser = ctx.useDKLS ? configDKLSBase(walletId, "USER", ctx.disableWebSockets) : configCGGMPBase(serverUrl, walletId, "USER");
757
- const createAccountFn = ctx.useDKLS ? global.dklsCreateAccount : global.createAccountV2;
758
- try {
759
- const newSigner = await new Promise(
760
- (resolve, reject) => createAccountFn(
761
- signerConfigUser,
762
- serverUrl,
763
- protocolId,
764
- secretKey,
765
- () => {
766
- },
767
- // no-op for deprecated callback to update progress percentage
768
- (err, result) => {
718
+ }
719
+ function ed25519Sign(ctx, share, userId, walletId, base64Bytes) {
720
+ return __async(this, null, function* () {
721
+ const { protocolId } = yield ctx.client.preSignMessage(userId, walletId, base64Bytes, import_core_sdk.WalletScheme.ED25519);
722
+ try {
723
+ const base64Sig = yield new Promise(
724
+ (resolve, reject) => global.ed25519Sign(share, protocolId, base64Bytes, (err, result) => {
769
725
  if (err) {
770
726
  reject(err);
771
727
  }
772
728
  resolve(result);
773
- }
774
- )
775
- );
776
- return { signer: newSigner, walletId };
777
- } catch (e) {
778
- throw new Error(`error creating account of type ${type} with walletId ${walletId}`);
779
- }
780
- }
781
- async function signMessage(ctx, share, walletId, userId, message) {
782
- const { protocolId, pendingTransactionId } = await ctx.client.preSignMessage(userId, walletId, message);
783
- if (pendingTransactionId) {
784
- return { pendingTransactionId };
785
- }
786
- if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
787
- return signMessageRequest(ctx, userId, walletId, protocolId, message, share);
788
- }
789
- const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
790
- const signMessageFn = ctx.useDKLS ? global.dklsSignMessage : global.signMessage;
791
- try {
792
- return new Promise(
793
- (resolve, reject) => signMessageFn(share, serverUrl, message, protocolId, (err, result) => {
794
- if (err) {
795
- reject(err);
796
- }
797
- resolve({ signature: result });
798
- })
799
- );
800
- } catch (e) {
801
- throw new Error(`error signing for account with userId ${userId} and walletId ${walletId}`);
802
- }
803
- }
804
- async function signTransaction(ctx, share, walletId, userId, tx, chainId) {
805
- const {
806
- data: { protocolId, pendingTransactionId }
807
- } = await ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId });
808
- if (pendingTransactionId) {
809
- return { pendingTransactionId };
810
- }
811
- if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
812
- return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
813
- }
814
- const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
815
- const signTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
816
- try {
817
- return new Promise(
818
- (resolve, reject) => signTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
819
- if (err) {
820
- reject(err);
821
- }
822
- resolve({ signature: result });
823
- })
824
- );
825
- } catch (e) {
826
- throw new Error(`error signing transaction for account with userId ${userId} and walletId ${walletId}`);
827
- }
828
- }
829
- async function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
830
- const {
831
- data: { protocolId, pendingTransactionId }
832
- } = await ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId });
833
- if (pendingTransactionId) {
834
- return { pendingTransactionId };
835
- }
836
- if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
837
- return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
838
- }
839
- const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
840
- const sendTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
841
- try {
842
- return new Promise(
843
- (resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
844
- if (err) {
845
- reject(err);
846
- }
847
- resolve({ signature: result });
848
- })
849
- );
850
- } catch (e) {
851
- throw new Error(`error signing transaction to send for account with userId ${userId} and walletId ${walletId}`);
852
- }
853
- }
854
- async function refresh(ctx, share, walletId, userId) {
855
- const {
856
- data: { protocolId }
857
- } = await ctx.client.refreshKeys(userId, walletId);
858
- const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
859
- const refreshFn = ctx.useDKLS ? global.dklsRefresh : global.refresh;
860
- try {
861
- return new Promise(
862
- (resolve, reject) => refreshFn(share, serverUrl, protocolId, (err, result) => {
863
- if (err) {
864
- reject(err);
865
- }
866
- resolve(result);
867
- })
868
- );
869
- } catch (e) {
870
- throw new Error(`error refreshing keys for account with userId ${userId} and walletId ${walletId}`);
871
- }
729
+ })
730
+ );
731
+ return { signature: base64Sig };
732
+ } catch (e) {
733
+ throw new Error(`error signing for account of type SOLANA with userId ${userId} and walletId ${walletId}`);
734
+ }
735
+ });
872
736
  }
873
- async function getPrivateKey(ctx, share, walletId, userId) {
874
- const paraShare = await ctx.client.getParaShare(userId, walletId);
875
- if (!paraShare) {
876
- console.error("unable to retrieve Para share");
877
- return "";
878
- }
879
- try {
880
- return new Promise(
881
- (resolve, reject) => global.getPrivateKey(share, paraShare, (err, result) => {
882
- if (err) {
883
- reject(err);
884
- }
885
- resolve(result);
886
- })
887
- );
888
- } catch (e) {
889
- throw new Error(`error getting private key for account with userId ${userId} and walletId ${walletId}`);
890
- }
737
+ function keygen(ctx, userId, type, secretKey) {
738
+ return __async(this, null, function* () {
739
+ const { walletId, protocolId } = yield ctx.client.createWallet(userId, {
740
+ useTwoSigners: true,
741
+ scheme: ctx.useDKLS ? import_core_sdk.WalletScheme.DKLS : import_core_sdk.WalletScheme.CGGMP,
742
+ type,
743
+ cosmosPrefix: type === import_core_sdk.WalletType.COSMOS ? ctx.cosmosPrefix : void 0
744
+ });
745
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
746
+ return {
747
+ signer: (yield keygenRequest(ctx, userId, walletId, protocolId)).signer,
748
+ walletId
749
+ };
750
+ }
751
+ const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
752
+ const signerConfigUser = ctx.useDKLS ? configDKLSBase(walletId, "USER", ctx.disableWebSockets) : configCGGMPBase(serverUrl, walletId, "USER");
753
+ const createAccountFn = ctx.useDKLS ? global.dklsCreateAccount : global.createAccountV2;
754
+ try {
755
+ const newSigner = yield new Promise(
756
+ (resolve, reject) => createAccountFn(
757
+ signerConfigUser,
758
+ serverUrl,
759
+ protocolId,
760
+ secretKey,
761
+ () => {
762
+ },
763
+ // no-op for deprecated callback to update progress percentage
764
+ (err, result) => {
765
+ if (err) {
766
+ reject(err);
767
+ }
768
+ resolve(result);
769
+ }
770
+ )
771
+ );
772
+ return { signer: newSigner, walletId };
773
+ } catch (e) {
774
+ throw new Error(`error creating account of type ${type} with userId ${userId} and walletId ${walletId}`);
775
+ }
776
+ });
891
777
  }
892
-
893
- // src/workers/worker.ts
894
- var rawWasm;
895
- async function requestWasmWithRetries(ctx, retries = 3) {
896
- for (let i = 0; i < retries; i++) {
778
+ function preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey) {
779
+ return __async(this, null, function* () {
780
+ const { walletId, protocolId } = yield ctx.client.createPregenWallet({
781
+ pregenIdentifier,
782
+ pregenIdentifierType,
783
+ type,
784
+ cosmosPrefix: type === import_core_sdk.WalletType.COSMOS ? ctx.cosmosPrefix : void 0
785
+ });
786
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
787
+ return {
788
+ signer: (yield keygenRequest(ctx, partnerId, walletId, protocolId)).signer,
789
+ walletId
790
+ };
791
+ }
792
+ const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
793
+ const signerConfigUser = ctx.useDKLS ? configDKLSBase(walletId, "USER", ctx.disableWebSockets) : configCGGMPBase(serverUrl, walletId, "USER");
794
+ const createAccountFn = ctx.useDKLS ? global.dklsCreateAccount : global.createAccountV2;
897
795
  try {
898
- return await import_axios.default.get(`${(0, import_core_sdk2.getPortalBaseURL)(ctx, true, true)}/static/js/main.wasm`, { responseType: "arraybuffer" });
796
+ const newSigner = yield new Promise(
797
+ (resolve, reject) => createAccountFn(
798
+ signerConfigUser,
799
+ serverUrl,
800
+ protocolId,
801
+ secretKey,
802
+ () => {
803
+ },
804
+ // no-op for deprecated callback to update progress percentage
805
+ (err, result) => {
806
+ if (err) {
807
+ reject(err);
808
+ }
809
+ resolve(result);
810
+ }
811
+ )
812
+ );
813
+ return { signer: newSigner, walletId };
899
814
  } catch (e) {
900
- if (i === retries - 1) {
901
- throw e;
902
- }
815
+ throw new Error(`error creating account of type ${type} with walletId ${walletId}`);
903
816
  }
904
- }
817
+ });
905
818
  }
906
- async function loadWasm(ctx) {
907
- await Promise.resolve().then(() => __toESM(require_wasm_exec()));
908
- global.WebSocket = require("ws");
909
- const goWasm = new global.Go();
910
- if (!rawWasm) {
911
- rawWasm = (await requestWasmWithRetries(ctx)).data;
912
- }
913
- const wasmBuffer = new Uint8Array(rawWasm);
914
- const webAssemblySource = await WebAssembly.instantiate(wasmBuffer, goWasm.importObject);
915
- goWasm.run(webAssemblySource.instance);
819
+ function signMessage(ctx, share, walletId, userId, message) {
820
+ return __async(this, null, function* () {
821
+ const { protocolId, pendingTransactionId } = yield ctx.client.preSignMessage(userId, walletId, message);
822
+ if (pendingTransactionId) {
823
+ return { pendingTransactionId };
824
+ }
825
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
826
+ return signMessageRequest(ctx, userId, walletId, protocolId, message, share);
827
+ }
828
+ const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
829
+ const signMessageFn = ctx.useDKLS ? global.dklsSignMessage : global.signMessage;
830
+ try {
831
+ return new Promise(
832
+ (resolve, reject) => signMessageFn(share, serverUrl, message, protocolId, (err, result) => {
833
+ if (err) {
834
+ reject(err);
835
+ }
836
+ resolve({ signature: result });
837
+ })
838
+ );
839
+ } catch (e) {
840
+ throw new Error(`error signing for account with userId ${userId} and walletId ${walletId}`);
841
+ }
842
+ });
916
843
  }
917
- async function executeMessage(ctx, message) {
918
- const { functionType, params } = message;
919
- switch (functionType) {
920
- case "KEYGEN": {
921
- const { userId, secretKey, type = import_core_sdk2.WalletType.EVM } = params;
922
- return keygen(ctx, userId, type, secretKey);
844
+ function signTransaction(ctx, share, walletId, userId, tx, chainId) {
845
+ return __async(this, null, function* () {
846
+ const {
847
+ data: { protocolId, pendingTransactionId }
848
+ } = yield ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId });
849
+ if (pendingTransactionId) {
850
+ return { pendingTransactionId };
923
851
  }
924
- case "SIGN_TRANSACTION": {
925
- const { share, walletId, userId, tx, chainId } = params;
926
- return signTransaction(ctx, share, walletId, userId, tx, chainId);
852
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
853
+ return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
927
854
  }
928
- case "SEND_TRANSACTION": {
929
- const { share, walletId, userId, tx, chainId } = params;
930
- return sendTransaction(ctx, share, walletId, userId, tx, chainId);
855
+ const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
856
+ const signTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
857
+ try {
858
+ return new Promise(
859
+ (resolve, reject) => signTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
860
+ if (err) {
861
+ reject(err);
862
+ }
863
+ resolve({ signature: result });
864
+ })
865
+ );
866
+ } catch (e) {
867
+ throw new Error(`error signing transaction for account with userId ${userId} and walletId ${walletId}`);
931
868
  }
932
- case "SIGN_MESSAGE": {
933
- const { share, walletId, userId, message: message2 } = params;
934
- return signMessage(ctx, share, walletId, userId, message2);
869
+ });
870
+ }
871
+ function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
872
+ return __async(this, null, function* () {
873
+ const {
874
+ data: { protocolId, pendingTransactionId }
875
+ } = yield ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId });
876
+ if (pendingTransactionId) {
877
+ return { pendingTransactionId };
935
878
  }
936
- case "REFRESH": {
937
- const { share, walletId, userId } = params;
938
- return refresh(ctx, share, walletId, userId);
879
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
880
+ return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
939
881
  }
940
- case "PREKEYGEN": {
941
- const { email, partnerId, secretKey, type = import_core_sdk2.WalletType.EVM } = params;
942
- let { pregenIdentifier, pregenIdentifierType } = params;
943
- if (email !== "null" && email !== "undefined" && email !== "" && email != null) {
944
- pregenIdentifier = email;
945
- pregenIdentifierType = "EMAIL";
946
- }
947
- const keygenRes = await preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey);
948
- return keygenRes;
882
+ const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
883
+ const sendTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
884
+ try {
885
+ return new Promise(
886
+ (resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
887
+ if (err) {
888
+ reject(err);
889
+ }
890
+ resolve({ signature: result });
891
+ })
892
+ );
893
+ } catch (e) {
894
+ throw new Error(`error signing transaction to send for account with userId ${userId} and walletId ${walletId}`);
949
895
  }
950
- case "GET_PRIVATE_KEY": {
951
- const { share, walletId, userId } = params;
952
- return await getPrivateKey(ctx, share, walletId, userId);
896
+ });
897
+ }
898
+ function refresh(ctx, share, walletId, userId) {
899
+ return __async(this, null, function* () {
900
+ const {
901
+ data: { protocolId }
902
+ } = yield ctx.client.refreshKeys(userId, walletId);
903
+ const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
904
+ const refreshFn = ctx.useDKLS ? global.dklsRefresh : global.refresh;
905
+ try {
906
+ return new Promise(
907
+ (resolve, reject) => refreshFn(share, serverUrl, protocolId, (err, result) => {
908
+ if (err) {
909
+ reject(err);
910
+ }
911
+ resolve(result);
912
+ })
913
+ );
914
+ } catch (e) {
915
+ throw new Error(`error refreshing keys for account with userId ${userId} and walletId ${walletId}`);
953
916
  }
954
- case "ED25519_KEYGEN": {
955
- const { userId } = params;
956
- return ed25519Keygen(ctx, userId);
917
+ });
918
+ }
919
+ function getPrivateKey(ctx, share, walletId, userId) {
920
+ return __async(this, null, function* () {
921
+ const paraShare = yield ctx.client.getParaShare(userId, walletId);
922
+ if (!paraShare) {
923
+ console.error("unable to retrieve Para share");
924
+ return "";
957
925
  }
958
- case "ED25519_SIGN": {
959
- const { share, walletId, userId, base64Bytes } = params;
960
- return ed25519Sign(ctx, share, userId, walletId, base64Bytes);
926
+ try {
927
+ return new Promise(
928
+ (resolve, reject) => global.getPrivateKey(share, paraShare, (err, result) => {
929
+ if (err) {
930
+ reject(err);
931
+ }
932
+ resolve(result);
933
+ })
934
+ );
935
+ } catch (e) {
936
+ throw new Error(`error getting private key for account with userId ${userId} and walletId ${walletId}`);
961
937
  }
962
- case "ED25519_PREKEYGEN": {
963
- const { email } = params;
964
- let { pregenIdentifier, pregenIdentifierType } = params;
965
- if (email !== "null" && email !== "undefined" && email !== "" && email != null) {
966
- pregenIdentifier = email;
967
- pregenIdentifierType = "EMAIL";
938
+ });
939
+ }
940
+
941
+ // src/workers/worker.ts
942
+ var rawWasm;
943
+ function requestWasmWithRetries(ctx, retries = 3) {
944
+ return __async(this, null, function* () {
945
+ for (let i = 0; i < retries; i++) {
946
+ try {
947
+ return yield import_axios.default.get(`${(0, import_core_sdk2.getPortalBaseURL)(ctx, true, true)}/static/js/main.wasm`, { responseType: "arraybuffer" });
948
+ } catch (e) {
949
+ if (i === retries - 1) {
950
+ throw e;
951
+ }
968
952
  }
969
- return ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType);
970
953
  }
971
- default: {
972
- throw new Error(`functionType: ${functionType} not supported`);
954
+ });
955
+ }
956
+ function loadWasm(ctx) {
957
+ return __async(this, null, function* () {
958
+ yield Promise.resolve().then(() => __toESM(require_wasm_exec()));
959
+ global.WebSocket = require("ws");
960
+ const goWasm = new global.Go();
961
+ if (!rawWasm) {
962
+ rawWasm = (yield requestWasmWithRetries(ctx)).data;
973
963
  }
974
- }
964
+ const wasmBuffer = new Uint8Array(rawWasm);
965
+ const webAssemblySource = yield WebAssembly.instantiate(wasmBuffer, goWasm.importObject);
966
+ goWasm.run(webAssemblySource.instance);
967
+ });
975
968
  }
976
- async function handleMessage(e) {
977
- const {
978
- env,
979
- apiKey,
980
- cosmosPrefix = "cosmos",
981
- offloadMPCComputationURL,
982
- disableWorkers,
983
- sessionCookie,
984
- useDKLS,
985
- disableWebSockets,
986
- workId
987
- } = e.data;
988
- const ctx = {
989
- env,
990
- apiKey,
991
- client: (0, import_core_sdk2.initClient)({ env, version: import_core_sdk2.paraVersion, apiKey, retrieveSessionCookie: () => sessionCookie }),
992
- offloadMPCComputationURL,
993
- mpcComputationClient: offloadMPCComputationURL ? import_core_sdk2.mpcComputationClient.initClient(offloadMPCComputationURL, !!disableWorkers) : void 0,
994
- useDKLS,
995
- disableWebSockets: !!disableWebSockets,
996
- cosmosPrefix
997
- };
998
- if (!ctx.offloadMPCComputationURL || ctx.useDKLS) {
999
- await loadWasm(ctx);
1000
- }
1001
- const result = await executeMessage(ctx, e.data);
1002
- result.workId = workId;
1003
- return result;
969
+ function executeMessage(ctx, message) {
970
+ return __async(this, null, function* () {
971
+ const { functionType, params } = message;
972
+ switch (functionType) {
973
+ case "KEYGEN": {
974
+ const { userId, secretKey, type = import_core_sdk2.WalletType.EVM } = params;
975
+ return keygen(ctx, userId, type, secretKey);
976
+ }
977
+ case "SIGN_TRANSACTION": {
978
+ const { share, walletId, userId, tx, chainId } = params;
979
+ return signTransaction(ctx, share, walletId, userId, tx, chainId);
980
+ }
981
+ case "SEND_TRANSACTION": {
982
+ const { share, walletId, userId, tx, chainId } = params;
983
+ return sendTransaction(ctx, share, walletId, userId, tx, chainId);
984
+ }
985
+ case "SIGN_MESSAGE": {
986
+ const { share, walletId, userId, message: message2 } = params;
987
+ return signMessage(ctx, share, walletId, userId, message2);
988
+ }
989
+ case "REFRESH": {
990
+ const { share, walletId, userId } = params;
991
+ return refresh(ctx, share, walletId, userId);
992
+ }
993
+ case "PREKEYGEN": {
994
+ const { email, partnerId, secretKey, type = import_core_sdk2.WalletType.EVM } = params;
995
+ let { pregenIdentifier, pregenIdentifierType } = params;
996
+ if (email !== "null" && email !== "undefined" && email !== "" && email != null) {
997
+ pregenIdentifier = email;
998
+ pregenIdentifierType = "EMAIL";
999
+ }
1000
+ const keygenRes = yield preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey);
1001
+ return keygenRes;
1002
+ }
1003
+ case "GET_PRIVATE_KEY": {
1004
+ const { share, walletId, userId } = params;
1005
+ return yield getPrivateKey(ctx, share, walletId, userId);
1006
+ }
1007
+ case "ED25519_KEYGEN": {
1008
+ const { userId } = params;
1009
+ return ed25519Keygen(ctx, userId);
1010
+ }
1011
+ case "ED25519_SIGN": {
1012
+ const { share, walletId, userId, base64Bytes } = params;
1013
+ return ed25519Sign(ctx, share, userId, walletId, base64Bytes);
1014
+ }
1015
+ case "ED25519_PREKEYGEN": {
1016
+ const { email } = params;
1017
+ let { pregenIdentifier, pregenIdentifierType } = params;
1018
+ if (email !== "null" && email !== "undefined" && email !== "" && email != null) {
1019
+ pregenIdentifier = email;
1020
+ pregenIdentifierType = "EMAIL";
1021
+ }
1022
+ return ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType);
1023
+ }
1024
+ default: {
1025
+ throw new Error(`functionType: ${functionType} not supported`);
1026
+ }
1027
+ }
1028
+ });
1029
+ }
1030
+ function handleMessage(e) {
1031
+ return __async(this, null, function* () {
1032
+ const {
1033
+ env,
1034
+ apiKey,
1035
+ cosmosPrefix = "cosmos",
1036
+ offloadMPCComputationURL,
1037
+ disableWorkers,
1038
+ sessionCookie,
1039
+ useDKLS,
1040
+ disableWebSockets,
1041
+ workId
1042
+ } = e.data;
1043
+ const ctx = {
1044
+ env,
1045
+ apiKey,
1046
+ client: (0, import_core_sdk2.initClient)({ env, version: import_core_sdk2.paraVersion, apiKey, retrieveSessionCookie: () => sessionCookie }),
1047
+ offloadMPCComputationURL,
1048
+ mpcComputationClient: offloadMPCComputationURL ? import_core_sdk2.mpcComputationClient.initClient(offloadMPCComputationURL, !!disableWorkers) : void 0,
1049
+ useDKLS,
1050
+ disableWebSockets: !!disableWebSockets,
1051
+ cosmosPrefix
1052
+ };
1053
+ if (!ctx.offloadMPCComputationURL || ctx.useDKLS) {
1054
+ yield loadWasm(ctx);
1055
+ }
1056
+ const result = yield executeMessage(ctx, e.data);
1057
+ result.workId = workId;
1058
+ return result;
1059
+ });
1004
1060
  }
1005
1061
  // Annotate the CommonJS export names for ESM import in node:
1006
1062
  0 && (module.exports = {