|
1 | | -import { BaseCoin, BitGoAPI, MethodNotImplementedError } from 'bitgo'; |
2 | | - |
3 | | -import { AbstractEthLikeNewCoins } from '@bitgo/abstract-eth'; |
4 | | -import { AbstractUtxoCoin } from '@bitgo/abstract-utxo'; |
5 | | - |
6 | | -import assert from 'assert'; |
7 | | - |
8 | | -import { |
9 | | - isEddsaCoin, |
10 | | - isEthLikeCoin, |
11 | | - isFormattedOfflineVaultTxInfo, |
12 | | - isUtxoCoin, |
13 | | -} from '../../../shared/coinUtils'; |
14 | | -import { |
15 | | - DEFAULT_MUSIG_ETH_GAS_PARAMS, |
16 | | - getReplayProtectionOptions, |
17 | | -} from '../../../shared/recoveryUtils'; |
18 | | -import { EnclavedExpressClient } from '../clients/enclavedExpressClient'; |
19 | 1 | import { MasterApiSpecRouteRequest } from '../routers/masterApiSpec'; |
20 | | - |
21 | | -interface RecoveryParams { |
22 | | - userKey: string; |
23 | | - backupKey: string; |
24 | | - walletContractAddress: string; |
25 | | - recoveryDestination: string; |
26 | | - apiKey: string; |
27 | | -} |
28 | | - |
29 | | -interface EnclavedRecoveryParams { |
30 | | - userPub: string; |
31 | | - backupPub: string; |
32 | | - apiKey: string; |
33 | | - unsignedSweepPrebuildTx: any; // TODO: type this properly once we have the SDK types |
34 | | - coinSpecificParams?: Record<string, undefined>; |
35 | | - walletContractAddress: string; |
36 | | -} |
37 | | - |
38 | | -async function handleEthLikeRecovery( |
39 | | - sdkCoin: BaseCoin, |
40 | | - commonRecoveryParams: RecoveryParams, |
41 | | - enclavedExpressClient: any, |
42 | | - params: EnclavedRecoveryParams, |
43 | | - env: EnvironmentName, |
44 | | -) { |
45 | | - try { |
46 | | - const { gasLimit, gasPrice, maxFeePerGas, maxPriorityFeePerGas } = DEFAULT_MUSIG_ETH_GAS_PARAMS; |
47 | | - const unsignedSweepPrebuildTx = await (sdkCoin as AbstractEthLikeNewCoins).recover({ |
48 | | - ...commonRecoveryParams, |
49 | | - gasPrice, |
50 | | - gasLimit, |
51 | | - eip1559: { |
52 | | - maxFeePerGas, |
53 | | - maxPriorityFeePerGas, |
54 | | - }, |
55 | | - replayProtectionOptions: getReplayProtectionOptions(env), |
56 | | - }); |
57 | | - |
58 | | - const fullSignedRecoveryTx = await enclavedExpressClient.recoveryMultisig({ |
59 | | - ...params, |
60 | | - unsignedSweepPrebuildTx, |
61 | | - }); |
62 | | - |
63 | | - return fullSignedRecoveryTx; |
64 | | - } catch (err) { |
65 | | - throw err; |
66 | | - } |
67 | | -} |
68 | | - |
69 | | -export type UtxoCoinSpecificRecoveryParams = Pick< |
70 | | - Parameters<AbstractUtxoCoin['recover']>[0], |
71 | | - | 'apiKey' |
72 | | - | 'userKey' |
73 | | - | 'backupKey' |
74 | | - | 'bitgoKey' |
75 | | - | 'ignoreAddressTypes' |
76 | | - | 'scan' |
77 | | - | 'feeRate' |
78 | | - | 'recoveryDestination' |
79 | | ->; |
80 | | - |
81 | | -async function handleUtxoLikeRecovery( |
82 | | - sdkCoin: BaseCoin, |
83 | | - enclavedClient: EnclavedExpressClient, |
84 | | - recoveryParams: UtxoCoinSpecificRecoveryParams, |
85 | | -): Promise<{ txHex: string }> { |
86 | | - const abstractUtxoCoin = sdkCoin as unknown as AbstractUtxoCoin; |
87 | | - const recoverTx = await abstractUtxoCoin.recover(recoveryParams); |
88 | | - |
89 | | - logger.info('UTXO recovery transaction created:', recoverTx); |
90 | | - if (!isFormattedOfflineVaultTxInfo(recoverTx)) { |
91 | | - throw new MethodNotImplementedError(`Unknown transaction ${JSON.stringify(recoverTx)} created`); |
92 | | - } |
93 | | - |
94 | | - return (await enclavedClient.recoveryMultisig({ |
95 | | - userPub: recoveryParams.userKey, |
96 | | - backupPub: recoveryParams.backupKey, |
97 | | - bitgoPub: recoveryParams.bitgoKey, |
98 | | - unsignedSweepPrebuildTx: recoverTx, |
99 | | - walletContractAddress: '', |
100 | | - })) as { txHex: string }; |
101 | | -} |
102 | | - |
103 | | -export async function handleRecoveryWalletOnPrem( |
104 | | - req: MasterApiSpecRouteRequest<'v1.wallet.recovery', 'post'>, |
| 2 | +import logger from '../../../logger'; |
| 3 | +import { isSolCoin } from '../../../shared/coinUtils'; |
| 4 | +import { MPCTx } from 'bitgo'; |
| 5 | +import { RecoveryTransaction } from '@bitgo/sdk-coin-trx'; |
| 6 | + |
| 7 | +// Handler for recovery from receive addresses (consolidation sweeps) |
| 8 | +export async function handleRecoveryConsolidationsOnPrem( |
| 9 | + req: MasterApiSpecRouteRequest<'v1.wallet.recoveryConsolidations', 'post'>, |
105 | 10 | ) { |
106 | 11 | const bitgo = req.bitgo; |
107 | 12 | const coin = req.decoded.coin; |
108 | 13 | const enclavedExpressClient = req.enclavedExpressClient; |
109 | | - const { recoveryDestinationAddress, coinSpecificParams } = req.decoded; |
110 | | - |
111 | | - const sdkCoin = bitgo.coin(coin); |
112 | 14 |
|
113 | | - // Handle TSS recovery |
114 | | - if (req.decoded.isTssRecovery) { |
115 | | - assert(req.decoded.tssRecoveryParams, 'TSS recovery parameters are required'); |
116 | | - const { commonKeychain } = req.decoded.tssRecoveryParams; |
117 | | - if (!commonKeychain) { |
118 | | - throw new Error('Common keychain is required for TSS recovery'); |
119 | | - } |
| 15 | + const { userPub, backupPub, bitgoPub } = req.decoded; |
120 | 16 |
|
121 | | - if (isEddsaCoin(sdkCoin)) { |
122 | | - return handleEddsaRecovery( |
123 | | - req.bitgo, |
124 | | - sdkCoin, |
125 | | - { |
126 | | - userKey: commonKeychain, |
127 | | - backupKey: commonKeychain, |
128 | | - walletContractAddress: '', |
129 | | - recoveryDestination: recoveryDestinationAddress, |
130 | | - apiKey: req.decoded.apiKey || '', |
131 | | - }, |
132 | | - enclavedExpressClient, |
133 | | - { |
134 | | - userPub: commonKeychain, |
135 | | - backupPub: commonKeychain, |
136 | | - apiKey: '', |
137 | | - walletContractAddress: '', |
138 | | - unsignedSweepPrebuildTx: undefined, |
139 | | - coinSpecificParams: undefined, |
140 | | - }, |
141 | | - ); |
142 | | - } else { |
143 | | - throw new MethodNotImplementedError( |
144 | | - `TSS recovery is not implemented for coin: ${coin}. Supported coins are Eddsa coins.`, |
145 | | - ); |
146 | | - } |
147 | | - } |
148 | | - |
149 | | - // Handle standard recovery |
150 | | - if (!req.decoded.multiSigRecoveryParams) { |
151 | | - throw new Error('MultiSig recovery parameters are required for standard recovery'); |
152 | | - } |
153 | | - |
154 | | - const { userPub, backupPub, bitgoPub, walletContractAddress } = |
155 | | - req.decoded.multiSigRecoveryParams; |
156 | | - const apiKey = req.decoded.apiKey || ''; |
157 | | - |
158 | | - if (!userPub || !backupPub) { |
159 | | - throw new Error('Missing required fields for standard recovery'); |
| 17 | + const sdkCoin = bitgo.coin(coin); |
| 18 | + let txs: MPCTx[] | RecoveryTransaction[] = []; |
| 19 | + // 1. Build unsigned consolidations |
| 20 | + if (isSolCoin(sdkCoin) && !req.decoded.durableNonces) { |
| 21 | + throw new Error('durableNonces is required for Solana consolidation recovery'); |
160 | 22 | } |
161 | 23 |
|
162 | | - // Check if the public key is valid |
163 | | - if (!sdkCoin.isValidPub(userPub)) { |
164 | | - throw new Error('Invalid user public key format'); |
165 | | - } else if (!sdkCoin.isValidPub(backupPub)) { |
166 | | - throw new Error('Invalid backup public key format'); |
| 24 | + if (typeof (sdkCoin as any).recoverConsolidations !== 'function') { |
| 25 | + throw new Error(`recoverConsolidations is not supported for coin: ${coin}`); |
167 | 26 | } |
168 | 27 |
|
169 | | - const commonRecoveryParams: RecoveryParams = { |
| 28 | + // Use type assertion to access recoverConsolidations |
| 29 | + const result = await (sdkCoin as any).recoverConsolidations({ |
| 30 | + ...req.decoded, |
170 | 31 | userKey: userPub, |
171 | 32 | backupKey: backupPub, |
172 | | - walletContractAddress, |
173 | | - recoveryDestination: recoveryDestinationAddress, |
174 | | - apiKey, |
175 | | - }; |
| 33 | + bitgoKey: bitgoPub, |
| 34 | + durableNonces: req.decoded.durableNonces, |
| 35 | + }); |
| 36 | + |
| 37 | + if ('transactions' in result) { |
| 38 | + txs = result.transactions; |
| 39 | + } else if ('txRequests' in result) { |
| 40 | + txs = result.txRequests; |
| 41 | + } else { |
| 42 | + throw new Error('recoverConsolidations did not return expected transactions'); |
| 43 | + } |
176 | 44 |
|
177 | | - if (isEthLikeCoin(sdkCoin)) { |
178 | | - if (!walletContractAddress) { |
179 | | - throw new Error('Missing walletContract address'); |
180 | | - } |
181 | | - return handleEthLikeRecovery( |
182 | | - sdkCoin, |
183 | | - commonRecoveryParams, |
184 | | - enclavedExpressClient, |
185 | | - { |
| 45 | + logger.debug(`Found ${txs.length} unsigned consolidation transactions`); |
| 46 | + |
| 47 | + // 2. For each unsigned sweep, get it signed by EBE (using recoveryMultisig) |
| 48 | + const signedTxs = []; |
| 49 | + try { |
| 50 | + for (const tx of txs) { |
| 51 | + const signedTx = await enclavedExpressClient.recoveryMultisig({ |
186 | 52 | userPub, |
187 | 53 | backupPub, |
188 | | - apiKey, |
189 | | - unsignedSweepPrebuildTx: undefined, |
190 | | - coinSpecificParams: undefined, |
191 | | - walletContractAddress, |
192 | | - }, |
193 | | - bitgo.env as EnvironmentName, |
194 | | - ); |
195 | | - } |
196 | | - if (!bitgoPub) { |
197 | | - throw new Error('BitGo public key is required for recovery'); |
198 | | - } |
| 54 | + unsignedSweepPrebuildTx: tx, |
| 55 | + walletContractAddress: '', |
| 56 | + }); |
| 57 | + signedTxs.push(signedTx); |
| 58 | + } |
199 | 59 |
|
200 | | - if (isUtxoCoin(sdkCoin)) { |
201 | | - return handleUtxoLikeRecovery(sdkCoin, req.enclavedExpressClient, { |
202 | | - userKey: userPub, |
203 | | - backupKey: backupPub, |
204 | | - bitgoKey: bitgoPub, |
205 | | - ignoreAddressTypes: coinSpecificParams?.ignoreAddressTypes ?? [], |
206 | | - scan: coinSpecificParams?.addressScan, |
207 | | - feeRate: coinSpecificParams?.feeRate, |
208 | | - recoveryDestination: recoveryDestinationAddress, |
209 | | - apiKey, |
210 | | - }); |
| 60 | + return { signedTxs }; |
| 61 | + } catch (err) { |
| 62 | + logger.error('Error during consolidation recovery:', err); |
| 63 | + throw err; |
211 | 64 | } |
212 | | - |
213 | | - throw new MethodNotImplementedError('Recovery wallet is not supported for this coin: ' + coin); |
214 | 65 | } |
0 commit comments