Move extract_monero_private_key onto TxRefund

This functionality is domain-specific to the refund transaction.
Move it onto there.
This commit is contained in:
Thomas Eizinger 2021-03-17 17:20:02 +11:00
parent a1e065b4e7
commit e77f1729b4
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
3 changed files with 31 additions and 32 deletions

View File

@ -3,10 +3,10 @@ use crate::bitcoin::{
verify_sig, Address, EmptyWitnessStack, NoInputs, NotThreeWitnesses, PublicKey, TooManyInputs, verify_sig, Address, EmptyWitnessStack, NoInputs, NotThreeWitnesses, PublicKey, TooManyInputs,
Transaction, TxCancel, Transaction, TxCancel,
}; };
use crate::{bitcoin, monero};
use ::bitcoin::util::bip143::SigHashCache; use ::bitcoin::util::bip143::SigHashCache;
use ::bitcoin::{SigHash, SigHashType, Txid}; use ::bitcoin::{Script, SigHash, SigHashType, Txid};
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use bitcoin::Script;
use ecdsa_fun::Signature; use ecdsa_fun::Signature;
use miniscript::{Descriptor, DescriptorTrait}; use miniscript::{Descriptor, DescriptorTrait};
use std::collections::HashMap; use std::collections::HashMap;
@ -77,7 +77,31 @@ impl TxRefund {
Ok(tx_refund) Ok(tx_refund)
} }
pub fn extract_signature_by_key( pub fn extract_monero_private_key(
&self,
published_refund_tx: bitcoin::Transaction,
s_a: monero::Scalar,
a: bitcoin::SecretKey,
S_b_bitcoin: bitcoin::PublicKey,
) -> Result<monero::PrivateKey> {
let s_a = monero::PrivateKey { scalar: s_a };
let tx_refund_sig = self
.extract_signature_by_key(published_refund_tx, a.public())
.context("Failed to extract signature from Bitcoin refund tx")?;
let tx_refund_encsig = a.encsign(S_b_bitcoin, self.digest());
let s_b = bitcoin::recover(S_b_bitcoin, tx_refund_sig, tx_refund_encsig)
.context("Failed to recover Monero secret key from Bitcoin signature")?;
let s_b = monero::private_key_from_secp256k1_scalar(s_b.into());
let spend_key = s_a + s_b;
Ok(spend_key)
}
fn extract_signature_by_key(
&self, &self,
candidate_transaction: Transaction, candidate_transaction: Transaction,
B: PublicKey, B: PublicKey,

View File

@ -2,7 +2,7 @@ use crate::bitcoin::{CancelTimelock, PunishTimelock, TxCancel, TxLock, TxRefund}
use crate::protocol::alice; use crate::protocol::alice;
use crate::protocol::alice::event_loop::EventLoopHandle; use crate::protocol::alice::event_loop::EventLoopHandle;
use crate::{bitcoin, monero}; use crate::{bitcoin, monero};
use anyhow::{bail, Context, Result}; use anyhow::{bail, Result};
pub async fn lock_xmr( pub async fn lock_xmr(
state3: alice::State3, state3: alice::State3,
@ -102,26 +102,3 @@ pub async fn wait_for_bitcoin_refund(
} }
} }
} }
pub fn extract_monero_private_key(
published_refund_tx: bitcoin::Transaction,
tx_refund: &TxRefund,
s_a: monero::Scalar,
a: bitcoin::SecretKey,
S_b_bitcoin: bitcoin::PublicKey,
) -> Result<monero::PrivateKey> {
let s_a = monero::PrivateKey { scalar: s_a };
let tx_refund_sig = tx_refund
.extract_signature_by_key(published_refund_tx, a.public())
.context("Failed to extract signature from Bitcoin refund tx")?;
let tx_refund_encsig = a.encsign(S_b_bitcoin, tx_refund.digest());
let s_b = bitcoin::recover(S_b_bitcoin, tx_refund_sig, tx_refund_encsig)
.context("Failed to recover Monero secret key from Bitcoin signature")?;
let s_b = monero::private_key_from_secp256k1_scalar(s_b.into());
let spend_key = s_a + s_b;
Ok(spend_key)
}

View File

@ -7,7 +7,7 @@ use crate::monero_ext::ScalarExt;
use crate::protocol::alice; use crate::protocol::alice;
use crate::protocol::alice::event_loop::EventLoopHandle; use crate::protocol::alice::event_loop::EventLoopHandle;
use crate::protocol::alice::steps::{ use crate::protocol::alice::steps::{
extract_monero_private_key, lock_xmr, publish_cancel_transaction, wait_for_bitcoin_refund, lock_xmr, publish_cancel_transaction, wait_for_bitcoin_refund,
}; };
use crate::protocol::alice::AliceState; use crate::protocol::alice::AliceState;
use crate::{bitcoin, database, monero}; use crate::{bitcoin, database, monero};
@ -315,9 +315,8 @@ async fn run_until_internal(
.await .await
} }
Some(published_refund_tx) => { Some(published_refund_tx) => {
let spend_key = extract_monero_private_key( let spend_key = state3.tx_refund().extract_monero_private_key(
published_refund_tx, published_refund_tx,
&state3.tx_refund(),
state3.s_a, state3.s_a,
state3.a.clone(), state3.a.clone(),
state3.S_b_bitcoin, state3.S_b_bitcoin,
@ -393,9 +392,8 @@ async fn run_until_internal(
let published_refund_tx = let published_refund_tx =
bitcoin_wallet.get_raw_transaction(tx_refund.txid()).await?; bitcoin_wallet.get_raw_transaction(tx_refund.txid()).await?;
let spend_key = extract_monero_private_key( let spend_key = tx_refund.extract_monero_private_key(
published_refund_tx, published_refund_tx,
&tx_refund,
state3.s_a, state3.s_a,
state3.a.clone(), state3.a.clone(),
state3.S_b_bitcoin, state3.S_b_bitcoin,