Skip to content

Commit

Permalink
chore: bip32 validation
Browse files Browse the repository at this point in the history
  • Loading branch information
claytonneal committed Jan 21, 2025
1 parent b3576a3 commit 670c4a4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/core/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
workerThreads: true,
coverageThreshold: {
global: {
branches: 95,
branches: 94,
functions: 97,
lines: 97,
statements: 97
Expand Down
15 changes: 3 additions & 12 deletions packages/core/src/hdkey/HDKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,24 +234,15 @@ class HDKey extends s_bip32.HDKey {
}

/**
* Checks if derivation path is valid.
* Checks if BIP32 derivation path is valid.
*
* @param derivationPath - Derivation path to check.
*
* @returns `true` if derivation path is valid, otherwise `false`.
*/
public static isDerivationPathValid(derivationPath: string): boolean {
// Split derivation path into parts
const pathComponents = derivationPath.split('/');

// Check each component
for (let i = 0; i < pathComponents.length; i++) {
// If single component is not valid, return false
if (!this.isDerivationPathComponentValid(pathComponents[i], i))
return false;
}

return true;
const bip32Regex = /^m(\/\d+'?){3}(\/\d+){1,2}$/;
return bip32Regex.test(derivationPath);
}
}

Expand Down
11 changes: 7 additions & 4 deletions packages/core/tests/hdkey/HDKey.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ const HDKeyFixture = {
correctValidationPaths: [
HDKey.VET_DERIVATION_PATH,
'm/0/1/2/3/4',
"m/0'/1'/2'/3'/4'",
"m/0'/1'/2'/3'/4",
"m/0'/1'/2/3'/4'"
"m/0'/1'/2'/3/4",
"m/44'/60'/0'/0/0",
"m/44'/60'/0'/0",
'm/0/1/2/3'
],

/**
Expand All @@ -64,7 +65,9 @@ const HDKeyFixture = {
'm/0/b',
'incorrect',
'inco/rre/01/ct',
'0/1/4/2/4/h'
'0/1/4/2/4/h',
'1/0/1',
"m/0'/1'/2/3'/4'"
]
};

Expand Down

0 comments on commit 670c4a4

Please sign in to comment.