项目作者: definancer

项目描述 :
An extensible HD Wallet Address management utility
高级语言: JavaScript
项目地址: git://github.com/definancer/hd-address.git
创建时间: 2020-07-22T09:32:52Z
项目社区:https://github.com/definancer/hd-address

开源协议:Apache License 2.0

下载


hd-address

NPM version

An extensible HD Wallet Address management utility

中文说明

Please contact us if you have any special needs txdev6@gmail.com

Install

  1. $ npm i hd-address
  2. $ yarn add hd-address

Reference

  1. ## API
  2. #### 1.HD Wallet private key API
  3. ```javascript
  4. const hdAddress = require("hd-address")
  5. // Generate mnemocie seed base58
  6. const mnemocie = hdAddress.mnemocie.getRandomMnemocie()
  7. const seed = hdAddress.seed.getRandomSeed()
  8. const base58 = hdAddress.base58.getRandomBase58()
  9. // Create Hd wallet
  10. const hdWallet =new hdAddress.mnemocie(mnemocie)
  11. const hdWallet =new hdAddress.seed(seed)
  12. const hdWallet =new hdAddress.base58(base58)
  13. // Generate keypair
  14. hdWallet.getHdPath(coinTypeCode, index, account = 0, change = 0)
  15. hdWallet.getKeyPair(coinTypeCode, index, account, change)
  16. hdWallet.getCoinKeyPair(coin, index, account, change)
  17. // ChainCode
  18. hdWallet.getChainCodeByPath(path)
  19. hdWallet.getPublicKeyByChainCode(parentPub, chainCode, path)
  20. hdWallet.getPrivateKeyByChainCode(parentPri, chainCode, path)

2.Get coin address API

  1. const hdAddress = require("hd-address")
  2. // Create a wallet with Coin Info
  3. let hd = hdAddress.HD(mnemonic,hdAddress.keyType.mnemonic,pwd)
  4. let hd = hdAddress.HD(seed,hdAddress.keyType.seed)
  5. let hd = hdAddress.HD(base58,hdAddress.keyType.base58)
  6. // Get coin info
  7. hd.BTC.getCoinKeyPair(index, account, change)
  8. hd.ETH.getCoinAddressKeyPair(index, account, change)
  9. hd.TRX.getAddress(index, account, change)
  10. hd.LTC.getAddressByPath(hdPath)
  11. hd.BCH.getAddressByPrivateKey(privateKey)
  12. hd.BTC_TEST.getAddressByPublicKey(publicKey)

Example

Initialization

1.Mnemonic Initialization

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

2.Seed Initialization

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

3.Base58 Initialization

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

Get coin address info

1.Get BTC ETH TRX address example

  1. let hdIndex=6677
  2. let btcAddr = hd.BTC.getAddress(hdIndex)
  3. console.log("BTC",btcAddr.address)
  4. let ethAddr = hd.ETH.getAddress(hdIndex)
  5. console.log("ETH",ethAddr.address)
  6. let trxAddr = hd.TRX.getAddress(hdIndex)
  7. console.log("TRX",trxAddr.address)

2.Get address by path

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

3.Get keypair

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

4.Get address using private key or public key

  1. let priAddr = hd.BTC.getAddressByPrivateKey(pri)
  2. console.assert(priAddr.address == address)
  3. let pubAddr = hd.BTC.getAddressByPublicKey(pub)
  4. console.assert(pubAddr.address == address)

Generate safe secret key

1.Get Random Mnemonic

  1. let wordList = hdAddress.mnemonic.wordLists.CN
  2. let strength = hdAddress.mnemonic.strength.high
  3. let cnMnemo = hdAddress.mnemonic.getRandomMnemonic(strength, wordList)
  4. let isMnemo = hdAddress.mnemonic.validateMnemonic(cnMnemo)
  5. console.log(isMnemo)

2.Get Random base58

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

EOS extension: example

You can extend hd-address by implementing AddressClass

  1. const AddressClass = require("hd-address").AddressClass //v3.0
  2. module.exports = class EosAddress extends AddressClass {
  3. constructor(hd) {
  4. let coin = "EOS"
  5. super(hd, coin);
  6. }
  7. getAddress(index) {
  8. console.log(this.coin, "implement getAddress method")
  9. }
  10. getAddressByPrivateKey(privateKey) {
  11. console.log(this.coin, "implement getAddressByPrivateKey method")
  12. }
  13. getAddressByPublicKey(privateKey) {
  14. console.log(this.coin, "implement getAddressByPublicKey method")
  15. }
  16. }

Get address using chain code: example

Chain Code can do hierarchical authorization management

  1. let hdPath = "m/44'/0'/1'"
  2. let {pub, chainCode} = hd.wallet.getChainCodeByPath(hdPath)
  3. console.log(hdPath, "chainCode", chainCode.toString("hex"),"\n")
  4. // pubKey + chainCode +childPath => address
  5. let childPath = "m/1/" + hdIndex
  6. let child = hd.wallet.getPublicKeyByChainCode(pub, chainCode, childPath)
  7. let childAaddr = hd.BTC.getAddressByPublicKey(child.pub)
  8. console.log(childPath, child.pub.toString("hex"),"BTC Address",childAaddr.address)
  9. //path => address
  10. let testPath = "m/44'/0'/1'/1/" + hdIndex
  11. let test = hd.wallet.getChainCodeByPath(testPath)
  12. let testAaddr = hd.BTC.getAddressByPublicKey(test.pub)
  13. console.log(testPath, test.pub.toString("hex"),"BTC Address",testAaddr.address)

Testing

  1. mocha

License

Apache-2.0 License