Skip to content

Commit

Permalink
Merge #33: Use Hash160 for strongly typed arguments
Browse files Browse the repository at this point in the history
1f93217 Use Hash160 for strongly typed arguments (GeneFerneau)

Tree-SHA512: d36745ac6a3f838ea79aa2b38c0ccf5ede1b80055cdc8e03b076abfa8216e8406776ef05c00f37e405e28d2ce56b88410bbdf7b79fd0b1e6ac9c9a83cddb472d
  • Loading branch information
chris-belcher committed Aug 19, 2021
2 parents b67dab5 + 1f93217 commit 1d9b34b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
17 changes: 10 additions & 7 deletions src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bitcoin::{
opcodes,
script::{Builder, Instruction, Script},
},
hashes::{hash160::Hash as Hash160, Hash},
secp256k1,
secp256k1::{Message, Secp256k1, SecretKey, Signature},
util::bip143::SigHashCache,
Expand Down Expand Up @@ -78,7 +79,7 @@ pub fn derive_maker_pubkey_and_nonce(
pub fn create_contract_redeemscript(
pub_hashlock: &PublicKey,
pub_timelock: &PublicKey,
hashvalue: [u8; 20],
hashvalue: Hash160,
locktime: u16,
) -> Script {
Builder::new()
Expand All @@ -105,8 +106,10 @@ pub fn create_contract_redeemscript(
}

//TODO put all these magic numbers in a const or something
pub fn read_hashvalue_from_contract(redeemscript: &Script) -> Result<[u8; 20], TryFromSliceError> {
redeemscript.to_bytes()[4..24].try_into()
pub fn read_hashvalue_from_contract(redeemscript: &Script) -> Result<Hash160, TryFromSliceError> {
Ok(Hash160::from_inner(
redeemscript.to_bytes()[4..24].try_into()?,
))
}

pub fn read_locktime_from_contract(redeemscript: &Script) -> Option<u16> {
Expand Down Expand Up @@ -186,7 +189,7 @@ fn is_contract_out_valid(
contract_output: &TxOut,
hashlock_pubkey: &PublicKey,
timelock_pubkey: &PublicKey,
hashvalue: [u8; 20],
hashvalue: Hash160,
locktime: u16,
) -> Result<(), Error> {
let minimum_locktime = 2; //TODO should be in config file or something
Expand Down Expand Up @@ -215,7 +218,7 @@ pub fn validate_and_sign_senders_contract_tx(
senders_contract_tx: &Transaction,
multisig_redeemscript: &Script,
funding_input_value: u64,
hashvalue: [u8; 20],
hashvalue: Hash160,
locktime: u16,
tweakable_privkey: &SecretKey,
wallet: &mut Wallet,
Expand Down Expand Up @@ -667,7 +670,7 @@ mod test {
#[test]
fn test_contract_script_generation() {
// create a random hashvalue
let hashvalue = thread_rng().gen::<[u8; 20]>();
let hashvalue = Hash160::from_inner(thread_rng().gen::<[u8; 20]>());

let pub_hashlock = PublicKey::from_str(
"032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af",
Expand All @@ -691,7 +694,7 @@ mod test {

// Below is hand made script string that should be expected
let expected = "827ca914".to_owned()
+ &hashvalue.to_hex()[..]
+ &hashvalue.as_inner().to_hex()[..]
+ "876321"
+ &pub_hashlock.to_string()[..]
+ "0120516721"
Expand Down
2 changes: 1 addition & 1 deletion src/maker_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ fn handle_hash_preimage(
wallet: Arc<RwLock<Wallet>>,
message: HashPreimage,
) -> Result<Option<MakerToTakerMessage>, Error> {
let hashvalue = Hash160::hash(&message.preimage).into_inner();
let hashvalue = Hash160::hash(&message.preimage);
{
let mut wallet_mref = wallet.write().unwrap();
for multisig_redeemscript in message.senders_multisig_redeemscripts {
Expand Down
3 changes: 2 additions & 1 deletion src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use serde::{Deserialize, Serialize};

use bitcoin::hashes::hash160::Hash as Hash160;
use bitcoin::secp256k1::{SecretKey, Signature};
use bitcoin::util::key::PublicKey;
use bitcoin::{Script, Transaction};
Expand Down Expand Up @@ -37,7 +38,7 @@ pub struct SenderContractTxNoncesInfo {
#[derive(Debug, Serialize, Deserialize)]
pub struct SignSendersContractTx {
pub txes_info: Vec<SenderContractTxNoncesInfo>,
pub hashvalue: [u8; 20],
pub hashvalue: Hash160,
pub locktime: u16,
}

Expand Down
6 changes: 3 additions & 3 deletions src/taker_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async fn send_coinswap(

let mut preimage = [0u8; 32];
OsRng.fill_bytes(&mut preimage);
let hashvalue = Hash160::hash(&preimage).into_inner();
let hashvalue = Hash160::hash(&preimage);

let first_swap_locktime = REFUND_LOCKTIME + REFUND_LOCKTIME_STEP * maker_count;

Expand Down Expand Up @@ -507,7 +507,7 @@ async fn request_senders_contract_tx_signatures<S: SwapCoin>(
maker_multisig_nonces: &[SecretKey],
maker_hashlock_nonces: &[SecretKey],
timelock_pubkeys: &[PublicKey],
hashvalue: [u8; 20],
hashvalue: Hash160,
locktime: u16,
) -> Result<Vec<Signature>, Error> {
println!(
Expand Down Expand Up @@ -713,7 +713,7 @@ async fn send_proof_of_funding_and_get_contract_txes(
next_peer_hashlock_pubkeys: &[PublicKey],
maker_refund_locktime: u16,
this_maker_contract_txes: &[Transaction],
hashvalue: [u8; 20],
hashvalue: Hash160,
) -> Result<(SignSendersAndReceiversContractTxes, Vec<Script>), Error> {
send_message(
socket_writer,
Expand Down
13 changes: 8 additions & 5 deletions src/wallet_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ use bitcoin::{
opcodes::all,
script::{Builder, Script},
},
hashes::hex::{FromHex, ToHex},
hashes::{
hash160::Hash as Hash160,
hex::{FromHex, ToHex},
},
secp256k1,
secp256k1::{Secp256k1, SecretKey, Signature},
util::{
Expand Down Expand Up @@ -604,7 +607,7 @@ impl Wallet {
pub fn find_incomplete_coinswaps(
&self,
rpc: &Client,
) -> Result<HashMap<[u8; 20], Vec<(ListUnspentResultEntry, &WalletSwapCoin)>>, Error> {
) -> Result<HashMap<Hash160, Vec<(ListUnspentResultEntry, &WalletSwapCoin)>>, Error> {
rpc.call::<Value>("lockunspent", &[Value::Bool(true)])
.map_err(|e| Error::Rpc(e))?;

Expand All @@ -613,11 +616,11 @@ impl Wallet {
.values()
.filter(|sc| sc.other_privkey.is_some())
.map(|sc| read_hashvalue_from_contract(&sc.contract_redeemscript).unwrap())
.collect::<HashSet<[u8; 20]>>();
.collect::<HashSet<Hash160>>();
//TODO make this read_hashvalue_from_contract() a struct function of WalletCoinSwap

let mut incomplete_swapcoin_groups =
HashMap::<[u8; 20], Vec<(ListUnspentResultEntry, &WalletSwapCoin)>>::new();
HashMap::<Hash160, Vec<(ListUnspentResultEntry, &WalletSwapCoin)>>::new();
for utxo in rpc.list_unspent(None, None, None, None, None)? {
if utxo.descriptor.is_none() {
continue;
Expand Down Expand Up @@ -1054,7 +1057,7 @@ impl Wallet {
total_coinswap_amount: u64,
other_multisig_pubkeys: &[PublicKey],
hashlock_pubkeys: &[PublicKey],
hashvalue: [u8; 20],
hashvalue: Hash160,
locktime: u16, //returns: funding_txes, swapcoins, timelock_pubkeys
) -> Result<(Vec<Transaction>, Vec<WalletSwapCoin>, Vec<PublicKey>), Error> {
let (coinswap_addresses, my_multisig_privkeys): (Vec<_>, Vec<_>) = other_multisig_pubkeys
Expand Down

0 comments on commit 1d9b34b

Please sign in to comment.