|
| 1 | +// This file is Copyright its original authors, visible in version control |
| 2 | +// history. |
| 3 | +// |
| 4 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE |
| 5 | +// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 7 | +// You may not use this file except in accordance with one or both of these |
| 8 | +// licenses. |
| 9 | + |
| 10 | +//! Types pertaining to funding channels. |
| 11 | +
|
| 12 | +#[cfg(splicing)] |
| 13 | +use bitcoin::{Amount, ScriptBuf, SignedAmount, TxOut}; |
| 14 | +use bitcoin::{Script, Sequence, Transaction, Weight}; |
| 15 | + |
| 16 | +use crate::events::bump_transaction::{Utxo, EMPTY_SCRIPT_SIG_WEIGHT}; |
| 17 | +use crate::sign::{P2TR_KEY_PATH_WITNESS_WEIGHT, P2WPKH_WITNESS_WEIGHT}; |
| 18 | + |
| 19 | +/// The components of a splice's funding transaction that are contributed by one party. |
| 20 | +#[cfg(splicing)] |
| 21 | +pub enum SpliceContribution { |
| 22 | + /// When funds are added to a channel. |
| 23 | + SpliceIn { |
| 24 | + /// The amount to contribute to the splice. |
| 25 | + value: Amount, |
| 26 | + |
| 27 | + /// The inputs included in the splice's funding transaction to meet the contributed amount. |
| 28 | + /// Any excess amount will be sent to a change output. |
| 29 | + inputs: Vec<FundingTxInput>, |
| 30 | + |
| 31 | + /// An optional change output script. This will be used if needed or, when not set, |
| 32 | + /// generated using [`SignerProvider::get_destination_script`]. |
| 33 | + change_script: Option<ScriptBuf>, |
| 34 | + }, |
| 35 | + /// When funds are removed from a channel. |
| 36 | + SpliceOut { |
| 37 | + /// The outputs to include in the splice's funding transaction. The total value of all |
| 38 | + /// outputs will be the amount that is removed. |
| 39 | + outputs: Vec<TxOut>, |
| 40 | + }, |
| 41 | +} |
| 42 | + |
| 43 | +#[cfg(splicing)] |
| 44 | +impl SpliceContribution { |
| 45 | + pub(super) fn value(&self) -> SignedAmount { |
| 46 | + match self { |
| 47 | + SpliceContribution::SpliceIn { value, .. } => { |
| 48 | + value.to_signed().unwrap_or(SignedAmount::MAX) |
| 49 | + }, |
| 50 | + SpliceContribution::SpliceOut { outputs } => { |
| 51 | + let value_removed = outputs |
| 52 | + .iter() |
| 53 | + .map(|txout| txout.value) |
| 54 | + .sum::<Amount>() |
| 55 | + .to_signed() |
| 56 | + .unwrap_or(SignedAmount::MAX); |
| 57 | + -value_removed |
| 58 | + }, |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + pub(super) fn inputs(&self) -> &[FundingTxInput] { |
| 63 | + match self { |
| 64 | + SpliceContribution::SpliceIn { inputs, .. } => &inputs[..], |
| 65 | + SpliceContribution::SpliceOut { .. } => &[], |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + pub(super) fn outputs(&self) -> &[TxOut] { |
| 70 | + match self { |
| 71 | + SpliceContribution::SpliceIn { .. } => &[], |
| 72 | + SpliceContribution::SpliceOut { outputs } => &outputs[..], |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + pub(super) fn into_tx_parts(self) -> (Vec<FundingTxInput>, Vec<TxOut>, Option<ScriptBuf>) { |
| 77 | + match self { |
| 78 | + SpliceContribution::SpliceIn { inputs, change_script, .. } => { |
| 79 | + (inputs, vec![], change_script) |
| 80 | + }, |
| 81 | + SpliceContribution::SpliceOut { outputs } => (vec![], outputs, None), |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/// An input to contribute to a channel's funding transaction either when using the v2 channel |
| 87 | +/// establishment protocol or when splicing. |
| 88 | +#[derive(Clone)] |
| 89 | +pub struct FundingTxInput { |
| 90 | + /// The unspent [`TxOut`] that the input spends. |
| 91 | + /// |
| 92 | + /// [`TxOut`]: bitcoin::TxOut |
| 93 | + pub(super) utxo: Utxo, |
| 94 | + |
| 95 | + /// The sequence number to use in the [`TxIn`]. |
| 96 | + /// |
| 97 | + /// [`TxIn`]: bitcoin::TxIn |
| 98 | + pub(super) sequence: Sequence, |
| 99 | + |
| 100 | + /// The transaction containing the unspent [`TxOut`] referenced by [`utxo`]. |
| 101 | + /// |
| 102 | + /// [`TxOut`]: bitcoin::TxOut |
| 103 | + /// [`utxo`]: Self::utxo |
| 104 | + pub(super) prevtx: Transaction, |
| 105 | +} |
| 106 | + |
| 107 | +impl FundingTxInput { |
| 108 | + fn new<F: FnOnce(&bitcoin::Script) -> bool>( |
| 109 | + prevtx: Transaction, vout: u32, witness_weight: Weight, script_filter: F, |
| 110 | + ) -> Result<Self, ()> { |
| 111 | + Ok(FundingTxInput { |
| 112 | + utxo: Utxo { |
| 113 | + outpoint: bitcoin::OutPoint { txid: prevtx.compute_txid(), vout }, |
| 114 | + output: prevtx |
| 115 | + .output |
| 116 | + .get(vout as usize) |
| 117 | + .filter(|output| script_filter(&output.script_pubkey)) |
| 118 | + .ok_or(())? |
| 119 | + .clone(), |
| 120 | + satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + witness_weight.to_wu(), |
| 121 | + }, |
| 122 | + sequence: Sequence::ENABLE_RBF_NO_LOCKTIME, |
| 123 | + prevtx, |
| 124 | + }) |
| 125 | + } |
| 126 | + |
| 127 | + /// Creates an input spending a P2WPKH output from the given `prevtx` at index `vout`. |
| 128 | + /// |
| 129 | + /// Uses [`Sequence::ENABLE_RBF_NO_LOCKTIME`] as the [`TxIn::sequence`], which can be overridden |
| 130 | + /// by [`set_sequence`]. |
| 131 | + /// |
| 132 | + /// Returns `Err` if no such output exists in `prevtx` at index `vout`. |
| 133 | + /// |
| 134 | + /// [`TxIn::sequence`]: bitcoin::TxIn::sequence |
| 135 | + /// [`set_sequence`]: Self::set_sequence |
| 136 | + pub fn new_p2wpkh(prevtx: Transaction, vout: u32) -> Result<Self, ()> { |
| 137 | + let witness_weight = Weight::from_wu(P2WPKH_WITNESS_WEIGHT); |
| 138 | + FundingTxInput::new(prevtx, vout, witness_weight, Script::is_p2wpkh) |
| 139 | + } |
| 140 | + |
| 141 | + /// Creates an input spending a P2WSH output from the given `prevtx` at index `vout`. |
| 142 | + /// |
| 143 | + /// Requires passing the weight of witness needed to satisfy the output's script. |
| 144 | + /// |
| 145 | + /// Uses [`Sequence::ENABLE_RBF_NO_LOCKTIME`] as the [`TxIn::sequence`], which can be overridden |
| 146 | + /// by [`set_sequence`]. |
| 147 | + /// |
| 148 | + /// Returns `Err` if no such output exists in `prevtx` at index `vout`. |
| 149 | + /// |
| 150 | + /// [`TxIn::sequence`]: bitcoin::TxIn::sequence |
| 151 | + /// [`set_sequence`]: Self::set_sequence |
| 152 | + pub fn new_p2wsh(prevtx: Transaction, vout: u32, witness_weight: Weight) -> Result<Self, ()> { |
| 153 | + FundingTxInput::new(prevtx, vout, witness_weight, Script::is_p2wsh) |
| 154 | + } |
| 155 | + |
| 156 | + /// Creates an input spending a P2TR output from the given `prevtx` at index `vout`. |
| 157 | + /// |
| 158 | + /// This is meant for inputs spending a taproot output using the key path. See |
| 159 | + /// [`new_p2tr_script_spend`] for when spending using a script path. |
| 160 | + /// |
| 161 | + /// Uses [`Sequence::ENABLE_RBF_NO_LOCKTIME`] as the [`TxIn::sequence`], which can be overridden |
| 162 | + /// by [`set_sequence`]. |
| 163 | + /// |
| 164 | + /// Returns `Err` if no such output exists in `prevtx` at index `vout`. |
| 165 | + /// |
| 166 | + /// [`new_p2tr_script_spend`]: Self::new_p2tr_script_spend |
| 167 | + /// |
| 168 | + /// [`TxIn::sequence`]: bitcoin::TxIn::sequence |
| 169 | + /// [`set_sequence`]: Self::set_sequence |
| 170 | + pub fn new_p2tr_key_spend(prevtx: Transaction, vout: u32) -> Result<Self, ()> { |
| 171 | + let witness_weight = Weight::from_wu(P2TR_KEY_PATH_WITNESS_WEIGHT); |
| 172 | + FundingTxInput::new(prevtx, vout, witness_weight, Script::is_p2tr) |
| 173 | + } |
| 174 | + |
| 175 | + /// Creates an input spending a P2TR output from the given `prevtx` at index `vout`. |
| 176 | + /// |
| 177 | + /// Requires passing the weight of witness needed to satisfy a script path of the taproot |
| 178 | + /// output. See [`new_p2tr_key_spend`] for when spending using the key path. |
| 179 | + /// |
| 180 | + /// Uses [`Sequence::ENABLE_RBF_NO_LOCKTIME`] as the [`TxIn::sequence`], which can be overridden |
| 181 | + /// by [`set_sequence`]. |
| 182 | + /// |
| 183 | + /// Returns `Err` if no such output exists in `prevtx` at index `vout`. |
| 184 | + /// |
| 185 | + /// [`new_p2tr_key_spend`]: Self::new_p2tr_key_spend |
| 186 | + /// |
| 187 | + /// [`TxIn::sequence`]: bitcoin::TxIn::sequence |
| 188 | + /// [`set_sequence`]: Self::set_sequence |
| 189 | + pub fn new_p2tr_script_spend( |
| 190 | + prevtx: Transaction, vout: u32, witness_weight: Weight, |
| 191 | + ) -> Result<Self, ()> { |
| 192 | + FundingTxInput::new(prevtx, vout, witness_weight, Script::is_p2tr) |
| 193 | + } |
| 194 | + |
| 195 | + /// The sequence number to use in the [`TxIn`]. |
| 196 | + /// |
| 197 | + /// [`TxIn`]: bitcoin::TxIn |
| 198 | + pub fn sequence(&self) -> Sequence { |
| 199 | + self.sequence |
| 200 | + } |
| 201 | + |
| 202 | + /// Sets the sequence number to use in the [`TxIn`]. |
| 203 | + /// |
| 204 | + /// [`TxIn`]: bitcoin::TxIn |
| 205 | + pub fn set_sequence(&mut self, sequence: Sequence) { |
| 206 | + self.sequence = sequence; |
| 207 | + } |
| 208 | +} |
0 commit comments