@getpara/web-sdk 2.0.0-alpha.18 → 2.0.0-alpha.19

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.
@@ -4,6 +4,7 @@ import {
4
4
  } from "../chunk-WLGKV3EF.js";
5
5
  import { setupWorker } from "../workers/workerWrapper.js";
6
6
  import { distributeNewShare, waitUntilTrue } from "@getpara/core-sdk";
7
+ import * as uuid from "uuid";
7
8
  function isKeygenComplete(ctx, userId, walletId) {
8
9
  return __async(this, null, function* () {
9
10
  const wallets = yield ctx.client.getWallets(userId);
@@ -26,6 +27,7 @@ function isPreKeygenComplete(ctx, pregenIdentifier, pregenIdentifierType, wallet
26
27
  }
27
28
  function keygen(ctx, userId, type, secretKey, skipDistribute = false, sessionCookie, emailProps = {}) {
28
29
  return new Promise((resolve, reject) => __async(this, null, function* () {
30
+ const workId = uuid.v4();
29
31
  let worker = null;
30
32
  worker = yield setupWorker(
31
33
  ctx,
@@ -39,7 +41,6 @@ function keygen(ctx, userId, type, secretKey, skipDistribute = false, sessionCoo
39
41
  walletId: res.walletId,
40
42
  recoveryShare: null
41
43
  });
42
- worker == null ? void 0 : worker.terminate();
43
44
  return;
44
45
  }
45
46
  const recoveryShare = yield distributeNewShare({
@@ -54,12 +55,11 @@ function keygen(ctx, userId, type, secretKey, skipDistribute = false, sessionCoo
54
55
  walletId: res.walletId,
55
56
  recoveryShare
56
57
  });
57
- worker == null ? void 0 : worker.terminate();
58
58
  }),
59
59
  (error) => {
60
- worker == null ? void 0 : worker.terminate();
61
60
  reject(error);
62
- }
61
+ },
62
+ workId
63
63
  );
64
64
  worker.postMessage({
65
65
  env: ctx.env,
@@ -72,12 +72,14 @@ function keygen(ctx, userId, type, secretKey, skipDistribute = false, sessionCoo
72
72
  sessionCookie,
73
73
  useDKLS: ctx.useDKLS,
74
74
  disableWebSockets: ctx.disableWebSockets,
75
- wasmOverride: ctx.wasmOverride
75
+ wasmOverride: ctx.wasmOverride,
76
+ workId
76
77
  });
77
78
  }));
78
79
  }
79
80
  function preKeygen(ctx, pregenIdentifier, pregenIdentifierType, type, secretKey, _skipDistribute = false, partnerId, sessionCookie) {
80
81
  return new Promise((resolve, reject) => __async(this, null, function* () {
82
+ const workId = uuid.v4();
81
83
  let worker = null;
82
84
  worker = yield setupWorker(
83
85
  ctx,
@@ -94,12 +96,11 @@ function preKeygen(ctx, pregenIdentifier, pregenIdentifierType, type, secretKey,
94
96
  walletId: res.walletId,
95
97
  recoveryShare: null
96
98
  });
97
- worker == null ? void 0 : worker.terminate();
98
99
  }),
99
100
  (error) => {
100
- worker == null ? void 0 : worker.terminate();
101
101
  reject(error);
102
- }
102
+ },
103
+ workId
103
104
  );
104
105
  const email = void 0;
105
106
  const params = { pregenIdentifier, pregenIdentifierType, type, secretKey, partnerId, email };
@@ -117,12 +118,14 @@ function preKeygen(ctx, pregenIdentifier, pregenIdentifierType, type, secretKey,
117
118
  sessionCookie,
118
119
  useDKLS: ctx.useDKLS,
119
120
  disableWebSockets: ctx.disableWebSockets,
120
- wasmOverride: ctx.wasmOverride
121
+ wasmOverride: ctx.wasmOverride,
122
+ workId
121
123
  });
122
124
  }));
123
125
  }
