forked from LIT-Protocol/js-serverless-function-test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignAndCombine.js
More file actions
241 lines (200 loc) · 8.18 KB
/
signAndCombine.js
File metadata and controls
241 lines (200 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import fs from "fs";
import axios from "axios";
import LitJsSdk from "lit-js-sdk/build/index.node.js";
import { serialize, recoverAddress } from "@ethersproject/transactions";
import {
hexlify,
splitSignature,
hexZeroPad,
joinSignature,
} from "@ethersproject/bytes";
import { recoverPublicKey, computePublicKey } from "@ethersproject/signing-key";
const authSig = {
sig: "0x2bdede6164f56a601fc17a8a78327d28b54e87cf3fa20373fca1d73b804566736d76efe2dd79a4627870a50e66e1a9050ca333b6f98d9415d8bca424980611ca1c",
derivedVia: "web3.eth.personal.sign",
signedMessage:
"localhost wants you to sign in with your Ethereum account:\n0x9D1a5EC58232A894eBFcB5e466E3075b23101B89\n\nThis is a key for Partiful\n\nURI: https://localhost/login\nVersion: 1\nChain ID: 1\nNonce: 1LF00rraLO4f7ZSIt\nIssued At: 2022-06-03T05:59:09.959Z",
address: "0x9D1a5EC58232A894eBFcB5e466E3075b23101B89",
};
// import LitJsSdk from "lit-js-sdk";
const testBlsSigning = async () => {
const bytes = fs.readFileSync("./blsFunctionTest.js");
const encodedJs = bytes.toString("base64");
console.log("encodedJs", encodedJs);
// need pubkeyset for BLS share combination
const handshakeResp = await axios.post(
"http://localhost:7470/web/handshake",
{ clientPublicKey: "test" },
{ headers: { "Content-Type": "application/json" } }
);
// console.log("handshakeResp", handshakeResp.data);
const { networkPublicKeySet } = handshakeResp.data;
// console.log("networkPublicKeySet", networkPublicKeySet);
const pkSetAsBytes = LitJsSdk.uint8arrayFromString(
networkPublicKeySet,
"base16"
);
console.log("pkSetAsBytes", pkSetAsBytes);
const basePort = 7470;
const promises = [];
for (let i = 0; i < 10; i++) {
const url = `http://127.0.0.1:${basePort + i}/web/execute`;
promises.push(axios.post(url, { code: encodedJs, authSig }));
}
Promise.all(promises).then((responses) => {
const signatureShares = responses.map((res) => res.data);
// sort the sig shares by share index. this is important when combining the shares for BLS
signatureShares.sort((a, b) => a.shareIndex - b.shareIndex);
const sigShares = signatureShares.map((s) => ({
shareHex: s.signatureShare,
shareIndex: s.shareIndex,
}));
console.log("sigShares", sigShares);
const signature = LitJsSdk.wasmBlsSdkHelpers.combine_signatures(
pkSetAsBytes,
sigShares
);
// console.log("raw sig", signature);
console.log(
"final signature is ",
LitJsSdk.uint8arrayToString(signature, "base16")
);
});
};
const testEcdsaSigning = async () => {
const bytes = fs.readFileSync("./ecdsaFunctionTest.js");
const encodedJs = bytes.toString("base64");
// console.log("encodedJs", encodedJs);
const basePort = 7470;
const promises = [];
for (let i = 0; i < 10; i++) {
const url = `http://127.0.0.1:${basePort + i}/web/execute`;
promises.push(axios.post(url, { code: encodedJs, authSig }));
}
const responses = await Promise.all(promises);
const responseData = responses.map((res) => res.data);
// console.log("responseData", JSON.stringify(responseData, null, 2));
const sig1Shares = responseData.map((r) => r.signedData.sig1);
// sort the sig shares by share index. this is important when combining the shares for BLS
sig1Shares.sort((a, b) => a.shareIndex - b.shareIndex);
const sigShares = sig1Shares.map((s) => ({
shareHex: s.signatureShare,
shareIndex: s.shareIndex,
localX: s.localX,
localY: s.localY,
publicKey: s.publicKey,
dataSigned: s.dataSigned,
}));
console.log("sigShares", sigShares);
// R_x & R_y values can come from any node (they will be different per node), and will generate a valid signature
const R_x = sigShares[0].localX;
const R_y = sigShares[0].localY;
// the public key can come from any node - it obviously will be identical from each node
const publicKey = sigShares[0].publicKey;
const dataSigned = "0x" + sigShares[0].dataSigned;
const validShares = sigShares.map((s) => s.shareHex);
const shares = JSON.stringify(validShares);
console.log("shares is", shares);
await LitJsSdk.wasmECDSA.initWasmEcdsaSdk(); // init WASM
const sig = JSON.parse(
LitJsSdk.wasmECDSA.combine_signature(R_x, R_y, shares)
);
console.log("signature", sig);
const encodedSig = joinSignature({
r: "0x" + sig.r,
s: "0x" + sig.s,
v: sig.recid, // doesn't work
});
console.log("encodedSig", encodedSig);
console.log("sig length in bytes: ", encodedSig.substring(2).length / 2);
console.log("dataSigned", dataSigned);
const recoveredPubkey = recoverPublicKey(dataSigned, encodedSig);
console.log("uncompressed recoveredPubkey", recoveredPubkey);
const compressedRecoveredPubkey = computePublicKey(recoveredPubkey, true);
console.log("compressed recoveredPubkey", compressedRecoveredPubkey);
const paddedPubkeyFromNode = hexZeroPad("0x" + publicKey, 33);
console.log("padded pubkey from node", paddedPubkeyFromNode);
const recoveredAddress = recoverAddress(dataSigned, encodedSig);
console.log("recoveredAddress", recoveredAddress);
if (paddedPubkeyFromNode === compressedRecoveredPubkey) {
console.log("pubkey recovered correctly");
} else {
console.log("bad recovery!!");
}
};
const testTxnSigning = async () => {
const bytes = fs.readFileSync("./build/signTxnTest.js");
const encodedJs = bytes.toString("base64");
// console.log("encodedJs", encodedJs);
const basePort = 7470;
const promises = [];
const chainId = 137;
for (let i = 0; i < 10; i++) {
const url = `http://127.0.0.1:${basePort + i}/web/execute`;
promises.push(axios.post(url, { code: encodedJs, authSig }));
}
const responses = await Promise.all(promises);
const signatureShares = responses.map((res) => res.data);
// sort the sig shares by share index. this is important when combining the shares for BLS
signatureShares.sort((a, b) => a.shareIndex - b.shareIndex);
const sigShares = signatureShares.map((s) => ({
shareHex: s.signatureShare,
shareIndex: s.shareIndex,
localX: s.localX,
localY: s.localY,
publicKey: s.publicKey,
dataSigned: s.dataSigned,
}));
console.log("sigShares", sigShares);
// R_x & R_y values can come from any node (they will be different per node), and will generate a valid signature
const R_x = sigShares[0].localX;
const R_y = sigShares[0].localY;
const dataSigned = "0x" + sigShares[0].dataSigned;
// the public key can come from any node - it obviously will be identical from each node
const public_key = sigShares[0].publicKey;
const valid_shares = sigShares.map((s) => s.shareHex);
const shares = JSON.stringify(valid_shares);
console.log("shares is", shares);
await LitJsSdk.wasmECDSA.initWasmEcdsaSdk(); // init WASM
const sig = JSON.parse(
LitJsSdk.wasmECDSA.combine_signature(R_x, R_y, shares)
);
console.log("signature", sig);
const encodedSig = joinSignature({
r: "0x" + sig.r,
s: "0x" + sig.s,
v: 1, //sig.recid,
});
console.log("encodedSig", encodedSig);
console.log("sig length in bytes: ", encodedSig.substring(2).length / 2);
console.log("dataSigned", dataSigned);
const splitSig = splitSignature(encodedSig);
console.log("splitSig", splitSig);
const recoveredPubkey = recoverPublicKey(dataSigned, encodedSig);
console.log("uncompressed recoveredPubkey", recoveredPubkey);
const compressedRecoveredPubkey = computePublicKey(recoveredPubkey, true);
console.log("compressed recoveredPubkey", compressedRecoveredPubkey);
const recoveredAddress = recoverAddress(dataSigned, encodedSig);
console.log("recoveredAddress", recoveredAddress);
// const txParams = {
// nonce: "0x0",
// gasPrice: "0x2e90edd000", // 200 gwei
// gasLimit: "0x" + (30000).toString(16), // 30k gas limit should be enough. only need 21k to send.
// to: "0x50e2dac5e78B5905CB09495547452cEE64426db2",
// value: "0x" + (10000).toString(16),
// chainId,
// };
const txParams = {
nonce: "0x0",
gasPrice: "0x2e90edd000",
gasLimit: "0x7530",
to: "0x50e2dac5e78B5905CB09495547452cEE64426db2",
value: "0x2710",
chainId: 137,
};
const txn = serialize(txParams, encodedSig);
console.log("txn", txn);
};
testBlsSigning();
// testEcdsaSigning();
// testTxnSigning();