Skip to content

Commit 554d18e

Browse files
committed
Validate that new index is within bounds
1 parent 165c7ae commit 554d18e

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ export const BYTES_KEY_LENGTH = 32;
33
export const MIN_BIP_44_DEPTH = 0;
44
export const MAX_BIP_44_DEPTH = 5;
55

6+
export const MAX_BIP_32_INDEX = 0xffffffff;
7+
68
export type MinBIP44Depth = typeof MIN_BIP_44_DEPTH;
79
export type MaxBIP44Depth = typeof MAX_BIP_44_DEPTH;
810
export type BIP44Depth = MinBIP44Depth | 1 | 2 | 3 | 4 | MaxBIP44Depth;

src/derivers/bip32.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { SLIP10Node } from '../SLIP10Node';
1616
import {
1717
isValidBytesKey,
1818
numberToUint32,
19+
validateBIP32Index,
1920
validateSpecification,
2021
} from '../utils';
2122

@@ -242,6 +243,8 @@ async function derivePrivateChildKey({
242243

243244
switch (specification) {
244245
case 'bip32': {
246+
validateBIP32Index(childIndex + 1);
247+
245248
const secretExtension = await deriveSecretExtension({
246249
privateKey,
247250
childIndex: childIndex + 1,
@@ -371,6 +374,8 @@ async function derivePublicChildKey({
371374

372375
switch (specification) {
373376
case 'bip32': {
377+
validateBIP32Index(childIndex + 1);
378+
374379
// As per BIP-32, if the resulting key is invalid, the key is generated
375380
// from the next child index instead.
376381
return await derivePublicChildKey({

src/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
CoinTypeHDPathString,
1717
CoinTypeToAddressTuple,
1818
HardenedBIP32Node,
19+
MAX_BIP_32_INDEX,
1920
UnhardenedBIP32Node,
2021
} from './constants';
2122
import { curves, SupportedCurve } from './curves';
@@ -172,7 +173,7 @@ export function validateBIP32Index(addressIndex: number) {
172173
* @returns Whether the index is a non-negative integer number.
173174
*/
174175
export function isValidBIP32Index(index: number): boolean {
175-
return isValidInteger(index);
176+
return isValidInteger(index) && index <= MAX_BIP_32_INDEX;
176177
}
177178

178179
/**

0 commit comments

Comments
 (0)