|
| 1 | +import { |
| 2 | + BitGoBase, |
| 3 | + getTxRequest, |
| 4 | + Wallet, |
| 5 | + TxRequest, |
| 6 | + IRequestTracer, |
| 7 | + EcdsaMPCv2Utils, |
| 8 | + commonTssMethods, |
| 9 | + RequestType, |
| 10 | +} from '@bitgo/sdk-core'; |
| 11 | +import { EnclavedExpressClient } from '../clients/enclavedExpressClient'; |
| 12 | +import logger from '../../../logger'; |
| 13 | +import { sendTxRequest } from '@bitgo/sdk-core/dist/src/bitgo/tss/common'; |
| 14 | + |
| 15 | +export async function handleEcdsaSigning( |
| 16 | + bitgo: BitGoBase, |
| 17 | + wallet: Wallet, |
| 18 | + txRequest: string | TxRequest, |
| 19 | + enclavedExpressClient: EnclavedExpressClient, |
| 20 | + source: 'user' | 'backup', |
| 21 | + commonKeychain: string, |
| 22 | + reqId?: IRequestTracer, |
| 23 | +) { |
| 24 | + let txRequestResolved: TxRequest; |
| 25 | + let txRequestId: string; |
| 26 | + const ecdsaMPCv2Utils = new EcdsaMPCv2Utils(bitgo, wallet.baseCoin); |
| 27 | + |
| 28 | + if (typeof txRequest === 'string') { |
| 29 | + txRequestResolved = await getTxRequest(bitgo, wallet.id(), txRequest, reqId); |
| 30 | + txRequestId = txRequestResolved.txRequestId; |
| 31 | + } else { |
| 32 | + txRequestResolved = txRequest; |
| 33 | + txRequestId = txRequest.txRequestId; |
| 34 | + } |
| 35 | + |
| 36 | + // Get BitGo GPG key for MPCv2 |
| 37 | + const bitgoGpgKey = await ecdsaMPCv2Utils.getBitgoPublicGpgKey(); |
| 38 | + |
| 39 | + // Round 1: Generate user's Round 1 share |
| 40 | + const { |
| 41 | + signatureShareRound1, |
| 42 | + userGpgPubKey, |
| 43 | + encryptedRound1Session, |
| 44 | + encryptedUserGpgPrvKey, |
| 45 | + encryptedDataKey, |
| 46 | + } = await enclavedExpressClient.signMpcV2Round1({ |
| 47 | + txRequest: txRequestResolved, |
| 48 | + bitgoGpgPubKey: bitgoGpgKey.armor(), |
| 49 | + source, |
| 50 | + pub: commonKeychain, |
| 51 | + }); |
| 52 | + |
| 53 | + // Send Round 1 share to BitGo and get updated txRequest |
| 54 | + const round1TxRequest = await commonTssMethods.sendSignatureShareV2( |
| 55 | + bitgo, |
| 56 | + wallet.id(), |
| 57 | + txRequestId, |
| 58 | + [signatureShareRound1], |
| 59 | + RequestType.tx, |
| 60 | + wallet.baseCoin.getMPCAlgorithm(), |
| 61 | + userGpgPubKey, |
| 62 | + undefined, |
| 63 | + wallet.multisigTypeVersion(), |
| 64 | + reqId, |
| 65 | + ); |
| 66 | + |
| 67 | + // Round 2: Generate user's Round 2 share |
| 68 | + const { signatureShareRound2, encryptedRound2Session } = |
| 69 | + await enclavedExpressClient.signMpcV2Round2({ |
| 70 | + txRequest: round1TxRequest, |
| 71 | + bitgoGpgPubKey: bitgoGpgKey.armor(), |
| 72 | + encryptedDataKey, |
| 73 | + encryptedUserGpgPrvKey, |
| 74 | + encryptedRound1Session, |
| 75 | + source, |
| 76 | + pub: commonKeychain, |
| 77 | + }); |
| 78 | + |
| 79 | + // Send Round 2 share to BitGo and get updated txRequest |
| 80 | + const round2TxRequest = await commonTssMethods.sendSignatureShareV2( |
| 81 | + bitgo, |
| 82 | + wallet.id(), |
| 83 | + txRequestId, |
| 84 | + [signatureShareRound2], |
| 85 | + RequestType.tx, |
| 86 | + wallet.baseCoin.getMPCAlgorithm(), |
| 87 | + userGpgPubKey, |
| 88 | + undefined, |
| 89 | + wallet.multisigTypeVersion(), |
| 90 | + reqId, |
| 91 | + ); |
| 92 | + |
| 93 | + // Round 3: Generate user's Round 3 share |
| 94 | + const { signatureShareRound3 } = await enclavedExpressClient.signMpcV2Round3({ |
| 95 | + txRequest: round2TxRequest, |
| 96 | + bitgoGpgPubKey: bitgoGpgKey.armor(), |
| 97 | + encryptedDataKey, |
| 98 | + encryptedUserGpgPrvKey, |
| 99 | + encryptedRound2Session, |
| 100 | + source, |
| 101 | + pub: commonKeychain, |
| 102 | + }); |
| 103 | + |
| 104 | + // Send Round 3 share to BitGo |
| 105 | + await commonTssMethods.sendSignatureShareV2( |
| 106 | + bitgo, |
| 107 | + wallet.id(), |
| 108 | + txRequestId, |
| 109 | + [signatureShareRound3], |
| 110 | + RequestType.tx, |
| 111 | + wallet.baseCoin.getMPCAlgorithm(), |
| 112 | + userGpgPubKey, |
| 113 | + undefined, |
| 114 | + wallet.multisigTypeVersion(), |
| 115 | + reqId, |
| 116 | + ); |
| 117 | + |
| 118 | + logger.debug('Successfully completed ECDSA MPCv2 signing!'); |
| 119 | + return sendTxRequest( |
| 120 | + bitgo, |
| 121 | + txRequestResolved.walletId, |
| 122 | + txRequestResolved.txRequestId, |
| 123 | + RequestType.tx, |
| 124 | + reqId, |
| 125 | + ); |
| 126 | +} |
0 commit comments