@getpara/server-sdk 2.0.0-dev.1 → 2.0.0-dev.3
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 +101 -0
- package/dist/cjs/ServerLocalStorage.js +48 -0
- package/dist/cjs/ServerSessionStorage.js +48 -0
- package/dist/cjs/ServerUtils.js +77 -0
- package/dist/cjs/index.js +3 -523
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/wallet/keygen.js +315 -0
- package/dist/cjs/wallet/privateKey.js +99 -0
- package/dist/cjs/wallet/signing.js +224 -0
- package/dist/cjs/wasm/wasm_exec.js +589 -0
- package/dist/cjs/workers/walletUtils.js +409 -0
- package/dist/cjs/workers/worker.js +62 -907
- package/dist/cjs/workers/workerWrapper.js +130 -0
- package/dist/esm/ParaServer.js +45 -0
- package/dist/esm/ServerLocalStorage.js +26 -0
- package/dist/esm/ServerSessionStorage.js +26 -0
- package/dist/esm/ServerUtils.js +55 -0
- package/dist/esm/chunk-FTA5RKYX.js +8 -0
- package/dist/esm/index.js +4 -466
- package/dist/esm/package.json +6 -0
- package/dist/esm/wallet/keygen.js +230 -0
- package/dist/esm/wallet/privateKey.js +45 -0
- package/dist/esm/wallet/signing.js +161 -0
- package/dist/esm/{workers/wasm_exec-CFNSOXDO.js → wasm/wasm_exec.js} +68 -74
- package/dist/esm/workers/walletUtils.js +314 -0
- package/dist/esm/workers/worker.js +78 -337
- package/dist/esm/workers/workerWrapper.js +85 -0
- package/dist/types/ParaServer.d.ts +4 -5
- package/dist/types/ServerUtils.d.ts +5 -6
- package/dist/types/wallet/keygen.d.ts +5 -3
- package/dist/types/workers/walletUtils.d.ts +4 -6
- package/dist/types/workers/worker.d.ts +10 -1
- package/dist/types/workers/workerWrapper.d.ts +2 -2
- package/package.json +20 -20
- package/dist/cjs/index.js.br +0 -0
- package/dist/cjs/index.js.gz +0 -0
- package/dist/cjs/workers/worker.js.br +0 -0
- package/dist/cjs/workers/worker.js.gz +0 -0
- package/dist/esm/index.js.br +0 -0
- package/dist/esm/index.js.gz +0 -0
- package/dist/esm/workers/chunk-ILICZWQV.js +0 -36
- package/dist/esm/workers/chunk-ILICZWQV.js.br +0 -0
- package/dist/esm/workers/chunk-ILICZWQV.js.gz +0 -0
- package/dist/esm/workers/wasm_exec-CFNSOXDO.js.br +0 -0
- package/dist/esm/workers/wasm_exec-CFNSOXDO.js.gz +0 -0
- package/dist/esm/workers/worker.js.br +0 -0
- package/dist/esm/workers/worker.js.gz +0 -0
|
@@ -1,325 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
var configDKLSBase = (walletId, id, disableWebSockets) => `{"walletId": "${walletId}", "id":"${id}", "otherId":"CAPSULE", "isReceiver": false, "disableWebSockets": ${disableWebSockets}}`;
|
|
20
|
-
function keygenRequest(ctx, userId, walletId, protocolId) {
|
|
21
|
-
return __async(this, null, function* () {
|
|
22
|
-
const { data } = yield ctx.mpcComputationClient.post("/wallets", {
|
|
23
|
-
userId,
|
|
24
|
-
walletId,
|
|
25
|
-
protocolId
|
|
26
|
-
});
|
|
27
|
-
return data;
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
function signMessageRequest(ctx, userId, walletId, protocolId, message, signer) {
|
|
31
|
-
return __async(this, null, function* () {
|
|
32
|
-
const { data } = yield ctx.mpcComputationClient.post(`/wallets/${walletId}/messages/sign`, {
|
|
33
|
-
userId,
|
|
34
|
-
protocolId,
|
|
35
|
-
message,
|
|
36
|
-
signer
|
|
37
|
-
});
|
|
38
|
-
return data;
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
function sendTransactionRequest(ctx, userId, walletId, protocolId, transaction, signer, chainId) {
|
|
42
|
-
return __async(this, null, function* () {
|
|
43
|
-
const { data } = yield ctx.mpcComputationClient.post(`/wallets/${walletId}/transactions/send`, {
|
|
44
|
-
userId,
|
|
45
|
-
protocolId,
|
|
46
|
-
transaction,
|
|
47
|
-
signer,
|
|
48
|
-
chainId
|
|
49
|
-
});
|
|
50
|
-
return data;
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
function ed25519Keygen(ctx, userId) {
|
|
54
|
-
return __async(this, null, function* () {
|
|
55
|
-
const { walletId, protocolId } = yield ctx.client.createWallet(userId, {
|
|
56
|
-
scheme: WalletScheme.ED25519,
|
|
57
|
-
type: WalletType.SOLANA
|
|
58
|
-
});
|
|
59
|
-
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
60
|
-
try {
|
|
61
|
-
const newSigner = yield new Promise(
|
|
62
|
-
(resolve, reject) => global.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
|
|
63
|
-
if (err) {
|
|
64
|
-
reject(err);
|
|
65
|
-
}
|
|
66
|
-
resolve(result);
|
|
67
|
-
})
|
|
68
|
-
);
|
|
69
|
-
return { signer: newSigner, walletId };
|
|
70
|
-
} catch (e) {
|
|
71
|
-
throw new Error(`error creating account of type SOLANA with userId ${userId} and walletId ${walletId}`);
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType) {
|
|
76
|
-
return __async(this, null, function* () {
|
|
77
|
-
const { walletId, protocolId } = yield ctx.client.createPregenWallet({
|
|
78
|
-
pregenIdentifier,
|
|
79
|
-
pregenIdentifierType,
|
|
80
|
-
scheme: WalletScheme.ED25519,
|
|
81
|
-
type: WalletType.SOLANA
|
|
82
|
-
});
|
|
83
|
-
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
84
|
-
try {
|
|
85
|
-
const newSigner = yield new Promise(
|
|
86
|
-
(resolve, reject) => global.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
|
|
87
|
-
if (err) {
|
|
88
|
-
reject(err);
|
|
89
|
-
}
|
|
90
|
-
resolve(result);
|
|
91
|
-
})
|
|
92
|
-
);
|
|
93
|
-
return { signer: newSigner, walletId };
|
|
94
|
-
} catch (e) {
|
|
95
|
-
throw new Error(`error creating account of type SOLANA with walletId ${walletId}`);
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
function ed25519Sign(ctx, share, userId, walletId, base64Bytes) {
|
|
100
|
-
return __async(this, null, function* () {
|
|
101
|
-
const { protocolId } = yield ctx.client.preSignMessage(userId, walletId, base64Bytes, WalletScheme.ED25519);
|
|
102
|
-
try {
|
|
103
|
-
const base64Sig = yield new Promise(
|
|
104
|
-
(resolve, reject) => global.ed25519Sign(share, protocolId, base64Bytes, (err, result) => {
|
|
105
|
-
if (err) {
|
|
106
|
-
reject(err);
|
|
107
|
-
}
|
|
108
|
-
resolve(result);
|
|
109
|
-
})
|
|
110
|
-
);
|
|
111
|
-
return { signature: base64Sig };
|
|
112
|
-
} catch (e) {
|
|
113
|
-
throw new Error(`error signing for account of type SOLANA with userId ${userId} and walletId ${walletId}`);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
function keygen(ctx, userId, type, secretKey) {
|
|
118
|
-
return __async(this, null, function* () {
|
|
119
|
-
const { walletId, protocolId } = yield ctx.client.createWallet(userId, {
|
|
120
|
-
useTwoSigners: true,
|
|
121
|
-
scheme: ctx.useDKLS ? WalletScheme.DKLS : WalletScheme.CGGMP,
|
|
122
|
-
type,
|
|
123
|
-
cosmosPrefix: type === WalletType.COSMOS ? ctx.cosmosPrefix : void 0
|
|
124
|
-
});
|
|
125
|
-
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
126
|
-
return {
|
|
127
|
-
signer: (yield keygenRequest(ctx, userId, walletId, protocolId)).signer,
|
|
128
|
-
walletId
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
132
|
-
const signerConfigUser = ctx.useDKLS ? configDKLSBase(walletId, "USER", ctx.disableWebSockets) : configCGGMPBase(serverUrl, walletId, "USER");
|
|
133
|
-
const createAccountFn = ctx.useDKLS ? global.dklsCreateAccount : global.createAccountV2;
|
|
134
|
-
try {
|
|
135
|
-
const newSigner = yield new Promise(
|
|
136
|
-
(resolve, reject) => createAccountFn(
|
|
137
|
-
signerConfigUser,
|
|
138
|
-
serverUrl,
|
|
139
|
-
protocolId,
|
|
140
|
-
secretKey,
|
|
141
|
-
() => {
|
|
142
|
-
},
|
|
143
|
-
// no-op for deprecated callback to update progress percentage
|
|
144
|
-
(err, result) => {
|
|
145
|
-
if (err) {
|
|
146
|
-
reject(err);
|
|
147
|
-
}
|
|
148
|
-
resolve(result);
|
|
149
|
-
}
|
|
150
|
-
)
|
|
151
|
-
);
|
|
152
|
-
return { signer: newSigner, walletId };
|
|
153
|
-
} catch (e) {
|
|
154
|
-
throw new Error(`error creating account of type ${type} with userId ${userId} and walletId ${walletId}`);
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
function preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey) {
|
|
159
|
-
return __async(this, null, function* () {
|
|
160
|
-
const { walletId, protocolId } = yield ctx.client.createPregenWallet({
|
|
161
|
-
pregenIdentifier,
|
|
162
|
-
pregenIdentifierType,
|
|
163
|
-
type,
|
|
164
|
-
cosmosPrefix: type === WalletType.COSMOS ? ctx.cosmosPrefix : void 0
|
|
165
|
-
});
|
|
166
|
-
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
167
|
-
return {
|
|
168
|
-
signer: (yield keygenRequest(ctx, partnerId, walletId, protocolId)).signer,
|
|
169
|
-
walletId
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
173
|
-
const signerConfigUser = ctx.useDKLS ? configDKLSBase(walletId, "USER", ctx.disableWebSockets) : configCGGMPBase(serverUrl, walletId, "USER");
|
|
174
|
-
const createAccountFn = ctx.useDKLS ? global.dklsCreateAccount : global.createAccountV2;
|
|
175
|
-
try {
|
|
176
|
-
const newSigner = yield new Promise(
|
|
177
|
-
(resolve, reject) => createAccountFn(
|
|
178
|
-
signerConfigUser,
|
|
179
|
-
serverUrl,
|
|
180
|
-
protocolId,
|
|
181
|
-
secretKey,
|
|
182
|
-
() => {
|
|
183
|
-
},
|
|
184
|
-
// no-op for deprecated callback to update progress percentage
|
|
185
|
-
(err, result) => {
|
|
186
|
-
if (err) {
|
|
187
|
-
reject(err);
|
|
188
|
-
}
|
|
189
|
-
resolve(result);
|
|
190
|
-
}
|
|
191
|
-
)
|
|
192
|
-
);
|
|
193
|
-
return { signer: newSigner, walletId };
|
|
194
|
-
} catch (e) {
|
|
195
|
-
throw new Error(`error creating account of type ${type} with walletId ${walletId}`);
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
function signMessage(ctx, share, walletId, userId, message) {
|
|
200
|
-
return __async(this, null, function* () {
|
|
201
|
-
const { protocolId, pendingTransactionId } = yield ctx.client.preSignMessage(userId, walletId, message);
|
|
202
|
-
if (pendingTransactionId) {
|
|
203
|
-
return { pendingTransactionId };
|
|
204
|
-
}
|
|
205
|
-
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
206
|
-
return signMessageRequest(ctx, userId, walletId, protocolId, message, share);
|
|
207
|
-
}
|
|
208
|
-
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
209
|
-
const signMessageFn = ctx.useDKLS ? global.dklsSignMessage : global.signMessage;
|
|
210
|
-
try {
|
|
211
|
-
return new Promise(
|
|
212
|
-
(resolve, reject) => signMessageFn(share, serverUrl, message, protocolId, (err, result) => {
|
|
213
|
-
if (err) {
|
|
214
|
-
reject(err);
|
|
215
|
-
}
|
|
216
|
-
resolve({ signature: result });
|
|
217
|
-
})
|
|
218
|
-
);
|
|
219
|
-
} catch (e) {
|
|
220
|
-
throw new Error(`error signing for account with userId ${userId} and walletId ${walletId}`);
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
function signTransaction(ctx, share, walletId, userId, tx, chainId) {
|
|
225
|
-
return __async(this, null, function* () {
|
|
226
|
-
const {
|
|
227
|
-
data: { protocolId, pendingTransactionId }
|
|
228
|
-
} = yield ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId });
|
|
229
|
-
if (pendingTransactionId) {
|
|
230
|
-
return { pendingTransactionId };
|
|
231
|
-
}
|
|
232
|
-
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
233
|
-
return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
|
|
234
|
-
}
|
|
235
|
-
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
236
|
-
const signTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
|
|
237
|
-
try {
|
|
238
|
-
return new Promise(
|
|
239
|
-
(resolve, reject) => signTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
|
|
240
|
-
if (err) {
|
|
241
|
-
reject(err);
|
|
242
|
-
}
|
|
243
|
-
resolve({ signature: result });
|
|
244
|
-
})
|
|
245
|
-
);
|
|
246
|
-
} catch (e) {
|
|
247
|
-
throw new Error(`error signing transaction for account with userId ${userId} and walletId ${walletId}`);
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
|
|
252
|
-
return __async(this, null, function* () {
|
|
253
|
-
const {
|
|
254
|
-
data: { protocolId, pendingTransactionId }
|
|
255
|
-
} = yield ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId });
|
|
256
|
-
if (pendingTransactionId) {
|
|
257
|
-
return { pendingTransactionId };
|
|
258
|
-
}
|
|
259
|
-
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
260
|
-
return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
|
|
261
|
-
}
|
|
262
|
-
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
263
|
-
const sendTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
|
|
264
|
-
try {
|
|
265
|
-
return new Promise(
|
|
266
|
-
(resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
|
|
267
|
-
if (err) {
|
|
268
|
-
reject(err);
|
|
269
|
-
}
|
|
270
|
-
resolve({ signature: result });
|
|
271
|
-
})
|
|
272
|
-
);
|
|
273
|
-
} catch (e) {
|
|
274
|
-
throw new Error(`error signing transaction to send for account with userId ${userId} and walletId ${walletId}`);
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
function refresh(ctx, share, walletId, userId) {
|
|
279
|
-
return __async(this, null, function* () {
|
|
280
|
-
const {
|
|
281
|
-
data: { protocolId }
|
|
282
|
-
} = yield ctx.client.refreshKeys(userId, walletId);
|
|
283
|
-
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
284
|
-
const refreshFn = ctx.useDKLS ? global.dklsRefresh : global.refresh;
|
|
285
|
-
try {
|
|
286
|
-
return new Promise(
|
|
287
|
-
(resolve, reject) => refreshFn(share, serverUrl, protocolId, (err, result) => {
|
|
288
|
-
if (err) {
|
|
289
|
-
reject(err);
|
|
290
|
-
}
|
|
291
|
-
resolve(result);
|
|
292
|
-
})
|
|
293
|
-
);
|
|
294
|
-
} catch (e) {
|
|
295
|
-
throw new Error(`error refreshing keys for account with userId ${userId} and walletId ${walletId}`);
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
function getPrivateKey(ctx, share, walletId, userId) {
|
|
300
|
-
return __async(this, null, function* () {
|
|
301
|
-
const paraShare = yield ctx.client.getParaShare(userId, walletId);
|
|
302
|
-
if (!paraShare) {
|
|
303
|
-
console.error("unable to retrieve Para share");
|
|
304
|
-
return "";
|
|
305
|
-
}
|
|
306
|
-
try {
|
|
307
|
-
return new Promise(
|
|
308
|
-
(resolve, reject) => global.getPrivateKey(share, paraShare, (err, result) => {
|
|
309
|
-
if (err) {
|
|
310
|
-
reject(err);
|
|
311
|
-
}
|
|
312
|
-
resolve(result);
|
|
313
|
-
})
|
|
314
|
-
);
|
|
315
|
-
} catch (e) {
|
|
316
|
-
throw new Error(`error getting private key for account with userId ${userId} and walletId ${walletId}`);
|
|
317
|
-
}
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
318
19
|
});
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
20
|
+
};
|
|
21
|
+
import axios from "axios";
|
|
22
|
+
import { getPortalBaseURL, initClient, mpcComputationClient, paraVersion } from "@getpara/core-sdk";
|
|
23
|
+
import * as walletUtils from "./walletUtils.js";
|
|
24
|
+
let rawWasm;
|
|
25
|
+
let wasmLoaded = false;
|
|
323
26
|
function requestWasmWithRetries(ctx, retries = 3) {
|
|
324
27
|
return __async(this, null, function* () {
|
|
325
28
|
for (let i = 0; i < retries; i++) {
|
|
@@ -335,8 +38,8 @@ function requestWasmWithRetries(ctx, retries = 3) {
|
|
|
335
38
|
}
|
|
336
39
|
function loadWasm(ctx) {
|
|
337
40
|
return __async(this, null, function* () {
|
|
338
|
-
yield import("
|
|
339
|
-
global.WebSocket =
|
|
41
|
+
yield import("../wasm/wasm_exec.js");
|
|
42
|
+
global.WebSocket = require("ws");
|
|
340
43
|
const goWasm = new global.Go();
|
|
341
44
|
if (!rawWasm) {
|
|
342
45
|
rawWasm = (yield requestWasmWithRetries(ctx)).data;
|
|
@@ -351,46 +54,48 @@ function executeMessage(ctx, message) {
|
|
|
351
54
|
const { functionType, params } = message;
|
|
352
55
|
switch (functionType) {
|
|
353
56
|
case "KEYGEN": {
|
|
354
|
-
const { userId, secretKey, type =
|
|
355
|
-
return keygen(ctx, userId, type, secretKey);
|
|
57
|
+
const { userId, secretKey, type = "EVM" } = params;
|
|
58
|
+
return walletUtils.keygen(ctx, userId, type, secretKey);
|
|
356
59
|
}
|
|
357
60
|
case "SIGN_TRANSACTION": {
|
|
358
61
|
const { share, walletId, userId, tx, chainId } = params;
|
|
359
|
-
return signTransaction(ctx, share, walletId, userId, tx, chainId);
|
|
62
|
+
return withRetry(() => walletUtils.signTransaction(ctx, share, walletId, userId, tx, chainId));
|
|
360
63
|
}
|
|
361
64
|
case "SEND_TRANSACTION": {
|
|
362
65
|
const { share, walletId, userId, tx, chainId } = params;
|
|
363
|
-
return sendTransaction(ctx, share, walletId, userId, tx, chainId);
|
|
66
|
+
return withRetry(() => walletUtils.sendTransaction(ctx, share, walletId, userId, tx, chainId));
|
|
364
67
|
}
|
|
365
68
|
case "SIGN_MESSAGE": {
|
|
366
69
|
const { share, walletId, userId, message: message2 } = params;
|
|
367
|
-
return signMessage(ctx, share, walletId, userId, message2);
|
|
70
|
+
return withRetry(() => walletUtils.signMessage(ctx, share, walletId, userId, message2));
|
|
368
71
|
}
|
|
369
72
|
case "REFRESH": {
|
|
370
73
|
const { share, walletId, userId } = params;
|
|
371
|
-
|
|
74
|
+
const signer = yield walletUtils.refresh(ctx, share, walletId, userId);
|
|
75
|
+
return { signer };
|
|
372
76
|
}
|
|
373
77
|
case "PREKEYGEN": {
|
|
374
|
-
const { email, partnerId, secretKey, type =
|
|
78
|
+
const { email, partnerId, secretKey, type = "EVM" } = params;
|
|
375
79
|
let { pregenIdentifier, pregenIdentifierType } = params;
|
|
376
80
|
if (email !== "null" && email !== "undefined" && email !== "" && email != null) {
|
|
377
81
|
pregenIdentifier = email;
|
|
378
82
|
pregenIdentifierType = "EMAIL";
|
|
379
83
|
}
|
|
380
|
-
const keygenRes = yield preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey);
|
|
84
|
+
const keygenRes = yield walletUtils.preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey);
|
|
381
85
|
return keygenRes;
|
|
382
86
|
}
|
|
383
87
|
case "GET_PRIVATE_KEY": {
|
|
384
88
|
const { share, walletId, userId } = params;
|
|
385
|
-
|
|
89
|
+
const privateKey = yield walletUtils.getPrivateKey(ctx, share, walletId, userId);
|
|
90
|
+
return { privateKey };
|
|
386
91
|
}
|
|
387
92
|
case "ED25519_KEYGEN": {
|
|
388
93
|
const { userId } = params;
|
|
389
|
-
return ed25519Keygen(ctx, userId);
|
|
94
|
+
return walletUtils.ed25519Keygen(ctx, userId);
|
|
390
95
|
}
|
|
391
96
|
case "ED25519_SIGN": {
|
|
392
97
|
const { share, walletId, userId, base64Bytes } = params;
|
|
393
|
-
return ed25519Sign(ctx, share, userId, walletId, base64Bytes);
|
|
98
|
+
return walletUtils.ed25519Sign(ctx, share, userId, walletId, base64Bytes);
|
|
394
99
|
}
|
|
395
100
|
case "ED25519_PREKEYGEN": {
|
|
396
101
|
const { email } = params;
|
|
@@ -399,7 +104,7 @@ function executeMessage(ctx, message) {
|
|
|
399
104
|
pregenIdentifier = email;
|
|
400
105
|
pregenIdentifierType = "EMAIL";
|
|
401
106
|
}
|
|
402
|
-
return ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType);
|
|
107
|
+
return walletUtils.ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType);
|
|
403
108
|
}
|
|
404
109
|
default: {
|
|
405
110
|
throw new Error(`functionType: ${functionType} not supported`);
|
|
@@ -407,6 +112,29 @@ function executeMessage(ctx, message) {
|
|
|
407
112
|
}
|
|
408
113
|
});
|
|
409
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
|
+
}
|
|
410
138
|
function handleMessage(e) {
|
|
411
139
|
return __async(this, null, function* () {
|
|
412
140
|
const {
|
|
@@ -430,8 +158,19 @@ function handleMessage(e) {
|
|
|
430
158
|
disableWebSockets: !!disableWebSockets,
|
|
431
159
|
cosmosPrefix
|
|
432
160
|
};
|
|
433
|
-
if (!ctx.offloadMPCComputationURL || ctx.useDKLS) {
|
|
161
|
+
if (!wasmLoaded && (!ctx.offloadMPCComputationURL || ctx.useDKLS)) {
|
|
434
162
|
yield loadWasm(ctx);
|
|
163
|
+
if (global.initWasm) {
|
|
164
|
+
yield new Promise(
|
|
165
|
+
(resolve, reject) => global.initWasm((err, result2) => {
|
|
166
|
+
if (err) {
|
|
167
|
+
reject(err);
|
|
168
|
+
}
|
|
169
|
+
resolve(result2);
|
|
170
|
+
})
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
wasmLoaded = true;
|
|
435
174
|
}
|
|
436
175
|
const result = yield executeMessage(ctx, e.data);
|
|
437
176
|
result.workId = workId;
|
|
@@ -439,5 +178,7 @@ function handleMessage(e) {
|
|
|
439
178
|
});
|
|
440
179
|
}
|
|
441
180
|
export {
|
|
442
|
-
handleMessage
|
|
181
|
+
handleMessage,
|
|
182
|
+
requestWasmWithRetries,
|
|
183
|
+
withRetry
|
|
443
184
|
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import "../chunk-FTA5RKYX.js";
|
|
2
|
+
import { Worker } from "worker_threads";
|
|
3
|
+
import { getPortalBaseURL } from "@getpara/core-sdk";
|
|
4
|
+
const CLEAR_WORKER_TIMEOUT_MS = 1e3 * 90;
|
|
5
|
+
let worker;
|
|
6
|
+
const resFunctionMap = {};
|
|
7
|
+
function removeWorkId(workId, skipClearTimeout) {
|
|
8
|
+
const { timeoutId } = resFunctionMap[workId];
|
|
9
|
+
delete resFunctionMap[workId];
|
|
10
|
+
if (skipClearTimeout) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
clearTimeout(timeoutId);
|
|
14
|
+
}
|
|
15
|
+
function resetWorker() {
|
|
16
|
+
if (worker) {
|
|
17
|
+
worker.terminate();
|
|
18
|
+
worker = void 0;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async function setupWorker(ctx, resFunction, errorFunction, workId, errorContext) {
|
|
22
|
+
const timeoutId = setTimeout(() => {
|
|
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
|
+
}
|
|
28
|
+
}, CLEAR_WORKER_TIMEOUT_MS);
|
|
29
|
+
resFunctionMap[workId] = {
|
|
30
|
+
fn: resFunction,
|
|
31
|
+
errorFn: errorFunction,
|
|
32
|
+
timeoutId,
|
|
33
|
+
errorContext
|
|
34
|
+
};
|
|
35
|
+
if (!worker || !worker.threadId) {
|
|
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}`);
|
|
39
|
+
worker = new Worker(await workerRes.text(), { eval: true });
|
|
40
|
+
const onmessage = async (message) => {
|
|
41
|
+
const { workId: messageWorkId } = message;
|
|
42
|
+
if (!resFunctionMap[messageWorkId]) {
|
|
43
|
+
console.warn(`received message for unknown workId: ${messageWorkId}`);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
delete message.workId;
|
|
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
|
+
}
|
|
57
|
+
};
|
|
58
|
+
worker.on("message", onmessage);
|
|
59
|
+
worker.on("error", (err) => {
|
|
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
|
+
});
|
|
68
|
+
});
|
|
69
|
+
worker.on("exit", (code) => {
|
|
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;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return worker;
|
|
81
|
+
}
|
|
82
|
+
export {
|
|
83
|
+
resetWorker,
|
|
84
|
+
setupWorker
|
|
85
|
+
};
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import ParaCore, { PlatformUtils, ConstructorOpts, Environment,
|
|
1
|
+
import ParaCore, { PlatformUtils, ConstructorOpts, Environment, CoreMethodParams, CoreMethodResponse } from '@getpara/core-sdk';
|
|
2
2
|
export declare class Para extends ParaCore {
|
|
3
3
|
constructor(env: Environment, apiKey?: string, opts?: ConstructorOpts);
|
|
4
|
+
protected ready(): Promise<void>;
|
|
4
5
|
protected getPlatformUtils(): PlatformUtils;
|
|
6
|
+
isPasskeySupported(): Promise<boolean>;
|
|
5
7
|
/**
|
|
6
8
|
* Claims a pregenerated wallet.
|
|
7
9
|
*
|
|
@@ -13,8 +15,5 @@ export declare class Para extends ParaCore {
|
|
|
13
15
|
* @param {TPregenIdentifierType} opts.pregenIdentifierType - the type of the identifier.
|
|
14
16
|
* @returns {Promise<string | undefined>} A promise that rejects with an error.
|
|
15
17
|
*/
|
|
16
|
-
claimPregenWallets(
|
|
17
|
-
pregenIdentifier?: string;
|
|
18
|
-
pregenIdentifierType?: TPregenIdentifierType;
|
|
19
|
-
}): Promise<string | undefined>;
|
|
18
|
+
claimPregenWallets(_: CoreMethodParams<'claimPregenWallets'>): CoreMethodResponse<'claimPregenWallets'>;
|
|
20
19
|
}
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import type { Ctx, SignatureRes, PlatformUtils, TPregenIdentifierType, WalletType } from '@getpara/core-sdk';
|
|
4
|
-
import { BackupKitEmailProps } from '@getpara/user-management-client';
|
|
1
|
+
import type { Ctx, SignatureRes, PlatformUtils, TPregenIdentifierType, TWalletType } from '@getpara/core-sdk';
|
|
2
|
+
import { BackupKitEmailProps, SDKType } from '@getpara/user-management-client';
|
|
5
3
|
import { ServerLocalStorage } from './ServerLocalStorage.js';
|
|
6
4
|
import { ServerSessionStorage } from './ServerSessionStorage.js';
|
|
7
5
|
export declare class ServerUtils implements PlatformUtils {
|
|
6
|
+
sdkType: SDKType;
|
|
8
7
|
getPrivateKey(ctx: Ctx, userId: string, walletId: string, share: string, sessionCookie: string): Promise<string>;
|
|
9
|
-
keygen(ctx: Ctx, userId: string, type: Exclude<
|
|
8
|
+
keygen(ctx: Ctx, userId: string, type: Exclude<TWalletType, 'SOLANA'>, secretKey: string | null, sessionCookie: string, emailProps?: BackupKitEmailProps): Promise<{
|
|
10
9
|
signer: string;
|
|
11
10
|
walletId: string;
|
|
12
11
|
}>;
|
|
13
12
|
refresh(_ctx: Ctx, _sessionCookie: string, _userId: string, _walletId: string, _share: string, _oldPartnerId?: string, _newPartnerId?: string): Promise<{
|
|
14
13
|
signer: string;
|
|
15
14
|
}>;
|
|
16
|
-
preKeygen(ctx: Ctx, partnerId: string, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType, type: Exclude<
|
|
15
|
+
preKeygen(ctx: Ctx, partnerId: string, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType, type: Exclude<TWalletType, 'SOLANA'>, secretKey: string | null, // should be acceptable as null in RN as we don't pre-gen them
|
|
17
16
|
sessionCookie: string): Promise<{
|
|
18
17
|
signer: string;
|
|
19
18
|
walletId: string;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { Ctx, TPregenIdentifierType } from '@getpara/core-sdk';
|
|
2
|
-
import { BackupKitEmailProps,
|
|
3
|
-
export declare function
|
|
2
|
+
import { BackupKitEmailProps, TWalletType } from '@getpara/user-management-client';
|
|
3
|
+
export declare function isKeygenComplete(ctx: Ctx, userId: string, walletId: string): Promise<boolean>;
|
|
4
|
+
export declare function isPreKeygenComplete(ctx: Ctx, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType, walletId: string): Promise<boolean>;
|
|
5
|
+
export declare function keygen(ctx: Ctx, userId: string, type: TWalletType, secretKey: string | null, sessionCookie?: string, _emailProps?: BackupKitEmailProps): Promise<{
|
|
4
6
|
signer: string;
|
|
5
7
|
walletId: string;
|
|
6
8
|
recoveryShare: string | null;
|
|
7
9
|
}>;
|
|
8
|
-
export declare function preKeygen(ctx: Ctx, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType, type:
|
|
10
|
+
export declare function preKeygen(ctx: Ctx, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType, type: TWalletType, secretKey: string | null, _skipDistribute: boolean, partnerId: string, sessionCookie?: string): Promise<{
|
|
9
11
|
signer: string;
|
|
10
12
|
walletId: string;
|
|
11
13
|
recoveryShare: string | null;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Ctx, TPregenIdentifierType, SignatureRes,
|
|
1
|
+
import { Ctx, TPregenIdentifierType, SignatureRes, TWalletType } from '@getpara/core-sdk';
|
|
2
2
|
export declare function ed25519Keygen(ctx: Ctx, userId: string): Promise<{
|
|
3
3
|
signer: string;
|
|
4
4
|
walletId: string;
|
|
@@ -7,14 +7,12 @@ export declare function ed25519PreKeygen(ctx: Ctx, pregenIdentifier: string, pre
|
|
|
7
7
|
signer: string;
|
|
8
8
|
walletId: string;
|
|
9
9
|
}>;
|
|
10
|
-
export declare function ed25519Sign(ctx: Ctx, share: string, userId: string, walletId: string, base64Bytes: string): Promise<
|
|
11
|
-
|
|
12
|
-
}>;
|
|
13
|
-
export declare function keygen(ctx: Ctx, userId: string, type: Exclude<WalletType, WalletType.SOLANA>, secretKey: string | null): Promise<{
|
|
10
|
+
export declare function ed25519Sign(ctx: Ctx, share: string, userId: string, walletId: string, base64Bytes: string): Promise<SignatureRes>;
|
|
11
|
+
export declare function keygen(ctx: Ctx, userId: string, type: Exclude<TWalletType, 'SOLANA'>, secretKey: string | null): Promise<{
|
|
14
12
|
signer: string;
|
|
15
13
|
walletId: string;
|
|
16
14
|
}>;
|
|
17
|
-
export declare function preKeygen(ctx: Ctx, partnerId: string, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType, type: Exclude<
|
|
15
|
+
export declare function preKeygen(ctx: Ctx, partnerId: string, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType, type: Exclude<TWalletType, 'SOLANA'>, secretKey: string | null): Promise<{
|
|
18
16
|
signer: string;
|
|
19
17
|
walletId: string;
|
|
20
18
|
}>;
|