124
126
  function refresh(ctx, sessionCookie, userId, walletId, share, oldPartnerId, newPartnerId, keyShareProtocolId) {
125
127
  return new Promise((resolve, reject) => __async(this, null, function* () {
128
+ const workId = uuid.v4();
126
129
  let worker = null;
127
130
  worker = yield setupWorker(
128
131
  ctx,
@@ -130,7 +133,6 @@ function refresh(ctx, sessionCookie, userId, walletId, share, oldPartnerId, newP
130
133
  if (!(yield waitUntilTrue(() => __async(this, null, function* () {
131
134
  return isRefreshComplete(ctx, userId, walletId, newPartnerId);
132
135
  }), 15e3, 1e3))) {
133
- worker == null ? void 0 : worker.terminate();
134
136
  reject(new Error("refresh failed"));
135
137
  return;
136
138
  }
@@ -139,12 +141,11 @@ function refresh(ctx, sessionCookie, userId, walletId, share, oldPartnerId, newP
139
141
  signer,
140
142
  protocolId
141
143
  });
142
- worker == null ? void 0 : worker.terminate();
143
144
  }),
144
145
  (error) => {
145
- worker == null ? void 0 : worker.terminate();
146
146
  reject(error);
147
- }
147
+ },
148
+ workId
148
149
  );
149
150
  worker.postMessage({
150
151
  env: ctx.env,
@@ -156,12 +157,14 @@ function refresh(ctx, sessionCookie, userId, walletId, share, oldPartnerId, newP
156
157
  useDKLS: ctx.useDKLS,
157
158
  disableWebSockets: ctx.disableWebSockets,
158
159
  wasmOverride: ctx.wasmOverride,
159
- returnObject: true
160
+ returnObject: true,
161
+ workId
160
162
  });
161
163
  }));
162
164
  }
163
165
  function ed25519Keygen(ctx, userId, sessionCookie, _emailProps = {}) {
164
166
  return new Promise((resolve, reject) => __async(this, null, function* () {
167
+ const workId = uuid.v4();
165
168
  let worker = null;
166
169
  worker = yield setupWorker(
167
170
  ctx,
@@ -174,12 +177,11 @@ function ed25519Keygen(ctx, userId, sessionCookie, _emailProps = {}) {
174
177
  walletId: res.walletId,
175
178
  recoveryShare: null
176
179
  });
177
- worker == null ? void 0 : worker.terminate();
178
180
  }),
179
181
  (error) => {
180
- worker == null ? void 0 : worker.terminate();
181
182
  reject(error);
182
- }
183
+ },
184
+ workId
183
185
  );
184
186
  worker.postMessage({
185
187
  env: ctx.env,
@@ -190,12 +192,14 @@ function ed25519Keygen(ctx, userId, sessionCookie, _emailProps = {}) {
190
192
  disableWorkers: ctx.disableWorkers,
191
193
  sessionCookie,
192
194
  disableWebSockets: ctx.disableWebSockets,
193
- wasmOverride: ctx.wasmOverride
195
+ wasmOverride: ctx.wasmOverride,
196
+ workId
194
197
  });
195
198
  }));
196
199
  }
197
200
  function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType, sessionCookie) {
198
201
  return new Promise((resolve, reject) => __async(this, null, function* () {
202
+ const workId = uuid.v4();
199
203
  let worker = null;
200
204
  worker = yield setupWorker(
201
205
  ctx,
@@ -212,12 +216,11 @@ function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType, sessionCo
212
216
  walletId: res.walletId,
213
217
  recoveryShare: null
214
218
  });
215
- worker == null ? void 0 : worker.terminate();
216
219
  }),
217
220
  (error) => {
218
- worker == null ? void 0 : worker.terminate();
219
221
  reject(error);
220
- }
222
+ },
223
+ workId
221
224
  );
222
225
  const email = void 0;
223
226
  const params = { pregenIdentifier, pregenIdentifierType, email };
@@ -233,7 +236,8 @@ function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType, sessionCo
233
236
  disableWorkers: ctx.disableWorkers,
234
237
  sessionCookie,
235
238
  disableWebSockets: ctx.disableWebSockets,
236
- wasmOverride: ctx.wasmOverride
239
+ wasmOverride: ctx.wasmOverride,
240
+ workId
237
241
  });
238
242
  }));
239
243
  }
@@ -3,20 +3,21 @@ import {
3
3
  __async
4
4
  } from "../chunk-WLGKV3EF.js";
5
5
  import { setupWorker } from "../workers/workerWrapper.js";
