Eliminate build_bitcoin_punish_transaction

We reduce indirection by constructing TxPunish directly based off
`State3` and make the type itself more powerful by moving the logic
of completing it with a signature onto it.
This commit is contained in:
Thomas Eizinger 2021-03-16 18:02:31 +11:00
parent dd6c66a594
commit 6beb732e35
No known key found for this signature in database
GPG key ID: 651AC83A6C6C8B96
5 changed files with 34 additions and 46 deletions

View file

@ -1,12 +1,11 @@
use crate::bitcoin::{Address, PublicKey, PunishTimelock, Transaction, TxCancel};
use crate::bitcoin::{self, Address, PunishTimelock, Transaction, TxCancel};
use ::bitcoin::util::bip143::SigHashCache;
use ::bitcoin::{SigHash, SigHashType};
use anyhow::Result;
use ecdsa_fun::Signature;
use anyhow::{Context, Result};
use miniscript::{Descriptor, DescriptorTrait};
use std::collections::HashMap;
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct TxPunish {
inner: Transaction,
digest: SigHash,
@ -39,22 +38,20 @@ impl TxPunish {
self.digest
}
pub fn add_signatures(
pub fn complete(
self,
(A, sig_a): (PublicKey, Signature),
(B, sig_b): (PublicKey, Signature),
tx_punish_sig_bob: bitcoin::Signature,
a: bitcoin::SecretKey,
B: bitcoin::PublicKey,
) -> Result<Transaction> {
let sig_a = a.sign(self.digest());
let sig_b = tx_punish_sig_bob;
let satisfier = {
let mut satisfier = HashMap::with_capacity(2);
let A = ::bitcoin::PublicKey {
compressed: true,
key: A.0.into(),
};
let B = ::bitcoin::PublicKey {
compressed: true,
key: B.0.into(),
};
let A = a.public().into();
let B = B.into();
// The order in which these are inserted doesn't matter
satisfier.insert(A, (sig_a.into(), ::bitcoin::SigHashType::All));
@ -65,7 +62,8 @@ impl TxPunish {
let mut tx_punish = self.inner;
self.cancel_output_descriptor
.satisfy(&mut tx_punish.input[0], satisfier)?;
.satisfy(&mut tx_punish.input[0], satisfier)
.context("Failed to satisfy inputs with given signatures")?;
Ok(tx_punish)
}