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

Check if BIP-32 path segment is <= 2^31-1 #134

Merged
merged 1 commit into from
Jun 20, 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
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export const BYTES_KEY_LENGTH = 32;
export const MIN_BIP_44_DEPTH = 0;
export const MAX_BIP_44_DEPTH = 5;

export const MAX_BIP_32_INDEX = 0xffffffff;
export const MAX_UNHARDENED_BIP_32_INDEX = 0x7fffffff; // 2^31 - 1
export const MAX_BIP_32_INDEX = 0xffffffff; // 2^32 - 1

export type MinBIP44Depth = typeof MIN_BIP_44_DEPTH;
export type MaxBIP44Depth = typeof MAX_BIP_44_DEPTH;
Expand Down
3 changes: 2 additions & 1 deletion src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,10 @@ describe('isValidBIP32PathSegment', () => {
expect(isValidBIP32PathSegment(`1'`)).toBe(true);
expect(isValidBIP32PathSegment(`1000`)).toBe(true);
expect(isValidBIP32PathSegment(`1000'`)).toBe(true);
expect(isValidBIP32PathSegment(`${2 ** 31 - 1}'`)).toBe(true);
});

it.each(['foo', `123''`, `'123'`, `123'/456'`, ...inputs])(
it.each([`${2 ** 31}'`, 'foo', `123''`, `'123'`, `123'/456'`, ...inputs])(
'returns false if the path segment is invalid',
(input) => {
// @ts-expect-error Invalid type.
Expand Down
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
CoinTypeToAddressTuple,
HardenedBIP32Node,
MAX_BIP_32_INDEX,
MAX_UNHARDENED_BIP_32_INDEX,
UnhardenedBIP32Node,
UNPREFIXED_BIP_32_PATH_REGEX,
UnprefixedNode,
Expand Down Expand Up @@ -192,7 +193,7 @@ export function isValidBIP32PathSegment(
}

const index = parseInt(match.groups.index, 10);
return isValidBIP32Index(index);
return isValidInteger(index) && index <= MAX_UNHARDENED_BIP_32_INDEX;
}

/**
Expand Down