Skip to content

Commit

Permalink
Add PartiallySignedTransaction::ensure_consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
ImplOfAnImpl committed Sep 17, 2024
1 parent 077c723 commit 88fa577
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions common/src/chain/transaction/partially_signed_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,43 @@ impl PartiallySignedTransaction {
destinations: Vec<Option<Destination>>,
htlc_secrets: Option<Vec<Option<HtlcSecret>>>,
) -> Result<Self, TransactionCreationError> {
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<Option<InputWitness>>) -> Self {
Expand Down

0 comments on commit 88fa577

Please sign in to comment.