Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hdnode: allow custom path #54

Merged
merged 3 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'))
}
})
})