|
| 1 | +import { getTxRequest, KeyIndices, RequestTracer } from '@bitgo/sdk-core'; |
| 2 | +import logger from '../../../logger'; |
| 3 | +import { signAndSendTxRequests } from './transactionRequests'; |
| 4 | +import { MasterApiSpecRouteRequest } from '../routers/masterApiSpec'; |
| 5 | + |
| 6 | +export async function handleSignAndSendTxRequest( |
| 7 | + req: MasterApiSpecRouteRequest<'v1.wallet.txrequest.signAndSend', 'post'>, |
| 8 | +) { |
| 9 | + const enclavedExpressClient = req.enclavedExpressClient; |
| 10 | + const reqId = new RequestTracer(); |
| 11 | + const bitgo = req.bitgo; |
| 12 | + const baseCoin = bitgo.coin(req.params.coin); |
| 13 | + |
| 14 | + const params = req.decoded; |
| 15 | + |
| 16 | + const walletId = req.params.walletId; |
| 17 | + const wallet = await baseCoin.wallets().get({ id: walletId, reqId }); |
| 18 | + if (!wallet) { |
| 19 | + throw new Error(`Wallet ${walletId} not found`); |
| 20 | + } |
| 21 | + |
| 22 | + if (wallet.type() !== 'cold' || wallet.subType() !== 'onPrem') { |
| 23 | + throw new Error('Wallet is not an on-prem wallet'); |
| 24 | + } |
| 25 | + |
| 26 | + const keyIdIndex = params.source === 'user' ? KeyIndices.USER : KeyIndices.BACKUP; |
| 27 | + logger.info(`Key ID index: ${keyIdIndex}`); |
| 28 | + logger.info(`Key IDs: ${JSON.stringify(wallet.keyIds(), null, 2)}`); |
| 29 | + |
| 30 | + // Get the signing keychain |
| 31 | + const signingKeychain = await baseCoin.keychains().get({ |
| 32 | + id: wallet.keyIds()[keyIdIndex], |
| 33 | + }); |
| 34 | + |
| 35 | + if (!signingKeychain) { |
| 36 | + throw new Error(`Signing keychain for ${params.source} not found`); |
| 37 | + } |
| 38 | + if (params.commonKeychain && signingKeychain.commonKeychain !== params.commonKeychain) { |
| 39 | + throw new Error( |
| 40 | + `Common keychain provided does not match the keychain on wallet for ${params.source}`, |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + logger.debug(`Signing keychain: ${JSON.stringify(signingKeychain, null, 2)}`); |
| 45 | + logger.debug(`Params: ${JSON.stringify(params, null, 2)}`); |
| 46 | + |
| 47 | + const txRequest = await getTxRequest(bitgo, wallet.id(), params.txRequestId, reqId); |
| 48 | + if (!txRequest) { |
| 49 | + throw new Error(`TxRequest ${params.txRequestId} not found`); |
| 50 | + } |
| 51 | + |
| 52 | + logger.debug(`TxRequest: ${JSON.stringify(txRequest, null, 2)}`); |
| 53 | + |
| 54 | + return signAndSendTxRequests( |
| 55 | + bitgo, |
| 56 | + wallet, |
| 57 | + txRequest, |
| 58 | + enclavedExpressClient, |
| 59 | + signingKeychain, |
| 60 | + reqId, |
| 61 | + ); |
| 62 | +} |
0 commit comments