@getpara/server-sdk 0.1.0
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.d.ts +4 -0
- package/dist/cjs/ParaServer.js +14 -0
- package/dist/cjs/ServerLocalStorage.d.ts +12 -0
- package/dist/cjs/ServerLocalStorage.js +30 -0
- package/dist/cjs/ServerSessionStorage.d.ts +12 -0
- package/dist/cjs/ServerSessionStorage.js +30 -0
- package/dist/cjs/ServerUtils.d.ts +43 -0
- package/dist/cjs/ServerUtils.js +55 -0
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.js +21 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/wallet/keygen.d.ts +22 -0
- package/dist/cjs/wallet/keygen.js +172 -0
- package/dist/cjs/wallet/privateKey.d.ts +2 -0
- package/dist/cjs/wallet/privateKey.js +62 -0
- package/dist/cjs/wallet/signing.d.ts +5 -0
- package/dist/cjs/wallet/signing.js +133 -0
- package/dist/cjs/wasm/wasm_exec.d.ts +1 -0
- package/dist/cjs/wasm/wasm_exec.js +521 -0
- package/dist/cjs/workers/walletUtils.d.ts +25 -0
- package/dist/cjs/workers/walletUtils.js +247 -0
- package/dist/cjs/workers/worker.d.ts +18 -0
- package/dist/cjs/workers/worker.js +154 -0
- package/dist/cjs/workers/workerWrapper.d.ts +4 -0
- package/dist/cjs/workers/workerWrapper.js +55 -0
- package/dist/esm/ParaServer.d.ts +4 -0
- package/dist/esm/ParaServer.js +7 -0
- package/dist/esm/ServerLocalStorage.d.ts +12 -0
- package/dist/esm/ServerLocalStorage.js +26 -0
- package/dist/esm/ServerSessionStorage.d.ts +12 -0
- package/dist/esm/ServerSessionStorage.js +26 -0
- package/dist/esm/ServerUtils.d.ts +43 -0
- package/dist/esm/ServerUtils.js +51 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/wallet/keygen.d.ts +22 -0
- package/dist/esm/wallet/keygen.js +142 -0
- package/dist/esm/wallet/privateKey.d.ts +2 -0
- package/dist/esm/wallet/privateKey.js +35 -0
- package/dist/esm/wallet/signing.d.ts +5 -0
- package/dist/esm/wallet/signing.js +103 -0
- package/dist/esm/wasm/wasm_exec.d.ts +1 -0
- package/dist/esm/wasm/wasm_exec.js +521 -0
- package/dist/esm/workers/walletUtils.d.ts +25 -0
- package/dist/esm/workers/walletUtils.js +234 -0
- package/dist/esm/workers/worker.d.ts +18 -0
- package/dist/esm/workers/worker.js +124 -0
- package/dist/esm/workers/workerWrapper.d.ts +4 -0
- package/dist/esm/workers/workerWrapper.js +51 -0
- package/dist/types/ParaServer.d.ts +4 -0
- package/dist/types/ServerLocalStorage.d.ts +12 -0
- package/dist/types/ServerSessionStorage.d.ts +12 -0
- package/dist/types/ServerUtils.d.ts +43 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/wallet/keygen.d.ts +22 -0
- package/dist/types/wallet/privateKey.d.ts +2 -0
- package/dist/types/wallet/signing.d.ts +5 -0
- package/dist/types/wasm/wasm_exec.d.ts +1 -0
- package/dist/types/workers/walletUtils.d.ts +25 -0
- package/dist/types/workers/worker.d.ts +18 -0
- package/dist/types/workers/workerWrapper.d.ts +4 -0
- package/package.json +33 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { getBaseMPCNetworkUrl, WalletScheme, WalletType } from '@getpara/core-sdk';
|
|
11
|
+
const configCGGMPBase = (serverUrl, walletId, id) => `{"ServerUrl":"${serverUrl}", "WalletId": "${walletId}", "Id":"${id}", "Ids":["USER","CAPSULE"], "Threshold":1}`;
|
|
12
|
+
const configDKLSBase = (walletId, id, disableWebSockets) => `{"walletId": "${walletId}", "id":"${id}", "otherId":"CAPSULE", "isReceiver": false, "disableWebSockets": ${disableWebSockets}}`;
|
|
13
|
+
function keygenRequest(ctx, userId, walletId, protocolId) {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
const { data } = yield ctx.mpcComputationClient.post('/wallets', {
|
|
16
|
+
userId,
|
|
17
|
+
walletId,
|
|
18
|
+
protocolId,
|
|
19
|
+
});
|
|
20
|
+
return data;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function signMessageRequest(ctx, userId, walletId, protocolId, message, signer) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
const { data } = yield ctx.mpcComputationClient.post(`/wallets/${walletId}/messages/sign`, {
|
|
26
|
+
userId,
|
|
27
|
+
protocolId,
|
|
28
|
+
message,
|
|
29
|
+
signer,
|
|
30
|
+
});
|
|
31
|
+
return data;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
function sendTransactionRequest(ctx, userId, walletId, protocolId, transaction, signer, chainId) {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
const { data } = yield ctx.mpcComputationClient.post(`/wallets/${walletId}/transactions/send`, {
|
|
37
|
+
userId,
|
|
38
|
+
protocolId,
|
|
39
|
+
transaction,
|
|
40
|
+
signer,
|
|
41
|
+
chainId,
|
|
42
|
+
});
|
|
43
|
+
return data;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
export function ed25519Keygen(ctx, userId) {
|
|
47
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
const { walletId, protocolId } = yield ctx.client.createWallet(userId, {
|
|
49
|
+
scheme: WalletScheme.ED25519,
|
|
50
|
+
type: WalletType.SOLANA,
|
|
51
|
+
});
|
|
52
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
53
|
+
const newSigner = (yield new Promise((resolve, reject) => global.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
|
|
54
|
+
if (err) {
|
|
55
|
+
reject(err);
|
|
56
|
+
}
|
|
57
|
+
resolve(result);
|
|
58
|
+
})));
|
|
59
|
+
return { signer: newSigner, walletId };
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
export function ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType) {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
const { walletId, protocolId } = yield ctx.client.createWalletPreGen({
|
|
65
|
+
pregenIdentifier,
|
|
66
|
+
pregenIdentifierType,
|
|
67
|
+
scheme: WalletScheme.ED25519,
|
|
68
|
+
type: WalletType.SOLANA,
|
|
69
|
+
});
|
|
70
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
71
|
+
const newSigner = (yield new Promise((resolve, reject) => global.ed25519CreateAccount(serverUrl, walletId, protocolId, (err, result) => {
|
|
72
|
+
if (err) {
|
|
73
|
+
reject(err);
|
|
74
|
+
}
|
|
75
|
+
resolve(result);
|
|
76
|
+
})));
|
|
77
|
+
return { signer: newSigner, walletId };
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
export function ed25519Sign(ctx, share, userId, walletId, base64Bytes) {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
const { protocolId } = yield ctx.client.preSignMessage(userId, walletId, base64Bytes, WalletScheme.ED25519);
|
|
83
|
+
const base64Sig = (yield new Promise((resolve, reject) => global.ed25519Sign(share, protocolId, base64Bytes, (err, result) => {
|
|
84
|
+
if (err) {
|
|
85
|
+
reject(err);
|
|
86
|
+
}
|
|
87
|
+
resolve(result);
|
|
88
|
+
})));
|
|
89
|
+
return { signature: base64Sig };
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
export function keygen(ctx, userId, type, secretKey) {
|
|
93
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
const { walletId, protocolId } = yield ctx.client.createWallet(userId, {
|
|
95
|
+
useTwoSigners: true,
|
|
96
|
+
scheme: ctx.useDKLS ? WalletScheme.DKLS : WalletScheme.CGGMP,
|
|
97
|
+
type,
|
|
98
|
+
cosmosPrefix: type === WalletType.COSMOS ? ctx.cosmosPrefix : undefined,
|
|
99
|
+
});
|
|
100
|
+
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
101
|
+
return {
|
|
102
|
+
signer: (yield keygenRequest(ctx, userId, walletId, protocolId)).signer,
|
|
103
|
+
walletId,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
107
|
+
const signerConfigUser = ctx.useDKLS
|
|
108
|
+
? configDKLSBase(walletId, 'USER', ctx.disableWebSockets)
|
|
109
|
+
: configCGGMPBase(serverUrl, walletId, 'USER');
|
|
110
|
+
const createAccountFn = ctx.useDKLS ? global.dklsCreateAccount : global.createAccountV2;
|
|
111
|
+
const newSigner = (yield new Promise((resolve, reject) => createAccountFn(signerConfigUser, serverUrl, protocolId, secretKey, () => { }, // no-op for deprecated callback to update progress percentage
|
|
112
|
+
(err, result) => {
|
|
113
|
+
if (err) {
|
|
114
|
+
reject(err);
|
|
115
|
+
}
|
|
116
|
+
resolve(result);
|
|
117
|
+
})));
|
|
118
|
+
return { signer: newSigner, walletId };
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
export function preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey) {
|
|
122
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
const { walletId, protocolId } = yield ctx.client.createWalletPreGen({
|
|
124
|
+
pregenIdentifier,
|
|
125
|
+
pregenIdentifierType,
|
|
126
|
+
type,
|
|
127
|
+
cosmosPrefix: type === WalletType.COSMOS ? ctx.cosmosPrefix : undefined,
|
|
128
|
+
});
|
|
129
|
+
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
130
|
+
return {
|
|
131
|
+
signer: (yield keygenRequest(ctx, partnerId, walletId, protocolId)).signer,
|
|
132
|
+
walletId,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
136
|
+
const signerConfigUser = ctx.useDKLS
|
|
137
|
+
? configDKLSBase(walletId, 'USER', ctx.disableWebSockets)
|
|
138
|
+
: configCGGMPBase(serverUrl, walletId, 'USER');
|
|
139
|
+
const createAccountFn = ctx.useDKLS ? global.dklsCreateAccount : global.createAccountV2;
|
|
140
|
+
const newSigner = (yield new Promise((resolve, reject) => createAccountFn(signerConfigUser, serverUrl, protocolId, secretKey, () => { }, // no-op for deprecated callback to update progress percentage
|
|
141
|
+
(err, result) => {
|
|
142
|
+
if (err) {
|
|
143
|
+
reject(err);
|
|
144
|
+
}
|
|
145
|
+
resolve(result);
|
|
146
|
+
})));
|
|
147
|
+
return { signer: newSigner, walletId };
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
export function signMessage(ctx, share, walletId, userId, message) {
|
|
151
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
152
|
+
const { protocolId, pendingTransactionId } = yield ctx.client.preSignMessage(userId, walletId, message);
|
|
153
|
+
if (pendingTransactionId) {
|
|
154
|
+
return { pendingTransactionId };
|
|
155
|
+
}
|
|
156
|
+
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
157
|
+
return signMessageRequest(ctx, userId, walletId, protocolId, message, share);
|
|
158
|
+
}
|
|
159
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
160
|
+
const signMessageFn = ctx.useDKLS ? global.dklsSignMessage : global.signMessage;
|
|
161
|
+
return new Promise((resolve, reject) => signMessageFn(share, serverUrl, message, protocolId, (err, result) => {
|
|
162
|
+
if (err) {
|
|
163
|
+
reject(err);
|
|
164
|
+
}
|
|
165
|
+
resolve({ signature: result });
|
|
166
|
+
}));
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
export function signTransaction(ctx, share, walletId, userId, tx, chainId) {
|
|
170
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
171
|
+
const { data: { protocolId, pendingTransactionId }, } = yield ctx.client.signTransaction(userId, walletId, { transaction: tx, chainId });
|
|
172
|
+
if (pendingTransactionId) {
|
|
173
|
+
return { pendingTransactionId };
|
|
174
|
+
}
|
|
175
|
+
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
176
|
+
return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
|
|
177
|
+
}
|
|
178
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
179
|
+
const signTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
|
|
180
|
+
return new Promise((resolve, reject) => signTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
|
|
181
|
+
if (err) {
|
|
182
|
+
reject(err);
|
|
183
|
+
}
|
|
184
|
+
resolve({ signature: result });
|
|
185
|
+
}));
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
export function sendTransaction(ctx, share, walletId, userId, tx, chainId) {
|
|
189
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
190
|
+
const { data: { protocolId, pendingTransactionId }, } = yield ctx.client.sendTransaction(userId, walletId, { transaction: tx, chainId });
|
|
191
|
+
if (pendingTransactionId) {
|
|
192
|
+
return { pendingTransactionId };
|
|
193
|
+
}
|
|
194
|
+
if (ctx.offloadMPCComputationURL && !ctx.useDKLS) {
|
|
195
|
+
return sendTransactionRequest(ctx, userId, walletId, protocolId, tx, share, chainId);
|
|
196
|
+
}
|
|
197
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
198
|
+
const sendTransactionFn = ctx.useDKLS ? global.dklsSendTransaction : global.sendTransaction;
|
|
199
|
+
return new Promise((resolve, reject) => sendTransactionFn(share, serverUrl, tx, chainId, protocolId, (err, result) => {
|
|
200
|
+
if (err) {
|
|
201
|
+
reject(err);
|
|
202
|
+
}
|
|
203
|
+
resolve({ signature: result });
|
|
204
|
+
}));
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
export function refresh(ctx, share, walletId, userId) {
|
|
208
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
209
|
+
const { data: { protocolId }, } = yield ctx.client.refreshKeys(userId, walletId);
|
|
210
|
+
const serverUrl = getBaseMPCNetworkUrl(ctx.env, !ctx.disableWebSockets);
|
|
211
|
+
const refreshFn = ctx.useDKLS ? global.dklsRefresh : global.refresh;
|
|
212
|
+
return new Promise((resolve, reject) => refreshFn(share, serverUrl, protocolId, (err, result) => {
|
|
213
|
+
if (err) {
|
|
214
|
+
reject(err);
|
|
215
|
+
}
|
|
216
|
+
resolve(result);
|
|
217
|
+
}));
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
export function getPrivateKey(ctx, share, walletId, userId) {
|
|
221
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
222
|
+
const paraShare = yield ctx.client.getParaShare(userId, walletId);
|
|
223
|
+
if (!paraShare) {
|
|
224
|
+
console.error('unable to retrieve Para share');
|
|
225
|
+
return '';
|
|
226
|
+
}
|
|
227
|
+
return new Promise((resolve, reject) => global.getPrivateKey(share, paraShare, (err, result) => {
|
|
228
|
+
if (err) {
|
|
229
|
+
reject(err);
|
|
230
|
+
}
|
|
231
|
+
resolve(result);
|
|
232
|
+
}));
|
|
233
|
+
});
|
|
234
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Environment } from '@getpara/core-sdk';
|
|
2
|
+
interface Message {
|
|
3
|
+
env: Environment;
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
cosmosPrefix?: string;
|
|
6
|
+
offloadMPCComputationURL?: string;
|
|
7
|
+
disableWorkers?: boolean;
|
|
8
|
+
functionType: string;
|
|
9
|
+
params: Record<string, any>;
|
|
10
|
+
sessionCookie?: string;
|
|
11
|
+
useDKLS?: boolean;
|
|
12
|
+
disableWebSockets?: boolean;
|
|
13
|
+
workId: string;
|
|
14
|
+
}
|
|
15
|
+
export declare function handleMessage(e: {
|
|
16
|
+
data: Message;
|
|
17
|
+
}): Promise<any>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import axios from 'axios';
|
|
11
|
+
import { getPortalBaseURL, initClient, mpcComputationClient, paraVersion, WalletType, } from '@getpara/core-sdk';
|
|
12
|
+
import * as walletUtils from './walletUtils.js';
|
|
13
|
+
let rawWasm;
|
|
14
|
+
function requestWasmWithRetries(ctx, retries = 3) {
|
|
15
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
16
|
+
for (let i = 0; i < retries; i++) {
|
|
17
|
+
try {
|
|
18
|
+
return yield axios.get(`${getPortalBaseURL(ctx, true, true)}/static/js/main.wasm`, { responseType: 'arraybuffer' });
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
if (i === retries - 1) {
|
|
22
|
+
throw e;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
function loadWasm(ctx) {
|
|
29
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
+
yield import('../wasm/wasm_exec.js');
|
|
31
|
+
global.WebSocket = require('ws');
|
|
32
|
+
const goWasm = new global.Go();
|
|
33
|
+
if (!rawWasm) {
|
|
34
|
+
rawWasm = (yield requestWasmWithRetries(ctx)).data;
|
|
35
|
+
}
|
|
36
|
+
const wasmBuffer = new Uint8Array(rawWasm);
|
|
37
|
+
const webAssemblySource = yield WebAssembly.instantiate(wasmBuffer, goWasm.importObject);
|
|
38
|
+
goWasm.run(webAssemblySource.instance);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function executeMessage(ctx, message) {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
const { functionType, params } = message;
|
|
44
|
+
switch (functionType) {
|
|
45
|
+
case 'KEYGEN': {
|
|
46
|
+
const { userId, secretKey, type = WalletType.EVM } = params;
|
|
47
|
+
return walletUtils.keygen(ctx, userId, type, secretKey);
|
|
48
|
+
}
|
|
49
|
+
case 'SIGN_TRANSACTION': {
|
|
50
|
+
const { share, walletId, userId, tx, chainId } = params;
|
|
51
|
+
return walletUtils.signTransaction(ctx, share, walletId, userId, tx, chainId);
|
|
52
|
+
}
|
|
53
|
+
case 'SEND_TRANSACTION': {
|
|
54
|
+
const { share, walletId, userId, tx, chainId } = params;
|
|
55
|
+
return walletUtils.sendTransaction(ctx, share, walletId, userId, tx, chainId);
|
|
56
|
+
}
|
|
57
|
+
case 'SIGN_MESSAGE': {
|
|
58
|
+
const { share, walletId, userId, message } = params;
|
|
59
|
+
return walletUtils.signMessage(ctx, share, walletId, userId, message);
|
|
60
|
+
}
|
|
61
|
+
case 'REFRESH': {
|
|
62
|
+
const { share, walletId, userId } = params;
|
|
63
|
+
return walletUtils.refresh(ctx, share, walletId, userId);
|
|
64
|
+
}
|
|
65
|
+
case 'PREKEYGEN': {
|
|
66
|
+
const { email, partnerId, secretKey, type = WalletType.EVM } = params;
|
|
67
|
+
let { pregenIdentifier, pregenIdentifierType } = params;
|
|
68
|
+
if (email !== 'null' && email !== 'undefined' && email !== '' && email != null) {
|
|
69
|
+
pregenIdentifier = email;
|
|
70
|
+
pregenIdentifierType = 'EMAIL';
|
|
71
|
+
}
|
|
72
|
+
const keygenRes = yield walletUtils.preKeygen(ctx, partnerId, pregenIdentifier, pregenIdentifierType, type, secretKey);
|
|
73
|
+
return keygenRes;
|
|
74
|
+
}
|
|
75
|
+
case 'GET_PRIVATE_KEY': {
|
|
76
|
+
const { share, walletId, userId } = params;
|
|
77
|
+
return yield walletUtils.getPrivateKey(ctx, share, walletId, userId);
|
|
78
|
+
}
|
|
79
|
+
case 'ED25519_KEYGEN': {
|
|
80
|
+
const { userId } = params;
|
|
81
|
+
return walletUtils.ed25519Keygen(ctx, userId);
|
|
82
|
+
}
|
|
83
|
+
case 'ED25519_SIGN': {
|
|
84
|
+
const { share, walletId, userId, base64Bytes } = params;
|
|
85
|
+
return walletUtils.ed25519Sign(ctx, share, userId, walletId, base64Bytes);
|
|
86
|
+
}
|
|
87
|
+
case 'ED25519_PREKEYGEN': {
|
|
88
|
+
const { email } = params;
|
|
89
|
+
let { pregenIdentifier, pregenIdentifierType } = params;
|
|
90
|
+
if (email !== 'null' && email !== 'undefined' && email !== '' && email != null) {
|
|
91
|
+
pregenIdentifier = email;
|
|
92
|
+
pregenIdentifierType = 'EMAIL';
|
|
93
|
+
}
|
|
94
|
+
return walletUtils.ed25519PreKeygen(ctx, pregenIdentifier, pregenIdentifierType);
|
|
95
|
+
}
|
|
96
|
+
default: {
|
|
97
|
+
throw new Error(`functionType: ${functionType} not supported`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
export function handleMessage(e) {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
const { env, apiKey, cosmosPrefix = 'cosmos', offloadMPCComputationURL, disableWorkers, sessionCookie, useDKLS, disableWebSockets, workId, } = e.data;
|
|
105
|
+
const ctx = {
|
|
106
|
+
env,
|
|
107
|
+
apiKey,
|
|
108
|
+
client: initClient({ env, version: paraVersion, apiKey, retrieveSessionCookie: () => sessionCookie }),
|
|
109
|
+
offloadMPCComputationURL: offloadMPCComputationURL,
|
|
110
|
+
mpcComputationClient: offloadMPCComputationURL
|
|
111
|
+
? mpcComputationClient.initClient(offloadMPCComputationURL, !!disableWorkers)
|
|
112
|
+
: undefined,
|
|
113
|
+
useDKLS,
|
|
114
|
+
disableWebSockets: !!disableWebSockets,
|
|
115
|
+
cosmosPrefix,
|
|
116
|
+
};
|
|
117
|
+
if (!ctx.offloadMPCComputationURL || ctx.useDKLS) {
|
|
118
|
+
yield loadWasm(ctx);
|
|
119
|
+
}
|
|
120
|
+
const result = yield executeMessage(ctx, e.data);
|
|
121
|
+
result.workId = workId;
|
|
122
|
+
return result;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { Worker } from 'worker_threads';
|
|
11
|
+
import { getPortalBaseURL } from '@getpara/core-sdk';
|
|
12
|
+
const CLEAR_WORKER_TIMEOUT_MS = 1000 * 90;
|
|
13
|
+
let worker;
|
|
14
|
+
const resFunctionMap = {};
|
|
15
|
+
function removeWorkId(workId, skipClearTimeout) {
|
|
16
|
+
const { timeoutId } = resFunctionMap[workId];
|
|
17
|
+
delete resFunctionMap[workId];
|
|
18
|
+
if (skipClearTimeout) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
clearTimeout(timeoutId);
|
|
22
|
+
}
|
|
23
|
+
export function setupWorker(ctx, resFunction, workId) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
const timeoutId = setTimeout(() => {
|
|
26
|
+
removeWorkId(workId, true);
|
|
27
|
+
}, CLEAR_WORKER_TIMEOUT_MS);
|
|
28
|
+
resFunctionMap[workId] = {
|
|
29
|
+
fn: resFunction,
|
|
30
|
+
timeoutId,
|
|
31
|
+
};
|
|
32
|
+
if (!worker || !worker.threadId) {
|
|
33
|
+
const workerRes = yield fetch(`${getPortalBaseURL(ctx)}/static/js/mpcWorkerServer-bundle.js`);
|
|
34
|
+
worker = new Worker(yield workerRes.text(), { eval: true });
|
|
35
|
+
const onmessage = (message) => __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
const { workId: messageWorkId } = message;
|
|
37
|
+
delete message.workId;
|
|
38
|
+
yield resFunctionMap[messageWorkId].fn(message);
|
|
39
|
+
removeWorkId(messageWorkId);
|
|
40
|
+
});
|
|
41
|
+
worker.on('message', onmessage);
|
|
42
|
+
worker.on('error', err => {
|
|
43
|
+
throw err;
|
|
44
|
+
});
|
|
45
|
+
worker.on('exit', code => {
|
|
46
|
+
console.error(`worker stopped with exit code ${code}`);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return worker;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { StorageUtils } from '@getpara/core-sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Implements `StorageUtils`
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare class ServerLocalStorage implements StorageUtils {
|
|
7
|
+
private localStorage;
|
|
8
|
+
get: (key: string) => string | null;
|
|
9
|
+
set: (key: string, value: string) => void;
|
|
10
|
+
removeItem: (key: string) => void;
|
|
11
|
+
clear: (prefix: string) => void;
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { StorageUtils } from '@getpara/core-sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Implements `StorageUtils` using a JavaScript object.
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare class ServerSessionStorage implements StorageUtils {
|
|
7
|
+
private sessionStorage;
|
|
8
|
+
get: (key: string) => string | null;
|
|
9
|
+
set: (key: string, value: string) => void;
|
|
10
|
+
removeItem: (key: string) => void;
|
|
11
|
+
clear: (prefix: string) => void;
|
|
12
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Ctx, SignatureRes, PlatformUtils, TPregenIdentifierType, WalletType } from '@getpara/core-sdk';
|
|
3
|
+
import { BackupKitEmailProps } from '@getpara/user-management-client';
|
|
4
|
+
import { ServerLocalStorage } from './ServerLocalStorage.js';
|
|
5
|
+
import { ServerSessionStorage } from './ServerSessionStorage.js';
|
|
6
|
+
export declare class ServerUtils implements PlatformUtils {
|
|
7
|
+
getPrivateKey(ctx: Ctx, userId: string, walletId: string, share: string, sessionCookie: string): Promise<string>;
|
|
8
|
+
keygen(ctx: Ctx, userId: string, type: Exclude<WalletType, WalletType.SOLANA>, secretKey: string | null, sessionCookie: string, emailProps?: BackupKitEmailProps): Promise<{
|
|
9
|
+
signer: string;
|
|
10
|
+
walletId: string;
|
|
11
|
+
}>;
|
|
12
|
+
refresh(_ctx: Ctx, _sessionCookie: string, _userId: string, _walletId: string, _share: string, _oldPartnerId?: string, _newPartnerId?: string): Promise<{
|
|
13
|
+
signer: string;
|
|
14
|
+
}>;
|
|
15
|
+
preKeygen(ctx: Ctx, partnerId: string, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType, type: Exclude<WalletType, WalletType.SOLANA>, secretKey: string | null, // should be acceptable as null in RN as we don't pre-gen them
|
|
16
|
+
sessionCookie: string): Promise<{
|
|
17
|
+
signer: string;
|
|
18
|
+
walletId: string;
|
|
19
|
+
}>;
|
|
20
|
+
signMessage(ctx: Ctx, userId: string, walletId: string, share: string, message: string, sessionCookie: string, isDKLS?: boolean): Promise<SignatureRes>;
|
|
21
|
+
signTransaction(ctx: Ctx, userId: string, walletId: string, share: string, message: string, chainId: string, sessionCookie: string, isDKLS?: boolean): Promise<SignatureRes>;
|
|
22
|
+
sendTransaction(ctx: Ctx, userId: string, walletId: string, share: string, tx: string, chainId: string, sessionCookie: string, isDKLS?: boolean): Promise<SignatureRes>;
|
|
23
|
+
signHash(_address: string, _hash: string): Promise<{
|
|
24
|
+
v: number;
|
|
25
|
+
r: Buffer;
|
|
26
|
+
s: Buffer;
|
|
27
|
+
}>;
|
|
28
|
+
ed25519Keygen(ctx: Ctx, userId: string, sessionCookie: string, emailProps?: BackupKitEmailProps): Promise<{
|
|
29
|
+
signer: string;
|
|
30
|
+
walletId: string;
|
|
31
|
+
}>;
|
|
32
|
+
ed25519PreKeygen(ctx: Ctx, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType, sessionCookie: string): Promise<{
|
|
33
|
+
signer: string;
|
|
34
|
+
walletId: string;
|
|
35
|
+
}>;
|
|
36
|
+
ed25519Sign(ctx: Ctx, userId: string, walletId: string, share: string, base64Bytes: string, sessionCookie: string): Promise<SignatureRes>;
|
|
37
|
+
localStorage: ServerLocalStorage;
|
|
38
|
+
sessionStorage: ServerSessionStorage;
|
|
39
|
+
secureStorage: any;
|
|
40
|
+
isSyncStorage: boolean;
|
|
41
|
+
disableProviderModal: boolean;
|
|
42
|
+
openPopup(_popupUrl: string): Window;
|
|
43
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Ctx, TPregenIdentifierType } from '@getpara/core-sdk';
|
|
2
|
+
import { BackupKitEmailProps, WalletType } from '@getpara/user-management-client';
|
|
3
|
+
export declare function keygen(ctx: Ctx, userId: string, type: WalletType, secretKey: string | null, skipDistribute?: boolean, sessionCookie?: string, _emailProps?: BackupKitEmailProps): Promise<{
|
|
4
|
+
signer: string;
|
|
5
|
+
walletId: string;
|
|
6
|
+
recoveryShare: string | null;
|
|
7
|
+
}>;
|
|
8
|
+
export declare function preKeygen(ctx: Ctx, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType, type: WalletType, secretKey: string | null, _skipDistribute: boolean, partnerId: string, sessionCookie?: string): Promise<{
|
|
9
|
+
signer: string;
|
|
10
|
+
walletId: string;
|
|
11
|
+
recoveryShare: string | null;
|
|
12
|
+
}>;
|
|
13
|
+
export declare function ed25519Keygen(ctx: Ctx, userId: string, sessionCookie?: string, _emailProps?: BackupKitEmailProps): Promise<{
|
|
14
|
+
signer: string;
|
|
15
|
+
walletId: string;
|
|
16
|
+
recoveryShare: string | null;
|
|
17
|
+
}>;
|
|
18
|
+
export declare function ed25519PreKeygen(ctx: Ctx, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType, sessionCookie?: string): Promise<{
|
|
19
|
+
signer: string;
|
|
20
|
+
walletId: string;
|
|
21
|
+
recoveryShare: string | null;
|
|
22
|
+
}>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Ctx, SignatureRes } from '@getpara/core-sdk';
|
|
2
|
+
export declare function signTransaction(ctx: Ctx, userId: string, walletId: string, share: string, tx: string, chainId: string, sessionCookie?: string, isDKLS?: boolean): Promise<SignatureRes>;
|
|
3
|
+
export declare function sendTransaction(ctx: Ctx, userId: string, walletId: string, share: string, tx: string, chainId: string, sessionCookie?: string, isDKLS?: boolean): Promise<SignatureRes>;
|
|
4
|
+
export declare function signMessage(ctx: Ctx, userId: string, walletId: string, share: string, message: string, sessionCookie?: string, isDKLS?: boolean): Promise<SignatureRes>;
|
|
5
|
+
export declare function ed25519Sign(ctx: Ctx, userId: string, walletId: string, share: string, base64Bytes: string, sessionCookie?: string): Promise<SignatureRes>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const globalThisCopy: typeof globalThis;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Ctx, TPregenIdentifierType, SignatureRes, WalletType } from '@getpara/core-sdk';
|
|
2
|
+
export declare function ed25519Keygen(ctx: Ctx, userId: string): Promise<{
|
|
3
|
+
signer: string;
|
|
4
|
+
walletId: string;
|
|
5
|
+
}>;
|
|
6
|
+
export declare function ed25519PreKeygen(ctx: Ctx, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType): Promise<{
|
|
7
|
+
signer: string;
|
|
8
|
+
walletId: string;
|
|
9
|
+
}>;
|
|
10
|
+
export declare function ed25519Sign(ctx: Ctx, share: string, userId: string, walletId: string, base64Bytes: string): Promise<{
|
|
11
|
+
signature: string;
|
|
12
|
+
}>;
|
|
13
|
+
export declare function keygen(ctx: Ctx, userId: string, type: Exclude<WalletType, WalletType.SOLANA>, secretKey: string | null): Promise<{
|
|
14
|
+
signer: string;
|
|
15
|
+
walletId: string;
|
|
16
|
+
}>;
|
|
17
|
+
export declare function preKeygen(ctx: Ctx, partnerId: string, pregenIdentifier: string, pregenIdentifierType: TPregenIdentifierType, type: Exclude<WalletType, WalletType.SOLANA>, secretKey: string | null): Promise<{
|
|
18
|
+
signer: string;
|
|
19
|
+
walletId: string;
|
|
20
|
+
}>;
|
|
21
|
+
export declare function signMessage(ctx: Ctx, share: string, walletId: string, userId: string, message: string): Promise<SignatureRes>;
|
|
22
|
+
export declare function signTransaction(ctx: Ctx, share: string, walletId: string, userId: string, tx: string, chainId: string): Promise<SignatureRes>;
|
|
23
|
+
export declare function sendTransaction(ctx: Ctx, share: string, walletId: string, userId: string, tx: string, chainId: string): Promise<SignatureRes>;
|
|
24
|
+
export declare function refresh(ctx: Ctx, share: string, walletId: string, userId: string): Promise<string>;
|
|
25
|
+
export declare function getPrivateKey(ctx: Ctx, share: string, walletId: string, userId: string): Promise<string>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Environment } from '@getpara/core-sdk';
|
|
2
|
+
interface Message {
|
|
3
|
+
env: Environment;
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
cosmosPrefix?: string;
|
|
6
|
+
offloadMPCComputationURL?: string;
|
|
7
|
+
disableWorkers?: boolean;
|
|
8
|
+
functionType: string;
|
|
9
|
+
params: Record<string, any>;
|
|
10
|
+
sessionCookie?: string;
|
|
11
|
+
useDKLS?: boolean;
|
|
12
|
+
disableWebSockets?: boolean;
|
|
13
|
+
workId: string;
|
|
14
|
+
}
|
|
15
|
+
export declare function handleMessage(e: {
|
|
16
|
+
data: Message;
|
|
17
|
+
}): Promise<any>;
|
|
18
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@getpara/server-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "dist/cjs/index.js",
|
|
5
|
+
"module": "dist/esm/index.js",
|
|
6
|
+
"types": "dist/types/index.d.ts",
|
|
7
|
+
"typings": "dist/types/index.d.ts",
|
|
8
|
+
"sideEffects": [
|
|
9
|
+
"wasm_exec.js"
|
|
10
|
+
],
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@getpara/core-sdk": "0.1.0",
|
|
13
|
+
"@getpara/user-management-client": "0.1.0",
|
|
14
|
+
"uuid": "^9.0.1",
|
|
15
|
+
"ws": "^8.14.2"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "yarn build:cjs && yarn build:esm && yarn build:types",
|
|
19
|
+
"build:cjs": "rm -rf dist/cjs && tsc --module commonjs --outDir dist/cjs && printf '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
|
|
20
|
+
"build:esm": "rm -rf dist/esm && tsc --module es2020 --outDir dist/esm && printf '{\"type\":\"module\",\"sideEffects\":[\"wasm_exec.js\"]}' > dist/esm/package.json",
|
|
21
|
+
"build:types": "rm -rf dist/types && tsc --module es2020 --declarationDir dist/types --emitDeclarationOnly --declaration",
|
|
22
|
+
"test": "vitest run --coverage"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/ws": "^8.5.7",
|
|
26
|
+
"typescript": "5.1.6"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"package.json"
|
|
31
|
+
],
|
|
32
|
+
"gitHead": "625aaa94001a5461dcde8d6775c3b73f3862c76c"
|
|
33
|
+
}
|