|
| 1 | +import * as utxolib from '@bitgo/utxo-lib'; |
| 2 | +import { Dimensions } from '@bitgo/unspents'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Build a staking transaction for a wallet that assumes 2-of-3 multisig for the inputs |
| 6 | + * |
| 7 | + * Given the inputs and the staking outputs, we will create the PSBT with the desired fee rate. |
| 8 | + * We always add the change address as the last output. |
| 9 | + * |
| 10 | + * @param rootWalletKeys |
| 11 | + * @param unspents |
| 12 | + * @param createStakingOutputs |
| 13 | + * @param changeAddressInfo |
| 14 | + * @param feeRateSatKB |
| 15 | + * @param network |
| 16 | + */ |
| 17 | +export function buildFixedWalletStakingPsbt({ |
| 18 | + rootWalletKeys, |
| 19 | + unspents, |
| 20 | + outputs, |
| 21 | + changeAddressInfo, |
| 22 | + feeRateSatKB, |
| 23 | + network, |
| 24 | + skipNonWitnessUtxo, |
| 25 | + dustAmount = BigInt(0), |
| 26 | +}: { |
| 27 | + rootWalletKeys: utxolib.bitgo.RootWalletKeys; |
| 28 | + unspents: utxolib.bitgo.WalletUnspent<bigint>[]; |
| 29 | + outputs: { |
| 30 | + script: Buffer; |
| 31 | + value: bigint; |
| 32 | + }[]; |
| 33 | + changeAddressInfo: { |
| 34 | + chain: utxolib.bitgo.ChainCode; |
| 35 | + index: number; |
| 36 | + address: string; |
| 37 | + }; |
| 38 | + feeRateSatKB: number; |
| 39 | + network: utxolib.Network; |
| 40 | + skipNonWitnessUtxo?: boolean; |
| 41 | + dustAmount?: bigint; |
| 42 | +}): utxolib.bitgo.UtxoPsbt { |
| 43 | + if (feeRateSatKB < 1000) { |
| 44 | + throw new Error('Fee rate must be at least 1 sat/vbyte'); |
| 45 | + } |
| 46 | + if (unspents.length === 0 || outputs.length === 0) { |
| 47 | + throw new Error('Must have at least one input and one output'); |
| 48 | + } |
| 49 | + |
| 50 | + // Check the change address info |
| 51 | + const changeScript = utxolib.bitgo.outputScripts.createOutputScript2of3( |
| 52 | + rootWalletKeys.deriveForChainAndIndex(changeAddressInfo.chain, changeAddressInfo.index).publicKeys, |
| 53 | + utxolib.bitgo.scriptTypeForChain(changeAddressInfo.chain), |
| 54 | + network |
| 55 | + ).scriptPubKey; |
| 56 | + if (!changeScript.equals(utxolib.addressFormat.toOutputScriptTryFormats(changeAddressInfo.address, network))) { |
| 57 | + throw new Error('Change address info does not match the derived change script'); |
| 58 | + } |
| 59 | + |
| 60 | + const psbt = utxolib.bitgo.createPsbtForNetwork({ network }); |
| 61 | + utxolib.bitgo.addXpubsToPsbt(psbt, rootWalletKeys); |
| 62 | + |
| 63 | + const inputAmount = unspents.reduce((sum, unspent) => sum + unspent.value, BigInt(0)); |
| 64 | + const outputAmount = outputs.reduce((sum, output) => sum + output.value, BigInt(0)); |
| 65 | + |
| 66 | + unspents.forEach((unspent) => |
| 67 | + utxolib.bitgo.addWalletUnspentToPsbt(psbt, unspent, rootWalletKeys, 'user', 'bitgo', { |
| 68 | + isReplaceableByFee: true, |
| 69 | + skipNonWitnessUtxo, |
| 70 | + }) |
| 71 | + ); |
| 72 | + outputs.forEach((output) => psbt.addOutput(output)); |
| 73 | + |
| 74 | + const fee = Math.ceil( |
| 75 | + (Dimensions.fromPsbt(psbt) |
| 76 | + .plus(Dimensions.fromOutput({ script: changeScript })) |
| 77 | + .getVSize() * |
| 78 | + feeRateSatKB) / |
| 79 | + 1000 |
| 80 | + ); |
| 81 | + |
| 82 | + const changeAmount = inputAmount - (outputAmount + BigInt(fee)); |
| 83 | + if (changeAmount < BigInt(0)) { |
| 84 | + throw new Error( |
| 85 | + `Input amount ${inputAmount.toString()} cannot cover the staking amount ${outputAmount} and the fee: ${fee}` |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + if (changeAmount > dustAmount) { |
| 90 | + utxolib.bitgo.addWalletOutputToPsbt( |
| 91 | + psbt, |
| 92 | + rootWalletKeys, |
| 93 | + changeAddressInfo.chain, |
| 94 | + changeAddressInfo.index, |
| 95 | + changeAmount |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + return psbt; |
| 100 | +} |
0 commit comments