diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 157bd84..1523d44 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/src/hdnode.ts b/src/hdnode.ts index a64987d..9a63db6 100644 --- a/src/hdnode.ts +++ b/src/hdnode.ts @@ -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) } diff --git a/tests/crypto.test.ts b/tests/crypto.test.ts index b03b977..265ee2e 100644 --- a/tests/crypto.test.ts +++ b/tests/crypto.test.ts @@ -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')) + } + }) })