Skip to content

Commit dfef449

Browse files
committed
feat(abstract-eth): update the derive address logic for recovery
ticket: WIN-5700
1 parent b4cb2f0 commit dfef449

File tree

1 file changed

+65
-49
lines changed

1 file changed

+65
-49
lines changed

modules/abstract-eth/src/abstractEthLikeNewCoins.ts

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,40 +1232,57 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
12321232
return calculateForwarderV1Address(forwarderFactoryAddress, calculationSalt, initCode);
12331233
}
12341234

1235-
deriveAddressFromPublicKey(publicKey: string): string {
1235+
deriveAddressFromPublicKey(commonKeychain: string, index: number): string {
1236+
const derivationPath = `m/${index}`;
1237+
const pubkeySize = 33;
1238+
1239+
const ecdsaMpc = new Ecdsa();
1240+
const derivedPublicKey = Buffer.from(ecdsaMpc.deriveUnhardened(commonKeychain, derivationPath), 'hex')
1241+
.subarray(0, pubkeySize)
1242+
.toString('hex');
1243+
1244+
const publicKey = Buffer.from(derivedPublicKey, 'hex').slice(0, 66).toString('hex');
1245+
12361246
const keyPair = new KeyPairLib({ pub: publicKey });
12371247
const address = keyPair.getAddress();
12381248
return address;
12391249
}
12401250

1241-
getConsolidationAddress(params: EthConsolidationRecoveryOptions, index: number): string {
1251+
getConsolidationAddress(params: EthConsolidationRecoveryOptions, index: number): string[] {
1252+
const possibleConsolidationAddresses: string[] = [];
12421253
if (params.walletContractAddress && params.bitgoFeeAddress) {
12431254
const ethNetwork = this.getNetwork();
12441255
const forwarderFactoryAddress = ethNetwork?.walletV4ForwarderFactoryAddress as string;
12451256
const forwarderImplementationAddress = ethNetwork?.walletV4ForwarderImplementationAddress as string;
12461257
try {
1247-
return this.generateForwarderAddress(
1258+
const forwarderAddress = this.generateForwarderAddress(
12481259
params.walletContractAddress,
12491260
params.bitgoFeeAddress,
12501261
forwarderFactoryAddress,
12511262
forwarderImplementationAddress,
12521263
index
12531264
);
1265+
possibleConsolidationAddresses.push(forwarderAddress);
12541266
} catch (e) {
12551267
console.log(`Failed to generate forwarder address: ${e.message}`);
12561268
}
12571269
}
12581270

12591271
if (params.userKey) {
12601272
try {
1261-
return this.deriveAddressFromPublicKey(params.userKey);
1273+
const derivedAddress = this.deriveAddressFromPublicKey(params.userKey, index);
1274+
possibleConsolidationAddresses.push(derivedAddress);
12621275
} catch (e) {
1263-
console.log(`Failed to generate derived address: ${e.message}`);
1276+
console.log(`Failed to generate derived address: ${e}`);
12641277
}
12651278
}
1266-
throw new Error(
1267-
'Unable to generate consolidation address. Check that wallet contract address, fee address, or user key is valid.'
1268-
);
1279+
1280+
if (possibleConsolidationAddresses.length === 0) {
1281+
throw new Error(
1282+
'Unable to generate consolidation address. Check that wallet contract address, fee address, or user key is valid.'
1283+
);
1284+
}
1285+
return possibleConsolidationAddresses;
12691286
}
12701287

12711288
async recoverConsolidations(params: EthConsolidationRecoveryOptions): Promise<Record<string, unknown> | undefined> {
@@ -1292,50 +1309,49 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
12921309

12931310
for (let i = startIdx; i < endIdx; i++) {
12941311
const consolidationAddress = this.getConsolidationAddress(params, i);
1295-
1296-
const recoverParams = {
1297-
apiKey: params.apiKey,
1298-
backupKey: params.backupKey,
1299-
gasLimit: params.gasLimit,
1300-
recoveryDestination: params.recoveryDestination,
1301-
userKey: params.userKey,
1302-
walletContractAddress: consolidationAddress,
1303-
derivationSeed: '',
1304-
isTss: params.isTss,
1305-
eip1559: {
1306-
maxFeePerGas: params.eip1559?.maxFeePerGas || 20,
1307-
maxPriorityFeePerGas: params.eip1559?.maxPriorityFeePerGas || 200000,
1308-
},
1309-
replayProtectionOptions: {
1310-
chain: params.replayProtectionOptions?.chain || 0,
1311-
hardfork: params.replayProtectionOptions?.hardfork || 'london',
1312-
},
1313-
bitgoKey: '',
1314-
ignoreAddressTypes: [],
1315-
};
1316-
1317-
let recoveryTransaction;
1318-
try {
1319-
recoveryTransaction = await this.recover(recoverParams);
1320-
} catch (e) {
1321-
if (
1322-
e.message === 'Did not find address with funds to recover' ||
1323-
e.message === 'Did not find token account to recover tokens, please check token account' ||
1324-
e.message === 'Not enough token funds to recover'
1325-
) {
1326-
lastScanIndex = i;
1327-
continue;
1312+
for (const address of consolidationAddress) {
1313+
const recoverParams = {
1314+
apiKey: params.apiKey,
1315+
backupKey: params.backupKey,
1316+
gasLimit: params.gasLimit,
1317+
recoveryDestination: params.recoveryDestination,
1318+
userKey: params.userKey,
1319+
walletContractAddress: address,
1320+
derivationSeed: '',
1321+
isTss: params.isTss,
1322+
eip1559: {
1323+
maxFeePerGas: params.eip1559?.maxFeePerGas || 20,
1324+
maxPriorityFeePerGas: params.eip1559?.maxPriorityFeePerGas || 200000,
1325+
},
1326+
replayProtectionOptions: {
1327+
chain: params.replayProtectionOptions?.chain || 0,
1328+
hardfork: params.replayProtectionOptions?.hardfork || 'london',
1329+
},
1330+
bitgoKey: '',
1331+
ignoreAddressTypes: [],
1332+
};
1333+
let recoveryTransaction;
1334+
try {
1335+
recoveryTransaction = await this.recover(recoverParams);
1336+
} catch (e) {
1337+
if (
1338+
e.message === 'Did not find address with funds to recover' ||
1339+
e.message === 'Did not find token account to recover tokens, please check token account' ||
1340+
e.message === 'Not enough token funds to recover'
1341+
) {
1342+
lastScanIndex = i;
1343+
continue;
1344+
}
1345+
throw e;
1346+
}
1347+
if (isUnsignedSweep) {
1348+
consolidationTransactions.push((recoveryTransaction as MPCSweepTxs).txRequests[0]);
1349+
} else {
1350+
consolidationTransactions.push(recoveryTransaction);
13281351
}
1329-
throw e;
1330-
}
1331-
1332-
if (isUnsignedSweep) {
1333-
consolidationTransactions.push((recoveryTransaction as MPCSweepTxs).txRequests[0]);
1334-
} else {
1335-
consolidationTransactions.push(recoveryTransaction);
13361352
}
13371353

1338-
lastScanIndex = i;
1354+
// lastScanIndex = i;
13391355
}
13401356

13411357
if (consolidationTransactions.length === 0) {

0 commit comments

Comments
 (0)