JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 27
  • Score
    100M100P100Q70044F
  • License Apache-2.0 License

HD wallet address generation utility

Package Exports

  • hd-address

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hd-address) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

hd-address

NPM version

HD wallet address generation utility.

Getting started

https://github.com/gisvr/hd-address-example

Install

npm i hd-address

Reference

HD Wallet (bip32)

Mnemonic wordlists reference (bip39)

HD Wallet coin type list (bip44)
m / purpose' / coin_type' / account' / change / address_index

                                                        / address 0
                coinType 0(btc) -- account 0 -- change 0  
              /                                         \ address 1
root -- BIP44 
              \
                coinType 60(eth) -- account 0 -- change 1 -- address 0
                          

Initialization

Mnemonic Initialization: example

    const hdAddress = require("hd-address")  
    const mnemonic = "star star star star star star"
    const pwd = "star" 
    //let hd = hdAddress.HD(mnemonic) //v3.0
    let hd = hdAddress.HD(mnemonic,hdAddress.keyType.mnemonic,pwd) //v3.1

Seed Initialization: example

    const seed ="03d0be996b63e90c7625dd3f5319c3bc11669d3d35ae5dc345595e5e59be74084f"
    const hdAddress = require("hd-address")
    // Seed should be at least 128 bits and most 512 bits
    let seedBuf = Buffer.from(seed, "hex")
    let hd = hdAddress.HD(seedBuf,hdAddress.keyType.seed) //v3.0

Base58 Initialization: example

    const base58 = "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi"
    const hdAddress = require("hd-address")  
    let hd = hdAddress.HD(base58,hdAddress.keyType.base58) //v3.1

Basic Usage

Get Random Mnemonic : example

    let mnemo = hdAddress.getRandomMnemonic() 
    console.log(mnemo)
    let isMnemo = hdAddress.validateMnemonic(mnemo) 
    console.log(isMnemo)

Get BTC ETH TRX address : example

    let hdIndex=6677
    let btcAddr =  hd.BTC.getAddress(hdIndex)
    console.log("BTC",btcAddr.address)
    
    let ethAddr =  hd.ETH.getAddress(hdIndex)
    console.log("ETH",ethAddr.address)
    
    let trxAddr =  hd.TRX.getAddress(hdIndex)
    console.log("TRX",trxAddr.address)

Get keypair: example

  let {address, path, pri, pub} =  hd.BTC.getCoinAddressKeyPair(hdIndex)
  console.log(address, path)

Get address using private key or public key

  let priAddr =  hd.BTC.getAddressByPrivateKey(pri)
  console.assert(priAddr.address == address)

  let pubAddr =  hd.BTC.getAddressByPublicKey(pub)
  console.assert(pubAddr.address == address)

Advanced Usage

EOS extension: example

You can extend hd-address by implementing AddressClass

const AddressClass =  require("hd-address").AddressClass //v3.0

module.exports = class EosAddress extends AddressClass {
    constructor(hd) {
        let coin = "EOS"
        super(hd, coin);
    }

    getAddress(index) {
        console.log(this.coin, "implement  getAddress method")
    }

    getAddressByPrivateKey(privateKey) {
        console.log(this.coin, "implement  getAddressByPrivateKey method")
    }

    getAddressByPublicKey(privateKey) {
        console.log(this.coin, "implement  getAddressByPublicKey method")
    }
}

Get address using chain code: example

Chain Code can do hierarchical authorization management

    let hdPath = "m/44'/0'/1'"
    let {pub, chainCode} = hd.wallet.getChainCodeByPath(hdPath)
    console.log(hdPath, "chainCode", chainCode.toString("hex"),"\n")

    // pubKey + chainCode +childPath =>  address
    let childPath = "m/1/" + hdIndex
    let child = hd.wallet.getPublicKeyByChainCode(pub, chainCode, childPath)
    let childAaddr = hd.BTC.getAddressByPublicKey(child.pub)
    console.log(childPath, child.pub.toString("hex"),"BTC Address",childAaddr.address)

    //path =>  address
    let testPath = "m/44'/0'/1'/1/" + hdIndex
    let test = hd.wallet.getChainCodeByPath(testPath)
    let testAaddr = hd.BTC.getAddressByPublicKey(test.pub)
    console.log(testPath, test.pub.toString("hex"),"BTC Address",testAaddr.address)

Testing

  mocha 

License

Apache-2.0 License

Donor Address

"BTC": "1HthGRdzxunKAiMSazDdL8PZhE4qWpeBNK", 
"BCH": "12owPGh3cXLk8HevCEx5fZAMPqZPBgvgmX",
"LTC": "LchXCPCtYTKUvksjf5RvkZhCwvYQrYewaa",
"ETH": "0x4E04823FDF08E862201a4cfA595dc1Ec72AdF3Ab",
"TRX": "TZFH9KReZpsWZZ9Q2bVyXGQtmvVL3PV8gE",