|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const bip39 = require('bip39') |
| 4 | +const EthereumTx = require('ethereumjs-tx') |
| 5 | +const ethereumUtil = require('ethereumjs-util') |
| 6 | +const ethereumUnits = require('ethereumjs-units') |
| 7 | +const bitcoinjs = require('bitcoinjs-lib') |
| 8 | +const prompt = require('prompt') |
| 9 | +const yargs = require('yargs') |
| 10 | + |
| 11 | +var decimalToHex = function (number) { |
| 12 | + if (typeof number === 'string') { |
| 13 | + number = parseInt(number) |
| 14 | + } |
| 15 | + |
| 16 | + if (number < 0) { |
| 17 | + return process.exit(1) |
| 18 | + } |
| 19 | + |
| 20 | + return ethereumUtil.addHexPrefix(number.toString(16)) |
| 21 | +} |
| 22 | + |
| 23 | +var buildTx = function (mnemonic, argv) { |
| 24 | + mnemonic = mnemonic.trim() |
| 25 | + |
| 26 | + if (!bip39.validateMnemonic(mnemonic)) { |
| 27 | + console.warn('your mnenomic phrase could not been validated.') |
| 28 | + process.exit(1) |
| 29 | + } |
| 30 | + |
| 31 | + const bip32RootKey = bitcoinjs.HDNode.fromSeedBuffer(bip39.mnemonicToSeed(mnemonic)) |
| 32 | + |
| 33 | + const child = bip32RootKey.derivePath(argv.path) |
| 34 | + |
| 35 | + const privateKey = child.keyPair.d.toBuffer() |
| 36 | + const senderAddress = ethereumUtil.toChecksumAddress(ethereumUtil.privateToAddress(privateKey).toString('hex')) |
| 37 | + |
| 38 | + if (senderAddress !== ethereumUtil.toChecksumAddress(argv.from)) { |
| 39 | + console.warn('your derivation path does not give the same address as you have specified.') |
| 40 | + console.warn('Your Address:', argv.from) |
| 41 | + console.warn('Derived Address:', senderAddress) |
| 42 | + process.exit(1) |
| 43 | + } |
| 44 | + |
| 45 | + console.log('') |
| 46 | + console.log('') |
| 47 | + console.log('Sender: ', senderAddress) |
| 48 | + console.log('Path: ' + argv.path) |
| 49 | + console.log('') |
| 50 | + |
| 51 | + const tx = new EthereumTx({ |
| 52 | + value: decimalToHex(ethereumUnits.convert(argv.value, 'ether', 'wei')), |
| 53 | + nonce: decimalToHex(argv.nonce), |
| 54 | + gasPrice: decimalToHex(argv.gasPrice), |
| 55 | + gasLimit: decimalToHex(argv.gasLimit), |
| 56 | + data: argv.data.toString(16), |
| 57 | + to: argv.to |
| 58 | + }, 1) |
| 59 | + |
| 60 | + const fee = tx.getUpfrontCost() |
| 61 | + console.log('Minimal Account Balance:', ethereumUnits.convert(fee.toString(), 'wei', 'eth'), 'ETH') |
| 62 | + |
| 63 | + tx.sign(privateKey) |
| 64 | + |
| 65 | + console.log('') |
| 66 | + console.log(senderAddress, '==>', ethereumUnits.convert(parseInt(tx.value.toString('hex'), 16), 'wei', 'eth'), 'ETH ==>', ethereumUtil.toChecksumAddress(tx.to.toString('hex'))) |
| 67 | + console.log('') |
| 68 | + console.log('') |
| 69 | + console.log('--- Signed Transaction ----') |
| 70 | + console.log(tx.serialize().toString('hex')) |
| 71 | +} |
| 72 | + |
| 73 | +var signTx = function (argv) { |
| 74 | + if (!argv.mnemonic) { |
| 75 | + prompt.start() |
| 76 | + prompt.get(['mnemonic'], (error, result) => { |
| 77 | + if (error) { |
| 78 | + console.warn(error) |
| 79 | + process.exit(1) |
| 80 | + } |
| 81 | + |
| 82 | + buildTx(result.mnemonic, argv) |
| 83 | + }) |
| 84 | + } else { |
| 85 | + buildTx(argv.mnemonic, argv) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +yargs.usage('$0 <cmd> [args]') |
| 90 | + .command('createEthTx [from] [to] [value] [nonce] [gasPrice] [gasLimit] [data]', 'sign a transaction using your derivation path and mnenomic phrase', { |
| 91 | + path: { type: 'string', describe: 'BIP32/BIP44 compatible derivation path', default: "m/44'/60'/0'/0/0" }, |
| 92 | + to: { type: 'string', describe: 'ETH Address to receive your funds' }, |
| 93 | + from: { type: 'string', describe: 'ETH Address you\'re sending from' }, |
| 94 | + value: { type: 'string', describe: 'Value to send in ETH' }, |
| 95 | + nonce: { type: 'string' }, |
| 96 | + gasPrice: { type: 'string' }, |
| 97 | + gasLimit: { type: 'string' }, |
| 98 | + data: { type: 'string', default: '0x' } |
| 99 | + }, signTx) |
| 100 | + .demandOption(['path'], 'A BIP32 or BIP44 compatible derivation path is necessary') |
| 101 | + .demandOption(['from'], 'ETH Address you\'re sending from. We need this to validate your path is correct') |
| 102 | + .demandOption(['to'], 'ETH Address that should receive your funds') |
| 103 | + .demandOption(['value'], 'How much ETH would you like to send?') |
| 104 | + .demandOption(['nonce'], 'The nonce is mandatory') |
| 105 | + .demandOption(['gasPrice'], 'Please provide the Gas Price in WEI') |
| 106 | + .demandOption(['gasLimit'], 'Please provide the Gas Limit in WEI') |
| 107 | + .help() |
| 108 | + .argv |
0 commit comments