@getpara/server-sdk 1.5.0 → 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.
@@ -1,6 +1,7 @@
1
1
  import {
2
+ __async,
2
3
  __require
3
- } from "./chunk-MCKGQKYU.js";
4
+ } from "./chunk-ILICZWQV.js";
4
5
 
5
6
  // src/workers/worker.ts
6
7
  import axios from "axios";
@@ -16,392 +17,426 @@ import {
16
17
  import { getBaseMPCNetworkUrl, WalletScheme, WalletType } from "@getpara/core-sdk";
17
18
  var configCGGMPBase = (serverUrl, walletId, id) => `{"ServerUrl":"${serverUrl}", "WalletId": "${walletId}", "Id":"${id}", "Ids":["USER","CAPSULE"], "Threshold":1}`;
18
19
  var configDKLSBase = (walletId, id, disableWebSockets) => `{"walletId": "${walletId}", "id":"${id}", "otherId":"CAPSULE", "isReceiver": false, "disableWebSockets": ${disableWebSockets}}`;
19
- async function keygenRequest(ctx, userId, walletId, protocolId) {
20
- const { data } = await ctx.mpcComputationClient.post("/wallets", {
21
- userId,
22
- walletId,
23
- protocolId
20
+ function keygenRequest(ctx, userId, walletId, protocolId) {
21
+ return __async(this, null, function* () {
22
+ const { data } = yield ctx.mpcComputationClient.post("/wallets", {
23
+ userId,
24
+ walletId,
25
+ protocolId
26
+ });
27
+ return data;
24
28
  });
25
- return data;
26
29
  }
27
- async function signMessageRequest(ctx, userId, walletId, protocolId, message, signer) {
28
- const { data } = await ctx.mpcComputationClient.post(`/wallets/${walletId}/messages/sign`, {
29
- userId,
30
- protocolId,
31
- message,
32
- signer
30
+ function signMessageRequest(ctx, userId, walletId, protocolId, message, signer) {
31
+ return __async(this, null, function* () {
32
+ const { data } = yield ctx.mpcComputationClient.post(`/wallets/${walletId}/messages/sign`, {
33
+ userId,
34
+ protocolId,
35
+ message,
36
+ signer
37
+ });
38
+ return data;
33
39
  });
34
- return data;
35
40
  }
36
- async function sendTransactionRequest(ctx, userId, walletId, protocolId, transaction, signer, chainId) {
37
- const { data } = await ctx.mpcComputationClient.post(`/wallets/${walletId}/transactions/send`, {
38
- userId,
39
- protocolId,
40
- transaction,
41
- signer,
42
- chainId
41
+ function sendTransactionRequest(ctx, userId, walletId, protocolId, transaction, signer, chainId) {
42
+ return __async(this, null, function* () {
43
+ const { data } = yield ctx.mpcComputationClient.post(`/wallets/${walletId}/transactions/send`, {
44
+ userId,
45
+ protocolId,
46
+ transaction,
47
+ signer,
48
+ chainId
49
+ });
50
+ return data;
43
51
  });
44
- return data;
45
52
  }
46
- async function ed25519Keygen(ctx, userId) {
47
- const { walletId, protocolId } = await ctx.client.createWallet(userId, {
48
- scheme: WalletScheme.ED25519,
49
- type: WalletType.SOLANA
50
- });
51
- const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
52
- try {
53
- const newSigner = await new Promise(
54
- (resolve, reject) => global.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
55
- if (err) {
56
- reject(err);
57
- }
58
- resolve(result);
59
- })
60
- );
61
- return { signer: newSigner, walletId };
62
- } catch (e) {
63
- throw new Error(`error creating account of type SOLANA with userId ${userId} and walletId ${walletId}`);
64
- }
65
- }
66
- async function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType) {
67
- const { walletId, protocolId } = await ctx.client.createPregenWallet({
68
- pregenIdentifier,
69
- pregenIdentifierType,
70
- scheme: WalletScheme.ED25519,
71
- type: WalletType.SOLANA
53
+ function ed25519Keygen(ctx, userId) {
54
+ return __async(this, null, function* () {
55
+ const { walletId, protocolId } = yield ctx.client.createWallet(userId, {
56
+ scheme: WalletScheme.ED25519,
57
+ type: WalletType.SOLANA
58
+ });
59
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
60
+ try {
61
+ const newSigner = yield new Promise(
62
+ (resolve, reject) => global.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
63
+ if (err) {
64
+ reject(err);
65
+ }
66
+ resolve(result);
67
+ })
68
+ );
69
+ return { signer: newSigner, walletId };
70
+ } catch (e) {
71
+ throw new Error(`error creating account of type SOLANA with userId ${userId} and walletId ${walletId}`);
72
+ }
72
73
  });
73
- const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
74
- try {
75
- const newSigner = await new Promise(
76
- (resolve, reject) => global.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
77
- if (err) {
78
- reject(err);
79
- }
80
- resolve(result);
81
- })
82
- );
83
- return { signer: newSigner, walletId };
84
- } catch (e) {
85
- throw new Error(`error creating account of type SOLANA with walletId ${walletId}`);
86
- }
87
74
  }
88
- async function ed25519Sign(ctx, share, userId, walletId, base64Bytes) {
89
- const { protocolId } = await ctx.client.preSignMessage(userId, walletId, base64Bytes, WalletScheme.ED25519);
90
- try {
91
- const base64Sig = await new Promise(
92
- (resolve, reject) => global.ed25519Sign(share, protocolId, base64Bytes, (err, result) => {
93
- if (err) {
94
- reject(err);
95
- }
96
- resolve(result);
97
- })
98
- );
99
- return { signature: base64Sig };
100
- } catch (e) {
101
- throw new Error(`error signing for account of type SOLANA with userId ${userId} and walletId ${walletId}`);
102
- }
103
- }
104
- async function keygen(ctx, userId, type, secretKey) {
105
- const { walletId, protocolId } = await ctx.client.createWallet(userId, {
106
- useTwoSigners: true,
107
- scheme: ctx.useDKLS ? WalletScheme.DKLS : WalletScheme.CGGMP,
108
- type,
109
- cosmosPrefix: type === WalletType.COSMOS ? ctx.cosmosPrefix : void 0
110
- });
111
- if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
112
- return {
113
- signer: (await keygenRequest(ctx, userId, walletId, protocolId)).signer,
114
- walletId
115
- };
116
- }
117
- const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
118
- const signerConfigUser = ctx.useDKLS ? configDKLSBase(walletId, "USER", ctx.disableWebSockets) : configCGGMPBase(serverUrl, walletId, "USER");
119
- const createAccountFn = ctx.useDKLS ? global.dklsCreateAccount : global.createAccountV2;
120
- try {
121
- const newSigner = await new Promise(
122
- (resolve, reject) => createAccountFn(
123
- signerConfigUser,
124
- serverUrl,
125
- protocolId,
126
- secretKey,
127
- () => {
128
- },
129
- // no-op for deprecated callback to update progress percentage
130
- (err, result) => {
75
+ function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType) {
76
+ return __async(this, null, function* () {
77
+ const { walletId, protocolId } = yield ctx.client.createPregenWallet({
78
+ pregenIdentifier,
79
+ pregenIdentifierType,
80
+ scheme: WalletScheme.ED25519,
81
+ type: WalletType.SOLANA
82
+ });
83
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
84
+ try {
85
+ const newSigner = yield new Promise(
86
+ (resolve, reject) => global.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
131
87
  if (err) {
132
88
  reject(err);
133
89
  }
134
90
  resolve(result);
135
- }
136
- )
137
- );
138
- return { signer: newSigner, walletId };
139
- } catch (e) {
140
- throw new Error(`error creating account of type ${type} with userId ${userId} and walletId ${walletId}`);
141
- }
142
- }
143
- async function preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey) {
144
- const { walletId, protocolId } = await ctx.client.createPregenWallet({
145
- pregenIdentifier,
146
- pregenIdentifierType,
147
- type,
148
- cosmosPrefix: type === WalletType.COSMOS ? ctx.cosmosPrefix : void 0
91
+ })
92
+ );
93
+ return { signer: newSigner, walletId };
94
+ } catch (e) {
95
+ throw new Error(`error creating account of type SOLANA with walletId ${walletId}`);
96
+ }
149
97
  });
150
- if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
151
- return {
152
- signer: (await keygenRequest(ctx, partnerId, walletId, protocolId)).signer,
153
- walletId
154
- };
155
- }
156
- const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
157
- const signerConfigUser = ctx.useDKLS ? configDKLSBase(walletId, "USER", ctx.disableWebSockets) : configCGGMPBase(serverUrl, walletId, "USER");
158
- const createAccountFn = ctx.useDKLS ? global.dklsCreateAccount : global.createAccountV2;
159
- try {
160
- const newSigner = await new Promise(
161
- (resolve, reject) => createAccountFn(
162
- signerConfigUser,
163
- serverUrl,
164
- protocolId,
165
- secretKey,
166
- () => {
167
- },
168
- // no-op for deprecated callback to update progress percentage
169
- (err, result) => {
98
+ }
99
+ function ed25519Sign(ctx, share, userId, walletId, base64Bytes) {
100
+ return __async(this, null, function* () {
101
+ const { protocolId } = yield ctx.client.preSignMessage(userId, walletId, base64Bytes, WalletScheme.ED25519);
102
+ try {
103
+ const base64Sig = yield new Promise(
104
+ (resolve, reject) => global.ed25519Sign(share, protocolId, base64Bytes, (err, result) => {
170
105
  if (err) {
171
106
  reject(err);
172
107
  }
173
108
  resolve(result);
174
- }
175
- )
176
- );
177
- return { signer: newSigner, walletId };
178
- } catch (e) {
179
- throw new Error(`error creating account of type ${type} with walletId ${walletId}`);
180
- }
181
- }
182
- async function signMessage(ctx, share, walletId, userId, message) {
183
- const { protocolId, pendingTransactionId } = await ctx.client.preSignMessage(userId, walletId, message);
184
- if (pendingTransactionId) {
185
- return { pendingTransactionId };
186
- }
187
- if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
188
- return signMessageRequest(ctx, userId, walletId, protocolId, message, share);
189
- }
190
- const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
191
- const signMessageFn = ctx.useDKLS ? global.dklsSignMessage : global.signMessage;
192
- try {
193
- return new Promise(
194
- (resolve, reject) => signMessageFn(share, serverUrl, message, protocolId, (err, result) => {
195
- if (err) {
196
- reject(err);
197
- }
198
- resolve({ signature: result });
199
- })
200
- );
201
- } catch (e) {
202
- throw new Error(`error signing for account with userId ${userId} and walletId ${walletId}`);
203
- }
204
- }
205
- async function signTransaction(ctx, share, walletId, userId, tx, chainId) {
206
- const {
207
- data: { protocolId, pendingTransactionId }
208
- } = await ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId });
209
- if (pendingTransactionId) {
210
- return { pendingTransactionId };
211
- }
212
- if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
213
- return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
214
- }
215
- const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
216
- const signTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
217
- try {
218
- return new Promise(
219
- (resolve, reject) => signTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
220
- if (err) {
221
- reject(err);
222
- }
223
- resolve({ signature: result });
224
- })
225
- );
226
- } catch (e) {
227
- throw new Error(`error signing transaction for account with userId ${userId} and walletId ${walletId}`);
228
- }
229
- }
230
- async function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
231
- const {
232
- data: { protocolId, pendingTransactionId }
233
- } = await ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId });
234
- if (pendingTransactionId) {
235
- return { pendingTransactionId };
236
- }
237
- if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
238
- return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
239
- }
240
- const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
241
- const sendTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
242
- try {
243
- return new Promise(
244
- (resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
245
- if (err) {
246
- reject(err);
247
- }
248
- resolve({ signature: result });
249
- })
250
- );
251
- } catch (e) {
252
- throw new Error(`error signing transaction to send for account with userId ${userId} and walletId ${walletId}`);
253
- }
254
- }
255
- async function refresh(ctx, share, walletId, userId) {
256
- const {
257
- data: { protocolId }
258
- } = await ctx.client.refreshKeys(userId, walletId);
259
- const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
260
- const refreshFn = ctx.useDKLS ? global.dklsRefresh : global.refresh;
261
- try {
262
- return new Promise(
263
- (resolve, reject) => refreshFn(share, serverUrl, protocolId, (err, result) => {
264
- if (err) {
265
- reject(err);
266
- }
267
- resolve(result);
268
- })
269
- );
270
- } catch (e) {
271
- throw new Error(`error refreshing keys for account with userId ${userId} and walletId ${walletId}`);
272
- }
109
+ })
110
+ );
111
+ return { signature: base64Sig };
112
+ } catch (e) {
113
+ throw new Error(`error signing for account of type SOLANA with userId ${userId} and walletId ${walletId}`);
114
+ }
115
+ });
273
116
  }