6
+ import * as uuid from "uuid";
6
7
  function getPrivateKey(ctx, userId, walletId, share, sessionCookie) {
7
8
  return __async(this, null, function* () {
8
9
  return new Promise((resolve, reject) => __async(this, null, function* () {
10
+ const workId = uuid.v4();
9
11
  let worker = null;
10
12
  worker = yield setupWorker(
11
13
  ctx,
12
14
  (res) => __async(this, null, function* () {
13
- resolve(res);
14
- worker == null ? void 0 : worker.terminate();
15
+ resolve((res == null ? void 0 : res.privateKey) || res);
15
16
  }),
16
17
  (error) => {
17
- worker == null ? void 0 : worker.terminate();
18
18
  reject(error);
19
- }
19
+ },
20
+ workId
20
21
  );
21
22
  worker.postMessage({
22
23
  env: ctx.env,
@@ -29,7 +30,8 @@ function getPrivateKey(ctx, userId, walletId, share, sessionCookie) {
29
30
  sessionCookie,
30
31
  useDKLS: ctx.useDKLS,
31
32
  disableWebSockets: ctx.disableWebSockets,
32
- wasmOverride: ctx.wasmOverride
33
+ wasmOverride: ctx.wasmOverride,
34
+ workId
33
35
  });
34
36
  }));
35
37
  });
@@ -3,20 +3,21 @@ import {
3
3
  __async
4
4
  } from "../chunk-WLGKV3EF.js";
5
5
  import { setupWorker } from "../workers/workerWrapper.js";
6
+ import * as uuid from "uuid";
6
7
  function signTransaction(ctx, userId, walletId, share, tx, chainId, sessionCookie, isDKLS) {
7
8
  return __async(this, null, function* () {
8
9
  return new Promise((resolve, reject) => __async(this, null, function* () {
10
+ const workId = uuid.v4();
9
11
  let worker = null;
10
12
  worker = yield setupWorker(
11
13
  ctx,
12
14
  (sendTransactionRes) => __async(this, null, function* () {
13
15
  resolve(sendTransactionRes);
14
- worker == null ? void 0 : worker.terminate();
15
16
  }),
16
17
  (error) => {
17
- worker == null ? void 0 : worker.terminate();
18
18
  reject(error);
19
- }
19
+ },
20
+ workId
20
21
  );
21
22
  worker.postMessage({
22
23
  env: ctx.env,
@@ -29,7 +30,8 @@ function signTransaction(ctx, userId, walletId, share, tx, chainId, sessionCooki
29
30
  sessionCookie,
30
31
  useDKLS: isDKLS,
31
32
  disableWebSockets: ctx.disableWebSockets,
32
- wasmOverride: ctx.wasmOverride
33
+ wasmOverride: ctx.wasmOverride,
34
+ workId
33
35
  });
34
36
  }));
35
37
  });
@@ -37,17 +39,17 @@ function signTransaction(ctx, userId, walletId, share, tx, chainId, sessionCooki
37
39
  function sendTransaction(ctx, userId, walletId, share, tx, chainId, sessionCookie, isDKLS) {
38
40
  return __async(this, null, function* () {
39
41
  return new Promise((resolve, reject) => __async(this, null, function* () {
42
+ const workId = uuid.v4();
40
43
  let worker = null;
41
44
  worker = yield setupWorker(
42
45
  ctx,
43
46
  (sendTransactionRes) => __async(this, null, function* () {
44
47
  resolve(sendTransactionRes);
45
- worker == null ? void 0 : worker.terminate();
46
48
  }),
47
49
  (error) => {
48
- worker == null ? void 0 : worker.terminate();
49
50
  reject(error);
50
- }
51
+ },
52
+ workId
51
53
  );
52
54
  worker.postMessage({
53
55
  env: ctx.env,
@@ -60,7 +62,8 @@ function sendTransaction(ctx, userId, walletId, share, tx, chainId, sessionCooki
60
62
  sessionCookie,
61
63
  useDKLS: isDKLS,
62
64
  disableWebSockets: ctx.disableWebSockets,
63
- wasmOverride: ctx.wasmOverride
65
+ wasmOverride: ctx.wasmOverride,
66
+ workId
64
67
  });
65
68
  }));
66
69
  });
@@ -68,18 +71,18 @@ function sendTransaction(ctx, userId, walletId, share, tx, chainId, sessionCooki
68
71
  function signMessage(ctx, userId, walletId, share, message, sessionCookie, isDKLS, cosmosSignDoc) {
69
72
  return __async(this, null, function* () {
70
73
  return new Promise((resolve, reject) => __async(this, null, function* () {
74
+ const workId = uuid.v4();
71
75
  let worker = null;
72
76
  worker = yield setupWorker(
73
77
  ctx,
74
78
  (signMessageRes) => __async(this, null, function* () {
75
79
  resolve(signMessageRes);
76
- worker == null ? void 0 : worker.terminate();
77
80
  }),
78
81
  (error) => {
79
82
  console.error(`Worker error in signMessage for userId ${userId}, walletId ${walletId}:`, error);
80
- worker == null ? void 0 : worker.terminate();
81
83
  reject(error);
82
- }
84
+ },
85
+ workId
83
86
  );
84
87
  worker.postMessage({
85
88
  env: ctx.env,
@@ -92,7 +95,8 @@ function signMessage(ctx, userId, walletId, share, message, sessionCookie, isDKL
92
95
  sessionCookie,
93
96
  useDKLS: isDKLS,
94
97
  disableWebSockets: ctx.disableWebSockets,
95
- wasmOverride: ctx.wasmOverride
98
+ wasmOverride: ctx.wasmOverride,
99
+ workId
96
100
  });
97
101
  }));
98
102
  });
@@ -100,18 +104,18 @@ function signMessage(ctx, userId, walletId, share, message, sessionCookie, isDKL
100
104
  function ed25519Sign(ctx, userId, walletId, share, base64Bytes, sessionCookie) {
101
105
  return __async(this, null, function* () {
102
106
  return new Promise((resolve, reject) => __async(this, null, function* () {
107
+ const workId = uuid.v4();
103
108
  let worker = null;
104
109
  worker = yield setupWorker(
105
110
  ctx,
106
111
  (signMessageRes) => __async(this, null, function* () {
107
112
  resolve(signMessageRes);
108
- worker == null ? void 0 : worker.terminate();
109
113
  }),
110
114
  (error) => {
111
115
  console.error(`Worker error in ed25519Sign for userId ${userId}, walletId ${walletId}:`, error);
112
- worker == null ? void 0 : worker.terminate();
113
116
  reject(error);
114
- }
117
+ },
118
+ workId
115
119
  );
116
120
  worker.postMessage({
117
121
  env: ctx.env,
@@ -122,7 +126,8 @@ function ed25519Sign(ctx, userId, walletId, share, base64Bytes, sessionCookie) {
122
126
  disableWorkers: ctx.disableWorkers,
123
127
  sessionCookie,
124
128
  disableWebSockets: ctx.disableWebSockets,
125
- wasmOverride: ctx.wasmOverride
129
+ wasmOverride: ctx.wasmOverride,
130
+ workId
126
131
  });
127
132
  }));
128
133
  });
@@ -3,6 +3,7 @@ import {
3
3
  __async
4
4
  } from "../chunk-WLGKV3EF.js";
5
5
  import { getBaseMPCNetworkUrl } from "@getpara/core-sdk";
6
+ import * as uuid from "uuid";
6
7
  const configCGGMPBase = (serverUrl, walletId, id) => `{"ServerUrl":"${serverUrl}", "WalletId": "${walletId}", "Id":"${id}", "Ids":["USER","CAPSULE"], "Threshold":1}`;
7
8
  const configDKLSBase = (walletId, id, disableWebSockets) => `{"walletId": "${walletId}", "id":"${id}", "otherId":"CAPSULE", "isReceiver": false, "disableWebSockets": ${disableWebSockets}}`;
8
9
  function keygenRequest(ctx, userId, walletId, protocolId) {
@@ -86,20 +87,27 @@ function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType) {
86
87
  }
87
88
  function ed25519Sign(ctx, share, userId, walletId, base64Bytes) {
88
89
  return __async(this, null, function* () {
89
- const { protocolId } = yield ctx.client.preSignMessage(userId, walletId, base64Bytes, "ED25519");
90
- try {
91
- const base64Sig = yield new Promise(
92
- (resolve, reject) => globalThis.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
- }
90
+ const protocolId = uuid.v4();
91
+ const preSignMessageRes = ctx.client.preSignMessage(userId, walletId, base64Bytes, "ED25519", void 0, protocolId);
92
+ const signRes = function() {
93
+ return __async(this, null, function* () {
94
+ try {
95
+ const base64Sig = yield new Promise(
96
+ (resolve, reject) => globalThis.ed25519Sign(share, protocolId, base64Bytes, (err, result) => {
97
+ if (err) {
98
+ reject(err);
99
+ }
100
+ resolve(result);
101
+ })
102
+ );
103
+ return { signature: base64Sig };
104
+ } catch (e) {
105
+ throw new Error(`error signing for account of type SOLANA with userId ${userId} and walletId ${walletId}`);
106
+ }
107
+ });
108
+ }();
109
+ yield preSignMessageRes;
110
+ return yield signRes;
103
111
  });
104
112
  }
105
113
  function keygen(ctx, userId, type, secretKey) {
@@ -179,16 +187,8 @@ function preKeygen(ctx, _partnerId, pregenIdentifier, pregenIdentifierType, type
179
187
  }
180
188
  function signMessage(ctx, share, walletId, userId, message, cosmosSignDoc) {
181
189
  return __async(this, null, function* () {
182
- const { protocolId, pendingTransactionId } = yield ctx.client.preSignMessage(
183
- userId,
184
- walletId,
185
- message,
186
- null,
187
- cosmosSignDoc
188
- );
189
- if (pendingTransactionId) {
190
- return { pendingTransactionId };
191
- }
190
+ const protocolId = uuid.v4();
191
+ const preSignMessageRes = ctx.client.preSignMessage(userId, walletId, message, null, cosmosSignDoc, protocolId);
192
192
  if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
193
193
  return signMessageRequest(ctx, userId, walletId, protocolId, message, share);
194
194
  }
@@ -199,28 +199,33 @@ function signMessage(ctx, share, walletId, userId, message, cosmosSignDoc) {
199
199
  parsedShare.disableWebSockets = ctx.disableWebSockets;
200
200
  }
201
201
  share = JSON.stringify(parsedShare);
202
- try {
203
- return yield new Promise(
204
- (resolve, reject) => signMessageFn(share, serverUrl, message, protocolId, (err, result) => {
205
- if (err) {
206
- reject(err);
207
- }
208
- resolve({ signature: result });
209
- })
210
- );
211
- } catch (e) {
212
- throw new Error(`error signing for account with userId ${userId} and walletId ${walletId}`);
202
+ const signMessageRes = function() {
203
+ return __async(this, null, function* () {
204
+ try {
205
+ return yield new Promise(
206
+ (resolve, reject) => signMessageFn(share, serverUrl, message, protocolId, (err, result) => {
207
+ if (err) {
208
+ reject(err);
209
+ }
210
+ resolve({ signature: result });
211
+ })
212
+ );
213
+ } catch (e) {
214
+ throw new Error(`error signing for account with userId ${userId} and walletId ${walletId}`);
215
+ }
216
+ });
217
+ }();
218
+ const { pendingTransactionId } = yield preSignMessageRes;
219
+ if (pendingTransactionId) {
220
+ return { pendingTransactionId };
213
221
  }
222
+ return yield signMessageRes;
214
223
  });
215
224
  }
216
225
  function signTransaction(ctx, share, walletId, userId, tx, chainId) {
217
226
  return __async(this, null, function* () {
218
- const {
219
- data: { protocolId, pendingTransactionId }
220
- } = yield ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId });
221
- if (pendingTransactionId) {
222
- return { pendingTransactionId };
223
- }
227
+ const protocolId = uuid.v4();
228
+ const signTransactionRes = ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId, protocolId });
224
229
  if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
225
230
  return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
226
231
  }
@@ -231,28 +236,35 @@ function signTransaction(ctx, share, walletId, userId, tx, chainId) {
231
236
  parsedShare.disableWebSockets = ctx.disableWebSockets;
232
237
  }
233
238
  share = JSON.stringify(parsedShare);
234
- try {
235
- return yield new Promise(
236
- (resolve, reject) => signTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
237
- if (err) {
238
- reject(err);
239
- }
240
- resolve({ signature: result });
241
- })
242
- );
243
- } catch (e) {
244
- throw new Error(`error signing transaction for account with userId ${userId} and walletId ${walletId}`);
239
+ const signTxRes = function() {
240
+ return __async(this, null, function* () {
241
+ try {
242
+ return yield new Promise(
243
+ (resolve, reject) => signTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
244
+ if (err) {
245
+ reject(err);
246
+ }
247
+ resolve({ signature: result });
248
+ })
249
+ );
250
+ } catch (e) {
251
+ throw new Error(`error signing transaction for account with userId ${userId} and walletId ${walletId}`);
252
+ }
253
+ });
254
+ }();
255
+ const {
256
+ data: { pendingTransactionId }
257
+ } = yield signTransactionRes;
258
+ if (pendingTransactionId) {
259
+ return { pendingTransactionId };
245
260
  }
261
+ return yield signTxRes;
246
262
  });
247
263
  }
248
264
  function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
249
265
  return __async(this, null, function* () {
250
- const {
251
- data: { protocolId, pendingTransactionId }
252
- } = yield ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId });
253
- if (pendingTransactionId) {
254
- return { pendingTransactionId };
255
- }
266
+ const protocolId = uuid.v4();
267
+ const sendTransactionRes = ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId, protocolId });
256
268
  if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
