From 46e29e5e880e3a3ecf4e65f7e1ef02d54bc4b4da Mon Sep 17 00:00:00 2001 From: chris-belcher Date: Mon, 9 May 2022 11:24:59 +0100 Subject: [PATCH] Implement spending of timelock fidelity bond coins --- src/direct_send.rs | 19 ++++++++++++++++++- src/wallet_sync.rs | 21 ++++++++++++++++----- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/direct_send.rs b/src/direct_send.rs index e9e03b28..5ad158f3 100644 --- a/src/direct_send.rs +++ b/src/direct_send.rs @@ -8,6 +8,7 @@ use bitcoincore_rpc::Client; use crate::contracts::SwapCoin; use crate::error::Error; +use crate::fidelity_bonds::get_locktime_from_index; use crate::wallet_sync::{UTXOSpendInfo, Wallet}; #[derive(Debug)] @@ -207,10 +208,26 @@ impl Wallet { }); } + let lock_time = unspent_inputs + .iter() + .map(|(_, spend_info)| { + if let UTXOSpendInfo::FidelityBondCoin { + index, + input_value: _, + } = spend_info + { + get_locktime_from_index(*index) as u32 + 1 + } else { + 0 //TODO add anti-fee-sniping here + } + }) + .max() + .unwrap(); + let mut tx = Transaction { input: tx_inputs, output, - lock_time: 0, + lock_time, version: 2, }; self.sign_transaction( diff --git a/src/wallet_sync.rs b/src/wallet_sync.rs index b4e58e69..d9394480 100644 --- a/src/wallet_sync.rs +++ b/src/wallet_sync.rs @@ -1351,11 +1351,22 @@ impl Wallet { .find_incoming_swapcoin(&swapcoin_multisig_redeemscript) .unwrap() .sign_hashlocked_transaction_input(ix, &tx_clone, &mut input, input_value), - UTXOSpendInfo::FidelityBondCoin { - index: _, - input_value: _, - } => { - panic!("not implemented yet"); + UTXOSpendInfo::FidelityBondCoin { index, input_value } => { + let privkey = self.get_timelocked_privkey_from_index(index); + let redeemscript = self.get_timelocked_redeemscript_from_index(index); + let sighash = SigHashCache::new(&tx_clone).signature_hash( + ix, + &redeemscript, + input_value, + SigHashType::All, + ); + let sig = secp.sign( + &secp256k1::Message::from_slice(&sighash[..]).unwrap(), + &privkey.key, + ); + input.witness.push(sig.serialize_der().to_vec()); + input.witness[0].push(SigHashType::All as u8); + input.witness.push(redeemscript.as_bytes().to_vec()); } } }