274
- async function getPrivateKey(ctx, share, walletId, userId) {
275
- const paraShare = await ctx.client.getParaShare(userId, walletId);
276
- if (!paraShare) {
277
- console.error("unable to retrieve Para share");
278
- return "";
279
- }
280
- try {
281
- return new Promise(
282
- (resolve, reject) => global.getPrivateKey(share, paraShare, (err, result) => {
283
- if (err) {
284
- reject(err);
285
- }
286
- resolve(result);
287
- })
288
- );
289
- } catch (e) {
290
- throw new Error(`error getting private key for account with userId ${userId} and walletId ${walletId}`);
291
- }
117
+ function keygen(ctx, userId, type, secretKey) {
118
+ return __async(this, null, function* () {
119
+ const { walletId, protocolId } = yield ctx.client.createWallet(userId, {
120
+ useTwoSigners: true,
121
+ scheme: ctx.useDKLS ? WalletScheme.DKLS : WalletScheme.CGGMP,
122
+ type,
123
+ cosmosPrefix: type === WalletType.COSMOS ? ctx.cosmosPrefix : void 0
124
+ });
125
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
126
+ return {
127
+ signer: (yield keygenRequest(ctx, userId, walletId, protocolId)).signer,
128
+ walletId
129
+ };
130
+ }
131
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
132
+ const signerConfigUser = ctx.useDKLS ? configDKLSBase(walletId, "USER", ctx.disableWebSockets) : configCGGMPBase(serverUrl, walletId, "USER");
133
+ const createAccountFn = ctx.useDKLS ? global.dklsCreateAccount : global.createAccountV2;
134
+ try {
135
+ const newSigner = yield new Promise(
136
+ (resolve, reject) => createAccountFn(
137
+ signerConfigUser,
138
+ serverUrl,
139
+ protocolId,
140
+ secretKey,
141
+ () => {
142
+ },
143
+ // no-op for deprecated callback to update progress percentage
144
+ (err, result) => {
145
+ if (err) {
146
+ reject(err);
147
+ }
148
+ resolve(result);
149
+ }
150
+ )
151
+ );
152
+ return { signer: newSigner, walletId };
153
+ } catch (e) {
154
+ throw new Error(`error creating account of type ${type} with userId ${userId} and walletId ${walletId}`);
155
+ }
156
+ });
292
157
  }
