Skip to content

Commit

Permalink
Removed SignTransactionInputInfo enum
Browse files Browse the repository at this point in the history
I noticed that SignTransactionInputInfo and UTXOSpendInfo are basically the
same thing, so removed one.
  • Loading branch information
chris-belcher committed Jan 16, 2022
1 parent 9b256b1 commit 8ecde48
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 38 deletions.
32 changes: 8 additions & 24 deletions src/wallet_direct_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bitcoincore_rpc::json::ListUnspentResultEntry;
use bitcoincore_rpc::Client;

use crate::error::Error;
use crate::wallet_sync::{UTXOSpendInfo, Wallet, NETWORK, SignTransactionInputInfo};
use crate::wallet_sync::{UTXOSpendInfo, Wallet, NETWORK};

#[derive(Debug)]
pub enum SendAmount {
Expand Down Expand Up @@ -149,7 +149,10 @@ impl Wallet {
}
}
if tx_inputs.len() != coins_to_spend.len() {
panic!("unable to find all given inputs, only found = {:?}", tx_inputs);
panic!(
"unable to find all given inputs, only found = {:?}",
tx_inputs
);
}
let dest_addr = match destination {
Destination::Wallet => self.get_next_external_address(rpc)?,
Expand Down Expand Up @@ -185,29 +188,10 @@ impl Wallet {
lock_time: 0,
version: 2,
};
self.sign_transaction(&mut tx, &mut unspent_inputs.iter()
.map(|(lure, usi)|
match usi {
UTXOSpendInfo::SeedUTXO { path } => {
SignTransactionInputInfo::SeedCoin {
path: path.clone(),
input_value: lure.amount.as_sat()
}
}
UTXOSpendInfo::SwapCoin { multisig_redeemscript } => {
SignTransactionInputInfo::SwapCoin {
multisig_redeemscript: multisig_redeemscript.clone()
}
}
UTXOSpendInfo::TimelockContract { swapcoin_multisig_redeemscript } => {
panic!("not implemented yet");
}
}
)
self.sign_transaction(
&mut tx,
&mut unspent_inputs.iter().map(|(_u, usi)| usi.clone()),
);

println!("tx = {:?}", tx);

Ok(tx)
}
}
31 changes: 17 additions & 14 deletions src/wallet_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ const WATCH_ONLY_SWAPCOIN_LABEL: &str = "watchonly_swapcoin_label";
//about a UTXO required to spend it
#[derive(Debug, Clone)]
pub enum UTXOSpendInfo {
SeedUTXO { path: String },
SeedCoin {
path: String,
input_value: u64,
},
SwapCoin {
multisig_redeemscript: Script,
},
Expand All @@ -108,11 +111,6 @@ pub enum UTXOSpendInfo {
},
}

pub enum SignTransactionInputInfo {
SeedCoin { path: String, input_value: u64 },
SwapCoin { multisig_redeemscript: Script },
}

//swapcoins are UTXOs + metadata which are not from the deterministic wallet
//they are made in the process of a coinswap
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -768,8 +766,9 @@ impl Wallet {
.derive_priv(&secp, &DerivationPath::from_str(DERIVATION_PATH).unwrap())
.unwrap();
if fingerprint == master_private_key.fingerprint(&secp).to_string() {
Some(UTXOSpendInfo::SeedUTXO {
path: format!("m/{}/{}", addr_type, index)
Some(UTXOSpendInfo::SeedCoin {
path: format!("m/{}/{}", addr_type, index),
input_value: u.amount.as_sat(),
})
} else {
None
Expand Down Expand Up @@ -1050,7 +1049,7 @@ impl Wallet {
pub fn sign_transaction(
&self,
spending_tx: &mut Transaction,
inputs_info: &mut dyn Iterator<Item = SignTransactionInputInfo>,
inputs_info: &mut dyn Iterator<Item = UTXOSpendInfo>,
) {
let secp = Secp256k1::new();
let master_private_key = self
Expand All @@ -1063,15 +1062,15 @@ impl Wallet {
spending_tx.input.iter_mut().zip(inputs_info).enumerate()
{
match input_info {
SignTransactionInputInfo::SwapCoin {
UTXOSpendInfo::SwapCoin {
multisig_redeemscript,
} => {
self.find_incoming_swapcoin(&multisig_redeemscript)
.unwrap()
.sign_transaction_input(ix, &tx_clone, &mut input, &multisig_redeemscript)
.unwrap();
}
SignTransactionInputInfo::SeedCoin { path, input_value } => {
UTXOSpendInfo::SeedCoin { path, input_value } => {
let privkey = master_private_key
.derive_priv(&secp, &DerivationPath::from_str(&path).unwrap())
.unwrap()
Expand All @@ -1094,6 +1093,11 @@ impl Wallet {
input.witness[0].push(SigHashType::All as u8);
input.witness.push(pubkey.to_bytes());
}
UTXOSpendInfo::TimelockContract {
swapcoin_multisig_redeemscript: _,
} => {
panic!("not implemented yet");
}
}
}
}
Expand Down Expand Up @@ -1245,7 +1249,7 @@ impl Wallet {
.map(|input_info| (input_info, input_info["bip32_derivs"].as_array().unwrap()))
.map(|(input_info, bip32_info)| {
if bip32_info.len() == 2 {
SignTransactionInputInfo::SwapCoin {
UTXOSpendInfo::SwapCoin {
multisig_redeemscript: Builder::from(
Vec::from_hex(
&input_info["witness_script"]["hex"].as_str().unwrap(),
Expand All @@ -1255,7 +1259,7 @@ impl Wallet {
.into_script(),
}
} else {
SignTransactionInputInfo::SeedCoin {
UTXOSpendInfo::SeedCoin {
path: bip32_info[0]["path"].as_str().unwrap().to_string(),
input_value: convert_json_rpc_bitcoin_to_satoshis(
&input_info["witness_utxo"]["amount"],
Expand Down Expand Up @@ -1530,4 +1534,3 @@ fn get_hd_path_from_descriptor<'a>(descriptor: &'a str) -> Option<(&'a str, u32,
}
Some((path_chunks[0], addr_type.unwrap(), index.unwrap()))
}

0 comments on commit 8ecde48

Please sign in to comment.