-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·74 lines (61 loc) · 2.03 KB
/
index.js
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
#!/usr/bin/env node
import {toBufferBE} from "bigint-buffer";
import {digest} from "@chainsafe/as-sha256";
import bls from "@chainsafe/bls";
import {bytesToBigInt, intToBytes} from "@lodestar/utils";
import { Keystore } from "@chainsafe/bls-keystore";
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import fs from 'fs'
yargs(hideBin(process.argv))
.command({
command: 'create',
describe: 'generates interop keystore files',
builder: {
count: {
describe: 'number of interop keystore files to generate',
demandOption: true,
type: 'number'
},
password: {
describe: 'password to encrypt the keystore file',
demandOption: true,
type: 'string'
},
out: {
describe: 'directory to generate the keystore files. if none is given, it is generated inside ./keystores',
demandOption: false,
type: 'string'
}
},
handler(argv) {
processCommand(argv);
}
})
.demandCommand(1)
.argv
function getCurveOrder() {
let curveOrder;
if (!curveOrder) {
curveOrder = BigInt("52435875175126190479447740508185965837690552500527637822603658699938581184513");
}
return curveOrder;
}
function interopSecretKey(index) {
const CURVE_ORDER = getCurveOrder();
return toBufferBE(bytesToBigInt(digest(intToBytes(index, 32))) % CURVE_ORDER, 32);
}
async function processCommand(argv) {
const count = argv.count;
const password = argv.password;
const destinationDir = argv.out || "./keystores"
if (!fs.existsSync(destinationDir)){
fs.mkdirSync(destinationDir, { recursive: true });
}
for (const index of Array(count).keys()) {
const sk = interopSecretKey(index);
const pk = bls.SecretKey.fromBytes(sk).toPublicKey().toBytes();
const keystore = await Keystore.create(password, sk, pk, "");
fs.writeFileSync(`${destinationDir}/keystore_interop_${index}.json`, keystore.stringify());
}
}