@getpara/web-sdk 1.15.1 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,7 @@ import {
4
4
  } from "../chunk-M66XENHI.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-M66XENHI.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-M66XENHI.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-M66XENHI.js";
5
5
  import { getBaseMPCNetworkUrl, WalletScheme, WalletType } 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) {
@@ -179,16 +180,8 @@ function preKeygen(ctx, _partnerId, pregenIdentifier, pregenIdentifierType, type
179
180
  }
180
181
  function signMessage(ctx, share, walletId, userId, message, cosmosSignDoc) {
181
182
  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
- }
183
+ const protocolId = uuid.v4();
184
+ const preSignMessageRes = ctx.client.preSignMessage(userId, walletId, message, null, cosmosSignDoc, protocolId);
192
185
  if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
193
186
  return signMessageRequest(ctx, userId, walletId, protocolId, message, share);
194
187
  }
@@ -199,28 +192,33 @@ function signMessage(ctx, share, walletId, userId, message, cosmosSignDoc) {
199
192
  parsedShare.disableWebSockets = ctx.disableWebSockets;
200
193
  }
201
194
  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}`);
195
+ const signMessageRes = function() {
196
+ return __async(this, null, function* () {
197
+ try {
198
+ return yield new Promise(
199
+ (resolve, reject) => signMessageFn(share, serverUrl, message, protocolId, (err, result) => {
200
+ if (err) {
201
+ reject(err);
202
+ }
203
+ resolve({ signature: result });
204
+ })
205
+ );
206
+ } catch (e) {
207
+ throw new Error(`error signing for account with userId ${userId} and walletId ${walletId}`);
208
+ }
209
+ });
210
+ }();
211
+ const { pendingTransactionId } = yield preSignMessageRes;
212
+ if (pendingTransactionId) {
213
+ return { pendingTransactionId };
213
214
  }
215
+ return yield signMessageRes;
214
216
  });
215
217
  }
216
218
  function signTransaction(ctx, share, walletId, userId, tx, chainId) {
217
219
  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
- }
220
+ const protocolId = uuid.v4();
221
+ const signTransactionRes = ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId, protocolId });
224
222
  if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
225
223
  return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
226
224
  }
@@ -231,28 +229,35 @@ function signTransaction(ctx, share, walletId, userId, tx, chainId) {
231
229
  parsedShare.disableWebSockets = ctx.disableWebSockets;
232
230
  }
233
231
  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}`);
232
+ const signTxRes = function() {
233
+ return __async(this, null, function* () {
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}`);
245
+ }
246
+ });
247
+ }();
248
+ const {
249
+ data: { pendingTransactionId }
250
+ } = yield signTransactionRes;
251
+ if (pendingTransactionId) {
252
+ return { pendingTransactionId };
245
253
  }
254
+ return yield signTxRes;
246
255
  });
247
256
  }
248
257
  function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
249
258
  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
- }
259
+ const protocolId = uuid.v4();
260
+ const sendTransactionRes = ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId, protocolId });
256
261
  if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
257
262
  return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
258
263
  }
@@ -263,18 +268,29 @@ function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
263
268
  parsedShare.disableWebSockets = ctx.disableWebSockets;
264
269
  }
265
270
  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}`);
271
+ const sendTxRes = function() {
272
+ return __async(this, null, function* () {
273
+ try {
274
+ return yield new Promise(
275
+ (resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
276
+ if (err) {
277
+ reject(err);
278
+ }
279
+ resolve({ signature: result });
280
+ })
281
+ );
282
+ } catch (e) {
283
+ throw new Error(`error signing transaction to send for account with userId ${userId} and walletId ${walletId}`);
284
+ }
285
+ });
286
+ }();
287
+ const {
288
+ data: { pendingTransactionId }
289
+ } = yield sendTransactionRes;
290
+ if (pendingTransactionId) {
291
+ return { pendingTransactionId };
277
292
  }
293
+ return yield sendTxRes;
278
294
  });
279
295
  }
280
296
  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: 8000)
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>;
@@ -28,6 +28,7 @@ import {
28
28
  paraVersion,
29
29
  WalletType
30
30
  } from "@getpara/core-sdk";
