diff --git a/common/src/chain/transaction/partially_signed_transaction.rs b/common/src/chain/transaction/partially_signed_transaction.rs index 3b1e58509..4705aa83c 100644 --- a/common/src/chain/transaction/partially_signed_transaction.rs +++ b/common/src/chain/transaction/partially_signed_transaction.rs @@ -41,34 +41,43 @@ impl PartiallySignedTransaction { destinations: Vec>, htlc_secrets: Option>>, ) -> Result { + let htlc_secrets = htlc_secrets.unwrap_or_else(|| vec![None; tx.inputs().len()]); + + let this = Self { + tx, + witnesses, + input_utxos, + destinations, + htlc_secrets, + }; + + this.ensure_consistency()?; + + Ok(this) + } + + pub fn ensure_consistency(&self) -> Result<(), TransactionCreationError> { ensure!( - tx.inputs().len() == witnesses.len(), + self.tx.inputs().len() == self.witnesses.len(), TransactionCreationError::InvalidWitnessCount ); ensure!( - tx.inputs().len() == input_utxos.len(), + self.tx.inputs().len() == self.input_utxos.len(), TransactionCreationError::InvalidInputUtxosCount, ); ensure!( - tx.inputs().len() == destinations.len(), + self.tx.inputs().len() == self.destinations.len(), TransactionCreationError::InvalidDestinationsCount ); - let htlc_secrets = htlc_secrets.unwrap_or_else(|| vec![None; tx.inputs().len()]); ensure!( - htlc_secrets.len() == tx.inputs().len(), + self.tx.inputs().len() == self.htlc_secrets.len(), TransactionCreationError::InvalidHtlcSecretsCount ); - Ok(Self { - tx, - witnesses, - input_utxos, - destinations, - htlc_secrets, - }) + Ok(()) } pub fn with_witnesses(mut self, witnesses: Vec>) -> Self {