@getpara/server-sdk 2.0.0-dev.0 → 2.0.0-dev.2

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.
Files changed (47) hide show
  1. package/dist/cjs/ParaServer.js +101 -0
  2. package/dist/cjs/ServerLocalStorage.js +48 -0
  3. package/dist/cjs/ServerSessionStorage.js +48 -0
  4. package/dist/cjs/ServerUtils.js +77 -0
  5. package/dist/cjs/index.js +3 -523
  6. package/dist/cjs/package.json +3 -0
  7. package/dist/cjs/wallet/keygen.js +315 -0
  8. package/dist/cjs/wallet/privateKey.js +99 -0
  9. package/dist/cjs/wallet/signing.js +224 -0
  10. package/dist/cjs/wasm/wasm_exec.js +589 -0
  11. package/dist/cjs/workers/walletUtils.js +409 -0
  12. package/dist/cjs/workers/worker.js +62 -907
  13. package/dist/cjs/workers/workerWrapper.js +130 -0
  14. package/dist/esm/ParaServer.js +45 -0
  15. package/dist/esm/ServerLocalStorage.js +26 -0
  16. package/dist/esm/ServerSessionStorage.js +26 -0
  17. package/dist/esm/ServerUtils.js +55 -0
  18. package/dist/esm/chunk-FTA5RKYX.js +8 -0
  19. package/dist/esm/index.js +4 -466
  20. package/dist/esm/package.json +6 -0
  21. package/dist/esm/wallet/keygen.js +230 -0
  22. package/dist/esm/wallet/privateKey.js +45 -0
  23. package/dist/esm/wallet/signing.js +161 -0
  24. package/dist/esm/{workers/wasm_exec-CFNSOXDO.js → wasm/wasm_exec.js} +68 -74
  25. package/dist/esm/workers/walletUtils.js +314 -0
  26. package/dist/esm/workers/worker.js +78 -337
  27. package/dist/esm/workers/workerWrapper.js +85 -0
  28. package/dist/types/ParaServer.d.ts +4 -5
  29. package/dist/types/ServerUtils.d.ts +5 -6
  30. package/dist/types/wallet/keygen.d.ts +5 -3
  31. package/dist/types/workers/walletUtils.d.ts +4 -6
  32. package/dist/types/workers/worker.d.ts +10 -1
  33. package/dist/types/workers/workerWrapper.d.ts +2 -2
  34. package/package.json +20 -20
  35. package/dist/cjs/index.js.br +0 -0
  36. package/dist/cjs/index.js.gz +0 -0
  37. package/dist/cjs/workers/worker.js.br +0 -0
  38. package/dist/cjs/workers/worker.js.gz +0 -0
  39. package/dist/esm/index.js.br +0 -0
  40. package/dist/esm/index.js.gz +0 -0
  41. package/dist/esm/workers/chunk-ILICZWQV.js +0 -36
  42. package/dist/esm/workers/chunk-ILICZWQV.js.br +0 -0
  43. package/dist/esm/workers/chunk-ILICZWQV.js.gz +0 -0
  44. package/dist/esm/workers/wasm_exec-CFNSOXDO.js.br +0 -0
  45. package/dist/esm/workers/wasm_exec-CFNSOXDO.js.gz +0 -0
  46. package/dist/esm/workers/worker.js.br +0 -0
  47. package/dist/esm/workers/worker.js.gz +0 -0
