forked from ethereumjs/helpeth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpeth
executable file
·437 lines (369 loc) · 12.1 KB
/
helpeth
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env node
var fs = require('fs')
var prompt = require('prompt-sync')()
var ethWallet = require('ethereumjs-wallet')
var ethICAP = require('ethereumjs-icap')
var ethUnits = require('ethereumjs-units')
var ethUtil = require('ethereumjs-util')
var ethTx = require('ethereumjs-tx')
var fakeEthTx = require('ethereumjs-tx/fake')
var BN = ethUtil.BN
var ethHDKey = require('ethereumjs-wallet/hdkey')
var bip39 = require('bip39')
var RLP = require('rlp')
var yargs = require('yargs')
.usage('Usage: $0 [command]')
.option('p', {
alias: 'private',
describe: 'Private key as a hex string',
type: 'string'
})
.option('password', {
describe: 'Password for the private key',
type: 'string'
})
.option('password-prompt', {
describe: 'Prompt for the private key password',
type: 'boolean'
})
.option('k', {
alias: 'keyfile',
describe: 'Encoded key file',
type: 'string',
normalize: true
})
.option('show-private', {
describe: 'Show private key details',
type: 'boolean'
})
.option('mnemonic', {
describe: 'Mnemonic for HD key derivation',
type: 'string'
})
.global([ 'p', 'k', 'password', 'password-prompt', 'show-private' ])
.command('signMessage <message>', 'Sign a message')
.command('verifySig <hash> <sig>', 'Verify signature')
.command('verifySigParams <hash> <r> <s> <v>', 'Verify signature parameters')
.command('createTx <nonce> <to> <value> <data> <gasLimit> <gasPrice>', 'Sign a transaction')
.command('assembleTx <nonce> <to> <value> <data> <gasLimit> <gasPrice> <v> <r> <s>', 'Assemble a transaction from its components')
.command('parseTx <tx>', 'Parse raw transaction')
.command('keyGenerate [format] [icapdirect]', 'Generate new key')
.command('keyConvert', 'Convert a key to V3 keystore format')
.command('keyDetails', 'Print key details')
.command('vanityKeyGenerate <pattern>', 'Generate vanity key')
.command('bip32Details <path>', 'Print key details for a given path')
.command('addressDetails <address>', 'Print details about an address')
.command('unitConvert <value> <from> <to>', 'Convert between Ethereum units')
.command('rlpInspect <rlp>', 'Inspect RLP encoded data')
.command('keccak256 <data>', 'Create Keccak-256 hash of the supplied data')
.strict()
.version()
.showHelpOnFail(false, 'Specify --help for available options')
.help()
.demand(1, 'Must provide a command')
var argv = yargs.argv
var command = argv._[0]
function abort (msg) {
console.log(msg || 'Error occured')
process.exit(1)
}
function ensurePrivateKey () {
if (!argv.p && !argv.k) {
abort('A private key is mandatory for this operation')
}
}
function askPassword () {
return prompt('Password: ', { echo: '*' })
}
function ensurePassword () {
if (argv.password) {
return argv.password
} else if (argv.passwordPrompt) {
var password = askPassword()
if (!password) {
abort('A password is mandatory for this operation')
} else {
return password
}
} else {
abort('A password is mandatory for this operation')
}
}
function loadWallet () {
ensurePrivateKey()
var wallet
if (argv.private) {
try {
wallet = ethWallet.fromExtendedPrivateKey(argv.private)
} catch (e) {
wallet = ethWallet.fromPrivateKey(ethUtil.toBuffer(argv.private))
}
} else if (argv.keyfile) {
var keyfile = fs.readFileSync(argv.keyfile).toString()
var password = ensurePassword()
wallet = ethWallet.fromV3(keyfile, password, true)
}
return wallet
}
function printAddressDetails (address) {
console.log('Address: ' + address)
console.log('Address (checksum): ' + ethUtil.toChecksumAddress(address))
console.log('ICAP: ' + ethICAP.fromAddress(address, true, true))
}
function printWalletDetails (wallet, showPrivate) {
printAddressDetails(wallet.getAddressString())
console.log('Public key: ' + wallet.getPublicKeyString())
if (argv.showPrivate || showPrivate) {
console.log('Private key: ' + wallet.getPrivateKeyString())
}
}
function saveWalletV3 (wallet) {
var password = ensurePassword()
var v3 = wallet.toV3String(password)
var filename = wallet.getV3Filename()
fs.writeFileSync(filename, Buffer.from(v3))
console.log('Key saved as ' + filename)
}
function printMaxCost (tx) {
var value = new BN(tx.value, 16)
var gasLimit = new BN(tx.gasLimit, 10)
var gasPrice = new BN(tx.gasPrice, 10)
var cost = gasPrice.mul(gasLimit)
console.log('Potential total transaction cost: ' + ethUnits.convert(cost, 'wei', 'eth') + ' ETH')
console.log('Minimum required account balance: ' + ethUnits.convert(value.add(cost), 'wei', 'eth') + ' ETH')
}
const N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16)
function printSigDetails (hash, r, s, v) {
console.log('Message hash (keccak): ' + hash)
var pubKey = ethUtil.ecrecover(
ethUtil.toBuffer(hash),
v,
ethUtil.toBuffer(r),
ethUtil.toBuffer(s)
)
if (new BN(s).cmp(N_DIV_2) === 1) {
console.log('Invalid signature on Homestead and later versions.')
}
console.log('Signed by:')
printAddressDetails(ethUtil.bufferToHex(ethUtil.publicToAddress(pubKey)))
}
function convertValue (value) {
if (/^\d+ \w+$/i.test(value)) {
return ethUnits.lazyConvert(value, 'wei').split(' ')[0]
} else {
return value
}
}
function shadyparser (input) {
if (ethUtil.isHexPrefixed(input)) {
return input
} else {
return '0x' + new BN(input, 10).toString(16)
}
}
var wallet
switch (command) {
case 'keyGenerate':
case 'vanityKeyGenerate':
if (command === 'vanityKeyGenerate') {
wallet = ethWallet.generateVanityAddress(argv.pattern)
} else {
wallet = ethWallet.generate(argv.icapdirect === 'true')
}
switch (argv.format) {
case 'v3':
printWalletDetails(wallet)
saveWalletV3(wallet)
break
case 'raw':
default:
printWalletDetails(wallet, true)
break
}
break
case 'keyDetails':
wallet = loadWallet()
printWalletDetails(wallet)
break
case 'keyConvert':
wallet = loadWallet()
saveWalletV3(wallet)
break
case 'signMessage':
wallet = loadWallet()
// NOTE: geth internally accepts any input and zero-pads it
var msg = argv.message
try {
msg = fs.readFileSync(msg)
} catch (e) {
msg = ethUtil.toBuffer(argv.message)
}
console.log('Input message: ' + ethUtil.bufferToHex(msg))
msg = ethUtil.keccak256(msg)
console.log('Message hash (Keccak): ' + ethUtil.bufferToHex(msg))
var sig = ethUtil.ecsign(msg, wallet.getPrivateKey())
// geth (and the RPC eth_sign method) uses the 65 byte format used by Bitcoin
// FIXME: this might change in the future - https://github.com/ethereum/go-ethereum/issues/2053
sig = ethUtil.bufferToHex(Buffer.concat([ sig.r, sig.s, ethUtil.toBuffer(sig.v - 27) ]))
console.log('The signature: ' + sig)
break
case 'verifySig':
var sig = ethUtil.toBuffer(argv.sig)
var v = parseInt(sig[64])
// support both versions of `eth_sign` responses
if (v < 27) {
v += 27
}
printSigDetails(argv.hash, sig.slice(0, 32), sig.slice(32, 64), v)
break
case 'verifySigParams':
printSigDetails(argv.hash, argv.r, argv.s, parseInt(argv.v))
break
case 'createTx':
// FIXME: validate inputs
var nonce = shadyparser(argv.nonce)
var to = shadyparser(argv.to)
var value = shadyparser(convertValue(argv.value))
var data = shadyparser(argv.data)
var gasLimit = shadyparser(argv.gasLimit)
var gasPrice = shadyparser(convertValue(argv.gasPrice))
console.log('Nonce: ' + nonce)
console.log('To: ' + to)
console.log('Value: ' + value)
console.log('Data: ' + data)
console.log('Gas limit: ' + gasLimit)
console.log('Gas price: ' + gasPrice)
/* eslint-disable new-cap */
var tx = new ethTx({
nonce: nonce,
to: to,
value: value,
data: data,
gasLimit: gasLimit,
gasPrice: gasPrice
})
/* eslint-enable new-cap */
wallet = loadWallet()
tx.sign(wallet.getPrivateKey())
printMaxCost(tx)
console.log('The signed transaction: ' + ethUtil.bufferToHex(tx.serialize()))
break
case 'assembleTx':
// FIXME: validate inputs
var nonce = shadyparser(argv.nonce)
var to = shadyparser(argv.to)
var value = shadyparser(convertValue(argv.value))
var data = shadyparser(argv.data)
var gasLimit = shadyparser(argv.gasLimit)
var gasPrice = shadyparser(convertValue(argv.gasPrice))
var v = shadyparser(argv.v)
var r = shadyparser(argv.r)
var s = shadyparser(argv.s)
console.log('Nonce: ' + nonce)
console.log('To: ' + to)
console.log('Value: ' + value)
console.log('Data: ' + data)
console.log('Gas limit: ' + gasLimit)
console.log('Gas price: ' + gasPrice)
console.log('Signature v: ' + v)
console.log('Signature r: ' + r)
console.log('Signature s: ' + s)
/* eslint-disable new-cap */
var tx = new fakeEthTx({
nonce: nonce,
to: to,
value: value,
data: data,
gasLimit: gasLimit,
gasPrice: gasPrice,
v: v,
r: r,
s: s
})
/* eslint-enable new-cap */
printMaxCost(tx)
console.log('The signed transaction: ' + ethUtil.bufferToHex(tx.serialize()))
break
case 'parseTx':
/* eslint-disable new-cap */
var tx = new fakeEthTx(argv.tx)
var value = new BN(tx.value).toString(10)
var gasLimit = new BN(tx.gasLimit).toString(10)
var gasPrice = new BN(tx.gasPrice).toString(10)
/* eslint-enable new-cap */
console.log('Signed by: ' + ethUtil.bufferToHex(tx.from))
console.log('Nonce: ' + ethUtil.bufferToHex(tx.nonce))
console.log('To: ' + ethUtil.bufferToHex(tx.to))
console.log('Value: ' + value + ' (' + ethUnits.convert(value, 'wei', 'eth') + ' ETH)')
console.log('Data: ' + ethUtil.bufferToHex(tx.data))
console.log('Gas limit: ' + gasLimit)
console.log('Gas price: ' + gasPrice + ' (' + ethUnits.convert(gasPrice, 'wei', 'gwei') + ' Gwei)')
printMaxCost(tx)
break
case 'addressDetails':
var address = argv.address
if (ethICAP.isICAP(address)) {
try {
address = ethICAP.toAddress(address)
} catch (e) {
abort('The address supplied is an Indirect ICAP')
}
} else if (!ethUtil.isValidAddress(address)) {
abort('Invalid address')
} else {
address = address.toLowerCase()
if ((address !== argv.address) && !ethUtil.isValidChecksumAddress(argv.address)) {
console.warn('The supplied address failed the checksum test. It might be invalid.')
}
}
printAddressDetails(address)
break
case 'unitConvert':
var ret = ethUnits.convert(argv.value, argv.from, argv.to)
console.log(argv.value + ' ' + argv.from + ' equals to ' + ret + ' ' + argv.to)
break
case 'bip32Details':
var hdkey
if (argv.mnemonic)
hdkey = ethHDKey.fromMasterSeed(bip39.mnemonicToSeed(argv.mnemonic))
else if (argv.private)
hdkey = ethHDKey.fromExtendedKey(argv.private)
else
abort('A mnemonic or extended key is mandatory for this operation')
var wallet = ethWallet.fromExtendedPrivateKey(hdkey.derivePath(argv.path).privateExtendedKey())
console.log('Path: ' + argv.path)
printWalletDetails(wallet)
break
case 'rlpInspect':
function toReadable (val) {
var len = val.length
if (len === 20) {
return 'possibly an address'
}
if (len <= 32) {
return new ethUtil.BN(val).toString(10)
}
}
var rlp = RLP.decode(argv.rlp)
console.log('The RLP encoded input consists of the following fields:')
for (var i = 0; i < rlp.length; i++) {
var item = rlp[i]
var detail = toReadable(item)
if (detail) {
console.log(' ' + ethUtil.bufferToHex(item) + ' (' + detail + ')')
} else {
console.log(' ' + ethUtil.bufferToHex(item))
}
}
break
case 'keccak256':
var data
try {
data = fs.readFileSync(argv.data)
} catch (e) {
data = ethUtil.toBuffer(argv.data)
}
console.log('Input data: ' + ethUtil.bufferToHex(data))
console.log('Data hash: ' + ethUtil.keccak256(data).toString('hex'))
break
}