Remove Tx arguments from add_signatures functions

The only reason we need this argument is because we need to access
the output descriptor. We can save that one ahead of time at when
we construct the type.
This commit is contained in:
Thomas Eizinger 2021-02-25 13:52:05 +11:00
parent 0f8fbd087f
commit 6c38d66864
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
9 changed files with 28 additions and 31 deletions

View File

@ -58,6 +58,7 @@ pub struct TxCancel {
inner: Transaction, inner: Transaction,
digest: SigHash, digest: SigHash,
pub(in crate::bitcoin) output_descriptor: Descriptor<::bitcoin::PublicKey>, pub(in crate::bitcoin) output_descriptor: Descriptor<::bitcoin::PublicKey>,
lock_output_descriptor: Descriptor<::bitcoin::PublicKey>,
} }
impl TxCancel { impl TxCancel {
@ -99,6 +100,7 @@ impl TxCancel {
inner: transaction, inner: transaction,
digest, digest,
output_descriptor: cancel_output_descriptor, output_descriptor: cancel_output_descriptor,
lock_output_descriptor: tx_lock.output_descriptor.clone(),
} }
} }
@ -120,7 +122,6 @@ impl TxCancel {
pub fn add_signatures( pub fn add_signatures(
self, self,
tx_lock: &TxLock,
(A, sig_a): (PublicKey, Signature), (A, sig_a): (PublicKey, Signature),
(B, sig_b): (PublicKey, Signature), (B, sig_b): (PublicKey, Signature),
) -> Result<Transaction> { ) -> Result<Transaction> {
@ -144,8 +145,7 @@ impl TxCancel {
}; };
let mut tx_cancel = self.inner; let mut tx_cancel = self.inner;
tx_lock self.lock_output_descriptor
.output_descriptor
.satisfy(&mut tx_cancel.input[0], satisfier)?; .satisfy(&mut tx_cancel.input[0], satisfier)?;
Ok(tx_cancel) Ok(tx_cancel)

View File

@ -2,13 +2,14 @@ use crate::bitcoin::{Address, PublicKey, PunishTimelock, Transaction, TxCancel};
use ::bitcoin::{util::bip143::SigHashCache, SigHash, SigHashType}; use ::bitcoin::{util::bip143::SigHashCache, SigHash, SigHashType};
use anyhow::Result; use anyhow::Result;
use ecdsa_fun::Signature; use ecdsa_fun::Signature;
use miniscript::DescriptorTrait; use miniscript::{Descriptor, DescriptorTrait};
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug)] #[derive(Debug)]
pub struct TxPunish { pub struct TxPunish {
inner: Transaction, inner: Transaction,
digest: SigHash, digest: SigHash,
cancel_output_descriptor: Descriptor<::bitcoin::PublicKey>,
} }
impl TxPunish { impl TxPunish {
@ -29,6 +30,7 @@ impl TxPunish {
Self { Self {
inner: tx_punish, inner: tx_punish,
digest, digest,
cancel_output_descriptor: tx_cancel.output_descriptor.clone(),
} }
} }
@ -38,7 +40,6 @@ impl TxPunish {
pub fn add_signatures( pub fn add_signatures(
self, self,
tx_cancel: &TxCancel,
(A, sig_a): (PublicKey, Signature), (A, sig_a): (PublicKey, Signature),
(B, sig_b): (PublicKey, Signature), (B, sig_b): (PublicKey, Signature),
) -> Result<Transaction> { ) -> Result<Transaction> {
@ -62,8 +63,7 @@ impl TxPunish {
}; };
let mut tx_punish = self.inner; let mut tx_punish = self.inner;
tx_cancel self.cancel_output_descriptor
.output_descriptor
.satisfy(&mut tx_punish.input[0], satisfier)?; .satisfy(&mut tx_punish.input[0], satisfier)?;
Ok(tx_punish) Ok(tx_punish)

View File

@ -5,13 +5,14 @@ use crate::bitcoin::{
use ::bitcoin::{util::bip143::SigHashCache, SigHash, SigHashType, Txid}; use ::bitcoin::{util::bip143::SigHashCache, SigHash, SigHashType, Txid};
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use ecdsa_fun::Signature; use ecdsa_fun::Signature;
use miniscript::DescriptorTrait; use miniscript::{Descriptor, DescriptorTrait};
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TxRedeem { pub struct TxRedeem {
inner: Transaction, inner: Transaction,
digest: SigHash, digest: SigHash,
lock_output_descriptor: Descriptor<::bitcoin::PublicKey>,
} }
impl TxRedeem { impl TxRedeem {
@ -30,6 +31,7 @@ impl TxRedeem {
Self { Self {
inner: tx_redeem, inner: tx_redeem,
digest, digest,
lock_output_descriptor: tx_lock.output_descriptor.clone(),
} }
} }
@ -43,7 +45,6 @@ impl TxRedeem {
pub fn add_signatures( pub fn add_signatures(
self, self,
tx_lock: &TxLock,
(A, sig_a): (PublicKey, Signature), (A, sig_a): (PublicKey, Signature),
(B, sig_b): (PublicKey, Signature), (B, sig_b): (PublicKey, Signature),
) -> Result<Transaction> { ) -> Result<Transaction> {
@ -67,8 +68,7 @@ impl TxRedeem {
}; };
let mut tx_redeem = self.inner; let mut tx_redeem = self.inner;
tx_lock self.lock_output_descriptor
.output_descriptor
.satisfy(&mut tx_redeem.input[0], satisfier)?; .satisfy(&mut tx_redeem.input[0], satisfier)?;
Ok(tx_redeem) Ok(tx_redeem)

View File

@ -5,13 +5,14 @@ use crate::bitcoin::{
use ::bitcoin::{util::bip143::SigHashCache, SigHash, SigHashType, Txid}; use ::bitcoin::{util::bip143::SigHashCache, SigHash, SigHashType, Txid};
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use ecdsa_fun::Signature; use ecdsa_fun::Signature;
use miniscript::DescriptorTrait; use miniscript::{Descriptor, DescriptorTrait};
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug)] #[derive(Debug)]
pub struct TxRefund { pub struct TxRefund {
inner: Transaction, inner: Transaction,
digest: SigHash, digest: SigHash,
cancel_output_descriptor: Descriptor<::bitcoin::PublicKey>,
} }
impl TxRefund { impl TxRefund {
@ -28,6 +29,7 @@ impl TxRefund {
Self { Self {
inner: tx_punish, inner: tx_punish,
digest, digest,
cancel_output_descriptor: tx_cancel.output_descriptor.clone(),
} }
} }
@ -41,7 +43,6 @@ impl TxRefund {
pub fn add_signatures( pub fn add_signatures(
self, self,
tx_cancel: &TxCancel,
(A, sig_a): (PublicKey, Signature), (A, sig_a): (PublicKey, Signature),
(B, sig_b): (PublicKey, Signature), (B, sig_b): (PublicKey, Signature),
) -> Result<Transaction> { ) -> Result<Transaction> {
@ -65,8 +66,7 @@ impl TxRefund {
}; };
let mut tx_refund = self.inner; let mut tx_refund = self.inner;
tx_cancel self.cancel_output_descriptor
.output_descriptor
.satisfy(&mut tx_refund.input[0], satisfier)?; .satisfy(&mut tx_refund.input[0], satisfier)?;
Ok(tx_refund) Ok(tx_refund)

View File

@ -205,7 +205,7 @@ impl From<Alice> for AliceState {
let tx_refund = TxRefund::new(&tx_cancel, &state3.refund_address); let tx_refund = TxRefund::new(&tx_cancel, &state3.refund_address);
AliceState::BtcPunishable { AliceState::BtcPunishable {
monero_wallet_restore_blockheight, monero_wallet_restore_blockheight,
tx_refund, tx_refund: Box::new(tx_refund),
state3: Box::new(state3), state3: Box::new(state3),
} }
} }

View File

@ -53,7 +53,7 @@ pub enum AliceState {
}, },
BtcPunishable { BtcPunishable {
monero_wallet_restore_blockheight: BlockHeight, monero_wallet_restore_blockheight: BlockHeight,
tx_refund: TxRefund, tx_refund: Box<TxRefund>,
state3: Box<State3>, state3: Box<State3>,
}, },
XmrRefunded, XmrRefunded,

View File

@ -124,7 +124,7 @@ pub fn build_bitcoin_redeem_transaction(
let sig_b = adaptor.decrypt_signature(&s_a, encrypted_signature); let sig_b = adaptor.decrypt_signature(&s_a, encrypted_signature);
let tx = tx_redeem let tx = tx_redeem
.add_signatures(&tx_lock, (a.public(), sig_a), (B, sig_b)) .add_signatures((a.public(), sig_a), (B, sig_b))
.context("sig_{a,b} are invalid for tx_redeem")?; .context("sig_{a,b} are invalid for tx_redeem")?;
Ok(tx) Ok(tx)
@ -179,7 +179,7 @@ where
let tx_cancel = tx_cancel let tx_cancel = tx_cancel
.clone() .clone()
.add_signatures(&tx_lock, (a.public(), sig_a), (B, sig_b)) .add_signatures((a.public(), sig_a), (B, sig_b))
.expect("sig_{a,b} to be valid signatures for tx_cancel"); .expect("sig_{a,b} to be valid signatures for tx_cancel");
// TODO(Franck): Error handling is delicate, why can't we broadcast? // TODO(Franck): Error handling is delicate, why can't we broadcast?
@ -224,7 +224,7 @@ where
pub fn extract_monero_private_key( pub fn extract_monero_private_key(
published_refund_tx: bitcoin::Transaction, published_refund_tx: bitcoin::Transaction,
tx_refund: TxRefund, tx_refund: &TxRefund,
s_a: monero::Scalar, s_a: monero::Scalar,
a: bitcoin::SecretKey, a: bitcoin::SecretKey,
S_b_bitcoin: bitcoin::PublicKey, S_b_bitcoin: bitcoin::PublicKey,
@ -261,7 +261,7 @@ pub fn build_bitcoin_punish_transaction(
let sig_b = tx_punish_sig_bob; let sig_b = tx_punish_sig_bob;
let signed_tx_punish = tx_punish let signed_tx_punish = tx_punish
.add_signatures(&tx_cancel, (a.public(), sig_a), (B, sig_b)) .add_signatures((a.public(), sig_a), (B, sig_b))
.expect("sig_{a,b} to be valid signatures for tx_cancel"); .expect("sig_{a,b} to be valid signatures for tx_cancel");
Ok(signed_tx_punish) Ok(signed_tx_punish)

View File

@ -343,7 +343,7 @@ async fn run_until_internal(
match published_refund_tx { match published_refund_tx {
None => { None => {
let state = AliceState::BtcPunishable { let state = AliceState::BtcPunishable {
tx_refund, tx_refund: Box::new(tx_refund),
state3, state3,
monero_wallet_restore_blockheight, monero_wallet_restore_blockheight,
}; };
@ -366,7 +366,7 @@ async fn run_until_internal(
Some(published_refund_tx) => { Some(published_refund_tx) => {
let spend_key = extract_monero_private_key( let spend_key = extract_monero_private_key(
published_refund_tx, published_refund_tx,
tx_refund, &tx_refund,
state3.s_a, state3.s_a,
state3.a.clone(), state3.a.clone(),
state3.S_b_bitcoin, state3.S_b_bitcoin,
@ -445,7 +445,7 @@ async fn run_until_internal(
Either::Left((published_refund_tx, _)) => { Either::Left((published_refund_tx, _)) => {
let spend_key = extract_monero_private_key( let spend_key = extract_monero_private_key(
published_refund_tx?, published_refund_tx?,
tx_refund, &tx_refund,
state3.s_a, state3.s_a,
state3.a.clone(), state3.a.clone(),
state3.S_b_bitcoin, state3.S_b_bitcoin,

View File

@ -459,7 +459,7 @@ impl State4 {
let tx_cancel = tx_cancel let tx_cancel = tx_cancel
.clone() .clone()
.add_signatures(&self.tx_lock, (self.A, sig_a), (self.b.public(), sig_b)) .add_signatures((self.A, sig_a), (self.b.public(), sig_b))
.expect( .expect(
"sig_{a,b} to be valid signatures for "sig_{a,b} to be valid signatures for
tx_cancel", tx_cancel",
@ -482,7 +482,7 @@ impl State4 {
let tx_cancel = tx_cancel let tx_cancel = tx_cancel
.clone() .clone()
.add_signatures(&self.tx_lock, (self.A, sig_a), (self.b.public(), sig_b)) .add_signatures((self.A, sig_a), (self.b.public(), sig_b))
.expect( .expect(
"sig_{a,b} to be valid signatures for "sig_{a,b} to be valid signatures for
tx_cancel", tx_cancel",
@ -562,11 +562,8 @@ impl State4 {
let sig_a = let sig_a =
adaptor.decrypt_signature(&self.s_b.to_secpfun_scalar(), self.tx_refund_encsig.clone()); adaptor.decrypt_signature(&self.s_b.to_secpfun_scalar(), self.tx_refund_encsig.clone());
let signed_tx_refund = tx_refund.add_signatures( let signed_tx_refund =
&tx_cancel.clone(), tx_refund.add_signatures((self.A, sig_a), (self.b.public(), sig_b))?;
(self.A, sig_a),
(self.b.public(), sig_b),
)?;
let txid = bitcoin_wallet let txid = bitcoin_wallet
.broadcast_signed_transaction(signed_tx_refund) .broadcast_signed_transaction(signed_tx_refund)