@getpara/server-sdk 2.0.0-alpha.15 → 2.0.0-alpha.16
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/wallet/keygen.js +214 -128
- package/dist/cjs/wallet/privateKey.js +35 -22
- package/dist/cjs/wallet/signing.js +136 -84
- package/dist/cjs/workers/workerWrapper.js +38 -6
- package/dist/esm/wallet/keygen.js +190 -120
- package/dist/esm/wallet/privateKey.js +35 -22
- package/dist/esm/wallet/signing.js +136 -84
- package/dist/esm/workers/workerWrapper.js +38 -6
- package/dist/types/workers/workerWrapper.d.ts +1 -1
- package/package.json +4 -4
|
@@ -2,29 +2,42 @@ import "../chunk-FTA5RKYX.js";
|
|
|
2
2
|
import * as uuid from "uuid";
|
|
3
3
|
import { setupWorker } from "../workers/workerWrapper.js";
|
|
4
4
|
async function getPrivateKey(ctx, userId, walletId, share, sessionCookie) {
|
|
5
|
-
return
|
|
5
|
+
return new Promise(async (resolve, reject) => {
|
|
6
6
|
const workId = uuid.v4();
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
7
|
+
try {
|
|
8
|
+
const worker = await setupWorker(
|
|
9
|
+
ctx,
|
|
10
|
+
async (res) => {
|
|
11
|
+
resolve(res.privateKey);
|
|
12
|
+
},
|
|
13
|
+
(error) => {
|
|
14
|
+
reject(error);
|
|
15
|
+
},
|
|
16
|
+
workId,
|
|
17
|
+
{
|
|
18
|
+
params: { walletId, userId },
|
|
19
|
+
functionType: "GET_PRIVATE_KEY",
|
|
20
|
+
disableWorkers: ctx.disableWorkers,
|
|
21
|
+
disableWebSockets: ctx.disableWebSockets
|
|
22
|
+
}
|
|
23
|
+
);
|
|
24
|
+
worker.postMessage({
|
|
25
|
+
env: ctx.env,
|
|
26
|
+
apiKey: ctx.apiKey,
|
|
27
|
+
cosmosPrefix: ctx.cosmosPrefix,
|
|
28
|
+
params: { share, walletId, userId },
|
|
29
|
+
functionType: "GET_PRIVATE_KEY",
|
|
30
|
+
offloadMPCComputationURL: ctx.offloadMPCComputationURL,
|
|
31
|
+
disableWorkers: ctx.disableWorkers,
|
|
32
|
+
sessionCookie,
|
|
33
|
+
useDKLS: ctx.useDKLS,
|
|
34
|
+
disableWebSockets: ctx.disableWebSockets,
|
|
35
|
+
wasmOverride: ctx.wasmOverride,
|
|
36
|
+
workId
|
|
37
|
+
});
|
|
38
|
+
} catch (error) {
|
|
39
|
+
reject(error);
|
|
40
|
+
}
|
|
28
41
|
});
|
|
29
42
|
}
|
|
30
43
|
export {
|
|
@@ -2,103 +2,155 @@ import "../chunk-FTA5RKYX.js";
|
|
|
2
2
|
import * as uuid from "uuid";
|
|
3
3
|
import { setupWorker } from "../workers/workerWrapper.js";
|
|
4
4
|
async function signTransaction(ctx, userId, walletId, share, tx, chainId, sessionCookie, isDKLS) {
|
|
5
|
-
return
|
|
5
|
+
return new Promise(async (resolve, reject) => {
|
|
6
6
|
const workId = uuid.v4();
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
7
|
+
try {
|
|
8
|
+
const worker = await setupWorker(
|
|
9
|
+
ctx,
|
|
10
|
+
async (sendTransactionRes) => {
|
|
11
|
+
resolve(sendTransactionRes);
|
|
12
|
+
},
|
|
13
|
+
(error) => {
|
|
14
|
+
reject(error);
|
|
15
|
+
},
|
|
16
|
+
workId,
|
|
17
|
+
{
|
|
18
|
+
params: { walletId, userId, tx, chainId },
|
|
19
|
+
functionType: "SIGN_TRANSACTION",
|
|
20
|
+
disableWorkers: ctx.disableWorkers,
|
|
21
|
+
disableWebSockets: ctx.disableWebSockets
|
|
22
|
+
}
|
|
23
|
+
);
|
|
24
|
+
worker.postMessage({
|
|
25
|
+
env: ctx.env,
|
|
26
|
+
apiKey: ctx.apiKey,
|
|
27
|
+
cosmosPrefix: ctx.cosmosPrefix,
|
|
28
|
+
params: { share, walletId, userId, tx, chainId },
|
|
29
|
+
functionType: "SIGN_TRANSACTION",
|
|
30
|
+
offloadMPCComputationURL: ctx.offloadMPCComputationURL,
|
|
31
|
+
disableWorkers: ctx.disableWorkers,
|
|
32
|
+
sessionCookie,
|
|
33
|
+
useDKLS: isDKLS,
|
|
34
|
+
disableWebSockets: ctx.disableWebSockets,
|
|
35
|
+
wasmOverride: ctx.wasmOverride,
|
|
36
|
+
workId
|
|
37
|
+
});
|
|
38
|
+
} catch (error) {
|
|
39
|
+
reject(error);
|
|
40
|
+
}
|
|
28
41
|
});
|
|
29
42
|
}
|
|
30
43
|
async function sendTransaction(ctx, userId, walletId, share, tx, chainId, sessionCookie, isDKLS) {
|
|
31
|
-
return
|
|
44
|
+
return new Promise(async (resolve, reject) => {
|
|
32
45
|
const workId = uuid.v4();
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
try {
|
|
47
|
+
const worker = await setupWorker(
|
|
48
|
+
ctx,
|
|
49
|
+
async (sendTransactionRes) => {
|
|
50
|
+
resolve(sendTransactionRes);
|
|
51
|
+
},
|
|
52
|
+
(error) => {
|
|
53
|
+
reject(error);
|
|
54
|
+
},
|
|
55
|
+
workId,
|
|
56
|
+
{
|
|
57
|
+
params: { walletId, userId, tx, chainId },
|
|
58
|
+
functionType: "SEND_TRANSACTION",
|
|
59
|
+
disableWorkers: ctx.disableWorkers,
|
|
60
|
+
disableWebSockets: ctx.disableWebSockets
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
worker.postMessage({
|
|
64
|
+
env: ctx.env,
|
|
65
|
+
apiKey: ctx.apiKey,
|
|
66
|
+
params: { share, walletId, userId, tx, chainId },
|
|
67
|
+
functionType: "SEND_TRANSACTION",
|
|
68
|
+
offloadMPCComputationURL: ctx.offloadMPCComputationURL,
|
|
69
|
+
disableWorkers: ctx.disableWorkers,
|
|
70
|
+
sessionCookie,
|
|
71
|
+
useDKLS: isDKLS,
|
|
72
|
+
disableWebSockets: ctx.disableWebSockets,
|
|
73
|
+
wasmOverride: ctx.wasmOverride,
|
|
74
|
+
workId
|
|
75
|
+
});
|
|
76
|
+
} catch (error) {
|
|
77
|
+
reject(error);
|
|
78
|
+
}
|
|
53
79
|
});
|
|
54
80
|
}
|
|
55
81
|
async function signMessage(ctx, userId, walletId, share, message, sessionCookie, isDKLS) {
|
|
56
|
-
return
|
|
82
|
+
return new Promise(async (resolve, reject) => {
|
|
57
83
|
const workId = uuid.v4();
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
84
|
+
try {
|
|
85
|
+
const worker = await setupWorker(
|
|
86
|
+
ctx,
|
|
87
|
+
async (signMessageRes) => {
|
|
88
|
+
resolve(signMessageRes);
|
|
89
|
+
},
|
|
90
|
+
(error) => {
|
|
91
|
+
reject(error);
|
|
92
|
+
},
|
|
93
|
+
workId,
|
|
94
|
+
{
|
|
95
|
+
params: { walletId, userId, message },
|
|
96
|
+
functionType: "SIGN_MESSAGE",
|
|
97
|
+
disableWorkers: ctx.disableWorkers,
|
|
98
|
+
disableWebSockets: ctx.disableWebSockets
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
worker.postMessage({
|
|
102
|
+
env: ctx.env,
|
|
103
|
+
apiKey: ctx.apiKey,
|
|
104
|
+
params: { share, walletId, userId, message },
|
|
105
|
+
functionType: "SIGN_MESSAGE",
|
|
106
|
+
offloadMPCComputationURL: ctx.offloadMPCComputationURL,
|
|
107
|
+
disableWorkers: ctx.disableWorkers,
|
|
108
|
+
sessionCookie,
|
|
109
|
+
useDKLS: isDKLS,
|
|
110
|
+
disableWebSockets: ctx.disableWebSockets,
|
|
111
|
+
wasmOverride: ctx.wasmOverride,
|
|
112
|
+
workId
|
|
113
|
+
});
|
|
114
|
+
} catch (error) {
|
|
115
|
+
reject(error);
|
|
116
|
+
}
|
|
78
117
|
});
|
|
79
118
|
}
|
|
80
119
|
async function ed25519Sign(ctx, userId, walletId, share, base64Bytes, sessionCookie) {
|
|
81
|
-
return
|
|
120
|
+
return new Promise(async (resolve, reject) => {
|
|
82
121
|
const workId = uuid.v4();
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
122
|
+
try {
|
|
123
|
+
const worker = await setupWorker(
|
|
124
|
+
ctx,
|
|
125
|
+
async (signMessageRes) => {
|
|
126
|
+
resolve(signMessageRes);
|
|
127
|
+
},
|
|
128
|
+
(error) => {
|
|
129
|
+
reject(error);
|
|
130
|
+
},
|
|
131
|
+
workId,
|
|
132
|
+
{
|
|
133
|
+
params: { walletId, userId, base64Bytes },
|
|
134
|
+
functionType: "ED25519_SIGN",
|
|
135
|
+
disableWorkers: ctx.disableWorkers,
|
|
136
|
+
disableWebSockets: ctx.disableWebSockets
|
|
137
|
+
}
|
|
138
|
+
);
|
|
139
|
+
worker.postMessage({
|
|
140
|
+
env: ctx.env,
|
|
141
|
+
apiKey: ctx.apiKey,
|
|
142
|
+
params: { share, walletId, userId, base64Bytes },
|
|
143
|
+
functionType: "ED25519_SIGN",
|
|
144
|
+
offloadMPCComputationURL: ctx.offloadMPCComputationURL,
|
|
145
|
+
disableWorkers: ctx.disableWorkers,
|
|
146
|
+
sessionCookie,
|
|
147
|
+
disableWebSockets: ctx.disableWebSockets,
|
|
148
|
+
wasmOverride: ctx.wasmOverride,
|
|
149
|
+
workId
|
|
150
|
+
});
|
|
151
|
+
} catch (error) {
|
|
152
|
+
reject(error);
|
|
153
|
+
}
|
|
102
154
|
});
|
|
103
155
|
}
|
|
104
156
|
export {
|
|
@@ -12,29 +12,61 @@ function removeWorkId(workId, skipClearTimeout) {
|
|
|
12
12
|
}
|
|
13
13
|
clearTimeout(timeoutId);
|
|
14
14
|
}
|
|
15
|
-
async function setupWorker(ctx, resFunction, workId) {
|
|
15
|
+
async function setupWorker(ctx, resFunction, errorFunction, workId, errorContext) {
|
|
16
16
|
const timeoutId = setTimeout(() => {
|
|
17
|
-
|
|
17
|
+
if (resFunctionMap[workId]) {
|
|
18
|
+
const errorMsg = `worker operation timed out after ${CLEAR_WORKER_TIMEOUT_MS}ms for workId ${workId} and opts ${JSON.stringify(resFunctionMap[workId].errorContext)}`;
|
|
19
|
+
resFunctionMap[workId].errorFn(new Error(errorMsg));
|
|
20
|
+
removeWorkId(workId, true);
|
|
21
|
+
}
|
|
18
22
|
}, CLEAR_WORKER_TIMEOUT_MS);
|
|
19
23
|
resFunctionMap[workId] = {
|
|
20
24
|
fn: resFunction,
|
|
21
|
-
|
|
25
|
+
errorFn: errorFunction,
|
|
26
|
+
timeoutId,
|
|
27
|
+
errorContext
|
|
22
28
|
};
|
|
23
29
|
if (!worker || !worker.threadId) {
|
|
24
30
|
const workerRes = await fetch(`${getPortalBaseURL(ctx)}/static/js/mpcWorkerServer-bundle.js`);
|
|
25
31
|
worker = new Worker(await workerRes.text(), { eval: true });
|
|
26
32
|
const onmessage = async (message) => {
|
|
27
33
|
const { workId: messageWorkId } = message;
|
|
34
|
+
if (!resFunctionMap[messageWorkId]) {
|
|
35
|
+
console.warn(`received message for unknown workId: ${messageWorkId}`);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
28
38
|
delete message.workId;
|
|
29
|
-
|
|
30
|
-
|
|
39
|
+
try {
|
|
40
|
+
await resFunctionMap[messageWorkId].fn(message);
|
|
41
|
+
removeWorkId(messageWorkId);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.error(`error in worker message handler for workId ${messageWorkId}:`, error);
|
|
44
|
+
if (resFunctionMap[messageWorkId]) {
|
|
45
|
+
resFunctionMap[messageWorkId].errorFn(error);
|
|
46
|
+
removeWorkId(messageWorkId);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
31
49
|
};
|
|
32
50
|
worker.on("message", onmessage);
|
|
33
51
|
worker.on("error", (err) => {
|
|
34
|
-
|
|
52
|
+
console.error("worker error:", err);
|
|
53
|
+
Object.keys(resFunctionMap).forEach((id) => {
|
|
54
|
+
if (resFunctionMap[id]) {
|
|
55
|
+
const errorMsg = `worker error with workId ${id} and opts ${JSON.stringify(resFunctionMap[id].errorContext)}: ${err.message}`;
|
|
56
|
+
resFunctionMap[id].errorFn(new Error(errorMsg));
|
|
57
|
+
removeWorkId(id);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
35
60
|
});
|
|
36
61
|
worker.on("exit", (code) => {
|
|
37
62
|
console.error(`worker stopped with exit code ${code}`);
|
|
63
|
+
Object.keys(resFunctionMap).forEach((id) => {
|
|
64
|
+
if (resFunctionMap[id]) {
|
|
65
|
+
resFunctionMap[id].errorFn(new Error(`worker exited unexpectedly with code ${code}`));
|
|
66
|
+
removeWorkId(id);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
worker = void 0;
|
|
38
70
|
});
|
|
39
71
|
}
|
|
40
72
|
return worker;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Worker } from 'worker_threads';
|
|
3
3
|
import { Ctx } from '@getpara/core-sdk';
|
|
4
|
-
export declare function setupWorker(ctx: Ctx, resFunction: (arg: any) => Promise<void>, workId: string): Promise<Worker>;
|
|
4
|
+
export declare function setupWorker(ctx: Ctx, resFunction: (arg: any) => Promise<void>, errorFunction: (error: Error) => void, workId: string, errorContext: object): Promise<Worker>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@getpara/server-sdk",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.16",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"types": "dist/types/index.d.ts",
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"wasm_exec.js"
|
|
10
10
|
],
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@getpara/core-sdk": "2.0.0-alpha.
|
|
13
|
-
"@getpara/user-management-client": "2.0.0-alpha.
|
|
12
|
+
"@getpara/core-sdk": "2.0.0-alpha.16",
|
|
13
|
+
"@getpara/user-management-client": "2.0.0-alpha.16",
|
|
14
14
|
"@sentry/node": "^9.1.0",
|
|
15
15
|
"uuid": "^11.1.0",
|
|
16
16
|
"ws": "^8.14.2"
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"dist",
|
|
32
32
|
"package.json"
|
|
33
33
|
],
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "84eb1a14fa8918c449ae4b163fe173d34688cdcd"
|
|
35
35
|
}
|