npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eth-hd-wallet

v0.5.1

Published

Lightweight Ethereum HD wallet implementation based on BIP-44 standard

Downloads

264

Readme

eth-hd-wallet

Build Status NPM module

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 ); /* true */
*/

generateAddresses(): Generating addresses

// generate 2 addresses
console.log( wallet.generateAddresses(2) )

/*
[
  '0xd7c0cd9e7d2701c710d64fc492c7086679bdf7b4',
  '0x1acfb961c5a8268eac8e09d6241a26cbeff42241',
]
*/

discardAddresses(): Discarding addresses

// generate 5 addresses
wallet.generateAddresses(5)
// discard the last 2 (leaving just the first 3)
console.log( wallet.discardAddresses(2) )

/*
[
  '0xd7c0cd9e7d2701c710d64fc492c7086679bdf7b4',
  '0x1acfb961c5a8268eac8e09d6241a26cbeff42241',
]
*/

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)

// get all addresses
console.log( wallet.getAddresses() )

/*
[
  '0xd7c0cd9e7d2701c710d64fc492c7086679bdf7b4',
  '0x1acfb961c5a8268eac8e09d6241a26cbeff42241',
  '0xabc2bca51709b8615147352c62420f547a63a00c',
  '0x26042cb13cc4140a281c0fcc7464074c5e9fd0b4',
  '0x5d0d1a012a3ab2b3424c2023246d8c834bf599d9'
]
*/

hasAddress(): Check if given address exists in current list of generated addresses

wallet.generateAddresses(2)
wallet.generateAddresses(3)

/*
[
  '0xd7c0cd9e7d2701c710d64fc492c7086679bdf7b4',
  '0x1acfb961c5a8268eac8e09d6241a26cbeff42241',
  '0xabc2bca51709b8615147352c62420f547a63a00c',
  '0x26042cb13cc4140a281c0fcc7464074c5e9fd0b4',
  '0x5d0d1a012a3ab2b3424c2023246d8c834bf599d9'
]
*/

wallet.hasAddress('0x1efd1a012a3ab2b3424c2023246d8c834bf58723') /* false */
wallet.hasAddress('0x26042cb13cc4140a281c0fcc7464074c5e9fd0b4') /* true */

getAddressCount(): Get no. of addresses

wallet.generateAddresses(2)
wallet.generateAddresses(3)

console.log( wallet.getAddressCount() ) /* 5 */

signTransaction(): Sign a transaction

const rawTx = wallet.signTransaction({
  from: '0x...',
  to: '0x...',
  value: 200000000000000000,
  nonce: 0x0,
  gasPrice: 50000000000,
  gasLimit: 21000,
  chainId: 1 /* see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md */
})

console.log( rawTx ) /* "0x...." */

web3.eth.sendRawTransaction(rawTx, (err) => { ... })

sign(): Sign data

const signature = wallet.sign({
  address: '0x...',
  data: '...'
})

console.log( signature ) /* "0x...." */

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 ) /* will be same as "address" */

getPrivateKey(): Get private key of address

const [ address ] = wallet.generateAddresses(1)

const privateKey = wallet.getPrivateKey(address)

console.log( privateKey.toString('hex') ) /* "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