Skip to content

Commit e92ba01

Browse files
committed
fix(abstract-eth): address review feedback on Safe keycard recovery
- eth.ts classified sweep/KRS recovery from whitespace-stripped keys while every sibling used the raw keys; extract classifyRecoveryKeys so the two cannot drift apart again - tempo: fail closed on a wrong keychain like the rest of the EVM TSS family - avaxc: forward apiKey to the signer check - cover the base-class missing commonKeyChain path, which also pins that validateUnsignedSweepTSSParams is awaited Aligning getConsolidationAddress onto the same keychain fallback was tried and reverted: recoverConsolidations is called with backupKey and no userKey, so it scanned an extra derived address and changed which addresses a consolidation sweeps. A NOTE at the call site records why it stays keyed on params.userKey. TICKET: WCN-3946
1 parent b2832ac commit e92ba01

12 files changed

Lines changed: 133 additions & 103 deletions

File tree

‎modules/abstract-eth/src/abstractEthLikeNewCoins.ts‎

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @prettier
33
*/
44
import {
5+
classifyRecoveryKeys,
56
AddressCoinSpecific,
67
BitGoBase,
78
BuildNftTransferDataOptions,
@@ -1515,6 +1516,9 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin implem
15151516
}
15161517
}
15171518

1519+
// NOTE: deliberately keyed on params.userKey only, not getTssCommonKeychain. Resolving the
1520+
// TSS fallback chain here would scan an extra derived address whenever a caller supplies
1521+
// backupKey without userKey, changing which addresses a consolidation sweeps.
15181522
if (params.userKey) {
15191523
try {
15201524
const derivedAddress = this.deriveAddressFromPublicKey(params.userKey, index);
@@ -1554,22 +1558,23 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin implem
15541558
const consolidatedTransactions: any[] = [];
15551559
let lastScanIndex = startIdx;
15561560

1561+
// A TSS recovery sweeps the m/0 address derived from the common keychain and never reads
1562+
// walletContractAddress, so the forwarder address was never the address being swept here.
1563+
// Pass the address actually swept, which is also what the key mismatch check compares.
1564+
// Loop-invariant, and left undefined when no keychain was supplied so recover() reports that.
1565+
const tssCommonKeychain = params.isTss ? this.getTssCommonKeychain(params) : '';
1566+
const tssBaseAddress = tssCommonKeychain ? this.deriveAddressFromPublicKey(tssCommonKeychain, 0) : undefined;
1567+
15571568
for (let i = startIdx; i < endIdx; i++) {
15581569
const consolidationAddress = this.getConsolidationAddress(params, i);
15591570
for (const address of consolidationAddress) {
1560-
// A TSS recovery sweeps the m/0 address derived from the common keychain and never reads
1561-
// walletContractAddress, so the forwarder address was never the address being swept here.
1562-
// Pass the address actually swept, which is also what the key mismatch check compares.
1563-
const recoverWalletContractAddress = params.isTss
1564-
? this.deriveAddressFromPublicKey(this.getTssCommonKeychain(params), 0)
1565-
: address;
15661571
const recoverParams = {
15671572
apiKey: params.apiKey,
15681573
backupKey: params.backupKey || '',
15691574
gasLimit: params.gasLimit,
15701575
recoveryDestination: params.recoveryDestination || '',
15711576
userKey: params.userKey || '',
1572-
walletContractAddress: recoverWalletContractAddress,
1577+
walletContractAddress: tssBaseAddress ?? address,
15731578
derivationSeed: '',
15741579
isTss: params.isTss,
15751580
eip1559: {
@@ -1635,18 +1640,9 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin implem
16351640
}
16361641

16371642
this.validateRecoveryParams(params);
1638-
// validateRecoveryParams guarantees both keys are present outside of unsigned TSS sweeps,
1639-
// which never reach this non-TSS path. Classify from the RAW keys, as before the type change:
1640-
// stripping whitespace first would flip the result for a key pasted with a leading newline.
1641-
const rawUserKey = params.userKey ?? '';
1642-
const rawBackupKey = params.backupKey ?? '';
1643-
const isUnsignedSweep =
1644-
params.isUnsignedSweep ??
1645-
getIsUnsignedSweep({ userKey: rawUserKey, backupKey: rawBackupKey, isTss: params.isTss });
1646-
1647-
// Clean up whitespace from entered values
1648-
let userKey = rawUserKey.replace(/\s/g, '');
1649-
const backupKey = rawBackupKey.replace(/\s/g, '');
1643+
const recoveryKeys = classifyRecoveryKeys(params);
1644+
const { backupKey, isUnsignedSweep } = recoveryKeys;
1645+
let userKey = recoveryKeys.userKey;
16501646
const gasLimit = new optionalDeps.ethUtil.BN(this.setGasLimit(params.gasLimit));
16511647
const gasPrice = params.eip1559
16521648
? new optionalDeps.ethUtil.BN(params.eip1559.maxFeePerGas)

‎modules/abstract-eth/src/ethLikeToken.ts‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import { bip32 } from '@bitgo/secp256k1';
77
import { BigNumber } from 'bignumber.js';
88

99
import {
10+
classifyRecoveryKeys,
1011
BitGoBase,
1112
CoinConstructor,
1213
NamedCoinConstructor,
1314
ErrorNoInputToRecover,
1415
RecoveryProviderError,
15-
getIsUnsignedSweep,
1616
Util,
1717
} from '@bitgo/sdk-core';
1818
import {
@@ -213,17 +213,9 @@ export class EthLikeToken extends AbstractEthLikeNewCoins {
213213
throw new Error('invalid recoveryDestination');
214214
}
215215

216-
// Classify from the RAW keys, as before the type change: stripping whitespace first would
217-
// flip the result for a key pasted with a leading newline.
218-
const rawUserKey = params.userKey ?? '';
219-
const rawBackupKey = params.backupKey ?? '';
220-
const isUnsignedSweep =
221-
params.isUnsignedSweep ??
222-
getIsUnsignedSweep({ userKey: rawUserKey, backupKey: rawBackupKey, isTss: params.isTss });
223-
224-
// Clean up whitespace from entered values
225-
let userKey = rawUserKey.replace(/\s/g, '');
226-
const backupKey = rawBackupKey.replace(/\s/g, '');
216+
const recoveryKeys = classifyRecoveryKeys(params);
217+
const { backupKey, isUnsignedSweep } = recoveryKeys;
218+
let userKey = recoveryKeys.userKey;
227219

228220
const gasLimit = new optionalDeps.ethUtil.BN(this.setGasLimit(params.gasLimit));
229221
const gasPrice = params.eip1559

‎modules/sdk-coin-avaxc/src/avaxc.ts‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import {
1515
EthereumNetwork,
1616
} from '@bitgo/statics';
1717
import {
18+
classifyRecoveryKeys,
1819
BaseCoin,
1920
BaseTransaction,
2021
BitGoBase,
2122
common,
2223
ErrorNoInputToRecover,
2324
FeeEstimateOptions,
2425
FullySignedTransaction,
25-
getIsUnsignedSweep,
2626
InvalidAddressError,
2727
IWallet,
2828
KeyPair,
@@ -659,16 +659,9 @@ export class AvaxC extends AbstractEthLikeNewCoins {
659659
throw new Error('invalid recoveryDestination');
660660
}
661661

662-
// Classify from the RAW keys, as before the type change: stripping whitespace first would
663-
// flip the result for a key pasted with a leading newline.
664-
const rawUserKey = params.userKey ?? '';
665-
const rawBackupKey = params.backupKey ?? '';
666-
// TODO (BG-56531): add support for krs
667-
const isUnsignedSweep = getIsUnsignedSweep({ userKey: rawUserKey, backupKey: rawBackupKey, isTss: params.isTss });
668-
669-
// Clean up whitespace from entered values
670-
let userKey = rawUserKey.replace(/\s/g, '');
671-
const backupKey = rawBackupKey.replace(/\s/g, '');
662+
const recoveryKeys = classifyRecoveryKeys({ ...params, isUnsignedSweep: undefined });
663+
const { backupKey, isUnsignedSweep } = recoveryKeys;
664+
let userKey = recoveryKeys.userKey;
672665

673666
// Set new tx fees (using default config values from platform)
674667
const gasLimit = new optionalDeps.ethUtil.BN(this.setGasLimit(params.gasLimit));
@@ -711,7 +704,12 @@ export class AvaxC extends AbstractEthLikeNewCoins {
711704
}
712705
backupKeyAddress = keyPair.getAddress();
713706
}
714-
await this.assertRecoveryKeysAreWalletSigners(userKey, backupKeyAddress, params.walletContractAddress);
707+
await this.assertRecoveryKeysAreWalletSigners(
708+
userKey,
709+
backupKeyAddress,
710+
params.walletContractAddress,
711+
params.apiKey
712+
);
715713

716714
const backupKeyNonce = await this.getAddressNonce(backupKeyAddress);
717715

‎modules/sdk-coin-etc/src/etc.ts‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import {
1212
SignTransactionOptions,
1313
} from '@bitgo/abstract-eth';
1414
import {
15+
classifyRecoveryKeys,
1516
BaseCoin,
1617
BitGoBase,
1718
ErrorNoInputToRecover,
1819
common,
19-
getIsUnsignedSweep,
2020
Util,
2121
Recipient,
2222
RecoveryProviderError,
@@ -80,15 +80,9 @@ export class Etc extends AbstractEthLikeCoin {
8080
async recover(params: RecoverOptions): Promise<RecoveryInfo | OfflineVaultTxInfo> {
8181
this.validateRecoveryParams(params);
8282

83-
// Classify from the RAW keys, as before the type change: stripping whitespace first would
84-
// flip the result for a key pasted with a leading newline.
85-
const rawUserKey = params.userKey ?? '';
86-
const rawBackupKey = params.backupKey ?? '';
87-
const isUnsignedSweep = getIsUnsignedSweep({ userKey: rawUserKey, backupKey: rawBackupKey, isTss: params.isTss });
88-
89-
// Clean up whitespace from entered values
90-
let userKey = rawUserKey.replace(/\s/g, '');
91-
const backupKey = rawBackupKey.replace(/\s/g, '');
83+
const recoveryKeys = classifyRecoveryKeys({ ...params, isUnsignedSweep: undefined });
84+
const { backupKey, isUnsignedSweep } = recoveryKeys;
85+
let userKey = recoveryKeys.userKey;
9286
const gasLimit = new optionalDeps.ethUtil.BN(this.setGasLimit(params.gasLimit));
9387
const gasPrice = params.eip1559
9488
? new optionalDeps.ethUtil.BN(params.eip1559.maxFeePerGas)

‎modules/sdk-coin-eth/src/erc20Token.ts‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
* @prettier
33
*/
44
import {
5+
classifyRecoveryKeys,
56
BitGoBase,
67
CoinConstructor,
78
ErrorNoInputToRecover,
89
RecoveryProviderError,
910
Util,
1011
checkKrsProvider,
11-
getIsKrsRecovery,
12-
getIsUnsignedSweep,
1312
MPCAlgorithm,
1413
NamedCoinConstructor,
1514
} from '@bitgo/sdk-core';
@@ -152,18 +151,9 @@ export class Erc20Token extends Eth {
152151
throw new Error('invalid recoveryDestination');
153152
}
154153

155-
// Classify from the RAW keys, as before the type change: stripping whitespace first would
156-
// flip the result for a key pasted with a leading newline.
157-
const rawUserKey = params.userKey ?? '';
158-
const rawBackupKey = params.backupKey ?? '';
159-
const isKrsRecovery = getIsKrsRecovery({ userKey: rawUserKey, backupKey: rawBackupKey });
160-
const isUnsignedSweep =
161-
params.isUnsignedSweep ??
162-
getIsUnsignedSweep({ userKey: rawUserKey, backupKey: rawBackupKey, isTss: params.isTss });
163-
164-
// Clean up whitespace from entered values
165-
let userKey = rawUserKey.replace(/\s/g, '');
166-
const backupKey = rawBackupKey.replace(/\s/g, '');
154+
const recoveryKeys = classifyRecoveryKeys(params);
155+
const { backupKey, isKrsRecovery, isUnsignedSweep } = recoveryKeys;
156+
let userKey = recoveryKeys.userKey;
167157

168158
if (isKrsRecovery) {
169159
checkKrsProvider(this, params.krsProvider, { checkCoinFamilySupport: false });

‎modules/sdk-coin-eth/src/erc721Token.ts‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
* @prettier
33
*/
44
import {
5+
classifyRecoveryKeys,
56
BitGoBase,
67
CoinConstructor,
78
ErrorNoInputToRecover,
89
RecoveryProviderError,
910
Util,
1011
checkKrsProvider,
11-
getIsKrsRecovery,
12-
getIsUnsignedSweep,
1312
MPCAlgorithm,
1413
NamedCoinConstructor,
1514
} from '@bitgo/sdk-core';
@@ -152,18 +151,9 @@ export class Erc721Token extends Eth {
152151
throw new Error('invalid recoveryDestination');
153152
}
154153

155-
// Classify from the RAW keys, as before the type change: stripping whitespace first would
156-
// flip the result for a key pasted with a leading newline.
157-
const rawUserKey = params.userKey ?? '';
158-
const rawBackupKey = params.backupKey ?? '';
159-
const isKrsRecovery = getIsKrsRecovery({ userKey: rawUserKey, backupKey: rawBackupKey });
160-
const isUnsignedSweep =
161-
params.isUnsignedSweep ??
162-
getIsUnsignedSweep({ userKey: rawUserKey, backupKey: rawBackupKey, isTss: params.isTss });
163-
164-
// Clean up whitespace from entered values
165-
let userKey = rawUserKey.replace(/\s/g, '');
166-
const backupKey = rawBackupKey.replace(/\s/g, '');
154+
const recoveryKeys = classifyRecoveryKeys(params);
155+
const { backupKey, isKrsRecovery, isUnsignedSweep } = recoveryKeys;
156+
let userKey = recoveryKeys.userKey;
167157

168158
if (isKrsRecovery) {
169159
checkKrsProvider(this, params.krsProvider, { checkCoinFamilySupport: false });

‎modules/sdk-coin-eth/src/erc7984Token.ts‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
* @prettier
33
*/
44
import {
5+
classifyRecoveryKeys,
56
BitGoBase,
67
CoinConstructor,
78
checkKrsProvider,
8-
getIsKrsRecovery,
9-
getIsUnsignedSweep,
109
MPCAlgorithm,
1110
NamedCoinConstructor,
1211
RecoveryProviderError,
@@ -950,17 +949,9 @@ export class Erc7984Token extends Eth {
950949
throw new Error('invalid recoveryDestination');
951950
}
952951

953-
// Classify from the RAW keys, as before the type change: stripping whitespace first would
954-
// flip the result for a key pasted with a leading newline.
955-
const rawUserKey = params.userKey ?? '';
956-
const rawBackupKey = params.backupKey ?? '';
957-
const isKrsRecovery = getIsKrsRecovery({ userKey: rawUserKey, backupKey: rawBackupKey });
958-
const isUnsignedSweep =
959-
params.isUnsignedSweep ??
960-
getIsUnsignedSweep({ userKey: rawUserKey, backupKey: rawBackupKey, isTss: params.isTss });
961-
962-
let userKey = rawUserKey.replace(/\s/g, '');
963-
const backupKey = rawBackupKey.replace(/\s/g, '');
952+
const recoveryKeys = classifyRecoveryKeys(params);
953+
const { backupKey, isKrsRecovery, isUnsignedSweep } = recoveryKeys;
954+
let userKey = recoveryKeys.userKey;
964955

965956
if (isKrsRecovery) {
966957
checkKrsProvider(this, params.krsProvider, { checkCoinFamilySupport: false });

‎modules/sdk-coin-eth/src/eth.ts‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ import { bip32 } from '@bitgo/secp256k1';
55
import _ from 'lodash';
66
import request from 'superagent';
77
import {
8+
classifyRecoveryKeys,
89
BaseCoin,
910
BitGoBase,
1011
checkKrsProvider,
1112
common,
1213
ErrorNoInputToRecover,
1314
FullySignedTransaction,
14-
getIsKrsRecovery,
15-
getIsUnsignedSweep,
1615
HalfSignedTransaction,
1716
MPCAlgorithm,
1817
MultisigType,
@@ -206,12 +205,9 @@ export class Eth extends AbstractEthLikeNewCoins {
206205

207206
this.validateRecoveryParams(params);
208207

209-
// Clean up whitespace from entered values
210-
let userKey = (params.userKey ?? '').replace(/\s/g, '');
211-
const backupKey = (params.backupKey ?? '').replace(/\s/g, '');
212-
213-
const isKrsRecovery = getIsKrsRecovery({ userKey, backupKey });
214-
const isUnsignedSweep = params.isUnsignedSweep ?? getIsUnsignedSweep({ userKey, backupKey, isTss: params.isTss });
208+
const recoveryKeys = classifyRecoveryKeys(params);
209+
const { backupKey, isKrsRecovery, isUnsignedSweep } = recoveryKeys;
210+
let userKey = recoveryKeys.userKey;
215211

216212
if (isKrsRecovery) {
217213
checkKrsProvider(this, params.krsProvider, { checkCoinFamilySupport: false });

‎modules/sdk-coin-eth/test/unit/ethSafeRecovery.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,24 @@ describe('Safe EVM TSS recovery end-to-end', function () {
305305
pendingBalanceMocks().should.have.length(2);
306306
});
307307

308+
it('rejects an unsigned sweep with no key material at all', async function () {
309+
// Also pins that validateUnsignedSweepTSSParams is awaited: before that fix this threw
310+
// inside an un-awaited promise, so the caller saw a downstream BigInt error instead and
311+
// the real rejection surfaced as an unhandled rejection.
312+
await basecoin
313+
.recover({
314+
userKey: '',
315+
backupKey: '',
316+
isTss: true,
317+
walletContractAddress,
318+
recoveryDestination,
319+
gasLimit: 200000,
320+
eip1559: { maxFeePerGas: 20000000000, maxPriorityFeePerGas: 10000000000 },
321+
replayProtectionOptions: { chain: '560048', hardfork: 'london' },
322+
})
323+
.should.be.rejectedWith('missing commonKeyChain');
324+
});
325+
308326
it('surfaces an explorer failure rather than reporting an empty wallet', async function () {
309327
const keyParams = await unsignedKeyParams(safeKeys);
310328
nock(baseUrl)

‎modules/sdk-coin-tempo/src/tempo.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ export class Tempo extends AbstractEthLikeNewCoins {
500500
const derivedCommonKeyChain = MPC.deriveUnhardened(commonKeyChain, 'm/0');
501501
const backupKeyPair = new KeyPair({ pub: derivedCommonKeyChain.slice(0, 66) });
502502
const baseAddress = backupKeyPair.getAddress();
503+
this.assertTssBaseAddressMatchesWallet(baseAddress, params);
503504

504505
const { tx: unsignedTx } = await this.buildTempoSweepTx(baseAddress, params);
505506
const serializedHex = await unsignedTx.serialize();
@@ -536,6 +537,11 @@ export class Tempo extends AbstractEthLikeNewCoins {
536537
const derivedCommonKeyChain = MPC.deriveUnhardened(backupKey, derivationPath);
537538
const backupKeyPair = new KeyPair({ pub: derivedCommonKeyChain.slice(0, 66) });
538539
const baseAddress = backupKeyPair.getAddress();
540+
// Fail closed on a wrong keychain, as the base class does. A derivationSeed sweeps a
541+
// pseudorandom child the caller cannot predict, so there is nothing to compare against.
542+
if (!params.derivationSeed) {
543+
this.assertTssBaseAddressMatchesWallet(baseAddress, params);
544+
}
539545

540546
const { tx, nonce, sweepAmount, gasMarginUnits } = await this.buildTempoSweepTx(baseAddress, params);
541547
const serializedHex = await tx.serialize();

0 commit comments

Comments
 (0)