Giter Club home page Giter Club logo

hd-address's Introduction

hd-address

NPM version

An extensible HD Wallet Address management utility

中文说明

Please contact us if you have any special needs [email protected]

Install

$ npm i hd-address
$ yarn add hd-address

Reference

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
                          

API

1.HD Wallet private key API

const hdAddress = require("hd-address")  
// Generate mnemocie seed base58
const mnemocie = hdAddress.mnemocie.getRandomMnemocie()
const seed = hdAddress.seed.getRandomSeed()
const base58 = hdAddress.base58.getRandomBase58()

// Create Hd wallet
const hdWallet =new hdAddress.mnemocie(mnemocie)
const hdWallet =new hdAddress.seed(seed)
const hdWallet =new hdAddress.base58(base58)

// Generate keypair
hdWallet.getHdPath(coinTypeCode, index, account = 0, change = 0) 
hdWallet.getKeyPair(coinTypeCode, index, account, change) 
hdWallet.getCoinKeyPair(coin, index, account, change) 

// ChainCode
hdWallet.getChainCodeByPath(path)
hdWallet.getPublicKeyByChainCode(parentPub, chainCode, path)
hdWallet.getPrivateKeyByChainCode(parentPri, chainCode, path)

2.Get coin address API

const hdAddress = require("hd-address")  
// Create a wallet with Coin Info
let hd = hdAddress.HD(mnemonic,hdAddress.keyType.mnemonic,pwd)
let hd = hdAddress.HD(seed,hdAddress.keyType.seed) 
let hd = hdAddress.HD(base58,hdAddress.keyType.base58) 

// Get coin info
hd.BTC.getCoinKeyPair(index, account, change)
hd.ETH.getCoinAddressKeyPair(index, account, change)
hd.TRX.getAddress(index, account, change)
hd.LTC.getAddressByPath(hdPath) 
hd.BCH.getAddressByPrivateKey(privateKey)
hd.BTC_TEST.getAddressByPublicKey(publicKey)

Example

Initialization

1.Mnemonic Initialization

    const mnemonic = hdAddress.mnemocie.getRandomMnemonic()    
    let hd = hdAddress.HD(mnemonic,hdAddress.keyType.mnemonic)  

2.Seed Initialization

    const {seed} =hdAddress.mnemocie.getRandomSeed() 
    let hd = hdAddress.HD(seed,hdAddress.keyType.seed)  

3.Base58 Initialization

    const {base58} =hdAddress.mnemocie.getRandomBase58() 
    let hd = hdAddress.HD(base58,hdAddress.keyType.base58) //v3.1

Get coin address info

1.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)

2.Get address by path

    let hdpath = "m/0'/1/1" // account/change/index
    let {address, pub, pri, path} = hd.BTC.getAddressByPath(hdpath)
    console.log(address, pub, pri, path) 

3.Get keypair

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

4.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)

Generate safe secret key

1.Get Random Mnemonic

    let wordList = hdAddress.mnemonic.wordLists.CN
    let strength = hdAddress.mnemonic.strength.high 
    let cnMnemo = hdAddress.mnemonic.getRandomMnemonic(strength, wordList)
    let isMnemo = hdAddress.mnemonic.validateMnemonic(cnMnemo) 
    console.log(isMnemo)

2.Get Random base58

    let strength = hdAddress.base58.strength.high 
    let base58 = hdAddress.mnemonic.getRandombase58(strength)

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

hd-address's People

Contributors

web3w avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.