This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
cli.js
194 lines (172 loc) · 4.2 KB
/
cli.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
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
const { table, getBorderCharacters } = require('table')
const meow = require('meow')
const { toChecksumAddress } = require('ethereum-checksum-address')
const HDWallet = require('./index')
const cli = meow(`
Usage
$ ethereum_hdwallet [options]
Options
-i, --index Account Index (e.g. 4)
-c, --columns Columns to display (e.g. address, publickey, privatekey, hdpath)
-r, --range Account Index Range (e.g 1-100)
-m, --mnemonic Mnemonic
-s, --seed Seed in hex format
-p, --hdpath HD Path
Examples
$ ethereum_hdwallet -m "tag volcano eight thank tide danger coast health ab
ove argue embrace heavy" -r 0-10
`, {
string: [
'property',
'range',
'mnemonic',
'seed',
'hdpath'
],
number: [
'index'
],
alias: {
i: 'index',
c: 'columns',
r: 'range',
m: 'mnemonic',
s: 'seed',
p: 'hdpath'
}
}
)
const args = process.argv
const { flags, input } = cli
const options = {
mnemonic: flags.mnemonic || flags.m || input[0],
seed: flags.seed || flags.s,
hdpath: flags.hdpath || flags.p || HDWallet.DefaultHDPath,
index: flags.index || flags.i,
range: flags.range || flags.r,
columns: flags.columns || flags.c
}
if (process.stdin) {
process.stdin.setEncoding('utf8')
process.stdin.resume()
var content = ''
process.stdin.on('data', (buf) => {
content += buf.toString()
})
setTimeout(() => {
options.mnemonic = (content || options.mnemonic || '').trim()
run(options)
process.exit(0)
}, 10)
} else {
run(options)
}
function run ({ mnemonic, seed, index, range, hdpath, columns }) {
if (!mnemonic && !seed) {
console.error('Error: mnemonic or seed is required')
return
}
const hdwallet = (seed ? HDWallet.fromSeed(Buffer.from(seed, 'hex')) : HDWallet.fromMnemonic(mnemonic)).derive(hdpath)
var start = 0
var end = 10
if (index != undefined) {
start = index
if (start < 0) {
start = 0
}
end = start + 1
if (end < 0) {
end = start + 1
}
if (start > end) {
end = start + 1
}
if (end < start) {
end = start + 1
}
} else if (range) {
const parts = range.split('-')
start = (parts[0] | 0)
end = (parts[1] | 0) + 1
if (start < 0) {
start = 0
}
if (end < 0) {
end = start + 1
}
if (start > end) {
end = start + 1
}
if (end < start) {
end = start + 10
}
if (start > end) {
start = 0
}
}
const columnsList = (columns || 'address').split(',').map(x => x.trim().toLowerCase())
const headerKeys = {
address: 'address',
private: 'private key',
public: 'public key',
hdpath: 'hd path'
}
const props = []
for (var i = 0; i < columnsList.length; i++) {
const property = columnsList[i]
var prop = null
if (/address|(public.*address)|addr/.test(property)) {
prop = 'address'
} else if (/private|priv/.test(property)) {
prop = 'private'
} else if (/public|pub/.test(property)) {
prop = 'public'
} else if (/hdpath|hd/.test(property)) {
prop = 'hdpath'
}
if (prop && props.indexOf(prop) === -1) {
props.push(prop)
}
}
const headers = []
for (var i = 0; i < props.length; i++) {
headers.push(headerKeys[props[i]])
}
const result = [['account', ...headers]]
for (var i = start; i < end; i++) {
const wallet = hdwallet.derive(i)
const values = []
for (var j = 0; j < props.length; j++) {
const prop = props[j]
var value = null
if (prop === 'address') {
value = toChecksumAddress('0x' + wallet.getAddress().toString('hex'))
} else if (prop === 'private') {
value = wallet.getPrivateKey().toString('hex')
} else if (prop === 'public') {
value = wallet.getPublicKey().toString('hex')
} else if (prop === 'hdpath') {
value = wallet.hdpath()
}
if (value) {
values.push(value)
}
}
result.push([
i,
...values
])
}
console.log(table(result, {
border: getBorderCharacters('void'),
columnDefault: {
paddingTop: 0,
paddingBottom: 0,
paddingLeft: 0,
paddingRight: 1
},
drawHorizontalLine: () => {
return false
}
}))
}