31
+ let wasmLoaded = false;
31
32
  function loadWasm(ctx, wasmOverride) {
32
33
  return __async(this, null, function* () {
33
34
  if (typeof self === "undefined") {
@@ -57,15 +58,15 @@ function executeMessage(ctx, message) {
57
58
  }
58
59
  case "SIGN_TRANSACTION": {
59
60
  const { share, walletId, userId, tx, chainId } = params;
60
- return walletUtils.signTransaction(ctx, share, walletId, userId, tx, chainId);
61
+ return withRetry(() => walletUtils.signTransaction(ctx, share, walletId, userId, tx, chainId));
61
62
  }
62
63
  case "SEND_TRANSACTION": {
63
64
  const { share, walletId, userId, tx, chainId } = params;
64
- return walletUtils.sendTransaction(ctx, share, walletId, userId, tx, chainId);
65
+ return withRetry(() => walletUtils.sendTransaction(ctx, share, walletId, userId, tx, chainId));
65
66
  }
66
67
  case "SIGN_MESSAGE": {
67
68
  const { share, walletId, userId, message: message2, cosmosSignDoc } = params;
68
- return walletUtils.signMessage(ctx, share, walletId, userId, message2, cosmosSignDoc);
69
+ return withRetry(() => walletUtils.signMessage(ctx, share, walletId, userId, message2, cosmosSignDoc));
69
70
  }
70
71
  case "REFRESH": {
71
72
  const { share, walletId, userId, oldPartnerId, newPartnerId, keyShareProtocolId } = params;
@@ -117,6 +118,29 @@ function executeMessage(ctx, message) {
117
118
  }
118
119
  });
119
120
  }
121
+ function withRetry(operation, maxRetries = 2, timeoutMs = 8e3) {
122
+ return __async(this, null, function* () {
123
+ let retries = 0;
124
+ while (true) {
125
+ try {
126
+ const operationPromise = operation();
127
+ const timeoutPromise = new Promise((_, reject) => {
128
+ const timeoutId = setTimeout(() => {
129
+ reject(new Error(`Operation timed out after ${timeoutMs}ms`));
130
+ }, timeoutMs);
131
+ operationPromise.finally(() => clearTimeout(timeoutId));
132
+ });
133
+ return yield Promise.race([operationPromise, timeoutPromise]);
134
+ } catch (error) {
135
+ retries++;
136
+ if (retries > maxRetries) {
137
+ throw error;
138
+ }
139
+ console.warn(`Operation failed (attempt ${retries}/${maxRetries}), retrying...`, error);
140
+ }
141
+ }
142
+ });
143
+ }
120
144
  function handleMessage(e, postMessage, useFetchAdapter) {
121
145
  return __async(this, null, function* () {
122
146
  const {
@@ -128,7 +152,8 @@ function handleMessage(e, postMessage, useFetchAdapter) {
128
152
  sessionCookie,
129
153
  useDKLS,
130
154
  disableWebSockets,
131
- wasmOverride
155
+ wasmOverride,
156
+ workId
132
157
  } = e.data;
133
158
  if (!env) {
134
159
  return true;
@@ -150,14 +175,32 @@ function handleMessage(e, postMessage, useFetchAdapter) {
150
175
  disableWebSockets: !!disableWebSockets,
151
176
  wasmOverride
152
177
  };
153
- if (!ctx.offloadMPCComputationURL || ctx.useDKLS) {
178
+ if (!wasmLoaded && (!ctx.offloadMPCComputationURL || ctx.useDKLS)) {
154
179
  yield loadWasm(ctx, wasmOverride);
180
+ if (globalThis.initWasm) {
181
+ yield new Promise(
182
+ (resolve, reject) => {
183
+ var _a;
184
+ return (_a = globalThis.initWasm) == null ? void 0 : _a.call(globalThis, (err, result2) => {
185
+ if (err) {
186
+ reject(err);
187
+ }
188
+ resolve(result2);
189
+ });
190
+ }
191
+ );
192
+ }
193
+ wasmLoaded = true;
155
194
  }
156
195
  const result = yield executeMessage(ctx, e.data);
196
+ if (workId) {
197
+ result.workId = workId;
198
+ }
157
199
  postMessage(result);
158
- return false;
200
+ return !!workId;
159
201
  });
160
202
  }
161
203
  export {
162
- handleMessage
204
+ handleMessage,
205
+ withRetry
163
206
  };
@@ -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-M66XENHI.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": "1.15.1",
3
+ "version": "1.16.0",
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": "1.15.1",
12
- "@getpara/user-management-client": "1.15.1",
11
+ "@getpara/core-sdk": "1.16.0",
12
+ "@getpara/user-management-client": "1.16.0",
13
13
  "base64url": "3.0.1",
14
14
  "buffer": "6.0.3",
15
15
  "cbor-web": "8.1.0",
@@ -35,5 +35,5 @@
35
35
  "dist",
36
36
  "package.json"
37
37
  ],
38
- "gitHead": "591a28303fdd9abd944d512e7dfdb73759a32ed8"
38
+ "gitHead": "27004dc519903d24ffafa0d8da16976dae9278a2"
39
39
  }