Skip to content

Commit

Permalink
add: api desc
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Wang committed Aug 21, 2020
1 parent 0270fd9 commit 87a1c09
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
50 changes: 41 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ An extensible HD Wallet Address management utility
[中文文档](https://github.com/gisvr/hd-address/blob/master/README_CN.md)
### Install
```
npm i hd-address
$ npm i hd-address
$ yarn add hd-address
```
### Reference
[HD Wallet (bip32)](https://github.com/bitcoin/bips/blob/master/bip-0032/derivation.png)
Expand All @@ -25,6 +26,45 @@ root -- BIP44
coinType 60(eth) -- account 0 -- change 1 -- address 0

```
## API
```shell script
const hdAddress = require("hd-address")
//------------Get hd wallet private key API------------
// 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)

//------------Get coin address API------------

// 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) // m/account'/change/address_index
hd.BCH.getAddressByPrivateKey(privateKey)
hd.BTC_TEST.getAddressByPublicKey(privateKey)
```
## Initialization
### Mnemonic Initialization: [example](https://github.com/gisvr/hd-address-example/blob/master/init/mnemonic.pwd.js)
Expand Down Expand Up @@ -168,11 +208,3 @@ Chain Code can do hierarchical authorization management
[Apache-2.0 License](./LICENSE)
## Donor Address
```js
"BTC": "1HthGRdzxunKAiMSazDdL8PZhE4qWpeBNK",
"BCH": "12owPGh3cXLk8HevCEx5fZAMPqZPBgvgmX",
"LTC": "LchXCPCtYTKUvksjf5RvkZhCwvYQrYewaa",
"ETH": "0x4E04823FDF08E862201a4cfA595dc1Ec72AdF3Ab",
"TRX": "TZFH9KReZpsWZZ9Q2bVyXGQtmvVL3PV8gE",
```
2 changes: 1 addition & 1 deletion lib/base/hd.wallet.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class HdWallet {
* change:找零。一般使用0對外收款,1接受每次交易的找零
* address_index: 位址索引
*/
getHdPath(coinTypeCode, index, account = 0, change = 0,) {
getHdPath(coinTypeCode, index, account = 0, change = 0) {
let hdPath = `m/44'/${coinTypeCode}'/${account}'/${change}`
if (/^[0-9]+'?$/.test(index)) {
hdPath =hdPath+ '/' + index;
Expand Down
7 changes: 7 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,11 @@ describe("address init", () => {
console.log(address)
})

it(" getHDWallet by mnemonic", () => {
const hdWallet = new hdAddress.mnemonic(mnemonic)
let {path, pub} = hdWallet.getCoinKeyPair("BTC", 0, 0, 0)
console.log(path)
console.log(pub.toString("hex"))
})

})

0 comments on commit 87a1c09

Please sign in to comment.