Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect Hash256 use for hash preimage #38

Merged
merged 1 commit into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

use serde::{Deserialize, Serialize};

use bitcoin::hashes::{hash160::Hash as Hash160, sha256::Hash as Hash256};
use bitcoin::hashes::hash160::Hash as Hash160;
use bitcoin::secp256k1::{SecretKey, Signature};
use bitcoin::util::key::PublicKey;
use bitcoin::{Script, Transaction};

pub const PREIMAGE_LEN: usize = 32;
pub type Preimage = [u8; PREIMAGE_LEN];

//TODO the structs here which are actual messages should have the word Message
//added to their name e.g. SignSendersContractTx
//to distinguish them from structs which just collect together
Expand Down Expand Up @@ -86,7 +89,7 @@ pub struct SignReceiversContractTx {
pub struct HashPreimage {
pub senders_multisig_redeemscripts: Vec<Script>,
pub receivers_multisig_redeemscripts: Vec<Script>,
pub preimage: Hash256,
pub preimage: Preimage,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
9 changes: 4 additions & 5 deletions src/taker_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tokio::prelude::*;
use tokio::time::sleep;

use bitcoin::consensus::encode::deserialize;
use bitcoin::hashes::{hash160::Hash as Hash160, sha256::Hash as Hash256};
use bitcoin::hashes::hash160::Hash as Hash160;
use bitcoin::hashes::{hex::ToHex, Hash};
use bitcoin::secp256k1::{SecretKey, Signature};
use bitcoin::util::key::PublicKey;
Expand All @@ -30,7 +30,7 @@ use crate::contracts::{
};
use crate::error::Error;
use crate::messages::{
ConfirmedCoinSwapTxInfo, HashPreimage, MakerToTakerMessage, NextCoinSwapTxInfo,
ConfirmedCoinSwapTxInfo, HashPreimage, MakerToTakerMessage, NextCoinSwapTxInfo, Preimage,
PrivateKeyHandover, ProofOfFunding, ReceiversContractTxInfo, SenderContractTxNoncesInfo,
SendersAndReceiversContractSigs, SignReceiversContractTx, SignSendersAndReceiversContractTxes,
SignSendersContractTx, SwapCoinPrivateKey, TakerHello, TakerToMakerMessage,
Expand Down Expand Up @@ -73,7 +73,6 @@ async fn send_coinswap(
let mut preimage = [0u8; 32];
OsRng.fill_bytes(&mut preimage);
let hashvalue = Hash160::hash(&preimage);
let preimage = Hash256::from_inner(preimage);

let first_swap_locktime = REFUND_LOCKTIME + REFUND_LOCKTIME_STEP * maker_count;

Expand Down Expand Up @@ -909,7 +908,7 @@ fn create_incoming_swapcoins(
next_peer_hashlock_keys_or_nonces: &[SecretKey],
next_peer_multisig_pubkeys: &[PublicKey],
next_peer_multisig_keys_or_nonces: &[SecretKey],
preimage: Hash256,
preimage: Preimage,
) -> Result<Vec<IncomingSwapCoin>, Error> {
let next_swap_multisig_redeemscripts = maker_sign_sender_and_receiver_contracts
.senders_contract_txes_info
Expand Down Expand Up @@ -1008,7 +1007,7 @@ async fn send_hash_preimage_and_get_private_keys(
socket_writer: &mut WriteHalf<'_>,
senders_multisig_redeemscripts: Vec<Script>,
receivers_multisig_redeemscripts: Vec<Script>,
preimage: Hash256,
preimage: Preimage,
) -> Result<PrivateKeyHandover, Error> {
let receivers_multisig_redeemscripts_len = receivers_multisig_redeemscripts.len();
send_message(
Expand Down
6 changes: 3 additions & 3 deletions src/wallet_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use bitcoin::{
hashes::{
hash160::Hash as Hash160,
hex::{FromHex, ToHex},
sha256::Hash as Hash256,
},
secp256k1,
secp256k1::{Secp256k1, SecretKey, Signature},
Expand Down Expand Up @@ -52,6 +51,7 @@ use rand::RngCore;
use crate::contracts;
use crate::contracts::{read_hashvalue_from_contract, SwapCoin};
use crate::error::Error;
use crate::messages::Preimage;

//these subroutines are coded so that as much as possible they keep all their
//data in the bitcoin core wallet
Expand Down Expand Up @@ -106,7 +106,7 @@ pub struct IncomingSwapCoin {
pub hashlock_privkey: SecretKey,
pub funding_amount: u64,
pub others_contract_sig: Option<Signature>,
pub hash_preimage: Option<Hash256>,
pub hash_preimage: Option<Preimage>,
}

//swapcoins are UTXOs + metadata which are not from the deterministic wallet
Expand All @@ -120,7 +120,7 @@ pub struct OutgoingSwapCoin {
pub timelock_privkey: SecretKey,
pub funding_amount: u64,
pub others_contract_sig: Option<Signature>,
pub hash_preimage: Option<Hash256>,
pub hash_preimage: Option<Preimage>,
}

impl IncomingSwapCoin {
Expand Down