@getpara/server-sdk 2.0.0-alpha.6 → 2.0.0-alpha.61

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.
@@ -57,109 +57,161 @@ var uuid = __toESM(require("uuid"));
57
57
  var import_workerWrapper = require("../workers/workerWrapper.js");
58
58
  function signTransaction(ctx, userId, walletId, share, tx, chainId, sessionCookie, isDKLS) {
59
59
  return __async(this, null, function* () {
60
- return yield new Promise((resolve) => __async(this, null, function* () {
60
+ return new Promise((resolve, reject) => __async(this, null, function* () {
61
61
  const workId = uuid.v4();
62
- const worker = yield (0, import_workerWrapper.setupWorker)(
63
- ctx,
64
- (sendTransactionRes) => __async(this, null, function* () {
65
- resolve(sendTransactionRes);
66
- }),
67
- workId
68
- );
69
- worker.postMessage({
70
- env: ctx.env,
71
- apiKey: ctx.apiKey,
72
- cosmosPrefix: ctx.cosmosPrefix,
73
- params: { share, walletId, userId, tx, chainId },
74
- functionType: "SIGN_TRANSACTION",
75
- offloadMPCComputationURL: ctx.offloadMPCComputationURL,
76
- disableWorkers: ctx.disableWorkers,
77
- sessionCookie,
78
- useDKLS: isDKLS,
79
- disableWebSockets: ctx.disableWebSockets,
80
- wasmOverride: ctx.wasmOverride,
81
- workId
82
- });
62
+ try {
63
+ const worker = yield (0, import_workerWrapper.setupWorker)(
64
+ ctx,
65
+ (sendTransactionRes) => __async(this, null, function* () {
66
+ resolve(sendTransactionRes);
67
+ }),
68
+ (error) => {
69
+ reject(error);
70
+ },
71
+ workId,
72
+ {
73
+ params: { walletId, userId, tx, chainId },
74
+ functionType: "SIGN_TRANSACTION",
75
+ disableWorkers: ctx.disableWorkers,
76
+ disableWebSockets: ctx.disableWebSockets
77
+ }
78
+ );
79
+ worker.postMessage({
80
+ env: ctx.env,
81
+ apiKey: ctx.apiKey,
82
+ cosmosPrefix: ctx.cosmosPrefix,
83
+ params: { share, walletId, userId, tx, chainId },
84
+ functionType: "SIGN_TRANSACTION",
85
+ offloadMPCComputationURL: ctx.offloadMPCComputationURL,
86
+ disableWorkers: ctx.disableWorkers,
87
+ sessionCookie,
88
+ useDKLS: isDKLS,
89
+ disableWebSockets: ctx.disableWebSockets,
90
+ wasmOverride: ctx.wasmOverride,
91
+ workId
92
+ });
93
+ } catch (error) {
94
+ reject(error);
95
+ }
83
96
  }));
84
97
  });
85
98
  }
86
99
  function sendTransaction(ctx, userId, walletId, share, tx, chainId, sessionCookie, isDKLS) {
87
100
  return __async(this, null, function* () {
88
- return yield new Promise((resolve) => __async(this, null, function* () {
101
+ return new Promise((resolve, reject) => __async(this, null, function* () {
89
102
  const workId = uuid.v4();
90
- const worker = yield (0, import_workerWrapper.setupWorker)(
91
- ctx,
92
- (sendTransactionRes) => __async(this, null, function* () {
93
- resolve(sendTransactionRes);
94
- }),
95
- workId
96
- );
97
- worker.postMessage({
98
- env: ctx.env,
99
- apiKey: ctx.apiKey,
100
- params: { share, walletId, userId, tx, chainId },
101
- functionType: "SEND_TRANSACTION",
102
- offloadMPCComputationURL: ctx.offloadMPCComputationURL,
103
- disableWorkers: ctx.disableWorkers,
104
- sessionCookie,
105
- useDKLS: isDKLS,
106
- disableWebSockets: ctx.disableWebSockets,
107
- wasmOverride: ctx.wasmOverride,
108
- workId
109
- });
103
+ try {
104
+ const worker = yield (0, import_workerWrapper.setupWorker)(
105
+ ctx,
106
+ (sendTransactionRes) => __async(this, null, function* () {
107
+ resolve(sendTransactionRes);
108
+ }),
109
+ (error) => {
110
+ reject(error);
111
+ },
112
+ workId,
113
+ {
114
+ params: { walletId, userId, tx, chainId },
115
+ functionType: "SEND_TRANSACTION",
116
+ disableWorkers: ctx.disableWorkers,
117
+ disableWebSockets: ctx.disableWebSockets
118
+ }
119
+ );
120
+ worker.postMessage({
121
+ env: ctx.env,
122
+ apiKey: ctx.apiKey,
123
+ params: { share, walletId, userId, tx, chainId },
124
+ functionType: "SEND_TRANSACTION",
125
+ offloadMPCComputationURL: ctx.offloadMPCComputationURL,
126
+ disableWorkers: ctx.disableWorkers,
127
+ sessionCookie,
128
+ useDKLS: isDKLS,
129
+ disableWebSockets: ctx.disableWebSockets,
130
+ wasmOverride: ctx.wasmOverride,
131
+ workId
132
+ });
133
+ } catch (error) {
134
+ reject(error);
135
+ }
110
136
  }));
111
137
  });
112
138
  }
113
139
  function signMessage(ctx, userId, walletId, share, message, sessionCookie, isDKLS) {
114
140
  return __async(this, null, function* () {
115
- return yield new Promise((resolve) => __async(this, null, function* () {
141
+ return new Promise((resolve, reject) => __async(this, null, function* () {
116
142
  const workId = uuid.v4();
117
- const worker = yield (0, import_workerWrapper.setupWorker)(
118
- ctx,
119
- (signMessageRes) => __async(this, null, function* () {
120
- resolve(signMessageRes);
121
- }),
122
- workId
123
- );
124
- worker.postMessage({
125
- env: ctx.env,
126
- apiKey: ctx.apiKey,
127
- params: { share, walletId, userId, message },
128
- functionType: "SIGN_MESSAGE",
129
- offloadMPCComputationURL: ctx.offloadMPCComputationURL,
130
- disableWorkers: ctx.disableWorkers,
131
- sessionCookie,
132
- useDKLS: isDKLS,
133
- disableWebSockets: ctx.disableWebSockets,
134
- wasmOverride: ctx.wasmOverride,
135
- workId
136
- });
143
+ try {
144
+ const worker = yield (0, import_workerWrapper.setupWorker)(
145
+ ctx,
146
+ (signMessageRes) => __async(this, null, function* () {
147
+ resolve(signMessageRes);
148
+ }),
149
+ (error) => {
150
+ reject(error);
151
+ },
152
+ workId,
153
+ {
154
+ params: { walletId, userId, message },
155
+ functionType: "SIGN_MESSAGE",
156
+ disableWorkers: ctx.disableWorkers,
157
+ disableWebSockets: ctx.disableWebSockets
158
+ }
159
+ );
160
+ worker.postMessage({
161
+ env: ctx.env,
162
+ apiKey: ctx.apiKey,
163
+ params: { share, walletId, userId, message },
164
+ functionType: "SIGN_MESSAGE",
165
+ offloadMPCComputationURL: ctx.offloadMPCComputationURL,
166
+ disableWorkers: ctx.disableWorkers,
167
+ sessionCookie,
168
+ useDKLS: isDKLS,
169
+ disableWebSockets: ctx.disableWebSockets,
170
+ wasmOverride: ctx.wasmOverride,
171
+ workId
172
+ });
173
+ } catch (error) {
174
+ reject(error);
175
+ }
137
176
  }));
138
177
  });
139
178
  }
140
179
  function ed25519Sign(ctx, userId, walletId, share, base64Bytes, sessionCookie) {
141
180
  return __async(this, null, function* () {
142
- return yield new Promise((resolve) => __async(this, null, function* () {
181
+ return new Promise((resolve, reject) => __async(this, null, function* () {
143
182
  const workId = uuid.v4();
144
- const worker = yield (0, import_workerWrapper.setupWorker)(
145
- ctx,
146
- (signMessageRes) => __async(this, null, function* () {
147
- resolve(signMessageRes);
148
- }),
149
- workId
150
- );
151
- worker.postMessage({
152
- env: ctx.env,
153
- apiKey: ctx.apiKey,
154
- params: { share, walletId, userId, base64Bytes },
155
- functionType: "ED25519_SIGN",
156
- offloadMPCComputationURL: ctx.offloadMPCComputationURL,
157
- disableWorkers: ctx.disableWorkers,
158
- sessionCookie,
159
- disableWebSockets: ctx.disableWebSockets,
160
- wasmOverride: ctx.wasmOverride,
161
- workId
162
- });
183
+ try {
184
+ const worker = yield (0, import_workerWrapper.setupWorker)(
185
+ ctx,
186
+ (signMessageRes) => __async(this, null, function* () {
187
+ resolve(signMessageRes);
188
+ }),
189
+ (error) => {
190
+ reject(error);
191
+ },
192
+ workId,
193
+ {
194
+ params: { walletId, userId, base64Bytes },
195
+ functionType: "ED25519_SIGN",
196
+ disableWorkers: ctx.disableWorkers,
197
+ disableWebSockets: ctx.disableWebSockets
198
+ }
199
+ );
200
+ worker.postMessage({
201
+ env: ctx.env,
202
+ apiKey: ctx.apiKey,
203
+ params: { share, walletId, userId, base64Bytes },
204
+ functionType: "ED25519_SIGN",
205
+ offloadMPCComputationURL: ctx.offloadMPCComputationURL,
206
+ disableWorkers: ctx.disableWorkers,
207
+ sessionCookie,
208
+ disableWebSockets: ctx.disableWebSockets,
209
+ wasmOverride: ctx.wasmOverride,
210
+ workId
211
+ });
212
+ } catch (error) {
213
+ reject(error);
214
+ }
163
215
  }));
164
216
  });
165
217
  }
@@ -1,6 +1,8 @@
1
+ var __create = Object.create;
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __export = (target, all) => {
6
8
  for (var name in all)
@@ -14,6 +16,14 @@ var __copyProps = (to, from, except, desc) => {
14
16
  }
15
17
  return to;
16
18
  };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
17
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
28
  var __async = (__this, __arguments, generator) => {
19
29
  return new Promise((resolve, reject) => {
@@ -50,6 +60,7 @@ __export(walletUtils_exports, {
50
60
  });
51
61
  module.exports = __toCommonJS(walletUtils_exports);
52
62
  var import_core_sdk = require("@getpara/core-sdk");
63
+ var uuid = __toESM(require("uuid"));
53
64
  const configCGGMPBase = (serverUrl, walletId, id) => `{"ServerUrl":"${serverUrl}", "WalletId": "${walletId}", "Id":"${id}", "Ids":["USER","CAPSULE"], "Threshold":1}`;
54
65
  const configDKLSBase = (walletId, id, disableWebSockets) => `{"walletId": "${walletId}", "id":"${id}", "otherId":"CAPSULE", "isReceiver": false, "disableWebSockets": ${disableWebSockets}}`;
55
66
  function keygenRequest(ctx, userId, walletId, protocolId) {
@@ -133,20 +144,30 @@ function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType) {
133
144
  }
134
145
  function ed25519Sign(ctx, share, userId, walletId, base64Bytes) {
135
146
  return __async(this, null, function* () {
136
- const { protocolId } = yield ctx.client.preSignMessage(userId, walletId, base64Bytes, "ED25519");
137
- try {
138
- const base64Sig = yield new Promise(
139
- (resolve, reject) => global.ed25519Sign(share, protocolId, base64Bytes, (err, result) => {
140
- if (err) {
141
- reject(err);
142
- }
143
- resolve(result);
144
- })
145
- );
146
- return { signature: base64Sig };
147
- } catch (e) {
148
- throw new Error(`error signing for account of type SOLANA with userId ${userId} and walletId ${walletId}`);
147
+ const protocolId = uuid.v4();
148
+ const preSignMessageRes = ctx.client.preSignMessage(userId, walletId, base64Bytes, "ED25519", void 0, protocolId);
149
+ const signRes = function() {
150
+ return __async(this, null, function* () {
151
+ try {
152
+ const base64Sig = yield new Promise(
153
+ (resolve, reject) => global.ed25519Sign(share, protocolId, base64Bytes, (err, result) => {
154
+ if (err) {
155
+ reject(err);
156
+ }
157
+ resolve(result);
158
+ })
159
+ );
160
+ return { signature: base64Sig };
161
+ } catch (e) {
162
+ throw new Error(`error signing for account of type SOLANA with userId ${userId} and walletId ${walletId}`);
163
+ }
164
+ });
165
+ }();
166
+ const { pendingTransactionId } = yield preSignMessageRes;
167
+ if (pendingTransactionId) {
168
+ return { pendingTransactionId };
149
169
  }
170
+ return yield signRes;
150
171
  });
151
172
  }
152
173
  function keygen(ctx, userId, type, secretKey) {
@@ -233,81 +254,102 @@ function preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type,
233
254
  }
234
255
  function signMessage(ctx, share, walletId, userId, message) {
235
256
  return __async(this, null, function* () {
236
- const { protocolId, pendingTransactionId } = yield ctx.client.preSignMessage(userId, walletId, message);
237
- if (pendingTransactionId) {
238
- return { pendingTransactionId };
239
- }
257
+ const protocolId = uuid.v4();
258
+ const preSignMessageRes = ctx.client.preSignMessage(userId, walletId, message, null, null, protocolId);
240
259
  if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
241
260
  return signMessageRequest(ctx, userId, walletId, protocolId, message, share);
242
261
  }
243
262
  const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
244
263
  const signMessageFn = ctx.useDKLS ? global.dklsSignMessage : global.signMessage;
245
- try {
246
- return yield new Promise(
247
- (resolve, reject) => signMessageFn(share, serverUrl, message, protocolId, (err, result) => {
248
- if (err) {
249
- reject(err);
250
- }
251
- resolve({ signature: result });
252
- })
253
- );
254
- } catch (e) {
255
- throw new Error(`error signing for account with userId ${userId} and walletId ${walletId}`);
264
+ const signMessageRes = function() {
265
+ return __async(this, null, function* () {
266
+ try {
267
+ return yield new Promise(
268
+ (resolve, reject) => signMessageFn(share, serverUrl, message, 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 for account with userId ${userId} and walletId ${walletId}`);
277
+ }
278
+ });
279
+ }();
280
+ const { pendingTransactionId } = yield preSignMessageRes;
281
+ if (pendingTransactionId) {
282
+ return { pendingTransactionId };
256
283
  }
284
+ return yield signMessageRes;
257
285
  });
258
286
  }
259
287
  function signTransaction(ctx, share, walletId, userId, tx, chainId) {
260
288
  return __async(this, null, function* () {
261
- const {
262
- data: { protocolId, pendingTransactionId }
263
- } = yield ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId });
264
- if (pendingTransactionId) {
265
- return { pendingTransactionId };
266
- }
289
+ const protocolId = uuid.v4();
290
+ const signTransactionRes = ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId, protocolId });
267
291
  if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
268
292
  return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
269
293
  }
270
294
  const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
271
295
  const signTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
272
- try {
273
- return yield new Promise(
274
- (resolve, reject) => signTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
275
- if (err) {
276
- reject(err);
277
- }
278
- resolve({ signature: result });
279
- })
280
- );
281
- } catch (e) {
282
- throw new Error(`error signing transaction for account with userId ${userId} and walletId ${walletId}`);
296
+ const signTxRes = function() {
297
+ return __async(this, null, function* () {
298
+ try {
299
+ return yield new Promise(
300
+ (resolve, reject) => signTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
301
+ if (err) {
302
+ reject(err);
303
+ }
304
+ resolve({ signature: result });
305
+ })
306
+ );
307
+ } catch (e) {
308
+ throw new Error(`error signing transaction for account with userId ${userId} and walletId ${walletId}`);
309
+ }
310
+ });
311
+ }();
312
+ const {
313
+ data: { pendingTransactionId }
314
+ } = yield signTransactionRes;
315
+ if (pendingTransactionId) {
316
+ return { pendingTransactionId };
283
317
  }
318
+ return yield signTxRes;
284
319
  });
285
320
  }
286
321
  function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
287
322
  return __async(this, null, function* () {
288
- const {
289
- data: { protocolId, pendingTransactionId }
290
- } = yield ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId });
291
- if (pendingTransactionId) {
292
- return { pendingTransactionId };
293
- }
323
+ const protocolId = uuid.v4();
324
+ const sendTransactionRes = ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId, protocolId });
294
325
  if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
295
326
  return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
296
327
  }
297
328
  const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
298
329
  const sendTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
299
- try {
300
- return yield new Promise(
301
- (resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
302
- if (err) {
303
- reject(err);
304
- }
305
- resolve({ signature: result });
306
- })
307
- );
308
- } catch (e) {
309
- throw new Error(`error signing transaction to send for account with userId ${userId} and walletId ${walletId}`);
330
+ const sendTxRes = function() {
331
+ return __async(this, null, function* () {
332
+ try {
333
+ return yield new Promise(
334
+ (resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
335
+ if (err) {
336
+ reject(err);
337
+ }
338
+ resolve({ signature: result });
339
+ })
340
+ );
341
+ } catch (e) {
342
+ throw new Error(`error signing transaction to send for account with userId ${userId} and walletId ${walletId}`);
343
+ }
344
+ });
345
+ }();
346
+ const {
347
+ data: { pendingTransactionId }
348
+ } = yield sendTransactionRes;
349
+ if (pendingTransactionId) {
350
+ return { pendingTransactionId };
310
351
  }
352
+ return yield sendTxRes;
311
353
  });
312
354
  }
313
355
  function refresh(ctx, share, walletId, userId) {
@@ -48,13 +48,15 @@ var __async = (__this, __arguments, generator) => {
48
48
  var worker_exports = {};
49
49
  __export(worker_exports, {
50
50
  handleMessage: () => handleMessage,
51
- requestWasmWithRetries: () => requestWasmWithRetries
51
+ requestWasmWithRetries: () => requestWasmWithRetries,
52
+ withRetry: () => withRetry
52
53
  });
53
54
  module.exports = __toCommonJS(worker_exports);
54
55
  var import_axios = __toESM(require("axios"));
55
56
  var import_core_sdk = require("@getpara/core-sdk");
56
57
  var walletUtils = __toESM(require("./walletUtils.js"));
57
58
  let rawWasm;
59
+ let wasmLoaded = false;
58
60
  function requestWasmWithRetries(ctx, retries = 3) {
59
61
  return __async(this, null, function* () {
60
62
  for (let i = 0; i < retries; i++) {
@@ -85,21 +87,24 @@ function executeMessage(ctx, message) {
85
87
  return __async(this, null, function* () {
86
88
  const { functionType, params } = message;
87
89
  switch (functionType) {
90
+ case "INIT": {
91
+ return {};
92
+ }
88
93
  case "KEYGEN": {
89
94
  const { userId, secretKey, type = "EVM" } = params;
90
95
  return walletUtils.keygen(ctx, userId, type, secretKey);
91
96
  }
92
97
  case "SIGN_TRANSACTION": {
93
98
  const { share, walletId, userId, tx, chainId } = params;
94
- return walletUtils.signTransaction(ctx, share, walletId, userId, tx, chainId);
99
+ return withRetry(() => walletUtils.signTransaction(ctx, share, walletId, userId, tx, chainId));
95
100
  }
96
101
  case "SEND_TRANSACTION": {
97
102
  const { share, walletId, userId, tx, chainId } = params;
98
- return walletUtils.sendTransaction(ctx, share, walletId, userId, tx, chainId);
103
+ return withRetry(() => walletUtils.sendTransaction(ctx, share, walletId, userId, tx, chainId));
99
104
  }
100
105
  case "SIGN_MESSAGE": {
101
106
  const { share, walletId, userId, message: message2 } = params;
102
- return walletUtils.signMessage(ctx, share, walletId, userId, message2);
107
+ return withRetry(() => walletUtils.signMessage(ctx, share, walletId, userId, message2));
103
108
  }
104
109
  case "REFRESH": {
105
110
  const { share, walletId, userId } = params;
@@ -144,6 +149,29 @@ function executeMessage(ctx, message) {
144
149
  }
145
150
  });
146
151
  }
152
+ function withRetry(operation, maxRetries = 2, timeoutMs = 1e4) {
153
+ return __async(this, null, function* () {
154
+ let retries = 0;
155
+ while (true) {
156
+ try {
157
+ const operationPromise = operation();
158
+ const timeoutPromise = new Promise((_, reject) => {
159
+ const timeoutId = setTimeout(() => {
160
+ reject(new Error(`Operation timed out after ${timeoutMs}ms`));
161
+ }, timeoutMs);
162
+ operationPromise.finally(() => clearTimeout(timeoutId));
163
+ });
164
+ return yield Promise.race([operationPromise, timeoutPromise]);
165
+ } catch (error) {
166
+ retries++;
167
+ if (retries > maxRetries) {
168
+ throw error;
169
+ }
170
+ console.warn(`Operation failed (attempt ${retries}/${maxRetries}), retrying...`, error);
171
+ }
172
+ }
173
+ });
174
+ }
147
175
  function handleMessage(e) {
148
176
  return __async(this, null, function* () {
149
177
  const {
@@ -167,8 +195,20 @@ function handleMessage(e) {
167
195
  disableWebSockets: !!disableWebSockets,
168
196
  cosmosPrefix
169
197
  };
170
- if (!ctx.offloadMPCComputationURL || ctx.useDKLS) {
198
+ if (!wasmLoaded && (!ctx.offloadMPCComputationURL || ctx.useDKLS)) {
171
199
  yield loadWasm(ctx);
200
+ if (global.initWasm) {
201
+ const serverUrl = (0, import_core_sdk.getBaseMPCNetworkUrl)(ctx.env, !ctx.disableWebSockets);
202
+ yield new Promise(
203
+ (resolve, reject) => global.initWasm((err, result2) => {
204
+ if (err) {
205
+ reject(err);
206
+ }
207
+ resolve(result2);
208
+ }, serverUrl)
209
+ );
210
+ }
211
+ wasmLoaded = true;
172
212
  }
173
213
  const result = yield executeMessage(ctx, e.data);
174
214
  result.workId = workId;
@@ -178,5 +218,6 @@ function handleMessage(e) {
178
218
  // Annotate the CommonJS export names for ESM import in node:
179
219
  0 && (module.exports = {
180
220
  handleMessage,
181
- requestWasmWithRetries
221
+ requestWasmWithRetries,
222
+ withRetry
182
223
  });
@@ -37,6 +37,7 @@ var __async = (__this, __arguments, generator) => {
37
37
  };
38
38
  var workerWrapper_exports = {};
39
39
  __export(workerWrapper_exports, {
40
+ resetWorker: () => resetWorker,
40
41
  setupWorker: () => setupWorker
41
42
  });
42
43
  module.exports = __toCommonJS(workerWrapper_exports);
@@ -53,30 +54,70 @@ function removeWorkId(workId, skipClearTimeout) {
53
54
  }
54
55
  clearTimeout(timeoutId);
55
56
  }
56
- function setupWorker(ctx, resFunction, workId) {
57
+ function resetWorker() {
58
+ if (worker) {
59
+ worker.terminate();
60
+ worker = void 0;
61
+ }
62
+ }
63
+ function setupWorker(ctx, resFunction, errorFunction, workId, errorContext) {
57
64
  return __async(this, null, function* () {
58
65
  const timeoutId = setTimeout(() => {
59
- removeWorkId(workId, true);
66
+ if (resFunctionMap[workId]) {
67
+ const errorMsg = `worker operation timed out after ${CLEAR_WORKER_TIMEOUT_MS}ms for workId ${workId} and opts ${JSON.stringify(resFunctionMap[workId].errorContext)}`;
68
+ resFunctionMap[workId].errorFn(new Error(errorMsg));
69
+ removeWorkId(workId, true);
70
+ }
60
71
  }, CLEAR_WORKER_TIMEOUT_MS);
61
72
  resFunctionMap[workId] = {
62
73
  fn: resFunction,
63
- timeoutId
74
+ errorFn: errorFunction,
75
+ timeoutId,
76
+ errorContext
64
77
  };
65
78
  if (!worker || !worker.threadId) {
66
- const workerRes = yield fetch(`${(0, import_core_sdk.getPortalBaseURL)(ctx)}/static/js/mpcWorkerServer-bundle.js`);
79
+ const isDeno = typeof (globalThis == null ? void 0 : globalThis.Deno) !== "undefined";
80
+ const workerFileName = isDeno ? "mpcWorkerServer-esm.js" : "mpcWorkerServer-bundle.js";
81
+ const workerRes = yield fetch(`${(0, import_core_sdk.getPortalBaseURL)(ctx)}/static/js/${workerFileName}`);
67
82
  worker = new import_worker_threads.Worker(yield workerRes.text(), { eval: true });
68
83
  const onmessage = (message) => __async(this, null, function* () {
69
84
  const { workId: messageWorkId } = message;
85
+ if (!resFunctionMap[messageWorkId]) {
86
+ console.warn(`received message for unknown workId: ${messageWorkId}`);
87
+ return;
88
+ }
70
89
  delete message.workId;
71
- yield resFunctionMap[messageWorkId].fn(message);
72
- removeWorkId(messageWorkId);
90
+ try {
91
+ yield resFunctionMap[messageWorkId].fn(message);
92
+ removeWorkId(messageWorkId);
93
+ } catch (error) {
94
+ console.error(`error in worker message handler for workId ${messageWorkId}:`, error);
95
+ if (resFunctionMap[messageWorkId]) {
96
+ resFunctionMap[messageWorkId].errorFn(error);
97
+ removeWorkId(messageWorkId);
98
+ }
99
+ }
73
100
  });
74
101
  worker.on("message", onmessage);
75
102
  worker.on("error", (err) => {
76
- throw err;
103
+ console.error("worker error:", err);
104
+ Object.keys(resFunctionMap).forEach((id) => {
105
+ if (resFunctionMap[id]) {
106
+ const errorMsg = `worker error with workId ${id} and opts ${JSON.stringify(resFunctionMap[id].errorContext)}: ${err.message}`;
107
+ resFunctionMap[id].errorFn(new Error(errorMsg));
108
+ removeWorkId(id);
109
+ }
110
+ });
77
111
  });
78
112
  worker.on("exit", (code) => {
79
113
  console.error(`worker stopped with exit code ${code}`);
114
+ Object.keys(resFunctionMap).forEach((id) => {
115
+ if (resFunctionMap[id]) {
116
+ resFunctionMap[id].errorFn(new Error(`worker exited unexpectedly with code ${code}`));
117
+ removeWorkId(id);
118
+ }
119
+ });
120
+ worker = void 0;
80
121
  });
81
122
  }
82
123
  return worker;
@@ -84,5 +125,6 @@ function setupWorker(ctx, resFunction, workId) {
84
125
  }
85
126
  // Annotate the CommonJS export names for ESM import in node:
86
127
  0 && (module.exports = {
128
+ resetWorker,
87
129
  setupWorker
88
130
  });