-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1_manage_keys.ts
130 lines (122 loc) · 4.47 KB
/
1_manage_keys.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
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
import * as fs from "fs";
import * as path from "path";
import * as promptly from "promptly";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import * as keystore from "./keystore";
async function addKey(keyName: string, keyDir: string, coinType: number, prefix: string) {
if (!fs.existsSync(keyDir)) {
fs.mkdirSync(keyDir, { recursive: true });
}
const mnemonic = await promptly.prompt("Enter BIP-39 seed phrase:");
const password = await promptly.password("Enter a password to encrypt the key:");
const repeat = await promptly.password("Repeat the password:");
if (password != repeat) {
throw new Error("Passwords don't match!");
}
const accAddress = keystore.save(keyName, keyDir, mnemonic, coinType, password, prefix);
console.log("Success! Address:", accAddress);
}
function listKeys(keyDir: string) {
fs.readdirSync(keyDir)
.filter((fn) => {
return fn.endsWith(".json");
})
.sort()
.forEach((fn) => {
const entity: keystore.Entity = JSON.parse(fs.readFileSync(path.join(keyDir, fn), "utf8"));
console.log(`- name: ${entity.name}`);
console.log(` address: ${entity.address}`);
});
}
function removeKey(keyName: string, keyDir: string) {
keystore.remove(keyName, keyDir);
console.log("Success!");
}
// export NODE_OPTIONS=--openssl-legacy-provider
// ts-node 1_manage_keys.ts add mainnet-juno --key-dir keys --coin-type 118 --prefix juno
// ts-node 1_manage_keys.ts add testnet-migaloo --key-dir keys --coin-type 118 --prefix migaloo
// ts-node 1_manage_keys.ts add mainnet-migaloo --key-dir keys --coin-type 118 --prefix migaloo
// ts-node 1_manage_keys.ts add mainnet-kujira --key-dir keys --coin-type 118 --prefix kujira
// ts-node 1_manage_keys.ts add testnet-osmosis --key-dir keys --coin-type 118 --prefix osmo
// ts-node 1_manage_keys.ts add mainnet-osmosis --key-dir keys --coin-type 118 --prefix osmo
// ts-node 1_manage_keys.ts add mainnet-chihuahua --key-dir keys --prefix chihuahua
// ts-node 1_manage_keys.ts add mainnet --key-dir keys --prefix terra
// ts-node 1_manage_keys.ts add mainnet-injective --key-dir keys --coin-type 60 --prefix inj
// ts-node 1_manage_keys.ts add mainnet-archway --key-dir keys --coin-type 118 --prefix archway
// ts-node 1_manage_keys.ts add key-mainnet --key-dir keys --coin-type 118 --prefix archway
// ts-node 1_manage_keys.ts add key-test --key-dir keys --coin-type 118 --prefix cosmos
// ts-node 1_manage_keys.ts add key-neutron --key-dir keys --coin-type 118 --prefix neutron
// ts-node 1_manage_keys.ts test
yargs(hideBin(process.argv))
// .command(
// "test",
// "",
// () => ({}),
// () => test()
// )
.command(
"add <key>",
"Add a key with the given name",
(yargs) => {
return yargs
.positional("key", {
type: "string",
describe: "name of the key",
demandOption: true,
})
.option("key-dir", {
type: "string",
describe: "path to the directory where encrypted key files are stored",
demandOption: false,
default: keystore.DEFAULT_KEY_DIR,
})
.option("coin-type", {
type: "number",
describe: "SLIP-0044 coin type for use in derivation of the private key",
demandOption: false,
default: 330, // Terra = 330, Cosmos = 118
})
.option("prefix", {
type: "string",
describe: "Prefix for address",
demandOption: false,
default: "terra", // Terra = 330, Cosmos = 118
});
},
(argv) => addKey(argv["key"], argv["key-dir"], argv["coin-type"], argv["prefix"]).catch(console.log)
)
.command(
"rm <key>",
"Remove a key of the given name",
(yargs) => {
return yargs
.positional("key", {
type: "string",
describe: "name of the key",
demandOption: true,
})
.option("key-dir", {
type: "string",
describe: "path to the directory where encrypted key files are stored",
demandOption: false,
default: keystore.DEFAULT_KEY_DIR,
});
},
(argv) => removeKey(argv["key"], argv["key-dir"])
)
.command(
"ls",
"List all keys",
(yargs) => {
return yargs.option("key-dir", {
type: "string",
describe: "path to the directory where encrypted key files are stored",
demandOption: false,
default: keystore.DEFAULT_KEY_DIR,
});
},
(argv) => listKeys(argv["key-dir"])
)
.wrap(100)
.parse();