-
Notifications
You must be signed in to change notification settings - Fork 1
/
decryptKeystore.ts
46 lines (39 loc) · 1.53 KB
/
decryptKeystore.ts
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
import fs from "fs";
import { task } from "hardhat/config";
import _ from "lodash";
import * as _path from "path";
import readlineSync from "readline-sync";
import { isKIP3Json, splitKeystoreKIP3 } from "@klaytn/js-ext-core";
import "../type-extensions";
export const TASK_DECRYPT_KEYSTORE = "decrypt-keystore";
task(TASK_DECRYPT_KEYSTORE, "Decrypt a keystore.json and print the private key")
.addPositionalParam("file", "Keystore file")
.addOptionalParam("password", "Keystore password", undefined)
.addFlag("json", "Print output in json")
.setAction(async (taskArgs) => {
let { file, password, json } = taskArgs;
const keystore = fs.readFileSync(file).toString();
let keystores: string[];
if (isKIP3Json(keystore)) {
keystores = splitKeystoreKIP3(keystore);
} else {
keystores = [keystore];
}
if (password === undefined) {
password = readlineSync.question("Keystore password: ", { hideEchoBack: true });
}
const wallets = keystores.map((keystore) => {
return hre.ethers.Wallet.fromEncryptedJsonSync(keystore, password);
});
if (json) {
const combined = wallets.map((wallet) => {
return { address: wallet.address, privateKey: wallet.privateKey };
});
console.log(JSON.stringify(combined, null, 2));
} else {
console.log(" address private key");
wallets.forEach((wallet, idx) => {
console.log(idx.toString().padStart(3, ' '), wallet.address,wallet.privateKey.substring(2));
});
}
});