Skip to content

Commit 8fb4afd

Browse files
committed
better private key error handling
1 parent acee871 commit 8fb4afd

File tree

3 files changed

+21
-36
lines changed

3 files changed

+21
-36
lines changed

src/helpers/accounts.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ export type DerivedAccount = Readonly<{
1212
account: HDAccount;
1313
derivation: DerivationInfo;
1414
/**
15-
* Derived private key (hex, 0x-prefixed) if available.
15+
* Derived private key (hex, 0x-prefixed).
1616
*
1717
* IMPORTANT: Never log this unless explicitly gated by config.
1818
*/
19-
privateKey?: Hex;
19+
privateKey: Hex;
2020
}>;
2121

2222
export function derivePath(index: number): `m/44'/60'/${string}` {
@@ -45,18 +45,17 @@ export function deriveAccountFromMnemonic(
4545
? toHex(hdKey.privateKey)
4646
: undefined;
4747

48-
const base: {
49-
account: HDAccount;
50-
derivation: DerivationInfo;
51-
privateKey?: Hex;
52-
} = {
48+
if (!privateKey) {
49+
// Sanity check: `mnemonicToAccount(...).getHdKey()` should normally yield a private key
50+
// for a valid mnemonic + derivation path. If this happens, something is unexpectedly
51+
// wrong (or the underlying library behavior changed), and callers cannot proceed
52+
// because they always require a signer.
53+
throw new Error("Derived account has no privateKey available");
54+
}
55+
56+
return {
5357
account,
5458
derivation: { index, path },
59+
privateKey,
5560
};
56-
57-
if (privateKey) {
58-
base.privateKey = privateKey;
59-
}
60-
61-
return base;
6261
}

src/processors/account-derive.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ export async function deriveAccount(
102102
cacheAccountIndex(scenarioVars, selection);
103103

104104
const derived = deriveAccountFromMnemonic(mnemonic, selection.index);
105-
if (!derived.privateKey) {
106-
throw new Error("Derived account has no privateKey available");
107-
}
108105

109106
persistVars(context, {
110107
privateKey: derived.privateKey,

src/processors/siwe-bootstrap.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,15 @@ export async function deriveAndPrint(
4646

4747
const printPk = readBoolEnv("PRINT_DERIVED_PRIVATE_KEY");
4848
if (printPk) {
49-
if (!derived.privateKey) {
50-
logger.warn(
51-
{
52-
index: selection.index,
53-
path: derived.derivation.path,
54-
address: derived.account.address,
55-
},
56-
"derived account has no privateKey available to print"
57-
);
58-
} else {
59-
logger.info(
60-
{
61-
index: selection.index,
62-
path: derived.derivation.path,
63-
address: derived.account.address,
64-
privateKey: derived.privateKey,
65-
},
66-
"derived account (PRINT_DERIVED_PRIVATE_KEY enabled)"
67-
);
68-
}
49+
logger.info(
50+
{
51+
index: selection.index,
52+
path: derived.derivation.path,
53+
address: derived.account.address,
54+
privateKey: derived.privateKey,
55+
},
56+
"derived account (PRINT_DERIVED_PRIVATE_KEY enabled)"
57+
);
6958
} else {
7059
logger.info(
7160
{

0 commit comments

Comments
 (0)