257
269
  return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
258
270
  }
@@ -263,18 +275,29 @@ function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
263
275
  parsedShare.disableWebSockets = ctx.disableWebSockets;
264
276
  }
265
277
  share = JSON.stringify(parsedShare);
266
- try {
267
- return yield new Promise(
268
- (resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
269
- if (err) {
270
- reject(err);
271
- }
272
- resolve({ signature: result });
273
- })
274
- );
275
- } catch (e) {
276
- throw new Error(`error signing transaction to send for account with userId ${userId} and walletId ${walletId}`);
278
+ const sendTxRes = function() {
279
+ return __async(this, null, function* () {
280
+ try {
281
+ return yield new Promise(
282
+ (resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
283
+ if (err) {
284
+ reject(err);
285
+ }
286
+ resolve({ signature: result });
287
+ })
288
+ );
289
+ } catch (e) {
290
+ throw new Error(`error signing transaction to send for account with userId ${userId} and walletId ${walletId}`);
291
+ }
292
+ });
293
+ }();
294
+ const {
295
+ data: { pendingTransactionId }
296
+ } = yield sendTransactionRes;
297
+ if (pendingTransactionId) {
298
+ return { pendingTransactionId };
277
299
  }
300
+ return yield sendTxRes;
278
301
  });
279
302
  }
