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

Chore/bip32 validation #1727

Merged
merged 2 commits into from
Jan 21, 2025
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
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
Loading