293
-
294
- // src/workers/worker.ts
295
- var rawWasm;
296
- async function requestWasmWithRetries(ctx, retries = 3) {
297
- for (let i = 0; i < retries; i++) {
158
+ function preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey) {
159
+ return __async(this, null, function* () {
160
+ const { walletId, protocolId } = yield ctx.client.createPregenWallet({
161
+ pregenIdentifier,
162
+ pregenIdentifierType,
163
+ type,
164
+ cosmosPrefix: type === WalletType.COSMOS ? ctx.cosmosPrefix : void 0
165
+ });
166
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
167
+ return {
168
+ signer: (yield keygenRequest(ctx, partnerId, walletId, protocolId)).signer,
169
+ walletId
170
+ };
171
+ }
172
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
173
+ const signerConfigUser = ctx.useDKLS ? configDKLSBase(walletId, "USER", ctx.disableWebSockets) : configCGGMPBase(serverUrl, walletId, "USER");
174
+ const createAccountFn = ctx.useDKLS ? global.dklsCreateAccount : global.createAccountV2;
298
175
  try {
299
- return await axios.get(`${getPortalBaseURL(ctx, true, true)}/static/js/main.wasm`, { responseType: "arraybuffer" });
176
+ const newSigner = yield new Promise(
177
+ (resolve, reject) => createAccountFn(
178
+ signerConfigUser,
179
+ serverUrl,
180
+ protocolId,
181
+ secretKey,
182
+ () => {
183
+ },
184
+ // no-op for deprecated callback to update progress percentage
185
+ (err, result) => {
186
+ if (err) {
187
+ reject(err);
188
+ }
189
+ resolve(result);
190
+ }
191
+ )
192
+ );
193
+ return { signer: newSigner, walletId };
300
194
  } catch (e) {
301
- if (i === retries - 1) {
302
- throw e;
303
- }
195
+ throw new Error(`error creating account of type ${type} with walletId ${walletId}`);
304
196
  }
305
- }
197
+ });
306
198
  }
