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 = const wallet = EthHdWallet console; /* true */*/
generateAddresses(): Generating addresses
// generate 2 addressesconsole /*[ '0xd7c0cd9e7d2701c710d64fc492c7086679bdf7b4', '0x1acfb961c5a8268eac8e09d6241a26cbeff42241',]*/
discardAddresses(): Discarding addresses
// generate 5 addresseswallet// discard the last 2 (leaving just the first 3)console /*[ '0xd7c0cd9e7d2701c710d64fc492c7086679bdf7b4', '0x1acfb961c5a8268eac8e09d6241a26cbeff42241',]*/
Note: the next time you run generateAddresses()
it will again generate
those discarded addresses.
getAddresses(): Get all generated addresses
walletwallet // get all addressesconsole /*[ '0xd7c0cd9e7d2701c710d64fc492c7086679bdf7b4', '0x1acfb961c5a8268eac8e09d6241a26cbeff42241', '0xabc2bca51709b8615147352c62420f547a63a00c', '0x26042cb13cc4140a281c0fcc7464074c5e9fd0b4', '0x5d0d1a012a3ab2b3424c2023246d8c834bf599d9']*/
hasAddress(): Check if given address exists in current list of generated addresses
walletwallet /*[ '0xd7c0cd9e7d2701c710d64fc492c7086679bdf7b4', '0x1acfb961c5a8268eac8e09d6241a26cbeff42241', '0xabc2bca51709b8615147352c62420f547a63a00c', '0x26042cb13cc4140a281c0fcc7464074c5e9fd0b4', '0x5d0d1a012a3ab2b3424c2023246d8c834bf599d9']*/ wallet /* false */wallet /* true */
getAddressCount(): Get no. of addresses
walletwallet console /* 5 */
signTransaction(): Sign a transaction
const rawTx = wallet console /* "0x...." */ web3eth
sign(): Sign data
const signature = wallet console /* "0x...." */
recoverSignerPublicKey(): Recover public key (address) of signer
const address = '0x...'const data = '...' const signature = wallet const publicKey = wallet console /* will be same as "address" */
getPrivateKey(): Get private key of address
const address = wallet const privateKey = wallet console /* "123FA..." */
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:
- https://github.com/ConsenSys/eth-lightwallet
- https://github.com/MetaMask/eth-hd-keyring
- https://github.com/trapp/ethereum-bip44
References
- https://github.com/ethereum/EIPs/issues/85
- https://github.com/MetaMask/metamask-extension/issues/640
License
MIT - see LICENSE.md