|
| 1 | +import * as utxolib from '@bitgo/utxo-lib'; |
| 2 | +import { ITransactionRecipient } from '@bitgo/sdk-core'; |
| 3 | + |
| 4 | +import { |
| 5 | + AbstractUtxoCoin, |
| 6 | + BaseOutput, |
| 7 | + BaseParsedTransaction, |
| 8 | + BaseParsedTransactionOutputs, |
| 9 | + ParseTransactionOptions, |
| 10 | +} from '../../abstractUtxoCoin'; |
| 11 | +import { getKeySignatures, toBip32Triple, UtxoNamedKeychains } from '../../keychains'; |
| 12 | +import { getDescriptorMapFromWallet, getPolicyForEnv } from '../../descriptor'; |
| 13 | +import { IDescriptorWallet } from '../../descriptor/descriptorWallet'; |
| 14 | +import * as coreDescriptors from '../../core/descriptor'; |
| 15 | +import { ParsedOutput } from '../../core/descriptor/psbt/parse'; |
| 16 | +import { outputDifferencesWithExpected, OutputDifferenceWithExpected } from './outputDifference'; |
| 17 | +import { fromExtendedAddressFormatToScript, toExtendedAddressFormat } from '../recipient'; |
| 18 | + |
| 19 | +function toParsedOutput(recipient: ITransactionRecipient, network: utxolib.Network): ParsedOutput { |
| 20 | + return { |
| 21 | + address: recipient.address, |
| 22 | + value: BigInt(recipient.amount), |
| 23 | + script: fromExtendedAddressFormatToScript(recipient.address, network), |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +type ParsedOutputs = OutputDifferenceWithExpected<ParsedOutput> & { |
| 28 | + outputs: ParsedOutput[]; |
| 29 | + changeOutputs: ParsedOutput[]; |
| 30 | +}; |
| 31 | + |
| 32 | +function parseOutputsWithPsbt( |
| 33 | + psbt: utxolib.bitgo.UtxoPsbt, |
| 34 | + descriptorMap: coreDescriptors.DescriptorMap, |
| 35 | + recipientOutputs: ParsedOutput[] |
| 36 | +): ParsedOutputs { |
| 37 | + const parsed = coreDescriptors.parse(psbt, descriptorMap, psbt.network); |
| 38 | + const externalOutputs = parsed.outputs.filter((o) => o.scriptId === undefined); |
| 39 | + const changeOutputs = parsed.outputs.filter((o) => o.scriptId !== undefined); |
| 40 | + const outputDiffs = outputDifferencesWithExpected(externalOutputs, recipientOutputs); |
| 41 | + return { |
| 42 | + outputs: parsed.outputs, |
| 43 | + changeOutputs, |
| 44 | + ...outputDiffs, |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +function sumValues(arr: { value: bigint }[]): bigint { |
| 49 | + return arr.reduce((sum, e) => sum + e.value, BigInt(0)); |
| 50 | +} |
| 51 | + |
| 52 | +function toBaseOutputs(outputs: ParsedOutput[], network: utxolib.Network): BaseOutput<bigint>[] { |
| 53 | + return outputs.map( |
| 54 | + (o): BaseOutput<bigint> => ({ |
| 55 | + address: toExtendedAddressFormat(o.script, network), |
| 56 | + amount: BigInt(o.value), |
| 57 | + external: o.scriptId === undefined, |
| 58 | + }) |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +function toBaseParsedTransactionOutputs( |
| 63 | + { outputs, changeOutputs, explicitExternalOutputs, implicitExternalOutputs, missingOutputs }: ParsedOutputs, |
| 64 | + network: utxolib.Network |
| 65 | +): BaseParsedTransactionOutputs<bigint, BaseOutput<bigint>> { |
| 66 | + return { |
| 67 | + outputs: toBaseOutputs(outputs, network), |
| 68 | + changeOutputs: toBaseOutputs(changeOutputs, network), |
| 69 | + explicitExternalOutputs: toBaseOutputs(explicitExternalOutputs, network), |
| 70 | + explicitExternalSpendAmount: sumValues(explicitExternalOutputs), |
| 71 | + implicitExternalOutputs: toBaseOutputs(implicitExternalOutputs, network), |
| 72 | + implicitExternalSpendAmount: sumValues(implicitExternalOutputs), |
| 73 | + missingOutputs: toBaseOutputs(missingOutputs, network), |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +export function toBaseParsedTransactionOutputsFromPsbt( |
| 78 | + psbt: utxolib.bitgo.UtxoPsbt, |
| 79 | + descriptorMap: coreDescriptors.DescriptorMap, |
| 80 | + recipients: ITransactionRecipient[], |
| 81 | + network: utxolib.Network |
| 82 | +): BaseParsedTransactionOutputs<bigint, BaseOutput<bigint>> { |
| 83 | + return toBaseParsedTransactionOutputs( |
| 84 | + parseOutputsWithPsbt( |
| 85 | + psbt, |
| 86 | + descriptorMap, |
| 87 | + recipients.map((r) => toParsedOutput(r, psbt.network)) |
| 88 | + ), |
| 89 | + network |
| 90 | + ); |
| 91 | +} |
| 92 | + |
| 93 | +export type ParsedDescriptorTransaction<TAmount extends number | bigint> = BaseParsedTransaction< |
| 94 | + TAmount, |
| 95 | + BaseOutput<TAmount> |
| 96 | +>; |
| 97 | + |
| 98 | +export function parse( |
| 99 | + coin: AbstractUtxoCoin, |
| 100 | + wallet: IDescriptorWallet, |
| 101 | + params: ParseTransactionOptions<number | bigint> |
| 102 | +): ParsedDescriptorTransaction<bigint> { |
| 103 | + if (params.txParams.allowExternalChangeAddress) { |
| 104 | + throw new Error('allowExternalChangeAddress is not supported for descriptor wallets'); |
| 105 | + } |
| 106 | + if (params.txParams.changeAddress) { |
| 107 | + throw new Error('changeAddress is not supported for descriptor wallets'); |
| 108 | + } |
| 109 | + const keychains = params.verification?.keychains; |
| 110 | + if (!keychains || !UtxoNamedKeychains.is(keychains)) { |
| 111 | + throw new Error('keychain is required for descriptor wallets'); |
| 112 | + } |
| 113 | + const { recipients } = params.txParams; |
| 114 | + if (!recipients) { |
| 115 | + throw new Error('recipients is required'); |
| 116 | + } |
| 117 | + const psbt = coin.decodeTransactionFromPrebuild(params.txPrebuild); |
| 118 | + if (!(psbt instanceof utxolib.bitgo.UtxoPsbt)) { |
| 119 | + throw new Error('expected psbt to be an instance of UtxoPsbt'); |
| 120 | + } |
| 121 | + const walletKeys = toBip32Triple(keychains); |
| 122 | + const descriptorMap = getDescriptorMapFromWallet(wallet, walletKeys, getPolicyForEnv(params.wallet.bitgo.env)); |
| 123 | + return { |
| 124 | + ...toBaseParsedTransactionOutputsFromPsbt(psbt, descriptorMap, recipients, psbt.network), |
| 125 | + keychains, |
| 126 | + keySignatures: getKeySignatures(wallet) ?? {}, |
| 127 | + customChange: undefined, |
| 128 | + needsCustomChangeKeySignatureVerification: false, |
| 129 | + }; |
| 130 | +} |
0 commit comments