307
- async function loadWasm(ctx) {
308
- await import("./wasm_exec-UV7SYRZI.js");
309
- global.WebSocket = __require("ws");
310
- const goWasm = new global.Go();
311
- if (!rawWasm) {
312
- rawWasm = (await requestWasmWithRetries(ctx)).data;
313
- }
314
- const wasmBuffer = new Uint8Array(rawWasm);
315
- const webAssemblySource = await WebAssembly.instantiate(wasmBuffer, goWasm.importObject);
316
- goWasm.run(webAssemblySource.instance);
199
+ function signMessage(ctx, share, walletId, userId, message) {
200
+ return __async(this, null, function* () {
201
+ const { protocolId, pendingTransactionId } = yield ctx.client.preSignMessage(userId, walletId, message);
202
+ if (pendingTransactionId) {
203
+ return { pendingTransactionId };
204
+ }
205
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
206
+ return signMessageRequest(ctx, userId, walletId, protocolId, message, share);
207
+ }
208
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
209
+ const signMessageFn = ctx.useDKLS ? global.dklsSignMessage : global.signMessage;
210
+ try {
211
+ return new Promise(
212
+ (resolve, reject) => signMessageFn(share, serverUrl, message, protocolId, (err, result) => {
213
+ if (err) {
214
+ reject(err);
215
+ }
216
+ resolve({ signature: result });
217
+ })
218
+ );
219
+ } catch (e) {
220
+ throw new Error(`error signing for account with userId ${userId} and walletId ${walletId}`);
221
+ }
222
+ });
317
223
  }
