Skip to content

Commit

Permalink
Implement spending of timelock fidelity bond coins
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-belcher committed May 11, 2022
1 parent ab48db5 commit 46e29e5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
19 changes: 18 additions & 1 deletion src/direct_send.rs
Expand Up @@ -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)]
Expand Down Expand Up @@ -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(
Expand Down
21 changes: 16 additions & 5 deletions src/wallet_sync.rs
Expand Up @@ -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());
}
}
}
Expand Down

0 comments on commit 46e29e5

Please sign in to comment.