@getpara/server-sdk 2.0.0-alpha.7 → 2.0.0-alpha.71
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.
- package/dist/cjs/ParaServer.js +12 -9
- package/dist/cjs/ServerUtils.js +28 -1
- package/dist/cjs/wallet/keygen.js +246 -126
- package/dist/cjs/wallet/privateKey.js +35 -22
- package/dist/cjs/wallet/signing.js +136 -84
- package/dist/cjs/workers/walletUtils.js +104 -62
- package/dist/cjs/workers/worker.js +47 -6
- package/dist/cjs/workers/workerWrapper.js +49 -7
- package/dist/esm/ParaServer.js +9 -12
- package/dist/esm/ServerUtils.js +5 -2
- package/dist/esm/wallet/keygen.js +221 -120
- package/dist/esm/wallet/privateKey.js +35 -22
- package/dist/esm/wallet/signing.js +136 -84
- package/dist/esm/workers/walletUtils.js +91 -67
- package/dist/esm/workers/worker.js +52 -6
- package/dist/esm/workers/workerWrapper.js +48 -7
- package/dist/types/ParaServer.d.ts +4 -1
- package/dist/types/ServerUtils.d.ts +2 -3
- package/dist/types/wallet/keygen.d.ts +1 -0
- package/dist/types/workers/walletUtils.d.ts +1 -3
- package/dist/types/workers/worker.d.ts +8 -0
- package/dist/types/workers/workerWrapper.d.ts +2 -2
- package/package.json +19 -20
|
@@ -2,103 +2,155 @@ import "../chunk-FTA5RKYX.js";
|
|
|
2
2
|
import * as uuid from "uuid";
|
|
3
3
|
import { setupWorker } from "../workers/workerWrapper.js";
|
|
4
4
|
async function signTransaction(ctx, userId, walletId, share, tx, chainId, sessionCookie, isDKLS) {
|
|
5
|
-
return
|
|
5
|
+
return new Promise(async (resolve, reject) => {
|
|
6
6
|
const workId = uuid.v4();
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
7
|
+
try {
|
|
8
|
+
const worker = await setupWorker(
|
|
9
|
+
ctx,
|
|
10
|
+
async (sendTransactionRes) => {
|
|
11
|
+
resolve(sendTransactionRes);
|
|
12
|
+
},
|
|
13
|
+
(error) => {
|
|
14
|
+
reject(error);
|
|
15
|
+
},
|
|
16
|
+
workId,
|
|
17
|
+
{
|
|
18
|
+
params: { walletId, userId, tx, chainId },
|
|
19
|
+
functionType: "SIGN_TRANSACTION",
|
|
20
|
+
disableWorkers: ctx.disableWorkers,
|
|
21
|
+
disableWebSockets: ctx.disableWebSockets
|
|
22
|
+
}
|
|
23
|
+
);
|
|
24
|
+
worker.postMessage({
|
|
25
|
+
env: ctx.env,
|
|
26
|
+
apiKey: ctx.apiKey,
|
|
27
|
+
cosmosPrefix: ctx.cosmosPrefix,
|
|
28
|
+
params: { share, walletId, userId, tx, chainId },
|
|
29
|
+
functionType: "SIGN_TRANSACTION",
|
|
30
|
+
offloadMPCComputationURL: ctx.offloadMPCComputationURL,
|
|
31
|
+
disableWorkers: ctx.disableWorkers,
|
|
32
|
+
sessionCookie,
|
|
33
|
+
useDKLS: isDKLS,
|
|
34
|
+
disableWebSockets: ctx.disableWebSockets,
|
|
35
|
+
wasmOverride: ctx.wasmOverride,
|
|
36
|
+
workId
|
|
37
|
+
});
|
|
38
|
+
} catch (error) {
|
|
39
|
+
reject(error);
|
|
40
|
+
}
|
|
28
41
|
});
|
|
29
42
|
}
|
|
30
43
|
async function sendTransaction(ctx, userId, walletId, share, tx, chainId, sessionCookie, isDKLS) {
|
|
31
|
-
return
|
|
44
|
+
return new Promise(async (resolve, reject) => {
|
|
32
45
|
const workId = uuid.v4();
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
try {
|
|
47
|
+
const worker = await setupWorker(
|
|
48
|
+
ctx,
|
|
49
|
+
async (sendTransactionRes) => {
|
|
50
|
+
resolve(sendTransactionRes);
|
|
51
|
+
},
|
|
52
|
+
(error) => {
|
|
53
|
+
reject(error);
|
|
54
|
+
},
|
|
55
|
+
workId,
|
|
56
|
+
{
|
|
57
|
+
params: { walletId, userId, tx, chainId },
|
|
58
|
+
functionType: "SEND_TRANSACTION",
|
|
59
|
+
disableWorkers: ctx.disableWorkers,
|
|
60
|
+
disableWebSockets: ctx.disableWebSockets
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
worker.postMessage({
|
|
64
|
+
env: ctx.env,
|
|
65
|
+
apiKey: ctx.apiKey,
|
|
66
|
+
params: { share, walletId, userId, tx, chainId },
|
|
67
|
+
functionType: "SEND_TRANSACTION",
|
|
68
|
+
offloadMPCComputationURL: ctx.offloadMPCComputationURL,
|
|
69
|
+
disableWorkers: ctx.disableWorkers,
|
|
70
|
+
sessionCookie,
|
|
71
|
+
useDKLS: isDKLS,
|
|
72
|
+
disableWebSockets: ctx.disableWebSockets,
|
|
73
|
+
wasmOverride: ctx.wasmOverride,
|
|
74
|
+
workId
|
|
75
|
+
});
|
|
76
|
+
} catch (error) {
|
|
77
|
+
reject(error);
|
|
78
|
+
}
|
|
53
79
|
});
|
|
54
80
|
}
|
|
55
81
|
async function signMessage(ctx, userId, walletId, share, message, sessionCookie, isDKLS) {
|
|
56
|
-
return
|
|
82
|
+
return new Promise(async (resolve, reject) => {
|
|
57
83
|
const workId = uuid.v4();
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
84
|
+
try {
|
|
85
|
+
const worker = await setupWorker(
|
|
86
|
+
ctx,
|
|
87
|
+
async (signMessageRes) => {
|
|
88
|
+
resolve(signMessageRes);
|
|
89
|
+
},
|
|
90
|
+
(error) => {
|
|
91
|
+
reject(error);
|
|
92
|
+
},
|
|
93
|
+
workId,
|
|
94
|
+
{
|
|
95
|
+
params: { walletId, userId, message },
|
|
96
|
+
functionType: "SIGN_MESSAGE",
|
|
97
|
+
disableWorkers: ctx.disableWorkers,
|
|
98
|
+
disableWebSockets: ctx.disableWebSockets
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
worker.postMessage({
|
|
102
|
+
env: ctx.env,
|
|
103
|
+
apiKey: ctx.apiKey,
|
|
104
|
+
params: { share, walletId, userId, message },
|
|
105
|
+
functionType: "SIGN_MESSAGE",
|
|
106
|
+
offloadMPCComputationURL: ctx.offloadMPCComputationURL,
|
|
107
|
+
disableWorkers: ctx.disableWorkers,
|
|
108
|
+
sessionCookie,
|
|
109
|
+
useDKLS: isDKLS,
|
|
110
|
+
disableWebSockets: ctx.disableWebSockets,
|
|
111
|
+
wasmOverride: ctx.wasmOverride,
|
|
112
|
+
workId
|
|
113
|
+
});
|
|
114
|
+
} catch (error) {
|
|
115
|
+
reject(error);
|
|
116
|
+
}
|
|
78
117
|
});
|
|
79
118
|
}
|
|
80
119
|
async function ed25519Sign(ctx, userId, walletId, share, base64Bytes, sessionCookie) {
|
|
81
|
-
return
|
|
120
|
+
return new Promise(async (resolve, reject) => {
|
|
82
121
|
const workId = uuid.v4();
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
122
|
+
try {
|
|
123
|
+
const worker = await setupWorker(
|
|
124
|
+
ctx,
|
|
125
|
+
async (signMessageRes) => {
|
|
126
|
+
resolve(signMessageRes);
|
|
127
|
+
},
|
|
128
|
+
(error) => {
|
|
129
|
+
reject(error);
|
|
130
|
+
},
|
|
131
|
+
workId,
|
|
132
|
+
{
|
|
133
|
+
params: { walletId, userId, base64Bytes },
|
|
134
|
+
functionType: "ED25519_SIGN",
|
|
135
|
+
disableWorkers: ctx.disableWorkers,
|
|
136
|
+
disableWebSockets: ctx.disableWebSockets
|
|
137
|
+
}
|
|
138
|
+
);
|
|
139
|
+
worker.postMessage({
|
|
140
|
+
env: ctx.env,
|
|
141
|
+
apiKey: ctx.apiKey,
|
|
142
|
+
params: { share, walletId, userId, base64Bytes },
|
|
143
|
+
functionType: "ED25519_SIGN",
|
|
144
|
+
offloadMPCComputationURL: ctx.offloadMPCComputationURL,
|
|
145
|
+
disableWorkers: ctx.disableWorkers,
|
|
146
|
+
sessionCookie,
|
|
147
|
+
disableWebSockets: ctx.disableWebSockets,
|
|
148
|
+
wasmOverride: ctx.wasmOverride,
|
|
149
|
+
workId
|
|
150
|
+
});
|
|
151
|
+
} catch (error) {
|
|
152
|
+
reject(error);
|
|
153
|
+
}
|
|
102
154
|
});
|
|
103
155
|
}
|
|
104
156
|
export {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import "../chunk-FTA5RKYX.js";
|
|
2
2
|
import { getBaseMPCNetworkUrl } from "@getpara/core-sdk";
|
|
3
|
+
import * as uuid from "uuid";
|
|
3
4
|
const configCGGMPBase = (serverUrl, walletId, id) => `{"ServerUrl":"${serverUrl}", "WalletId": "${walletId}", "Id":"${id}", "Ids":["USER","CAPSULE"], "Threshold":1}`;
|
|
4
5
|
const configDKLSBase = (walletId, id, disableWebSockets) => `{"walletId": "${walletId}", "id":"${id}", "otherId":"CAPSULE", "isReceiver": false, "disableWebSockets": ${disableWebSockets}}`;
|
|
5
6
|
async function keygenRequest(ctx, userId, walletId, protocolId) {
|
|
@@ -45,7 +46,7 @@ async function ed25519Keygen(ctx, userId) {
|
|
|
45
46
|
})
|
|
46
47
|
);
|
|
47
48
|
return { signer: newSigner, walletId };
|
|
48
|
-
} catch
|
|
49
|
+
} catch {
|
|
49
50
|
throw new Error(`error creating account of type SOLANA with userId ${userId} and walletId ${walletId}`);
|
|
50
51
|
}
|
|
51
52
|
}
|
|
@@ -67,25 +68,33 @@ async function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType) {
|
|
|
67
68
|
})
|
|
68
69
|
);
|
|
69
70
|
return { signer: newSigner, walletId };
|
|
70
|
-
} catch
|
|
71
|
+
} catch {
|
|
71
72
|
throw new Error(`error creating account of type SOLANA with walletId ${walletId}`);
|
|
72
73
|
}
|
|
73
74
|
}
|
|
74
75
|
async function ed25519Sign(ctx, share, userId, walletId, base64Bytes) {
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
76
|
+
const protocolId = uuid.v4();
|
|
77
|
+
const preSignMessageRes = ctx.client.preSignMessage(userId, walletId, base64Bytes, "ED25519", void 0, protocolId);
|
|
78
|
+
const signRes = async function() {
|
|
79
|
+
try {
|
|
80
|
+
const base64Sig = await new Promise(
|
|
81
|
+
(resolve, reject) => global.ed25519Sign(share, protocolId, base64Bytes, (err, result) => {
|
|
82
|
+
if (err) {
|
|
83
|
+
reject(err);
|
|
84
|
+
}
|
|
85
|
+
resolve(result);
|
|
86
|
+
})
|
|
87
|
+
);
|
|
88
|
+
return { signature: base64Sig };
|
|
89
|
+
} catch {
|
|
90
|
+
throw new Error(`error signing for account of type SOLANA with userId ${userId} and walletId ${walletId}`);
|
|
91
|
+
}
|
|
92
|
+
}();
|
|
93
|
+
const { pendingTransactionId } = await preSignMessageRes;
|
|
94
|
+
if (pendingTransactionId) {
|
|
95
|
+
return { pendingTransactionId };
|
|
88
96
|
}
|
|
97
|
+
return await signRes;
|
|
89
98
|
}
|
|
90
99
|
async function keygen(ctx, userId, type, secretKey) {
|
|
91
100
|
const { walletId, protocolId } = await ctx.client.createWallet(userId, {
|
|
@@ -122,7 +131,7 @@ async function keygen(ctx, userId, type, secretKey) {
|
|
|
122
131
|
)
|
|
123
132
|
);
|
|
124
133
|
return { signer: newSigner, walletId };
|
|
125
|
-
} catch
|
|
134
|
+
} catch {
|
|
126
135
|
throw new Error(`error creating account of type ${type} with userId ${userId} and walletId ${walletId}`);
|
|
127
136
|
}
|
|
128
137
|
}
|
|
@@ -161,82 +170,97 @@ async function preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType,
|
|
|
161
170
|
)
|
|
162
171
|
);
|
|
163
172
|
return { signer: newSigner, walletId };
|
|
164
|
-
} catch
|
|
173
|
+
} catch {
|
|
165
174
|
throw new Error(`error creating account of type ${type} with walletId ${walletId}`);
|
|
166
175
|
}
|
|
167
176
|
}
|
|
168
177
|
async function signMessage(ctx, share, walletId, userId, message) {
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
return { pendingTransactionId };
|
|
172
|
-
}
|
|
178
|
+
const protocolId = uuid.v4();
|
|
179
|
+
const preSignMessageRes = ctx.client.preSignMessage(userId, walletId, message, null, null, protocolId);
|
|
173
180
|
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
174
181
|
return signMessageRequest(ctx, userId, walletId, protocolId, message, share);
|
|
175
182
|
}
|
|
176
183
|
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
177
184
|
const signMessageFn = ctx.useDKLS ? global.dklsSignMessage : global.signMessage;
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const {
|
|
193
|
-
data: { protocolId, pendingTransactionId }
|
|
194
|
-
} = await ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId });
|
|
185
|
+
const signMessageRes = async function() {
|
|
186
|
+
try {
|
|
187
|
+
return await new Promise(
|
|
188
|
+
(resolve, reject) => signMessageFn(share, serverUrl, message, protocolId, (err, result) => {
|
|
189
|
+
if (err) {
|
|
190
|
+
reject(err);
|
|
191
|
+
}
|
|
192
|
+
resolve({ signature: result });
|
|
193
|
+
})
|
|
194
|
+
);
|
|
195
|
+
} catch {
|
|
196
|
+
throw new Error(`error signing for account with userId ${userId} and walletId ${walletId}`);
|
|
197
|
+
}
|
|
198
|
+
}();
|
|
199
|
+
const { pendingTransactionId } = await preSignMessageRes;
|
|
195
200
|
if (pendingTransactionId) {
|
|
196
201
|
return { pendingTransactionId };
|
|
197
202
|
}
|
|
203
|
+
return await signMessageRes;
|
|
204
|
+
}
|
|
205
|
+
async function signTransaction(ctx, share, walletId, userId, tx, chainId) {
|
|
206
|
+
const protocolId = uuid.v4();
|
|
207
|
+
const signTransactionRes = ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId, protocolId });
|
|
198
208
|
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
199
209
|
return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
|
|
200
210
|
}
|
|
201
211
|
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
202
212
|
const signTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
|
|
213
|
+
const signTxRes = async function() {
|
|
214
|
+
try {
|
|
215
|
+
return await new Promise(
|
|
216
|
+
(resolve, reject) => signTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
|
|
217
|
+
if (err) {
|
|
218
|
+
reject(err);
|
|
219
|
+
}
|
|
220
|
+
resolve({ signature: result });
|
|
221
|
+
})
|
|
222
|
+
);
|
|
223
|
+
} catch {
|
|
224
|
+
throw new Error(`error signing transaction for account with userId ${userId} and walletId ${walletId}`);
|
|
225
|
+
}
|
|
226
|
+
}();
|
|
217
227
|
const {
|
|
218
|
-
data: {
|
|
219
|
-
} = await
|
|
228
|
+
data: { pendingTransactionId }
|
|
229
|
+
} = await signTransactionRes;
|
|
220
230
|
if (pendingTransactionId) {
|
|
221
231
|
return { pendingTransactionId };
|
|
222
232
|
}
|
|
233
|
+
return await signTxRes;
|
|
234
|
+
}
|
|
235
|
+
async function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
|
|
236
|
+
const protocolId = uuid.v4();
|
|
237
|
+
const sendTransactionRes = ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId, protocolId });
|
|
223
238
|
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
224
239
|
return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
|
|
225
240
|
}
|
|
226
241
|
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
227
242
|
const sendTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
243
|
+
const sendTxRes = async function() {
|
|
244
|
+
try {
|
|
245
|
+
return await new Promise(
|
|
246
|
+
(resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
|
|
247
|
+
if (err) {
|
|
248
|
+
reject(err);
|
|
249
|
+
}
|
|
250
|
+
resolve({ signature: result });
|
|
251
|
+
})
|
|
252
|
+
);
|
|
253
|
+
} catch {
|
|
254
|
+
throw new Error(`error signing transaction to send for account with userId ${userId} and walletId ${walletId}`);
|
|
255
|
+
}
|
|
256
|
+
}();
|
|
257
|
+
const {
|
|
258
|
+
data: { pendingTransactionId }
|
|
259
|
+
} = await sendTransactionRes;
|
|
260
|
+
if (pendingTransactionId) {
|
|
261
|
+
return { pendingTransactionId };
|
|
239
262
|
}
|
|
263
|
+
return await sendTxRes;
|
|
240
264
|
}
|
|
241
265
|
async function refresh(ctx, share, walletId, userId) {
|
|
242
266
|
const {
|
|
@@ -253,7 +277,7 @@ async function refresh(ctx, share, walletId, userId) {
|
|
|
253
277
|
resolve(result);
|
|
254
278
|
})
|
|
255
279
|
);
|
|
256
|
-
} catch
|
|
280
|
+
} catch {
|
|
257
281
|
throw new Error(`error refreshing keys for account with userId ${userId} and walletId ${walletId}`);
|
|
258
282
|
}
|
|
259
283
|
}
|
|
@@ -272,7 +296,7 @@ async function getPrivateKey(ctx, share, walletId, userId) {
|
|
|
272
296
|
resolve(result);
|
|
273
297
|
})
|
|
274
298
|
);
|
|
275
|
-
} catch
|
|
299
|
+
} catch {
|
|
276
300
|
throw new Error(`error getting private key for account with userId ${userId} and walletId ${walletId}`);
|
|
277
301
|
}
|
|
278
302
|
}
|
|
@@ -19,9 +19,16 @@ var __async = (__this, __arguments, generator) => {
|
|
|
19
19
|
});
|
|
20
20
|
};
|
|
21
21
|
import axios from "axios";
|
|
22
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
getBaseMPCNetworkUrl,
|
|
24
|
+
getPortalBaseURL,
|
|
25
|
+
initClient,
|
|
26
|
+
mpcComputationClient,
|
|
27
|
+
paraVersion
|
|
28
|
+
} from "@getpara/core-sdk";
|
|
23
29
|
import * as walletUtils from "./walletUtils.js";
|
|
24
30
|
let rawWasm;
|
|
31
|
+
let wasmLoaded = false;
|
|
25
32
|
function requestWasmWithRetries(ctx, retries = 3) {
|
|
26
33
|
return __async(this, null, function* () {
|
|
27
34
|
for (let i = 0; i < retries; i++) {
|
|
@@ -52,21 +59,24 @@ function executeMessage(ctx, message) {
|
|
|
52
59
|
return __async(this, null, function* () {
|
|
53
60
|
const { functionType, params } = message;
|
|
54
61
|
switch (functionType) {
|
|
62
|
+
case "INIT": {
|
|
63
|
+
return {};
|
|
64
|
+
}
|
|
55
65
|
case "KEYGEN": {
|
|
56
66
|
const { userId, secretKey, type = "EVM" } = params;
|
|
57
67
|
return walletUtils.keygen(ctx, userId, type, secretKey);
|
|
58
68
|
}
|
|
59
69
|
case "SIGN_TRANSACTION": {
|
|
60
70
|
const { share, walletId, userId, tx, chainId } = params;
|
|
61
|
-
return walletUtils.signTransaction(ctx, share, walletId, userId, tx, chainId);
|
|
71
|
+
return withRetry(() => walletUtils.signTransaction(ctx, share, walletId, userId, tx, chainId));
|
|
62
72
|
}
|
|
63
73
|
case "SEND_TRANSACTION": {
|
|
64
74
|
const { share, walletId, userId, tx, chainId } = params;
|
|
65
|
-
return walletUtils.sendTransaction(ctx, share, walletId, userId, tx, chainId);
|
|
75
|
+
return withRetry(() => walletUtils.sendTransaction(ctx, share, walletId, userId, tx, chainId));
|
|
66
76
|
}
|
|
67
77
|
case "SIGN_MESSAGE": {
|
|
68
78
|
const { share, walletId, userId, message: message2 } = params;
|
|
69
|
-
return walletUtils.signMessage(ctx, share, walletId, userId, message2);
|
|
79
|
+
return withRetry(() => walletUtils.signMessage(ctx, share, walletId, userId, message2));
|
|
70
80
|
}
|
|
71
81
|
case "REFRESH": {
|
|
72
82
|
const { share, walletId, userId } = params;
|
|
@@ -111,6 +121,29 @@ function executeMessage(ctx, message) {
|
|
|
111
121
|
}
|
|
112
122
|
});
|
|
113
123
|
}
|
|
124
|
+
function withRetry(operation, maxRetries = 2, timeoutMs = 1e4) {
|
|
125
|
+
return __async(this, null, function* () {
|
|
126
|
+
let retries = 0;
|
|
127
|
+
while (true) {
|
|
128
|
+
try {
|
|
129
|
+
const operationPromise = operation();
|
|
130
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
131
|
+
const timeoutId = setTimeout(() => {
|
|
132
|
+
reject(new Error(`Operation timed out after ${timeoutMs}ms`));
|
|
133
|
+
}, timeoutMs);
|
|
134
|
+
operationPromise.finally(() => clearTimeout(timeoutId));
|
|
135
|
+
});
|
|
136
|
+
return yield Promise.race([operationPromise, timeoutPromise]);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
retries++;
|
|
139
|
+
if (retries > maxRetries) {
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
console.warn(`Operation failed (attempt ${retries}/${maxRetries}), retrying...`, error);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
114
147
|
function handleMessage(e) {
|
|
115
148
|
return __async(this, null, function* () {
|
|
116
149
|
const {
|
|
@@ -134,8 +167,20 @@ function handleMessage(e) {
|
|
|
134
167
|
disableWebSockets: !!disableWebSockets,
|
|
135
168
|
cosmosPrefix
|
|
136
169
|
};
|
|
137
|
-
if (!ctx.offloadMPCComputationURL || ctx.useDKLS) {
|
|
170
|
+
if (!wasmLoaded && (!ctx.offloadMPCComputationURL || ctx.useDKLS)) {
|
|
138
171
|
yield loadWasm(ctx);
|
|
172
|
+
if (global.initWasm) {
|
|
173
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
174
|
+
yield new Promise(
|
|
175
|
+
(resolve, reject) => global.initWasm((err, result2) => {
|
|
176
|
+
if (err) {
|
|
177
|
+
reject(err);
|
|
178
|
+
}
|
|
179
|
+
resolve(result2);
|
|
180
|
+
}, serverUrl)
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
wasmLoaded = true;
|
|
139
184
|
}
|
|
140
185
|
const result = yield executeMessage(ctx, e.data);
|
|
141
186
|
result.workId = workId;
|
|
@@ -144,5 +189,6 @@ function handleMessage(e) {
|
|
|
144
189
|
}
|
|
145
190
|
export {
|
|
146
191
|
handleMessage,
|
|
147
|
-
requestWasmWithRetries
|
|
192
|
+
requestWasmWithRetries,
|
|
193
|
+
withRetry
|
|
148
194
|
};
|
|
@@ -12,33 +12,74 @@ function removeWorkId(workId, skipClearTimeout) {
|
|
|
12
12
|
}
|
|
13
13
|
clearTimeout(timeoutId);
|
|
14
14
|
}
|
|
15
|
-
|
|
15
|
+
function resetWorker() {
|
|
16
|
+
if (worker) {
|
|
17
|
+
worker.terminate();
|
|
18
|
+
worker = void 0;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async function setupWorker(ctx, resFunction, errorFunction, workId, errorContext) {
|
|
16
22
|
const timeoutId = setTimeout(() => {
|
|
17
|
-
|
|
23
|
+
if (resFunctionMap[workId]) {
|
|
24
|
+
const errorMsg = `worker operation timed out after ${CLEAR_WORKER_TIMEOUT_MS}ms for workId ${workId} and opts ${JSON.stringify(resFunctionMap[workId].errorContext)}`;
|
|
25
|
+
resFunctionMap[workId].errorFn(new Error(errorMsg));
|
|
26
|
+
removeWorkId(workId, true);
|
|
27
|
+
}
|
|
18
28
|
}, CLEAR_WORKER_TIMEOUT_MS);
|
|
19
29
|
resFunctionMap[workId] = {
|
|
20
30
|
fn: resFunction,
|
|
21
|
-
|
|
31
|
+
errorFn: errorFunction,
|
|
32
|
+
timeoutId,
|
|
33
|
+
errorContext
|
|
22
34
|
};
|
|
23
35
|
if (!worker || !worker.threadId) {
|
|
24
|
-
const
|
|
36
|
+
const isDeno = typeof globalThis?.Deno !== "undefined";
|
|
37
|
+
const workerFileName = isDeno ? "mpcWorkerServer-esm.js" : "mpcWorkerServer-bundle.js";
|
|
38
|
+
const workerRes = await fetch(`${getPortalBaseURL(ctx)}/static/js/${workerFileName}`);
|
|
25
39
|
worker = new Worker(await workerRes.text(), { eval: true });
|
|
26
40
|
const onmessage = async (message) => {
|
|
27
41
|
const { workId: messageWorkId } = message;
|
|
42
|
+
if (!resFunctionMap[messageWorkId]) {
|
|
43
|
+
console.warn(`received message for unknown workId: ${messageWorkId}`);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
28
46
|
delete message.workId;
|
|
29
|
-
|
|
30
|
-
|
|
47
|
+
try {
|
|
48
|
+
await resFunctionMap[messageWorkId].fn(message);
|
|
49
|
+
removeWorkId(messageWorkId);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error(`error in worker message handler for workId ${messageWorkId}:`, error);
|
|
52
|
+
if (resFunctionMap[messageWorkId]) {
|
|
53
|
+
resFunctionMap[messageWorkId].errorFn(error);
|
|
54
|
+
removeWorkId(messageWorkId);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
31
57
|
};
|
|
32
58
|
worker.on("message", onmessage);
|
|
33
59
|
worker.on("error", (err) => {
|
|
34
|
-
|
|
60
|
+
console.error("worker error:", err);
|
|
61
|
+
Object.keys(resFunctionMap).forEach((id) => {
|
|
62
|
+
if (resFunctionMap[id]) {
|
|
63
|
+
const errorMsg = `worker error with workId ${id} and opts ${JSON.stringify(resFunctionMap[id].errorContext)}: ${err.message}`;
|
|
64
|
+
resFunctionMap[id].errorFn(new Error(errorMsg));
|
|
65
|
+
removeWorkId(id);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
35
68
|
});
|
|
36
69
|
worker.on("exit", (code) => {
|
|
37
70
|
console.error(`worker stopped with exit code ${code}`);
|
|
71
|
+
Object.keys(resFunctionMap).forEach((id) => {
|
|
72
|
+
if (resFunctionMap[id]) {
|
|
73
|
+
resFunctionMap[id].errorFn(new Error(`worker exited unexpectedly with code ${code}`));
|
|
74
|
+
removeWorkId(id);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
worker = void 0;
|
|
38
78
|
});
|
|
39
79
|
}
|
|
40
80
|
return worker;
|
|
41
81
|
}
|
|
42
82
|
export {
|
|
83
|
+
resetWorker,
|
|
43
84
|
setupWorker
|
|
44
85
|
};
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import ParaCore, { PlatformUtils, ConstructorOpts, Environment, CoreMethodParams, CoreMethodResponse } from '@getpara/core-sdk';
|
|
2
2
|
export declare class Para extends ParaCore {
|
|
3
|
-
constructor(env: Environment, apiKey
|
|
3
|
+
constructor(env: Environment | undefined, apiKey: string, opts?: ConstructorOpts);
|
|
4
|
+
constructor(apiKey: string, opts?: ConstructorOpts);
|
|
5
|
+
ready(): Promise<void>;
|
|
4
6
|
protected getPlatformUtils(): PlatformUtils;
|
|
7
|
+
isPasskeySupported(): Promise<boolean>;
|
|
5
8
|
/**
|
|
6
9
|
* Claims a pregenerated wallet.
|
|
7
10
|
*
|