From d15536b49d71ce327b3ce3efd3763b2d234eb568 Mon Sep 17 00:00:00 2001 From: chris-belcher Date: Mon, 17 Jan 2022 20:05:40 +0000 Subject: [PATCH] Have wallet-balance show hashlocked coins too Also have recover-from-incomplete-coinswap broadcast the hashlock contract transactions as well. --- src/contracts.rs | 2 +- src/lib.rs | 63 +++++++++++++++++++++++---- src/wallet_sync.rs | 104 ++++++++++++++++++++++----------------------- 3 files changed, 106 insertions(+), 63 deletions(-) diff --git a/src/contracts.rs b/src/contracts.rs index d51059e1..294b86ab 100644 --- a/src/contracts.rs +++ b/src/contracts.rs @@ -32,7 +32,7 @@ use crate::wallet_sync::{ pub const REFUND_LOCKTIME: u16 = 30; //in blocks pub const REFUND_LOCKTIME_STEP: u16 = 5; //in blocks -//like the WalletSwapCoin struct but no privkey or signature information +//like the Incoming/OutgoingSwapCoin structs but no privkey or signature information //used by the taker to monitor coinswaps between two makers #[derive(Debug, Clone)] pub struct WatchOnlySwapCoin { diff --git a/src/lib.rs b/src/lib.rs index 5d8ca5fc..904830fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ use bitcoin_wallet::mnemonic; use bitcoincore_rpc::{Auth, Client, Error, RpcApi}; pub mod wallet_sync; -use wallet_sync::{Wallet, WalletSyncAddressAmount}; +use wallet_sync::{Wallet, WalletSwapCoin, WalletSyncAddressAmount}; pub mod wallet_direct_send; use wallet_direct_send::{CoinToSpend, Destination, SendAmount}; @@ -250,25 +250,28 @@ pub fn display_wallet_balance(wallet_file_name: &PathBuf, long_form: Option= timelock.into() { "unlocked" } else { "locked" }, @@ -276,6 +279,38 @@ pub fn display_wallet_balance(wallet_file_name: &PathBuf, long_form: Option Transaction { - if self.others_contract_sig.is_none() { - panic!("invalid state: others_contract_sig not known"); - } - let my_pubkey = self.get_my_pubkey(); - let multisig_redeemscript = create_multisig_redeemscript(&my_pubkey, &self.other_pubkey); - let index = 0; - let secp = Secp256k1::new(); - let sighash = secp256k1::Message::from_slice( - &SigHashCache::new(&self.contract_tx).signature_hash( - index, - &multisig_redeemscript, - self.funding_amount, - SigHashType::All, - )[..], - ) - .unwrap(); - let sig_mine = secp.sign(&sighash, &self.my_privkey); - - let mut signed_contract_tx = self.contract_tx.clone(); - apply_two_signatures_to_2of2_multisig_spend( - &my_pubkey, - &self.other_pubkey, - &sig_mine, - &self.others_contract_sig.unwrap(), - &mut signed_contract_tx.input[index], - &multisig_redeemscript, - ); - signed_contract_tx - } - fn sign_timelocked_transaction_input( &self, index: usize, @@ -297,37 +266,66 @@ impl OutgoingSwapCoin { } } -pub trait WalletSwapCoin { +pub trait WalletSwapCoin: SwapCoin { fn get_my_pubkey(&self) -> PublicKey; fn get_other_pubkey(&self) -> &PublicKey; + fn get_fully_signed_contract_tx(&self) -> Transaction; } -impl WalletSwapCoin for IncomingSwapCoin { - fn get_my_pubkey(&self) -> PublicKey { - let secp = Secp256k1::new(); - PublicKey { - compressed: true, - key: secp256k1::PublicKey::from_secret_key(&secp, &self.my_privkey), +macro_rules! add_walletswapcoin_functions { + () => { + fn get_my_pubkey(&self) -> PublicKey { + let secp = Secp256k1::new(); + PublicKey { + compressed: true, + key: secp256k1::PublicKey::from_secret_key(&secp, &self.my_privkey), + } } - } - fn get_other_pubkey(&self) -> &PublicKey { - &self.other_pubkey - } -} + fn get_other_pubkey(&self) -> &PublicKey { + &self.other_pubkey + } -impl WalletSwapCoin for OutgoingSwapCoin { - fn get_my_pubkey(&self) -> PublicKey { - let secp = Secp256k1::new(); - PublicKey { - compressed: true, - key: secp256k1::PublicKey::from_secret_key(&secp, &self.my_privkey), + fn get_fully_signed_contract_tx(&self) -> Transaction { + if self.others_contract_sig.is_none() { + panic!("invalid state: others_contract_sig not known"); + } + let my_pubkey = self.get_my_pubkey(); + let multisig_redeemscript = + create_multisig_redeemscript(&my_pubkey, &self.other_pubkey); + let index = 0; + let secp = Secp256k1::new(); + let sighash = secp256k1::Message::from_slice( + &SigHashCache::new(&self.contract_tx).signature_hash( + index, + &multisig_redeemscript, + self.funding_amount, + SigHashType::All, + )[..], + ) + .unwrap(); + let sig_mine = secp.sign(&sighash, &self.my_privkey); + + let mut signed_contract_tx = self.contract_tx.clone(); + apply_two_signatures_to_2of2_multisig_spend( + &my_pubkey, + &self.other_pubkey, + &sig_mine, + &self.others_contract_sig.unwrap(), + &mut signed_contract_tx.input[index], + &multisig_redeemscript, + ); + signed_contract_tx } - } + }; +} - fn get_other_pubkey(&self) -> &PublicKey { - &self.other_pubkey - } +impl WalletSwapCoin for IncomingSwapCoin { + add_walletswapcoin_functions!(); +} + +impl WalletSwapCoin for OutgoingSwapCoin { + add_walletswapcoin_functions!(); } impl Wallet {