eth-hd-wallet

Features:
- Lightweight, works in Node.js and browsers
- Supports custom-generated mnemonics
- Batch-generate addresses in iterations
- Sign transactions and data
- Recover signer public key
- Comprehensive test coverage
Installation
npm install eth-hd-wallet
Or if using Yarn (we recommend this):
yarn add eth-hd-wallet
API
(static) fromMnemonic(): Generate wallet from mnemonic
const { generateMnemonic, EthHdWallet } = require('eth-hd-wallet')
const wallet = EthHdWallet.fromMnemonic(generateMnemonic())
console.log( wallet instanceof EthHdWallet );
*/
generateAddresses(): Generating addresses
console.log( wallet.generateAddresses(2) )
discardAddresses(): Discarding addresses
wallet.generateAddresses(5)
console.log( wallet.discardAddresses(2) )
Note: the next time you run generateAddresses() it will again generate
those discarded addresses.
getAddresses(): Get all generated addresses
wallet.generateAddresses(2)
wallet.generateAddresses(3)
console.log( wallet.getAddresses() )
hasAddress(): Check if given address exists in current list of generated addresses
wallet.generateAddresses(2)
wallet.generateAddresses(3)
wallet.hasAddress('0x1efd1a012a3ab2b3424c2023246d8c834bf58723')
wallet.hasAddress('0x26042cb13cc4140a281c0fcc7464074c5e9fd0b4')
getAddressCount(): Get no. of addresses
wallet.generateAddresses(2)
wallet.generateAddresses(3)
console.log( wallet.getAddressCount() )
signTransaction(): Sign a transaction
const rawTx = wallet.signTransaction({
from: '0x...',
to: '0x...',
value: 200000000000000000,
nonce: 0x0,
gasPrice: 50000000000,
gasLimit: 21000,
chainId: 1
})
console.log( rawTx )
web3.eth.sendRawTransaction(rawTx, (err) => { ... })
sign(): Sign data
const signature = wallet.sign({
address: '0x...',
data: '...'
})
console.log( signature )
recoverSignerPublicKey(): Recover public key (address) of signer
const address = '0x...'
const data = '...'
const signature = wallet.sign({ address, data })
const publicKey = wallet.recoverSignerPublicKey({ signature, data })
console.log( publicKey )
getPrivateKey(): Get private key of address
const [ address ] = wallet.generateAddresses(1)
const privateKey = wallet.getPrivateKey(address)
console.log( privateKey.toString('hex') )
Developing
Ensure you have geth installed and
available in your PATH.
- To run tests:
yarn test
- Tests with coverage:
yarn test:coverage
- Tests with watcher:
yarn test:watch
- Linter:
yarn lint
- Build
dist/: yarn build
Note: If you've never installed geth before then make
sure you run geth makedag 0 ~/.ethash to generate the DAG needed for mining,
otherwise the tests will timeout.
Acknowledgements
Inspired by code from the following great projects:
References
License
MIT - see LICENSE.md