318
- async function executeMessage(ctx, message) {
319
- const { functionType, params } = message;
320
- switch (functionType) {
321
- case "KEYGEN": {
322
- const { userId, secretKey, type = WalletType2.EVM } = params;
323
- return keygen(ctx, userId, type, secretKey);
224
+ function signTransaction(ctx, share, walletId, userId, tx, chainId) {
225
+ return __async(this, null, function* () {
226
+ const {
227
+ data: { protocolId, pendingTransactionId }
228
+ } = yield ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId });
229
+ if (pendingTransactionId) {
230
+ return { pendingTransactionId };
324
231
  }
325
- case "SIGN_TRANSACTION": {
326
- const { share, walletId, userId, tx, chainId } = params;
327
- return signTransaction(ctx, share, walletId, userId, tx, chainId);
232
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
233
+ return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
328
234
  }
329
- case "SEND_TRANSACTION": {
330
- const { share, walletId, userId, tx, chainId } = params;
331
- return sendTransaction(ctx, share, walletId, userId, tx, chainId);
235
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
236
+ const signTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
237
+ try {
238
+ return new Promise(
239
+ (resolve, reject) => signTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
240
+ if (err) {
241
+ reject(err);
242
+ }
243
+ resolve({ signature: result });
244
+ })
245
+ );
246
+ } catch (e) {
247
+ throw new Error(`error signing transaction for account with userId ${userId} and walletId ${walletId}`);
332
248
  }
333
- case "SIGN_MESSAGE": {
334
- const { share, walletId, userId, message: message2 } = params;
335
- return signMessage(ctx, share, walletId, userId, message2);
249
+ });
250
+ }
251
+ function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
252
+ return __async(this, null, function* () {
253
+ const {
254
+ data: { protocolId, pendingTransactionId }
255
+ } = yield ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId });
256
+ if (pendingTransactionId) {
257
+ return { pendingTransactionId };
336
258
  }
337
- case "REFRESH": {
338
- const { share, walletId, userId } = params;
339
- return refresh(ctx, share, walletId, userId);
259
+ if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
260
+ return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
340
261
  }
341
- case "PREKEYGEN": {
342
- const { email, partnerId, secretKey, type = WalletType2.EVM } = params;
343
- let { pregenIdentifier, pregenIdentifierType } = params;
344
- if (email !== "null" && email !== "undefined" && email !== "" && email != null) {
345
- pregenIdentifier = email;
346
- pregenIdentifierType = "EMAIL";
347
- }
348
- const keygenRes = await preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey);
349
- return keygenRes;
262
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
263
+ const sendTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
264
+ try {
265
+ return new Promise(
266
+ (resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
267
+ if (err) {
268
+ reject(err);
269
+ }
270
+ resolve({ signature: result });
271
+ })
272
+ );
273
+ } catch (e) {
274
+ throw new Error(`error signing transaction to send for account with userId ${userId} and walletId ${walletId}`);
350
275
  }
