Skip to content

Commit 972590d

Browse files
refactor: use ALLOWED_HD_PATHS const for deriving AllowedHdPath type
- Define individual path constants (BIP44_HD_PATH_PREFIX, LEGACY_MEW_PATH, SLIP0044_TESTNET_PATH) - Create ALLOWED_HD_PATHS const array from these paths - Derive AllowedHdPath type from the const array using typeof - Use simpler regex pattern to extract prefix/index, then validate prefix with isAllowedHdPath helper - Improves consistency with legacy keyring pattern and makes allowed paths more maintainable Co-authored-by: mathieu.artu <mathieu.artu@consensys.net>
1 parent caa9eb3 commit 972590d

1 file changed

Lines changed: 54 additions & 23 deletions

File tree

packages/keyring-eth-trezor/src/trezor-keyring-v2.ts

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,53 @@ const trezorKeyringV2Capabilities: KeyringCapabilities = {
3737
},
3838
};
3939

40+
/**
41+
* BIP-44 standard HD path prefix constant for Ethereum.
42+
* Used as default for derive-index operations.
43+
*/
44+
const BIP44_HD_PATH_PREFIX = `m/44'/60'/0'/0` as const;
45+
46+
/**
47+
* Legacy MEW HD path.
48+
*/
49+
const LEGACY_MEW_PATH = `m/44'/60'/0'` as const;
50+
51+
/**
52+
* SLIP-0044 testnet HD path.
53+
*/
54+
const SLIP0044_TESTNET_PATH = `m/44'/1'/0'/0` as const;
55+
4056
/**
4157
* Allowed HD paths for Trezor keyring.
4258
* These must match the keys in ALLOWED_HD_PATHS from trezor-keyring.ts.
4359
*/
44-
type AllowedHdPath = `m/44'/60'/0'/0` | `m/44'/60'/0'` | `m/44'/1'/0'/0`;
60+
const ALLOWED_HD_PATHS = [
61+
BIP44_HD_PATH_PREFIX,
62+
LEGACY_MEW_PATH,
63+
SLIP0044_TESTNET_PATH,
64+
] as const;
4565

4666
/**
47-
* BIP-44 standard HD path prefix constant for Ethereum.
48-
* Used as default for derive-index operations.
67+
* Type representing allowed HD paths for Trezor keyring.
4968
*/
50-
const BIP44_HD_PATH_PREFIX: AllowedHdPath = `m/44'/60'/0'/0`;
69+
type AllowedHdPath = (typeof ALLOWED_HD_PATHS)[number];
5170

5271
/**
53-
* Regex pattern for validating and parsing derivation paths.
54-
* Only matches the allowed Trezor HD paths from the V1 implementation:
55-
* - m/44'/60'/0'/0/{index} (BIP44 standard)
56-
* - m/44'/60'/0'/{index} (legacy MEW)
57-
* - m/44'/1'/0'/0/{index} (SLIP0044 testnet)
58-
* Captures: [1] = base path, [2] = index
72+
* Regex pattern for parsing derivation paths.
73+
* Matches any path in the format {prefix}/{index} where index is a non-negative integer.
74+
* Captures: [1] = base path prefix, [2] = index
5975
*/
60-
const DERIVATION_PATH_PATTERN =
61-
/^(m\/44'\/60'\/0'\/0|m\/44'\/60'\/0'|m\/44'\/1'\/0'\/0)\/(\d+)$/u;
76+
const DERIVATION_PATH_PATTERN = /^(.+)\/(\d+)$/u;
77+
78+
/**
79+
* Checks if a path prefix is an allowed HD path.
80+
*
81+
* @param prefix - The path prefix to check.
82+
* @returns True if the prefix is allowed, false otherwise.
83+
*/
84+
function isAllowedHdPath(prefix: string): prefix is AllowedHdPath {
85+
return ALLOWED_HD_PATHS.includes(prefix as AllowedHdPath);
86+
}
6287

6388
/**
6489
* Concrete {@link KeyringV2} adapter for {@link TrezorKeyring}.
@@ -131,25 +156,31 @@ export class TrezorKeyringV2
131156
*
132157
* @param derivationPath - The full derivation path (e.g., m/44'/60'/0'/0/5).
133158
* @returns The base HD path and account index.
134-
* @throws If the path format is invalid.
159+
* @throws If the path format is invalid or the base path is not allowed.
135160
*/
136161
#parseDerivationPath(derivationPath: string): {
137162
basePath: AllowedHdPath;
138163
index: number;
139164
} {
140165
const match = derivationPath.match(DERIVATION_PATH_PATTERN);
141-
if (match) {
142-
return {
143-
basePath: match[1] as AllowedHdPath,
144-
index: parseInt(match[2] as string, 10),
145-
};
166+
if (!match) {
167+
throw new Error(
168+
`Invalid derivation path format: ${derivationPath}. ` +
169+
`Expected format: {base}/{index}.`,
170+
);
171+
}
172+
173+
const prefix = match[1] as string;
174+
const index = parseInt(match[2] as string, 10);
175+
176+
if (!isAllowedHdPath(prefix)) {
177+
throw new Error(
178+
`Invalid derivation path: ${derivationPath}. ` +
179+
`Base path must be one of: ${ALLOWED_HD_PATHS.join(', ')}.`,
180+
);
146181
}
147182

148-
throw new Error(
149-
`Invalid derivation path: ${derivationPath}. ` +
150-
`Expected format: {base}/{index} where base is one of: ` +
151-
`m/44'/60'/0'/0, m/44'/60'/0', m/44'/1'/0'/0.`,
152-
);
183+
return { basePath: prefix, index };
153184
}
154185

155186
/**

0 commit comments

Comments
 (0)