@@ -2253,21 +2253,10 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
2253
2253
funding_inputs.push(prev_funding_input);
2254
2254
}
2255
2255
2256
- let mut funding_inputs_prev_outputs: Vec<&TxOut> = Vec::with_capacity(funding_inputs.len());
2257
- // Check that vouts exist for each TxIn in provided transactions.
2258
- for (idx, (txin, tx)) in funding_inputs.iter().enumerate() {
2259
- if let Some(output) = tx.as_transaction().output.get(txin.previous_output.vout as usize) {
2260
- funding_inputs_prev_outputs.push(output);
2261
- } else {
2262
- return Err(APIError::APIMisuseError {
2263
- err: format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn at funding_inputs[{}]",
2264
- tx.as_transaction().compute_txid(), txin.previous_output.vout, idx) });
2265
- }
2266
- }
2256
+ let funding_inputs_prev_outputs = DualFundingChannelContext::txouts_from_input_prev_txs(&funding_inputs)
2257
+ .map_err(|err| APIError::APIMisuseError { err: err.to_string() })?;
2267
2258
2268
- let total_input_satoshis: u64 = funding_inputs.iter().map(
2269
- |(txin, tx)| tx.as_transaction().output.get(txin.previous_output.vout as usize).map(|out| out.value.to_sat()).unwrap_or(0)
2270
- ).sum();
2259
+ let total_input_satoshis: u64 = funding_inputs_prev_outputs.iter().map(|txout| txout.value.to_sat()).sum();
2271
2260
if total_input_satoshis < self.dual_funding_context.our_funding_satoshis {
2272
2261
return Err(APIError::APIMisuseError {
2273
2262
err: format!("Total value of funding inputs must be at least funding amount. It was {} sats",
@@ -5055,6 +5044,33 @@ pub(super) struct DualFundingChannelContext {
5055
5044
pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
5056
5045
}
5057
5046
5047
+ impl DualFundingChannelContext {
5048
+ /// Obtain prev outputs for each supplied input and matching transaction.
5049
+ /// Can error when there a prev tx does not have an output for the specified vout number.
5050
+ /// Also checks for matching of transaction IDs.
5051
+ fn txouts_from_input_prev_txs(inputs: &Vec<(TxIn, TransactionU16LenLimited)>) -> Result<Vec<&TxOut>, ChannelError> {
5052
+ let mut prev_outputs: Vec<&TxOut> = Vec::with_capacity(inputs.len());
5053
+ // Check that vouts exist for each TxIn in provided transactions.
5054
+ for (idx, (txin, tx)) in inputs.iter().enumerate() {
5055
+ let txid = tx.as_transaction().compute_txid();
5056
+ if txin.previous_output.txid != txid {
5057
+ return Err(ChannelError::Warn(
5058
+ format!("Transaction input txid mismatch, {} vs. {}, at index {}", txin.previous_output.txid, txid, idx)
5059
+ ));
5060
+ }
5061
+ if let Some(output) = tx.as_transaction().output.get(txin.previous_output.vout as usize) {
5062
+ prev_outputs.push(output);
5063
+ } else {
5064
+ return Err(ChannelError::Warn(
5065
+ format!("Transaction with txid {} does not have an output with vout of {} corresponding to TxIn, at index {}",
5066
+ txid, txin.previous_output.vout, idx)
5067
+ ));
5068
+ }
5069
+ }
5070
+ Ok(prev_outputs)
5071
+ }
5072
+ }
5073
+
5058
5074
// Holder designates channel data owned for the benefit of the user client.
5059
5075
// Counterparty designates channel data owned by the another channel participant entity.
5060
5076
pub(super) struct FundedChannel<SP: Deref> where SP::Target: SignerProvider {
@@ -10035,17 +10051,18 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
10035
10051
unfunded_channel_age_ticks: 0,
10036
10052
holder_commitment_point: HolderCommitmentPoint::new(&context.holder_signer, &context.secp_ctx),
10037
10053
};
10054
+ let dual_funding_context = DualFundingChannelContext {
10055
+ our_funding_satoshis: funding_satoshis,
10056
+ their_funding_satoshis: None,
10057
+ funding_tx_locktime,
10058
+ funding_feerate_sat_per_1000_weight,
10059
+ our_funding_inputs: funding_inputs,
10060
+ };
10038
10061
let chan = Self {
10039
10062
funding,
10040
10063
context,
10041
10064
unfunded_context,
10042
- dual_funding_context: DualFundingChannelContext {
10043
- our_funding_satoshis: funding_satoshis,
10044
- their_funding_satoshis: None,
10045
- funding_tx_locktime,
10046
- funding_feerate_sat_per_1000_weight,
10047
- our_funding_inputs: funding_inputs,
10048
- },
10065
+ dual_funding_context,
10049
10066
interactive_tx_constructor: None,
10050
10067
interactive_tx_signing_session: None,
10051
10068
};
0 commit comments