280
303
  function refresh(ctx, share, walletId, userId, oldPartnerId, newPartnerId, keyShareProtocolId) {
@@ -13,7 +13,16 @@ export interface Message {
13
13
  disableWebSockets?: boolean;
14
14
  wasmOverride?: ArrayBuffer;
15
15
  returnObject?: boolean;
16
+ workId?: string;
16
17
  }
18
+ /**
19
+ * Executes an operation with retry capabilities
20
+ * @param operation The function to execute
21
+ * @param maxRetries Maximum number of retries (default: 2)
22
+ * @param timeoutMs Timeout in milliseconds (default: 10000)
23
+ * @returns The result of the operation
24
+ */
25
+ export declare function withRetry<T>(operation: () => Promise<T>, maxRetries?: number, timeoutMs?: number): Promise<T>;
17
26
  export declare function handleMessage(e: {
18
27
  data: Message;
19
28
  }, postMessage: (message: any) => void, useFetchAdapter?: boolean): Promise<boolean>;
@@ -22,6 +22,7 @@ var __async = (__this, __arguments, generator) => {
22
22
  import "../wasm/wasm_exec.js";
23
23
  import * as walletUtils from "./walletUtils.js";
24
24
  import { getPortalBaseURL, initClient, mpcComputationClient, paraVersion } from "@getpara/core-sdk";
25
+ let wasmLoaded = false;
25
26
  function loadWasm(ctx, wasmOverride) {
26
27
  return __async(this, null, function* () {
27
28
  if (typeof self === "undefined") {
@@ -51,15 +52,15 @@ function executeMessage(ctx, message) {
51
52
  }
52
53
  case "SIGN_TRANSACTION": {
53
54
  const { share, walletId, userId, tx, chainId } = params;
54
- return walletUtils.signTransaction(ctx, share, walletId, userId, tx, chainId);
55
+ return withRetry(() => walletUtils.signTransaction(ctx, share, walletId, userId, tx, chainId));
55
56
  }
56
57
  case "SEND_TRANSACTION": {
57
58
  const { share, walletId, userId, tx, chainId } = params;
58
- return walletUtils.sendTransaction(ctx, share, walletId, userId, tx, chainId);
59
+ return withRetry(() => walletUtils.sendTransaction(ctx, share, walletId, userId, tx, chainId));
59
60
  }
60
61
  case "SIGN_MESSAGE": {
61
62
  const { share, walletId, userId, message: message2, cosmosSignDoc } = params;
62
- return walletUtils.signMessage(ctx, share, walletId, userId, message2, cosmosSignDoc);
63
+ return withRetry(() => walletUtils.signMessage(ctx, share, walletId, userId, message2, cosmosSignDoc));
63
64
  }
64
65
  case "REFRESH": {
65
66
  const { share, walletId, userId, oldPartnerId, newPartnerId, keyShareProtocolId } = params;
@@ -94,7 +95,7 @@ function executeMessage(ctx, message) {
94
95
  }
95
96
  case "ED25519_SIGN": {
96
97
  const { share, walletId, userId, base64Bytes } = params;
97
- return walletUtils.ed25519Sign(ctx, share, userId, walletId, base64Bytes);
98
+ return withRetry(() => walletUtils.ed25519Sign(ctx, share, userId, walletId, base64Bytes));
98
99
  }
99
100
  case "ED25519_PREKEYGEN": {
100
101
  const { email } = params;
@@ -111,6 +112,29 @@ function executeMessage(ctx, message) {
111
112
  }
112
113
  });
113
114
  }
115
+ function withRetry(operation, maxRetries = 2, timeoutMs = 1e4) {
116
+ return __async(this, null, function* () {
117
+ let retries = 0;
118
+ while (true) {
119
+ try {
120
+ const operationPromise = operation();
121
+ const timeoutPromise = new Promise((_, reject) => {
122
+ const timeoutId = setTimeout(() => {
123
+ reject(new Error(`Operation timed out after ${timeoutMs}ms`));
124
+ }, timeoutMs);
125
+ operationPromise.finally(() => clearTimeout(timeoutId));
126
+ });
127
+ return yield Promise.race([operationPromise, timeoutPromise]);
128
+ } catch (error) {
129
+ retries++;
130
+ if (retries > maxRetries) {
131
+ throw error;
132
+ }
133
+ console.warn(`Operation failed (attempt ${retries}/${maxRetries}), retrying...`, error);
134
+ }
135
+ }
136
+ });
137
+ }
114
138
  function handleMessage(e, postMessage, useFetchAdapter) {
115
139
  return __async(this, null, function* () {
116
140
  const {
@@ -122,7 +146,8 @@ function handleMessage(e, postMessage, useFetchAdapter) {
122
146
  sessionCookie,
123
147
  useDKLS,
124
148
  disableWebSockets,
125
- wasmOverride
149
+ wasmOverride,
150
+ workId
126
151
  } = e.data;
127
152
  if (!env) {
128
153
  return true;
@@ -144,14 +169,32 @@ function handleMessage(e, postMessage, useFetchAdapter) {
144
169
  disableWebSockets: !!disableWebSockets,
145
170
  wasmOverride
146
171
  };
147
- if (!ctx.offloadMPCComputationURL || ctx.useDKLS) {
172
+ if (!wasmLoaded && (!ctx.offloadMPCComputationURL || ctx.useDKLS)) {
148
173
  yield loadWasm(ctx, wasmOverride);
174
+ if (globalThis.initWasm) {
175
+ yield new Promise(
176
+ (resolve, reject) => {
177
+ var _a;
178
+ return (_a = globalThis.initWasm) == null ? void 0 : _a.call(globalThis, (err, result2) => {
179
+ if (err) {
180
+ reject(err);
181
+ }
182
+ resolve(result2);
183
+ });
184
+ }
185
+ );
186
+ }
187
+ wasmLoaded = true;
149
188
  }
150
189
  const result = yield executeMessage(ctx, e.data);
190
+ if (workId) {
191
+ result.workId = workId;
192
+ }
151
193
  postMessage(result);
152
- return false;
194
+ return !!workId;
153
195
  });
154
196
  }
155
197
  export {
156
- handleMessage
198
+ handleMessage,
199
+ withRetry
157
200
  };
@@ -3,4 +3,4 @@ export interface SyncWorker {
3
3
  postMessage: (message: any) => void;
4
4
  terminate: () => void;
5
5
  }
6
- export declare function setupWorker(ctx: Ctx, resFunction: (arg: any) => void, errorFunction: (err: Error) => void): Promise<Worker | SyncWorker>;
6
+ export declare function setupWorker(ctx: Ctx, resFunction: (arg: any) => Promise<void>, errorFunction: (err: Error) => void, workId: string, errorContext?: object): Promise<Worker | SyncWorker>;
@@ -4,26 +4,31 @@ import {
4
4
  } from "../chunk-WLGKV3EF.js";
5
5
  import { getPortalBaseURL } from "@getpara/core-sdk";
6
6
  import { handleMessage } from "./worker.js";
7
- function setupWorker(ctx, resFunction, errorFunction) {
7
+ const CLEAR_WORKER_TIMEOUT_MS = 1e3 * 90;
8
+ let workerInstance;
9
+ const resFunctionMap = {};
10
+ function removeWorkId(workId, skipClearTimeout) {
11
+ const { timeoutId } = resFunctionMap[workId];
12
+ delete resFunctionMap[workId];
13
+ if (skipClearTimeout) {
14
+ return;
15
+ }
16
+ clearTimeout(timeoutId);
17
+ }
18
+ function setupWorker(ctx, resFunction, errorFunction, workId, errorContext) {
8
19
  return __async(this, null, function* () {
9
- const onmessage = (event) => {
10
- if (event.data.functionType === "CUSTOM") {
11
- return;
12
- }
13
- resFunction(event.data);
14
- };
15
- const onerror = (error) => {
16
- errorFunction(error);
17
- };
18
20
  if (ctx.disableWorkers) {
19
21
  const syncWorker = {
20
22
  postMessage: function(message) {
21
23
  (function() {
22
24
  return __async(this, null, function* () {
23
25
  try {
26
+ const onmessage = (event) => {
27
+ resFunction(event.data);
28
+ };
24
29
  yield handleMessage({ data: message }, (data) => onmessage({ data }), ctx.disableWorkers);
25
30
  } catch (error) {
26
- onerror(error);
31
+ errorFunction(error);
27
32
  }
28
33
  });
29
34
  })();
@@ -34,18 +39,45 @@ function setupWorker(ctx, resFunction, errorFunction) {
34
39
  };
35
40
  return syncWorker;
36
41
  }
37
- let worker;
42
+ const timeoutId = setTimeout(() => {
43
+ removeWorkId(workId, true);
44
+ }, CLEAR_WORKER_TIMEOUT_MS);
45
+ resFunctionMap[workId] = {
46
+ fn: resFunction,
47
+ timeoutId,
48
+ errorFn: errorFunction,
49
+ errorContext: errorContext || {}
50
+ };
38
51
  if (ctx.useLocalFiles) {
39
52
  throw new Error("useLocalFiles only supported locally");
40
- } else {
53
+ } else if (!workerInstance) {
41
54
  const workerRes = yield fetch(`${getPortalBaseURL(ctx)}/static/js/mpcWorker-bundle.js`);
42
55
  const workerBlob = new Blob([yield workerRes.text()], { type: "application/javascript" });
43
56
  const workerScriptURL = URL.createObjectURL(workerBlob);
44
- worker = new Worker(workerScriptURL);
57
+ workerInstance = new Worker(workerScriptURL);
58
+ const onmessage = (event) => __async(this, null, function* () {
59
+ const { workId: messageWorkId } = event.data;
60
+ delete event.data.workId;
61
+ if (messageWorkId && resFunctionMap[messageWorkId]) {
62
+ yield resFunctionMap[messageWorkId].fn(event.data);
63
+ removeWorkId(messageWorkId);
64
+ }
65
+ });
66
+ workerInstance.onmessage = onmessage;
67
+ workerInstance.onerror = (err) => {
68
+ console.error("worker error:", err);
69
+ Object.keys(resFunctionMap).forEach((id) => {
70
+ if (resFunctionMap[id]) {
71
+ const errorMsg = `worker error with workId ${id} and opts ${JSON.stringify(resFunctionMap[id].errorContext)}: ${err.message}`;
72
+ resFunctionMap[id].errorFn(new Error(errorMsg));
73
+ removeWorkId(id);
74
+ }
75
+ });
76
+ workerInstance == null ? void 0 : workerInstance.terminate();
77
+ workerInstance = void 0;
78
+ };
45
79
  }
46
- worker.onmessage = onmessage;
47
- worker.onerror = onerror;
48
- return worker;
80
+ return workerInstance;
49
81
  });
50
82
  }
51
83
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getpara/web-sdk",
3
- "version": "2.0.0-alpha.18",
3
+ "version": "2.0.0-alpha.19",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,8 +8,8 @@
8
8
  "wasm_exec.js"
9
9
  ],
10
10
  "dependencies": {
11
- "@getpara/core-sdk": "2.0.0-alpha.18",
12
- "@getpara/user-management-client": "2.0.0-alpha.18",
11
+ "@getpara/core-sdk": "2.0.0-alpha.19",
12
+ "@getpara/user-management-client": "2.0.0-alpha.19",
13
13
  "@sentry/browser": "^9.1.0",
14
14
  "base64url": "3.0.1",
15
15
  "buffer": "6.0.3",
@@ -36,5 +36,5 @@
36
36
  "dist",
37
37
  "package.json"
38
38
  ],
39
- "gitHead": "2333bc727fe61593821d448ee12facaee230c4b0"
39
+ "gitHead": "80d2c5d7e1203fb12378aa29bfd4d7003c7d7075"
40
40
  }