@getpara/web-sdk 2.0.0-alpha.3 → 2.0.0-alpha.6

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