Skip to content

Commit

Permalink
Merge pull request #54 from libotony/master
Browse files Browse the repository at this point in the history
hdnode: allow custom path
  • Loading branch information
qianbin authored May 17, 2023
2 parents a608f20 + fec11ad commit e399224
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
16 changes: 4 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,15 @@ jobs:
name: Node ${{ matrix.node }}
strategy:
matrix:
os: [ubuntu-latest]
node: [8, 10, 12, 14]
node: [14,16,18]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- uses: actions/setup-node@v2
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}

- name: Cache Node.js modules
uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.OS }}-node-${{matrix.node}}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-node-
cache: 'npm'

- name: Install Dependencies
run: npm ci
Expand Down
4 changes: 2 additions & 2 deletions src/hdnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export interface HDNode {

export namespace HDNode {
/** create node from mnemonic words */
export function fromMnemonic(words: string[]) {
export function fromMnemonic(words: string[], path=VET_DERIVATION_PATH) {
// normalize words to lowercase
const joinedWords = words.join(' ').toLowerCase()
const node = HD.fromMnemonic(joinedWords).derivePath(VET_DERIVATION_PATH)
const node = HD.fromMnemonic(joinedWords).derivePath(path)
return createHDNode(node)
}

Expand Down
19 changes: 19 additions & 0 deletions tests/crypto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,23 @@ describe('mnemonic', () => {
const node2 = HDNode.fromMnemonic(words.map(w => w.toUpperCase()))
expect(node.address === node2.address)
})

it('hdNode custom path', () => {
const eth_path = `m/44'/60'/0'/0`
const node = HDNode.fromMnemonic(words, eth_path)
// test case generated via https://iancoleman.io/bip39/
const addresses = [
'4473c83a6a9661ab9cdb6b07749998ad9e77a580',
'858531457566df8b60cf1355b54e48e04e36be33',
'40b5aa8b54aafaf6323b58ce5737ce320d92cf99',
'988f3af24dca0a3080f9ab5a1f57d706c6b8f011',
'ffb0e35ba82856f8f5b7a57104c38a73f3ceff03'
]
for (let i = 0; i < 5; i++) {
const child = node.derive(i)
expect(address.fromPublicKey(child.publicKey).slice(2)).equal(addresses[i])
expect(child.address).equal('0x' + addresses[i])
expect(secp256k1.derivePublicKey(child.privateKey!).toString('hex')).equal(child.publicKey.toString('hex'))
}
})
})

0 comments on commit e399224

Please sign in to comment.