|
1 | | -import { RequestTracer, KeyIndices, CustomSigningFunction } from '@bitgo/sdk-core'; |
| 1 | +import { RequestTracer, KeyIndices } from '@bitgo/sdk-core'; |
2 | 2 | import logger from '../../../logger'; |
3 | 3 | import { MasterApiSpecRouteRequest } from '../routers/masterApiSpec'; |
| 4 | +import { getWalletAndSigningKeychain, makeCustomSigningFunction } from '../../../shared/coinUtils'; |
4 | 5 |
|
5 | 6 | export async function handleConsolidateUnspents( |
6 | | - req: MasterApiSpecRouteRequest<'v1.wallet.consolidateUnspents', 'post'>, |
| 7 | + req: MasterApiSpecRouteRequest<'v1.wallet.consolidateunspents', 'post'>, |
7 | 8 | ) { |
8 | 9 | const enclavedExpressClient = req.enclavedExpressClient; |
9 | 10 | const reqId = new RequestTracer(); |
10 | 11 | const bitgo = req.bitgo; |
11 | | - const baseCoin = bitgo.coin(req.params.coin); |
12 | 12 | const params = req.decoded; |
13 | 13 | const walletId = req.params.walletId; |
14 | | - const wallet = await baseCoin.wallets().get({ id: walletId, reqId }); |
15 | | - |
16 | | - if (!wallet) { |
17 | | - throw new Error(`Wallet ${walletId} not found`); |
18 | | - } |
19 | | - |
20 | | - // Get the signing keychain based on source |
21 | | - const keyIdIndex = params.source === 'user' ? KeyIndices.USER : KeyIndices.BACKUP; |
22 | | - const signingKeychain = await baseCoin.keychains().get({ |
23 | | - id: wallet.keyIds()[keyIdIndex], |
| 14 | + const coin = req.params.coin; |
| 15 | + |
| 16 | + const { wallet, signingKeychain } = await getWalletAndSigningKeychain({ |
| 17 | + bitgo, |
| 18 | + coin, |
| 19 | + walletId, |
| 20 | + params, |
| 21 | + reqId, |
| 22 | + KeyIndices, |
24 | 23 | }); |
25 | 24 |
|
26 | | - if (!signingKeychain || !signingKeychain.pub) { |
27 | | - throw new Error(`Signing keychain for ${params.source} not found`); |
28 | | - } |
29 | | - |
30 | | - if (params.pubkey && params.pubkey !== signingKeychain.pub) { |
31 | | - throw new Error(`Pub provided does not match the keychain on wallet for ${params.source}`); |
32 | | - } |
33 | | - |
34 | 25 | try { |
35 | 26 | // Create custom signing function that delegates to EBE |
36 | | - const customSigningFunction: CustomSigningFunction = async (signParams) => { |
37 | | - const signedTx = await enclavedExpressClient.signMultisig({ |
38 | | - txPrebuild: signParams.txPrebuild, |
39 | | - source: params.source, |
40 | | - pub: signingKeychain.pub!, |
41 | | - }); |
42 | | - return signedTx; |
43 | | - }; |
| 27 | + const customSigningFunction = makeCustomSigningFunction({ |
| 28 | + enclavedExpressClient, |
| 29 | + source: params.source, |
| 30 | + pub: signingKeychain.pub!, |
| 31 | + }); |
44 | 32 |
|
45 | 33 | // Prepare consolidation parameters |
46 | 34 | const consolidationParams = { |
|
0 commit comments