351
- case "GET_PRIVATE_KEY": {
352
- const { share, walletId, userId } = params;
353
- return await getPrivateKey(ctx, share, walletId, userId);
276
+ });
277
+ }
278
+ function refresh(ctx, share, walletId, userId) {
279
+ return __async(this, null, function* () {
280
+ const {
281
+ data: { protocolId }
282
+ } = yield ctx.client.refreshKeys(userId, walletId);
283
+ const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
284
+ const refreshFn = ctx.useDKLS ? global.dklsRefresh : global.refresh;
285
+ try {
286
+ return new Promise(
287
+ (resolve, reject) => refreshFn(share, serverUrl, protocolId, (err, result) => {
288
+ if (err) {
289
+ reject(err);
290
+ }
291
+ resolve(result);
292
+ })
293
+ );
294
+ } catch (e) {
295
+ throw new Error(`error refreshing keys for account with userId ${userId} and walletId ${walletId}`);
354
296
  }
355
- case "ED25519_KEYGEN": {
356
- const { userId } = params;
357
- return ed25519Keygen(ctx, userId);
297
+ });
298
+ }
299
+ function getPrivateKey(ctx, share, walletId, userId) {
300
+ return __async(this, null, function* () {
301
+ const paraShare = yield ctx.client.getParaShare(userId, walletId);
302
+ if (!paraShare) {
303
+ console.error("unable to retrieve Para share");
304
+ return "";
358
305
  }
359
- case "ED25519_SIGN": {
360
- const { share, walletId, userId, base64Bytes } = params;
361
- return ed25519Sign(ctx, share, userId, walletId, base64Bytes);
306
+ try {
307
+ return new Promise(
308
+ (resolve, reject) => global.getPrivateKey(share, paraShare, (err, result) => {
309
+ if (err) {
310
+ reject(err);
311
+ }
312
+ resolve(result);
313
+ })
314
+ );
315
+ } catch (e) {
316
+ throw new Error(`error getting private key for account with userId ${userId} and walletId ${walletId}`);
362
317
  }
363
- case "ED25519_PREKEYGEN": {
364
- const { email } = params;
365
- let { pregenIdentifier, pregenIdentifierType } = params;
366
- if (email !== "null" && email !== "undefined" && email !== "" && email != null) {
367
- pregenIdentifier = email;
368
- pregenIdentifierType = "EMAIL";
318
+ });
319
+ }
320
+
321
+ // src/workers/worker.ts
322
+ var rawWasm;
323
+ function requestWasmWithRetries(ctx, retries = 3) {
324
+ return __async(this, null, function* () {
325
+ for (let i = 0; i < retries; i++) {
326
+ try {
327
+ return yield axios.get(`${getPortalBaseURL(ctx, true, true)}/static/js/main.wasm`, { responseType: "arraybuffer" });
328
+ } catch (e) {
329
+ if (i === retries - 1) {
330
+ throw e;
331
+ }
369
332
  }
370
- return ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType);
371
333
  }
372
- default: {
373
- throw new Error(`functionType: ${functionType} not supported`);
334
+ });
335
+ }
336
+ function loadWasm(ctx) {
337
+ return __async(this, null, function* () {
338
+ yield import("./wasm_exec-CFNSOXDO.js");
339
+ global.WebSocket = __require("ws");
340
+ const goWasm = new global.Go();
341
+ if (!rawWasm) {
342
+ rawWasm = (yield requestWasmWithRetries(ctx)).data;
343
+ }
344
+ const wasmBuffer = new Uint8Array(rawWasm);
345
+ const webAssemblySource = yield WebAssembly.instantiate(wasmBuffer, goWasm.importObject);
346
+ goWasm.run(webAssemblySource.instance);
347
+ });
348
+ }
349
+ function executeMessage(ctx, message) {
350
+ return __async(this, null, function* () {
351
+ const { functionType, params } = message;
352
+ switch (functionType) {
353
+ case "KEYGEN": {
354
+ const { userId, secretKey, type = WalletType2.EVM } = params;
355
+ return keygen(ctx, userId, type, secretKey);
356
+ }
357
+ case "SIGN_TRANSACTION": {
358
+ const { share, walletId, userId, tx, chainId } = params;
359
+ return signTransaction(ctx, share, walletId, userId, tx, chainId);
360
+ }
361
+ case "SEND_TRANSACTION": {
362
+ const { share, walletId, userId, tx, chainId } = params;
363
+ return sendTransaction(ctx, share, walletId, userId, tx, chainId);
364
+ }
365
+ case "SIGN_MESSAGE": {
366
+ const { share, walletId, userId, message: message2 } = params;
367
+ return signMessage(ctx, share, walletId, userId, message2);
368
+ }
369
+ case "REFRESH": {
370
+ const { share, walletId, userId } = params;
371
+ return refresh(ctx, share, walletId, userId);
372
+ }
373
+ case "PREKEYGEN": {
374
+ const { email, partnerId, secretKey, type = WalletType2.EVM } = params;
375
+ let { pregenIdentifier, pregenIdentifierType } = params;
376
+ if (email !== "null" && email !== "undefined" && email !== "" && email != null) {
377
+ pregenIdentifier = email;
378
+ pregenIdentifierType = "EMAIL";
379
+ }
380
+ const keygenRes = yield preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey);
381
+ return keygenRes;
382
+ }
383
+ case "GET_PRIVATE_KEY": {
384
+ const { share, walletId, userId } = params;
385
+ return yield getPrivateKey(ctx, share, walletId, userId);
386
+ }
387
+ case "ED25519_KEYGEN": {
388
+ const { userId } = params;
389
+ return ed25519Keygen(ctx, userId);
390
+ }
391
+ case "ED25519_SIGN": {
392
+ const { share, walletId, userId, base64Bytes } = params;
393
+ return ed25519Sign(ctx, share, userId, walletId, base64Bytes);
394
+ }
395
+ case "ED25519_PREKEYGEN": {
396
+ const { email } = params;
397
+ let { pregenIdentifier, pregenIdentifierType } = params;
398
+ if (email !== "null" && email !== "undefined" && email !== "" && email != null) {
399
+ pregenIdentifier = email;
400
+ pregenIdentifierType = "EMAIL";
401
+ }
402
+ return ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType);
403
+ }
404
+ default: {
405
+ throw new Error(`functionType: ${functionType} not supported`);
406
+ }
374
407
  }
375
- }
408
+ });
376
409
  }
377
- async function handleMessage(e) {
378
- const {
379
- env,
380
- apiKey,
381
- cosmosPrefix = "cosmos",
382
- offloadMPCComputationURL,
383
- disableWorkers,
384
- sessionCookie,
385
- useDKLS,
386
- disableWebSockets,
387
- workId
388
- } = e.data;
389
- const ctx = {
390
- env,
391
- apiKey,
392
- client: initClient({ env, version: paraVersion, apiKey, retrieveSessionCookie: () => sessionCookie }),
393
- offloadMPCComputationURL,
394
- mpcComputationClient: offloadMPCComputationURL ? mpcComputationClient.initClient(offloadMPCComputationURL, !!disableWorkers) : void 0,
395
- useDKLS,
396
- disableWebSockets: !!disableWebSockets,
397
- cosmosPrefix
398
- };
399
- if (!ctx.offloadMPCComputationURL || ctx.useDKLS) {
400
- await loadWasm(ctx);
401
- }
402
- const result = await executeMessage(ctx, e.data);
403
- result.workId = workId;
404
- return result;
410
+ function handleMessage(e) {
411
+ return __async(this, null, function* () {
412
+ const {
413
+ env,
414
+ apiKey,
415
+ cosmosPrefix = "cosmos",
416
+ offloadMPCComputationURL,
417
+ disableWorkers,
418
+ sessionCookie,
419
+ useDKLS,
420
+ disableWebSockets,
421
+ workId
422
+ } = e.data;
423
+ const ctx = {
424
+ env,
425
+ apiKey,
426
+ client: initClient({ env, version: paraVersion, apiKey, retrieveSessionCookie: () => sessionCookie }),
427
+ offloadMPCComputationURL,
428
+ mpcComputationClient: offloadMPCComputationURL ? mpcComputationClient.initClient(offloadMPCComputationURL, !!disableWorkers) : void 0,
429
+ useDKLS,
430
+ disableWebSockets: !!disableWebSockets,
431
+ cosmosPrefix
432
+ };
433
+ if (!ctx.offloadMPCComputationURL || ctx.useDKLS) {
434
+ yield loadWasm(ctx);
435
+ }
436
+ const result = yield executeMessage(ctx, e.data);
437
+ result.workId = workId;
438
+ return result;
439
+ });
405
440
  }
406
441
  export {
407
442
  handleMessage