@getpara/web-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/LocalStorage.js +36 -0
- package/dist/ParaWeb.d.ts +9 -0
- package/dist/ParaWeb.js +71 -0
- package/dist/SessionStorage.js +36 -0
- package/dist/WebUtils.d.ts +4 -4
- package/dist/WebUtils.js +110 -0
- package/dist/chunk-WLGKV3EF.js +35 -0
- package/dist/cryptography/webAuth.js +162 -0
- package/dist/errors.js +12 -0
- package/dist/index.js +12 -1817
- package/dist/package.json +6 -0
- package/dist/utils/emailUtils.js +10 -0
- package/dist/utils/formattingUtils.js +37 -0
- package/dist/utils/isMobile.js +47 -0
- package/dist/utils/isPasskeySupported.js +17 -0
- package/dist/utils/truncateEthAddress.js +11 -0
- package/dist/wallet/keygen.d.ts +3 -3
- package/dist/wallet/keygen.js +250 -0
- package/dist/wallet/privateKey.js +41 -0
- package/dist/wallet/signing.js +140 -0
- package/dist/wasm/wasm_exec.js +564 -0
- package/dist/workers/walletUtils.d.ts +4 -6
- package/dist/workers/walletUtils.js +363 -0
- package/dist/workers/worker.d.ts +9 -1
- package/dist/workers/worker.js +62 -910
- package/dist/workers/workerWrapper.d.ts +1 -1
- package/dist/workers/workerWrapper.js +85 -0
- package/package.json +30 -25
- package/dist/index.js.br +0 -0
- package/dist/index.js.gz +0 -0
- package/dist/workers/worker.js.br +0 -0
- package/dist/workers/worker.js.gz +0 -0
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
__async
|
|
4
|
+
} from "../chunk-WLGKV3EF.js";
|
|
5
|
+
import { getBaseMPCNetworkUrl } from "@getpara/core-sdk";
|
|
6
|
+
import * as uuid from "uuid";
|
|
7
|
+
const configCGGMPBase = (serverUrl, walletId, id) => `{"ServerUrl":"${serverUrl}", "WalletId": "${walletId}", "Id":"${id}", "Ids":["USER","CAPSULE"], "Threshold":1}`;
|
|
8
|
+
const configDKLSBase = (walletId, id, disableWebSockets) => `{"walletId": "${walletId}", "id":"${id}", "otherId":"CAPSULE", "isReceiver": false, "disableWebSockets": ${disableWebSockets}}`;
|
|
9
|
+
function keygenRequest(ctx, userId, walletId, protocolId) {
|
|
10
|
+
return __async(this, null, function* () {
|
|
11
|
+
const { data } = yield ctx.mpcComputationClient.post("/wallets", {
|
|
12
|
+
userId,
|
|
13
|
+
walletId,
|
|
14
|
+
protocolId
|
|
15
|
+
});
|
|
16
|
+
return data;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
function signMessageRequest(ctx, userId, walletId, protocolId, message, signer) {
|
|
20
|
+
return __async(this, null, function* () {
|
|
21
|
+
const { data } = yield ctx.mpcComputationClient.post(`/wallets/${walletId}/messages/sign`, {
|
|
22
|
+
userId,
|
|
23
|
+
protocolId,
|
|
24
|
+
message,
|
|
25
|
+
signer
|
|
26
|
+
});
|
|
27
|
+
return data;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function sendTransactionRequest(ctx, userId, walletId, protocolId, transaction, signer, chainId) {
|
|
31
|
+
return __async(this, null, function* () {
|
|
32
|
+
const { data } = yield ctx.mpcComputationClient.post(`/wallets/${walletId}/transactions/send`, {
|
|
33
|
+
userId,
|
|
34
|
+
protocolId,
|
|
35
|
+
transaction,
|
|
36
|
+
signer,
|
|
37
|
+
chainId
|
|
38
|
+
});
|
|
39
|
+
return data;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function ed25519Keygen(ctx, userId) {
|
|
43
|
+
return __async(this, null, function* () {
|
|
44
|
+
const { walletId, protocolId } = yield ctx.client.createWallet(userId, {
|
|
45
|
+
scheme: "ED25519",
|
|
46
|
+
type: "SOLANA"
|
|
47
|
+
});
|
|
48
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
49
|
+
try {
|
|
50
|
+
const newSigner = yield new Promise(
|
|
51
|
+
(resolve, reject) => globalThis.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
|
|
52
|
+
if (err) {
|
|
53
|
+
reject(err);
|
|
54
|
+
}
|
|
55
|
+
resolve(result);
|
|
56
|
+
})
|
|
57
|
+
);
|
|
58
|
+
return { signer: newSigner, walletId };
|
|
59
|
+
} catch (e) {
|
|
60
|
+
throw new Error(`error creating account of type SOLANA with userId ${userId} and walletId ${walletId}`);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType) {
|
|
65
|
+
return __async(this, null, function* () {
|
|
66
|
+
const { walletId, protocolId } = yield ctx.client.createPregenWallet({
|
|
67
|
+
pregenIdentifier,
|
|
68
|
+
pregenIdentifierType,
|
|
69
|
+
scheme: "ED25519",
|
|
70
|
+
type: "SOLANA"
|
|
71
|
+
});
|
|
72
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
73
|
+
try {
|
|
74
|
+
const newSigner = yield new Promise(
|
|
75
|
+
(resolve, reject) => globalThis.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
|
|
76
|
+
if (err) {
|
|
77
|
+
reject(err);
|
|
78
|
+
}
|
|
79
|
+
resolve(result);
|
|
80
|
+
})
|
|
81
|
+
);
|
|
82
|
+
return { signer: newSigner, walletId };
|
|
83
|
+
} catch (e) {
|
|
84
|
+
throw new Error(`error creating account of type SOLANA with walletId ${walletId}`);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function ed25519Sign(ctx, share, userId, walletId, base64Bytes) {
|
|
89
|
+
return __async(this, null, function* () {
|
|
90
|
+
const protocolId = uuid.v4();
|
|
91
|
+
const preSignMessageRes = ctx.client.preSignMessage(userId, walletId, base64Bytes, "ED25519", void 0, protocolId);
|
|
92
|
+
const signRes = function() {
|
|
93
|
+
return __async(this, null, function* () {
|
|
94
|
+
try {
|
|
95
|
+
const base64Sig = yield new Promise(
|
|
96
|
+
(resolve, reject) => globalThis.ed25519Sign(share, protocolId, base64Bytes, (err, result) => {
|
|
97
|
+
if (err) {
|
|
98
|
+
reject(err);
|
|
99
|
+
}
|
|
100
|
+
resolve(result);
|
|
101
|
+
})
|
|
102
|
+
);
|
|
103
|
+
return { signature: base64Sig };
|
|
104
|
+
} catch (e) {
|
|
105
|
+
throw new Error(`error signing for account of type SOLANA with userId ${userId} and walletId ${walletId}`);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}();
|
|
109
|
+
const { pendingTransactionId } = yield preSignMessageRes;
|
|
110
|
+
if (pendingTransactionId) {
|
|
111
|
+
return { pendingTransactionId };
|
|
112
|
+
}
|
|
113
|
+
return yield signRes;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
function keygen(ctx, userId, type, secretKey) {
|
|
117
|
+
return __async(this, null, function* () {
|
|
118
|
+
const { walletId, protocolId } = yield ctx.client.createWallet(userId, {
|
|
119
|
+
useTwoSigners: true,
|
|
120
|
+
scheme: ctx.useDKLS ? "DKLS" : "CGGMP",
|
|
121
|
+
type,
|
|
122
|
+
cosmosPrefix: type === "COSMOS" ? ctx.cosmosPrefix : void 0
|
|
123
|
+
});
|
|
124
|
+
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
125
|
+
return {
|
|
126
|
+
signer: (yield keygenRequest(ctx, userId, walletId, protocolId)).signer,
|
|
127
|
+
walletId
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
131
|
+
const signerConfigUser = ctx.useDKLS ? configDKLSBase(walletId, "USER", ctx.disableWebSockets) : configCGGMPBase(serverUrl, walletId, "USER");
|
|
132
|
+
const createAccountFn = ctx.useDKLS ? globalThis.dklsCreateAccount : globalThis.createAccountV2;
|
|
133
|
+
try {
|
|
134
|
+
const newSigner = yield new Promise(
|
|
135
|
+
(resolve, reject) => createAccountFn(
|
|
136
|
+
signerConfigUser,
|
|
137
|
+
serverUrl,
|
|
138
|
+
protocolId,
|
|
139
|
+
secretKey,
|
|
140
|
+
() => {
|
|
141
|
+
},
|
|
142
|
+
// no-op for deprecated callback to update progress percentage
|
|
143
|
+
(err, result) => {
|
|
144
|
+
if (err) {
|
|
145
|
+
reject(err);
|
|
146
|
+
}
|
|
147
|
+
resolve(result);
|
|
148
|
+
}
|
|
149
|
+
)
|
|
150
|
+
);
|
|
151
|
+
return { signer: newSigner, walletId };
|
|
152
|
+
} catch (e) {
|
|
153
|
+
throw new Error(`error creating account of type ${type} with userId ${userId} and walletId ${walletId}`);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
function preKeygen(ctx, _partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey) {
|
|
158
|
+
return __async(this, null, function* () {
|
|
159
|
+
const { walletId, protocolId } = yield ctx.client.createPregenWallet({
|
|
160
|
+
pregenIdentifier,
|
|
161
|
+
pregenIdentifierType,
|
|
162
|
+
type,
|
|
163
|
+
cosmosPrefix: type === "COSMOS" ? ctx.cosmosPrefix : void 0
|
|
164
|
+
});
|
|
165
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
166
|
+
const signerConfigUser = configDKLSBase(walletId, "USER", ctx.disableWebSockets);
|
|
167
|
+
try {
|
|
168
|
+
const newSigner = yield new Promise(
|
|
169
|
+
(resolve, reject) => globalThis.dklsCreateAccount(
|
|
170
|
+
signerConfigUser,
|
|
171
|
+
serverUrl,
|
|
172
|
+
protocolId,
|
|
173
|
+
secretKey,
|
|
174
|
+
() => {
|
|
175
|
+
},
|
|
176
|
+
// no-op for deprecated callback to update progress percentage
|
|
177
|
+
(err, result) => {
|
|
178
|
+
if (err) {
|
|
179
|
+
reject(err);
|
|
180
|
+
}
|
|
181
|
+
resolve(result);
|
|
182
|
+
}
|
|
183
|
+
)
|
|
184
|
+
);
|
|
185
|
+
return { signer: newSigner, walletId };
|
|
186
|
+
} catch (e) {
|
|
187
|
+
throw new Error(`error creating account of type ${type} with walletId ${walletId}`);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
function signMessage(ctx, share, walletId, userId, message, cosmosSignDoc) {
|
|
192
|
+
return __async(this, null, function* () {
|
|
193
|
+
const protocolId = uuid.v4();
|
|
194
|
+
const preSignMessageRes = ctx.client.preSignMessage(userId, walletId, message, null, cosmosSignDoc, protocolId);
|
|
195
|
+
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
196
|
+
return signMessageRequest(ctx, userId, walletId, protocolId, message, share);
|
|
197
|
+
}
|
|
198
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
199
|
+
const signMessageFn = ctx.useDKLS ? globalThis.dklsSignMessage : globalThis.signMessage;
|
|
200
|
+
const parsedShare = JSON.parse(share);
|
|
201
|
+
if (!parsedShare.disableWebSockets !== !ctx.disableWebSockets) {
|
|
202
|
+
parsedShare.disableWebSockets = ctx.disableWebSockets;
|
|
203
|
+
}
|
|
204
|
+
share = JSON.stringify(parsedShare);
|
|
205
|
+
const signMessageRes = function() {
|
|
206
|
+
return __async(this, null, function* () {
|
|
207
|
+
try {
|
|
208
|
+
return yield new Promise(
|
|
209
|
+
(resolve, reject) => signMessageFn(share, serverUrl, message, protocolId, (err, result) => {
|
|
210
|
+
if (err) {
|
|
211
|
+
reject(err);
|
|
212
|
+
}
|
|
213
|
+
resolve({ signature: result });
|
|
214
|
+
})
|
|
215
|
+
);
|
|
216
|
+
} catch (e) {
|
|
217
|
+
throw new Error(`error signing for account with userId ${userId} and walletId ${walletId}`);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}();
|
|
221
|
+
const { pendingTransactionId } = yield preSignMessageRes;
|
|
222
|
+
if (pendingTransactionId) {
|
|
223
|
+
return { pendingTransactionId };
|
|
224
|
+
}
|
|
225
|
+
return yield signMessageRes;
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
function signTransaction(ctx, share, walletId, userId, tx, chainId) {
|
|
229
|
+
return __async(this, null, function* () {
|
|
230
|
+
const protocolId = uuid.v4();
|
|
231
|
+
const signTransactionRes = ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId, protocolId });
|
|
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 ? globalThis.dklsSendTransaction : globalThis.sendTransaction;
|
|
237
|
+
const parsedShare = JSON.parse(share);
|
|
238
|
+
if (!parsedShare.disableWebSockets !== !ctx.disableWebSockets) {
|
|
239
|
+
parsedShare.disableWebSockets = ctx.disableWebSockets;
|
|
240
|
+
}
|
|
241
|
+
share = JSON.stringify(parsedShare);
|
|
242
|
+
const signTxRes = function() {
|
|
243
|
+
return __async(this, null, function* () {
|
|
244
|
+
try {
|
|
245
|
+
return yield new Promise(
|
|
246
|
+
(resolve, reject) => signTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
|
|
247
|
+
if (err) {
|
|
248
|
+
reject(err);
|
|
249
|
+
}
|
|
250
|
+
resolve({ signature: result });
|
|
251
|
+
})
|
|
252
|
+
);
|
|
253
|
+
} catch (e) {
|
|
254
|
+
throw new Error(`error signing transaction for account with userId ${userId} and walletId ${walletId}`);
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
}();
|
|
258
|
+
const {
|
|
259
|
+
data: { pendingTransactionId }
|
|
260
|
+
} = yield signTransactionRes;
|
|
261
|
+
if (pendingTransactionId) {
|
|
262
|
+
return { pendingTransactionId };
|
|
263
|
+
}
|
|
264
|
+
return yield signTxRes;
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
|
|
268
|
+
return __async(this, null, function* () {
|
|
269
|
+
const protocolId = uuid.v4();
|
|
270
|
+
const sendTransactionRes = ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId, protocolId });
|
|
271
|
+
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
272
|
+
return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
|
|
273
|
+
}
|
|
274
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
275
|
+
const sendTransactionFn = ctx.useDKLS ? globalThis.dklsSendTransaction : globalThis.sendTransaction;
|
|
276
|
+
const parsedShare = JSON.parse(share);
|
|
277
|
+
if (!parsedShare.disableWebSockets !== !ctx.disableWebSockets) {
|
|
278
|
+
parsedShare.disableWebSockets = ctx.disableWebSockets;
|
|
279
|
+
}
|
|
280
|
+
share = JSON.stringify(parsedShare);
|
|
281
|
+
const sendTxRes = function() {
|
|
282
|
+
return __async(this, null, function* () {
|
|
283
|
+
try {
|
|
284
|
+
return yield new Promise(
|
|
285
|
+
(resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
|
|
286
|
+
if (err) {
|
|
287
|
+
reject(err);
|
|
288
|
+
}
|
|
289
|
+
resolve({ signature: result });
|
|
290
|
+
})
|
|
291
|
+
);
|
|
292
|
+
} catch (e) {
|
|
293
|
+
throw new Error(`error signing transaction to send for account with userId ${userId} and walletId ${walletId}`);
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
}();
|
|
297
|
+
const {
|
|
298
|
+
data: { pendingTransactionId }
|
|
299
|
+
} = yield sendTransactionRes;
|
|
300
|
+
if (pendingTransactionId) {
|
|
301
|
+
return { pendingTransactionId };
|
|
302
|
+
}
|
|
303
|
+
return yield sendTxRes;
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
function refresh(ctx, share, walletId, userId, oldPartnerId, newPartnerId, keyShareProtocolId) {
|
|
307
|
+
return __async(this, null, function* () {
|
|
308
|
+
const {
|
|
309
|
+
data: { protocolId }
|
|
310
|
+
} = yield ctx.client.refreshKeys(userId, walletId, oldPartnerId, newPartnerId, keyShareProtocolId);
|
|
311
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
312
|
+
const refreshFn = ctx.useDKLS ? globalThis.dklsRefresh : globalThis.refresh;
|
|
313
|
+
const parsedShare = JSON.parse(share);
|
|
314
|
+
if (!parsedShare.disableWebSockets !== !ctx.disableWebSockets) {
|
|
315
|
+
parsedShare.disableWebSockets = ctx.disableWebSockets;
|
|
316
|
+
}
|
|
317
|
+
share = JSON.stringify(parsedShare);
|
|
318
|
+
try {
|
|
319
|
+
return yield new Promise(
|
|
320
|
+
(resolve, reject) => refreshFn(share, serverUrl, protocolId, (err, result) => {
|
|
321
|
+
if (err) {
|
|
322
|
+
reject(err);
|
|
323
|
+
}
|
|
324
|
+
resolve({ protocolId, signer: result });
|
|
325
|
+
})
|
|
326
|
+
);
|
|
327
|
+
} catch (e) {
|
|
328
|
+
throw new Error(`error refreshing keys for account with userId ${userId} and walletId ${walletId}`);
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
function getPrivateKey(ctx, share, walletId, userId) {
|
|
333
|
+
return __async(this, null, function* () {
|
|
334
|
+
const paraShare = yield ctx.client.getParaShare(userId, walletId);
|
|
335
|
+
if (!paraShare) {
|
|
336
|
+
return "";
|
|
337
|
+
}
|
|
338
|
+
try {
|
|
339
|
+
return yield new Promise(
|
|
340
|
+
(resolve, reject) => globalThis.getPrivateKey(share, paraShare, (err, result) => {
|
|
341
|
+
if (err) {
|
|
342
|
+
reject(err);
|
|
343
|
+
}
|
|
344
|
+
resolve(result);
|
|
345
|
+
})
|
|
346
|
+
);
|
|
347
|
+
} catch (e) {
|
|
348
|
+
throw new Error(`error getting private key for account with userId ${userId} and walletId ${walletId}`);
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
export {
|
|
353
|
+
ed25519Keygen,
|
|
354
|
+
ed25519PreKeygen,
|
|
355
|
+
ed25519Sign,
|
|
356
|
+
getPrivateKey,
|
|
357
|
+
keygen,
|
|
358
|
+
preKeygen,
|
|
359
|
+
refresh,
|
|
360
|
+
sendTransaction,
|
|
361
|
+
signMessage,
|
|
362
|
+
signTransaction
|
|
363
|
+
};
|
package/dist/workers/worker.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import '../wasm/wasm_exec.js';
|
|
2
1
|
import { Environment } from '@getpara/core-sdk';
|
|
3
2
|
export interface Message {
|
|
4
3
|
env: Environment;
|
|
@@ -13,7 +12,16 @@ export interface Message {
|
|
|
13
12
|
disableWebSockets?: boolean;
|
|
14
13
|
wasmOverride?: ArrayBuffer;
|
|
15
14
|
returnObject?: boolean;
|
|
15
|
+
workId?: string;
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Executes an operation with retry capabilities
|
|
19
|
+
* @param operation The function to execute
|
|
20
|
+
* @param maxRetries Maximum number of retries (default: 2)
|
|
21
|
+
* @param timeoutMs Timeout in milliseconds (default: 10000)
|
|
22
|
+
* @returns The result of the operation
|
|
23
|
+
*/
|
|
24
|
+
export declare function withRetry<T>(operation: () => Promise<T>, maxRetries?: number, timeoutMs?: number): Promise<T>;
|
|
17
25
|
export declare function handleMessage(e: {
|
|
18
26
|
data: Message;
|
|
19
27
|
}, postMessage: (message: any) => void, useFetchAdapter?: boolean): Promise<boolean>;
|