Skip to content

Commit

Permalink
fix: integrate new error handling in hdnode
Browse files Browse the repository at this point in the history
  • Loading branch information
rodolfopietro97 committed Oct 27, 2023
1 parent f40033f commit 4c238d2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
27 changes: 21 additions & 6 deletions packages/core/src/hdnode/hdnode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ethers } from 'ethers';
import {
ERRORS,
MNEMONIC_WORDLIST_ALLOWED_SIZES,
VET_DERIVATION_PATH,
X_PRIV_PREFIX,
Expand All @@ -11,6 +10,7 @@ import { address } from '../address';
import { sha256 } from '../hash';
import { secp256k1 } from '../secp256k1';
import { type WordlistSizeType } from '../mnemonic';
import { buildError, HDNODE } from '@vechain-sdk/errors';

/**
* Generates an HDNode instance using mnemonic words.
Expand All @@ -26,7 +26,10 @@ function fromMnemonic(words: string[], path = VET_DERIVATION_PATH): IHDNode {
words.length as WordlistSizeType
)
) {
throw new Error(ERRORS.HDNODE.INVALID_MNEMONICS);
throw buildError(
HDNODE.INVALID_HDNODE_MNEMONICS,
'Invalid mnemonic size. It must be 12, 15, 18, 21, or 24.'
);
}
// normalize words to lowercase
const joinedWords = words.join(' ').toLowerCase();
Expand All @@ -48,11 +51,17 @@ function fromMnemonic(words: string[], path = VET_DERIVATION_PATH): IHDNode {
function fromPublicKey(publicKey: Buffer, chainCode: Buffer): IHDNode {
// Invalid public key
if (publicKey.length !== 65)
throw new Error(ERRORS.HDNODE.INVALID_PUBLICKEY);
throw buildError(
HDNODE.INVALID_HDNODE_PUBLIC_KEY,
'Invalid public key. Length must be 65 bytes.'
);

// Invalid chain code
if (chainCode.length !== 32)
throw new Error(ERRORS.HDNODE.INVALID_CHAINCODE);
throw buildError(
HDNODE.INVALID_HDNODE_CHAIN_CODE,
'Invalid chain code. Length must be 32 bytes.'
);

const compressed = secp256k1.extendedPublicKeyToArray(publicKey, true);
const key = Buffer.concat([
Expand Down Expand Up @@ -80,11 +89,17 @@ function fromPublicKey(publicKey: Buffer, chainCode: Buffer): IHDNode {
function fromPrivateKey(privateKey: Buffer, chainCode: Buffer): IHDNode {
// Invalid private key
if (privateKey.length !== 32)
throw new Error(ERRORS.HDNODE.INVALID_PRIVATEKEY);
throw buildError(
HDNODE.INVALID_HDNODE_PRIVATE_KEY,
'Invalid private key. Length must be 32 bytes.'
);

// Invalid chain code
if (chainCode.length !== 32)
throw new Error(ERRORS.HDNODE.INVALID_CHAINCODE);
throw buildError(
HDNODE.INVALID_HDNODE_CHAIN_CODE,
'Invalid chain code. Length must be 32 bytes.'
);

const key = Buffer.concat([
X_PRIV_PREFIX,
Expand Down
19 changes: 12 additions & 7 deletions packages/core/tests/hdnode/hdnode.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, test } from '@jest/globals';
import {
ERRORS,
HDNode,
type WordlistSizeType,
ZERO_BUFFER,
Expand All @@ -9,6 +8,12 @@ import {
secp256k1
} from '../../src';
import { addresses, words, wrongWords } from './fixture';
import {
InvalidHDNodeChaincodeError,
InvalidHDNodeMnemonicsError,
InvalidHDNodePrivateKeyError,
InvalidHDNodePublicKeyError
} from '@vechain-sdk/errors';

/**
* Mnemonic tests
Expand Down Expand Up @@ -116,8 +121,8 @@ describe('Hdnode', () => {
* Test invalid mnemonic
*/
test('Invalid mnemonic', () => {
expect(() => HDNode.fromMnemonic(wrongWords)).toThrow(
ERRORS.HDNODE.INVALID_MNEMONICS
expect(() => HDNode.fromMnemonic(wrongWords)).toThrowError(
InvalidHDNodeMnemonicsError
);
});

Expand All @@ -127,7 +132,7 @@ describe('Hdnode', () => {
test('Invalid private key', () => {
expect(() =>
HDNode.fromPrivateKey(ZERO_BUFFER(31), ZERO_BUFFER(32))
).toThrow(ERRORS.HDNODE.INVALID_PRIVATEKEY);
).toThrowError(InvalidHDNodePrivateKeyError);
});

/**
Expand All @@ -136,7 +141,7 @@ describe('Hdnode', () => {
test('Invalid public key', () => {
expect(() =>
HDNode.fromPublicKey(ZERO_BUFFER(31), ZERO_BUFFER(32))
).toThrow(ERRORS.HDNODE.INVALID_PUBLICKEY);
).toThrowError(InvalidHDNodePublicKeyError);
});

/**
Expand All @@ -145,7 +150,7 @@ describe('Hdnode', () => {
test('Invalid chain code private key', () => {
expect(() =>
HDNode.fromPrivateKey(ZERO_BUFFER(32), ZERO_BUFFER(31))
).toThrow(ERRORS.HDNODE.INVALID_CHAINCODE);
).toThrowError(InvalidHDNodeChaincodeError);
});

/**
Expand All @@ -154,6 +159,6 @@ describe('Hdnode', () => {
test('Invalid chain code public key', () => {
expect(() =>
HDNode.fromPublicKey(ZERO_BUFFER(65), ZERO_BUFFER(31))
).toThrow(ERRORS.HDNODE.INVALID_CHAINCODE);
).toThrowError(InvalidHDNodeChaincodeError);
});
});

0 comments on commit 4c238d2

Please sign in to comment.