@@ -0,0 +1,45 @@
1
+ import "../chunk-FTA5RKYX.js";
2
+ import * as uuid from "uuid";
3
+ import { setupWorker } from "../workers/workerWrapper.js";
4
+ async function getPrivateKey(ctx, userId, walletId, share, sessionCookie) {
5
+ return new Promise(async (resolve, reject) => {
6
+ const workId = uuid.v4();
7
+ try {
8
+ const worker = await setupWorker(
9
+ ctx,
10
+ async (res) => {
11
+ resolve(res.privateKey);
12
+ },
13
+ (error) => {
14
+ reject(error);
15
+ },
16
+ workId,
17
+ {
18
+ params: { walletId, userId },
19
+ functionType: "GET_PRIVATE_KEY",
20
+ disableWorkers: ctx.disableWorkers,
21
+ disableWebSockets: ctx.disableWebSockets
22
+ }
23
+ );
24
+ worker.postMessage({
25
+ env: ctx.env,
26
+ apiKey: ctx.apiKey,
27
+ cosmosPrefix: ctx.cosmosPrefix,
28
+ params: { share, walletId, userId },
29
+ functionType: "GET_PRIVATE_KEY",
30
+ offloadMPCComputationURL: ctx.offloadMPCComputationURL,
31
+ disableWorkers: ctx.disableWorkers,
32
+ sessionCookie,
33
+ useDKLS: ctx.useDKLS,
34
+ disableWebSockets: ctx.disableWebSockets,
35
+ wasmOverride: ctx.wasmOverride,
36
+ workId
37
+ });
38
+ } catch (error) {
39
+ reject(error);
40
+ }
41
+ });
42
+ }
43
+ export {
44
+ getPrivateKey
45
+ };
@@ -0,0 +1,161 @@
1
+ import "../chunk-FTA5RKYX.js";
2
+ import * as uuid from "uuid";
3
+ import { setupWorker } from "../workers/workerWrapper.js";
4
+ async function signTransaction(ctx, userId, walletId, share, tx, chainId, sessionCookie, isDKLS) {
5
+ return new Promise(async (resolve, reject) => {
6
+ const workId = uuid.v4();
7
+ try {
8
+ const worker = await setupWorker(
9
+ ctx,
10
+ async (sendTransactionRes) => {
11
+ resolve(sendTransactionRes);
12
+ },
13
+ (error) => {
14
+ reject(error);
15
+ },
16
+ workId,
17
+ {
18
+ params: { walletId, userId, tx, chainId },
19
+ functionType: "SIGN_TRANSACTION",
20
+ disableWorkers: ctx.disableWorkers,
21
+ disableWebSockets: ctx.disableWebSockets
22
+ }
23
+ );
24
+ worker.postMessage({
25
+ env: ctx.env,
26
+ apiKey: ctx.apiKey,
27
+ cosmosPrefix: ctx.cosmosPrefix,
28
+ params: { share, walletId, userId, tx, chainId },
29
+ functionType: "SIGN_TRANSACTION",
30
+ offloadMPCComputationURL: ctx.offloadMPCComputationURL,
31
+ disableWorkers: ctx.disableWorkers,
32
+ sessionCookie,
33
+ useDKLS: isDKLS,
34
+ disableWebSockets: ctx.disableWebSockets,
35
+ wasmOverride: ctx.wasmOverride,
36
+ workId
37
+ });
38
+ } catch (error) {
39
+ reject(error);
40
+ }
41
+ });
42
+ }
43
+ async function sendTransaction(ctx, userId, walletId, share, tx, chainId, sessionCookie, isDKLS) {
44
+ return new Promise(async (resolve, reject) => {
45
+ const workId = uuid.v4();
46
+ try {
47
+ const worker = await setupWorker(
48
+ ctx,
49
+ async (sendTransactionRes) => {
50
+ resolve(sendTransactionRes);
51
+ },
52
+ (error) => {
53
+ reject(error);
54
+ },
55
+ workId,
56
+ {
57
+ params: { walletId, userId, tx, chainId },
58
+ functionType: "SEND_TRANSACTION",
59
+ disableWorkers: ctx.disableWorkers,
60
+ disableWebSockets: ctx.disableWebSockets
61
+ }
62
+ );
63
+ worker.postMessage({
64
+ env: ctx.env,
65
+ apiKey: ctx.apiKey,
66
+ params: { share, walletId, userId, tx, chainId },
67
+ functionType: "SEND_TRANSACTION",
68
+ offloadMPCComputationURL: ctx.offloadMPCComputationURL,
69
+ disableWorkers: ctx.disableWorkers,
70
+ sessionCookie,
71
+ useDKLS: isDKLS,
72
+ disableWebSockets: ctx.disableWebSockets,
73
+ wasmOverride: ctx.wasmOverride,
74
+ workId
75
+ });
76
+ } catch (error) {
77
+ reject(error);
78
+ }
79
+ });
80
+ }
81
+ async function signMessage(ctx, userId, walletId, share, message, sessionCookie, isDKLS) {
82
+ return new Promise(async (resolve, reject) => {
83
+ const workId = uuid.v4();
84
+ try {
85
+ const worker = await setupWorker(
86
+ ctx,
87
+ async (signMessageRes) => {
88
+ resolve(signMessageRes);
89
+ },
90
+ (error) => {
91
+ reject(error);
92
+ },
93
+ workId,
94
+ {
95
+ params: { walletId, userId, message },
96
+ functionType: "SIGN_MESSAGE",
97
+ disableWorkers: ctx.disableWorkers,
98
+ disableWebSockets: ctx.disableWebSockets
99
+ }
100
+ );
101
+ worker.postMessage({
102
+ env: ctx.env,
103
+ apiKey: ctx.apiKey,
104
+ params: { share, walletId, userId, message },
105
+ functionType: "SIGN_MESSAGE",
106
+ offloadMPCComputationURL: ctx.offloadMPCComputationURL,
107
+ disableWorkers: ctx.disableWorkers,
108
+ sessionCookie,
109
+ useDKLS: isDKLS,
110
+ disableWebSockets: ctx.disableWebSockets,
111
+ wasmOverride: ctx.wasmOverride,
112
+ workId
113
+ });
114
+ } catch (error) {
115
+ reject(error);
116
+ }
117
+ });
118
+ }
119
+ async function ed25519Sign(ctx, userId, walletId, share, base64Bytes, sessionCookie) {
120
+ return new Promise(async (resolve, reject) => {
121
+ const workId = uuid.v4();
122
+ try {
123
+ const worker = await setupWorker(
124
+ ctx,
125
+ async (signMessageRes) => {
126
+ resolve(signMessageRes);
127
+ },
128
+ (error) => {
129
+ reject(error);
130
+ },
131
+ workId,
132
+ {
133
+ params: { walletId, userId, base64Bytes },
134
+ functionType: "ED25519_SIGN",
135
+ disableWorkers: ctx.disableWorkers,
136
+ disableWebSockets: ctx.disableWebSockets
137
+ }
138
+ );
139
+ worker.postMessage({
140
+ env: ctx.env,
141
+ apiKey: ctx.apiKey,
142
+ params: { share, walletId, userId, base64Bytes },
143
+ functionType: "ED25519_SIGN",
144
+ offloadMPCComputationURL: ctx.offloadMPCComputationURL,
145
+ disableWorkers: ctx.disableWorkers,
146
+ sessionCookie,
147
+ disableWebSockets: ctx.disableWebSockets,
148
+ wasmOverride: ctx.wasmOverride,
149
+ workId
150
+ });
151
+ } catch (error) {
152
+ reject(error);
153
+ }
154
+ });
155
+ }
156
+ export {
157
+ ed25519Sign,
158
+ sendTransaction,
159
+ signMessage,
160
+ signTransaction
161
+ };
@@ -1,17 +1,13 @@
1
1
  import {
2
- __async,
3
- __commonJS,
4
- __require
5
- } from "./chunk-ILICZWQV.js";
6
-
7
- // src/wasm/wasm_exec.js
2
+ __commonJS
3
+ } from "../chunk-FTA5RKYX.js";
8
4
  var require_wasm_exec = __commonJS({
9
5
  "src/wasm/wasm_exec.js"(exports, module) {
10
- var globalThisCopy = global;
6
+ const globalThisCopy = global;
11
7
  if (!globalThisCopy.crypto) {
12
- globalThisCopy.crypto = __require("crypto");
8
+ globalThisCopy.crypto = require("crypto");
13
9
  }
14
- var crypto = globalThisCopy.crypto;
10
+ const crypto = globalThisCopy.crypto;
15
11
  (() => {
16
12
  const enosys = () => {
17
13
  const err = new Error("not implemented");
@@ -484,73 +480,71 @@ var require_wasm_exec = __commonJS({
484
480
  }
485
481
  };
486
482
  }
487
- run(instance) {
488
- return __async(this, null, function* () {
489
- if (!(instance instanceof WebAssembly.Instance)) {
490
- throw new Error("Go.run: WebAssembly.Instance expected");
491
- }
492
- this._inst = instance;
493
- this.mem = new DataView(this._inst.exports.mem.buffer);
494
- this._values = [
495
- // JS values that Go currently has references to, indexed by reference id
496
- NaN,
497
- 0,
498
- null,
499
- true,
500
- false,
501
- globalThis,
502
- this
503
- ];
504
- this._goRefCounts = new Array(this._values.length).fill(Infinity);
505
- this._ids = /* @__PURE__ */ new Map([
506
- // mapping from JS values to reference ids
507
- [0, 1],
508
- [null, 2],
509
- [true, 3],
510
- [false, 4],
511
- [globalThis, 5],
512
- [this, 6]
513
- ]);
514
- this._idPool = [];
515
- this.exited = false;
516
- let offset = 4096;
517
- const strPtr = (str) => {
518
- const ptr = offset;
519
- const bytes = encoder.encode(str + "\0");
520
- new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
521
- offset += bytes.length;
522
- if (offset % 8 !== 0) {
523
- offset += 8 - offset % 8;
524
- }
525
- return ptr;
526
- };
527
- const argc = this.argv.length;
528
- const argvPtrs = [];
529
- this.argv.forEach((arg) => {
530
- argvPtrs.push(strPtr(arg));
531
- });
532
- argvPtrs.push(0);
533
- const keys = Object.keys(this.env).sort();
534
- keys.forEach((key) => {
535
- argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
536
- });
537
- argvPtrs.push(0);
538
- const argv = offset;
539
- argvPtrs.forEach((ptr) => {
540
- this.mem.setUint32(offset, ptr, true);
541
- this.mem.setUint32(offset + 4, 0, true);
542
- offset += 8;
543
- });
544
- const wasmMinDataAddr = 4096 + 8192;
545
- if (offset >= wasmMinDataAddr) {
546
- throw new Error("total length of command line and environment variables exceeds limit");
547
- }
548
- this._inst.exports.run(argc, argv);
549
- if (this.exited) {
550
- this._resolveExitPromise();
483
+ async run(instance) {
484
+ if (!(instance instanceof WebAssembly.Instance)) {
485
+ throw new Error("Go.run: WebAssembly.Instance expected");
486
+ }
487
+ this._inst = instance;
488
+ this.mem = new DataView(this._inst.exports.mem.buffer);
489
+ this._values = [
490
+ // JS values that Go currently has references to, indexed by reference id
491
+ NaN,
492
+ 0,
493
+ null,
494
+ true,
495
+ false,
496
+ globalThis,
497
+ this
498
+ ];
499
+ this._goRefCounts = new Array(this._values.length).fill(Infinity);
500
+ this._ids = /* @__PURE__ */ new Map([
501
+ // mapping from JS values to reference ids
502
+ [0, 1],
503
+ [null, 2],
504
+ [true, 3],
505
+ [false, 4],
506
+ [globalThis, 5],
507
+ [this, 6]
508
+ ]);
509
+ this._idPool = [];
510
+ this.exited = false;
511
+ let offset = 4096;
512
+ const strPtr = (str) => {
513
+ const ptr = offset;
514
+ const bytes = encoder.encode(str + "\0");
515
+ new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
516
+ offset += bytes.length;
517
+ if (offset % 8 !== 0) {
518
+ offset += 8 - offset % 8;
551
519
  }
552
- yield this._exitPromise;
520
+ return ptr;
521
+ };
522
+ const argc = this.argv.length;
523
+ const argvPtrs = [];
524
+ this.argv.forEach((arg) => {
525
+ argvPtrs.push(strPtr(arg));
526
+ });
527
+ argvPtrs.push(0);
528
+ const keys = Object.keys(this.env).sort();
529
+ keys.forEach((key) => {
530
+ argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
553
531
  });
532
+ argvPtrs.push(0);
533
+ const argv = offset;
534
+ argvPtrs.forEach((ptr) => {
535
+ this.mem.setUint32(offset, ptr, true);
536
+ this.mem.setUint32(offset + 4, 0, true);
537
+ offset += 8;
538
+ });
539
+ const wasmMinDataAddr = 4096 + 8192;
540
+ if (offset >= wasmMinDataAddr) {
541
+ throw new Error("total length of command line and environment variables exceeds limit");
542
+ }
543
+ this._inst.exports.run(argc, argv);
544
+ if (this.exited) {
545
+ this._resolveExitPromise();
546
+ }
547
+ await this._exitPromise;
554
548
  }
555
549
  _resume() {
556
550
  if (this.exited) {
@@ -0,0 +1,314 @@
1
+ import "../chunk-FTA5RKYX.js";
2
+ import { getBaseMPCNetworkUrl } from "@getpara/core-sdk";
3
+ import * as uuid from "uuid";
4
+ const configCGGMPBase = (serverUrl, walletId, id) => `{"ServerUrl":"${serverUrl}", "WalletId": "${walletId}", "Id":"${id}", "Ids":["USER","CAPSULE"], "Threshold":1}`;
5
+ const configDKLSBase = (walletId, id, disableWebSockets) => `{"walletId": "${walletId}", "id":"${id}", "otherId":"CAPSULE", "isReceiver": false, "disableWebSockets": ${disableWebSockets}}`;
6
+ async function keygenRequest(ctx, userId, walletId, protocolId) {
7
+ const { data } = await ctx.mpcComputationClient.post("/wallets", {
8
+ userId,
9
+ walletId,
10
+ protocolId
11
+ });
12
+ return data;
13
+ }
14
+ async function signMessageRequest(ctx, userId, walletId, protocolId, message, signer) {
15
+ const { data } = await ctx.mpcComputationClient.post(`/wallets/${walletId}/messages/sign`, {
16
+ userId,
17
+ protocolId,
18
+ message,
19
+ signer
20
+ });
21
+ return data;
22
+ }
23
+ async function sendTransactionRequest(ctx, userId, walletId, protocolId, transaction, signer, chainId) {
24
+ const { data } = await ctx.mpcComputationClient.post(`/wallets/${walletId}/transactions/send`, {
25
+ userId,
26
+ protocolId,
27
+ transaction,
28
+ signer,
29
+ chainId
30
+ });
31
+ return data;
32
+ }
33
+ async function ed25519Keygen(ctx, userId) {
34
+ const { walletId, protocolId } = await ctx.client.createWallet(userId, {
35
+ scheme: "ED25519",
36
+ type: "SOLANA"
37
+ });
38
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
39
+ try {
40
+ const newSigner = await new Promise(
41
+ (resolve, reject) => global.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
42
+ if (err) {
43
+ reject(err);
44
+ }
45
+ resolve(result);
46
+ })
47
+ );
48
+ return { signer: newSigner, walletId };
49
+ } catch (e) {
50
+ throw new Error(`error creating account of type SOLANA with userId ${userId} and walletId ${walletId}`);
51
+ }
52
+ }
53
+ async function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType) {
54
+ const { walletId, protocolId } = await ctx.client.createPregenWallet({
55
+ pregenIdentifier,
56
+ pregenIdentifierType,
57
+ scheme: "ED25519",
58
+ type: "SOLANA"
59
+ });
60
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
61
+ try {
62
+ const newSigner = await new Promise(
63
+ (resolve, reject) => global.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
64
+ if (err) {
65
+ reject(err);
66
+ }
67
+ resolve(result);
68
+ })
69
+ );
70
+ return { signer: newSigner, walletId };
71
+ } catch (e) {
72
+ throw new Error(`error creating account of type SOLANA with walletId ${walletId}`);
73
+ }
74
+ }
75
+ async function ed25519Sign(ctx, share, userId, walletId, base64Bytes) {
76
+ const protocolId = uuid.v4();
77
+ const preSignMessageRes = ctx.client.preSignMessage(userId, walletId, base64Bytes, "ED25519", void 0, protocolId);
78
+ const signRes = async function() {
79
+ try {
80
+ const base64Sig = await new Promise(
81
+ (resolve, reject) => global.ed25519Sign(share, protocolId, base64Bytes, (err, result) => {
82
+ if (err) {
83
+ reject(err);
84
+ }
85
+ resolve(result);
86
+ })
87
+ );
88
+ return { signature: base64Sig };
89
+ } catch (e) {
90
+ throw new Error(`error signing for account of type SOLANA with userId ${userId} and walletId ${walletId}`);
91
+ }
92
+ }();
93
+ const { pendingTransactionId } = await preSignMessageRes;
94
+ if (pendingTransactionId) {
95
+ return { pendingTransactionId };
96
+ }
97
+ return await signRes;
98
+ }
99
+ async function keygen(ctx, userId, type, secretKey) {
100
+ const { walletId, protocolId } = await ctx.client.createWallet(userId, {
101
+ useTwoSigners: true,
102
+ scheme: ctx.useDKLS ? "DKLS" : "CGGMP",
103
+ type,
104
+ cosmosPrefix: type === "COSMOS" ? ctx.cosmosPrefix : void 0
105
+ });
106
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
107
+ return {
108
+ signer: (await keygenRequest(ctx, userId, walletId, protocolId)).signer,
109
+ walletId
110
+ };
111
+ }
112
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
113
+ const signerConfigUser = ctx.useDKLS ? configDKLSBase(walletId, "USER", ctx.disableWebSockets) : configCGGMPBase(serverUrl, walletId, "USER");
114
+ const createAccountFn = ctx.useDKLS ? global.dklsCreateAccount : global.createAccountV2;
115
+ try {
116
+ const newSigner = await new Promise(
117
+ (resolve, reject) => createAccountFn(
118
+ signerConfigUser,
119
+ serverUrl,
120
+ protocolId,
121
+ secretKey,
122
+ () => {
123
+ },
124
+ // no-op for deprecated callback to update progress percentage
125
+ (err, result) => {
126
+ if (err) {
127
+ reject(err);
128
+ }
129
+ resolve(result);
130
+ }
131
+ )
132
+ );
133
+ return { signer: newSigner, walletId };
134
+ } catch (e) {
135
+ throw new Error(`error creating account of type ${type} with userId ${userId} and walletId ${walletId}`);
136
+ }
137
+ }
138
+ async function preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey) {
139
+ const { walletId, protocolId } = await ctx.client.createPregenWallet({
140
+ pregenIdentifier,
141
+ pregenIdentifierType,
142
+ type,
143
+ cosmosPrefix: type === "COSMOS" ? ctx.cosmosPrefix : void 0
144
+ });
145
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
146
+ return {
147
+ signer: (await keygenRequest(ctx, partnerId, walletId, protocolId)).signer,
148
+ walletId
149
+ };
150
+ }
151
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
152
+ const signerConfigUser = ctx.useDKLS ? configDKLSBase(walletId, "USER", ctx.disableWebSockets) : configCGGMPBase(serverUrl, walletId, "USER");
153
+ const createAccountFn = ctx.useDKLS ? global.dklsCreateAccount : global.createAccountV2;
154
+ try {
155
+ const newSigner = await new Promise(
156
+ (resolve, reject) => createAccountFn(
157
+ signerConfigUser,
158
+ serverUrl,
159
+ protocolId,
160
+ secretKey,
161
+ () => {
162
+ },
163
+ // no-op for deprecated callback to update progress percentage
164
+ (err, result) => {
165
+ if (err) {
166
+ reject(err);
167
+ }
168
+ resolve(result);
169
+ }
170
+ )
171
+ );
172
+ return { signer: newSigner, walletId };
173
+ } catch (e) {
174
+ throw new Error(`error creating account of type ${type} with walletId ${walletId}`);
175
+ }
176
+ }
177
+ async function signMessage(ctx, share, walletId, userId, message) {
178
+ const protocolId = uuid.v4();
179
+ const preSignMessageRes = ctx.client.preSignMessage(userId, walletId, message, null, null, protocolId);
180
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
181
+ return signMessageRequest(ctx, userId, walletId, protocolId, message, share);
182
+ }
183
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
184
+ const signMessageFn = ctx.useDKLS ? global.dklsSignMessage : global.signMessage;
185
+ const signMessageRes = async function() {
186
+ try {
187
+ return await new Promise(
188
+ (resolve, reject) => signMessageFn(share, serverUrl, message, protocolId, (err, result) => {
189
+ if (err) {
190
+ reject(err);
191
+ }
192
+ resolve({ signature: result });
193
+ })
194
+ );
195
+ } catch (e) {
196
+ throw new Error(`error signing for account with userId ${userId} and walletId ${walletId}`);
197
+ }
198
+ }();
199
+ const { pendingTransactionId } = await preSignMessageRes;
200
+ if (pendingTransactionId) {
201
+ return { pendingTransactionId };
202
+ }
203
+ return await signMessageRes;
204
+ }
205
+ async function signTransaction(ctx, share, walletId, userId, tx, chainId) {
206
+ const protocolId = uuid.v4();
207
+ const signTransactionRes = ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId, protocolId });
208
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
209
+ return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
210
+ }
211
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
212
+ const signTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
213
+ const signTxRes = async function() {
214
+ try {
215
+ return await new Promise(
216
+ (resolve, reject) => signTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
217
+ if (err) {
218
+ reject(err);
219
+ }
220
+ resolve({ signature: result });
221
+ })
222
+ );
223
+ } catch (e) {
224
+ throw new Error(`error signing transaction for account with userId ${userId} and walletId ${walletId}`);
225
+ }
226
+ }();
227
+ const {
228
+ data: { pendingTransactionId }
229
+ } = await signTransactionRes;
230
+ if (pendingTransactionId) {
231
+ return { pendingTransactionId };
232
+ }
233
+ return await signTxRes;
234
+ }
235
+ async function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
236
+ const protocolId = uuid.v4();
237
+ const sendTransactionRes = ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId, protocolId });
238
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
239
+ return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
240
+ }
241
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
242
+ const sendTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
243
+ const sendTxRes = async function() {
244
+ try {
245
+ return await new Promise(
246
+ (resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
247
+ if (err) {
248
+ reject(err);
249
+ }
250
+ resolve({ signature: result });
251
+ })
252
+ );
253
+ } catch (e) {
254
+ throw new Error(`error signing transaction to send for account with userId ${userId} and walletId ${walletId}`);
255
+ }
256
+ }();
257
+ const {
258
+ data: { pendingTransactionId }
259
+ } = await sendTransactionRes;
260
+ if (pendingTransactionId) {
261
+ return { pendingTransactionId };
262
+ }
263
+ return await sendTxRes;
264
+ }
265
+ async function refresh(ctx, share, walletId, userId) {
266
+ const {
267
+ data: { protocolId }
268
+ } = await ctx.client.refreshKeys(userId, walletId);
269
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
270
+ const refreshFn = ctx.useDKLS ? global.dklsRefresh : global.refresh;
271
+ try {
272
+ return await new Promise(
273
+ (resolve, reject) => refreshFn(share, serverUrl, protocolId, (err, result) => {
274
+ if (err) {
275
+ reject(err);
276
+ }
277
+ resolve(result);
278
+ })
279
+ );
280
+ } catch (e) {
281
+ throw new Error(`error refreshing keys for account with userId ${userId} and walletId ${walletId}`);
282
+ }
283
+ }
284
+ async function getPrivateKey(ctx, share, walletId, userId) {
285
+ const paraShare = await ctx.client.getParaShare(userId, walletId);
286
+ if (!paraShare) {
287
+ console.error("unable to retrieve Para share");
288
+ return "";
289
+ }
290
+ try {
291
+ return await new Promise(
292
+ (resolve, reject) => global.getPrivateKey(share, paraShare, (err, result) => {
293
+ if (err) {
294
+ reject(err);
295
+ }
296
+ resolve(result);
297
+ })
298
+ );
299
+ } catch (e) {
300
+ throw new Error(`error getting private key for account with userId ${userId} and walletId ${walletId}`);
301
+ }
302
+ }
303
+ export {
304
+ ed25519Keygen,
305
+ ed25519PreKeygen,
306
+ ed25519Sign,
307
+ getPrivateKey,
308
+ keygen,
309
+ preKeygen,
310
+ refresh,
311
+ sendTransaction,
312
+ signMessage,
313
+ signTransaction
314
+ };