Skip to content

Commit

Permalink
Add BIP-32 test vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Mar 7, 2023
1 parent 0380556 commit d2a664d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/BIP44Node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ describe('BIP44Node', () => {
`Invalid HD tree path depth: The depth must be a positive integer N such that 0 <= N <= 5. Received: "6"`,
);
});

it.each(fixtures.bip32InvalidExtendedKeys)(
'throws if the extended key is invalid',
async (extendedKey) => {
await expect(BIP44Node.fromExtendedKey(extendedKey)).rejects.toThrow(
/Invalid extended key: .*\./u,
);
},
);
});

describe('fromDerivationPath', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/extended-keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('decodeExtendedKey', () => {
'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMP';

expect(() => decodeExtendedKey(extendedKey)).toThrow(
'Invalid value: Value is not base58-encoded, or the checksum is invalid.',
'Invalid extended key: Value is not base58-encoded, or the checksum is invalid.',
);
});

Expand Down
2 changes: 1 addition & 1 deletion src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ describe('decodeBase58Check', () => {

it('throws if the checksum is invalid', () => {
expect(() => decodeBase58check('SQHFQMRT97ajZff')).toThrow(
'Invalid value: Value is not base58-encoded, or the checksum is invalid.',
'Invalid extended key: Value is not base58-encoded, or the checksum is invalid.',
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export const decodeBase58check = (value: string): Uint8Array => {
return base58Check.decode(value);
} catch {
throw new Error(
`Invalid value: Value is not base58-encoded, or the checksum is invalid.`,
`Invalid extended key: Value is not base58-encoded, or the checksum is invalid.`,
);
}
};
Expand Down
22 changes: 22 additions & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,28 @@ export default {
},
],

// The BIP-32 specification invalid extended key test vectors.
// https://github.com/bitcoin/bips/blob/a3a397c82384220fc871852c809f73898a4d547c/bip-0032.mediawiki#test-vector-5
// The state of the default branch as of 2023-03-06.
bip32InvalidExtendedKeys: [
'xpub661MyMwAqRbcEYS8w7XLSVeEsBXy79zSzH1J8vCdxAZningWLdN3zgtU6LBpB85b3D2yc8sfvZU521AAwdZafEz7mnzBBsz4wKY5fTtTQBm',
'xprv9s21ZrQH143K24Mfq5zL5MhWK9hUhhGbd45hLXo2Pq2oqzMMo63oStZzFGTQQD3dC4H2D5GBj7vWvSQaaBv5cxi9gafk7NF3pnBju6dwKvH',
'xpub661MyMwAqRbcEYS8w7XLSVeEsBXy79zSzH1J8vCdxAZningWLdN3zgtU6Txnt3siSujt9RCVYsx4qHZGc62TG4McvMGcAUjeuwZdduYEvFn',
'xprv9s21ZrQH143K24Mfq5zL5MhWK9hUhhGbd45hLXo2Pq2oqzMMo63oStZzFGpWnsj83BHtEy5Zt8CcDr1UiRXuWCmTQLxEK9vbz5gPstX92JQ',
'xpub661MyMwAqRbcEYS8w7XLSVeEsBXy79zSzH1J8vCdxAZningWLdN3zgtU6N8ZMMXctdiCjxTNq964yKkwrkBJJwpzZS4HS2fxvyYUA4q2Xe4',
'xprv9s21ZrQH143K24Mfq5zL5MhWK9hUhhGbd45hLXo2Pq2oqzMMo63oStZzFAzHGBP2UuGCqWLTAPLcMtD9y5gkZ6Eq3Rjuahrv17fEQ3Qen6J',
'xprv9s2SPatNQ9Vc6GTbVMFPFo7jsaZySyzk7L8n2uqKXJen3KUmvQNTuLh3fhZMBoG3G4ZW1N2kZuHEPY53qmbZzCHshoQnNf4GvELZfqTUrcv',
'xpub661no6RGEX3uJkY4bNnPcw4URcQTrSibUZ4NqJEw5eBkv7ovTwgiT91XX27VbEXGENhYRCf7hyEbWrR3FewATdCEebj6znwMfQkhRYHRLpJ',
'xprv9s21ZrQH4r4TsiLvyLXqM9P7k1K3EYhA1kkD6xuquB5i39AU8KF42acDyL3qsDbU9NmZn6MsGSUYZEsuoePmjzsB3eFKSUEh3Gu1N3cqVUN',
'xpub661MyMwAuDcm6CRQ5N4qiHKrJ39Xe1R1NyfouMKTTWcguwVcfrZJaNvhpebzGerh7gucBvzEQWRugZDuDXjNDRmXzSZe4c7mnTK97pTvGS8',
'DMwo58pR1QLEFihHiXPVykYB6fJmsTeHvyTp7hRThAtCX8CvYzgPcn8XnmdfHGMQzT7ayAmfo4z3gY5KfbrZWZ6St24UVf2Qgo6oujFktLHdHY4',
'DMwo58pR1QLEFihHiXPVykYB6fJmsTeHvyTp7hRThAtCX8CvYzgPcn8XnmdfHPmHJiEDXkTiJTVV9rHEBUem2mwVbbNfvT2MTcAqj3nesx8uBf9',
'xprv9s21ZrQH143K24Mfq5zL5MhWK9hUhhGbd45hLXo2Pq2oqzMMo63oStZzF93Y5wvzdUayhgkkFoicQZcP3y52uPPxFnfoLZB21Teqt1VvEHx',
'xprv9s21ZrQH143K24Mfq5zL5MhWK9hUhhGbd45hLXo2Pq2oqzMMo63oStZzFAzHGBP2UuGCqWLTAPLcMtD5SDKr24z3aiUvKr9bJpdrcLg1y3G',
'xpub661MyMwAqRbcEYS8w7XLSVeEsBXy79zSzH1J8vCdxAZningWLdN3zgtU6Q5JXayek4PRsn35jii4veMimro1xefsM58PgBMrvdYre8QyULY',
'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHL',
],

// https://github.com/cryptocoinjs/secp256k1-node/blob/f73cd3a2b95ec6f549a2feabfbf2b8f17b30dcc7/test/privatekey.js
// The state of the default branch as of 2022-03-10
// Fixtures generated by running the tests and logging the (pseudo-)randomly generated private key
Expand Down

0 comments on commit d2a664d

Please sign in to comment.