Skip to content

Commit

Permalink
Remove NETWORK const, instead detect from json-rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-belcher committed Feb 24, 2022
1 parent f9bff3f commit 4b8581e
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 135 deletions.
33 changes: 19 additions & 14 deletions src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use bitcoin::{
secp256k1::{Message, Secp256k1, SecretKey, Signature},
util::bip143::SigHashCache,
util::ecdsa::PublicKey,
Address, OutPoint, SigHashType, Transaction, TxIn, TxOut,
OutPoint, SigHashType, Transaction, TxIn, TxOut,
};

use bitcoincore_rpc::{Client, RpcApi};
Expand All @@ -23,7 +23,7 @@ use rand::RngCore;
use crate::error::Error;
use crate::messages::ConfirmedCoinSwapTxInfo;
use crate::wallet_sync::{
create_multisig_redeemscript, IncomingSwapCoin, OutgoingSwapCoin, Wallet, NETWORK,
create_multisig_redeemscript, IncomingSwapCoin, OutgoingSwapCoin, Wallet,
};

//relatively simple handling of miner fees for now, each funding transaction is considered
Expand Down Expand Up @@ -79,6 +79,14 @@ pub fn calculate_coinswap_fee(
+ (time_in_blocks * time_relative_fee_ppb / 1_000_000_000)
}

pub fn redeemscript_to_scriptpubkey(redeemscript: &Script) -> Script {
//p2wsh address
Script::new_witness_program(
bitcoin::bech32::u5::try_from_u8(0).unwrap(),
&redeemscript.wscript_hash().to_vec(),
)
}

pub fn calculate_maker_pubkey_from_nonce(
tweakable_point: PublicKey,
nonce: SecretKey,
Expand Down Expand Up @@ -249,8 +257,6 @@ pub fn create_senders_contract_tx(
input_value: u64,
contract_redeemscript: &Script,
) -> Transaction {
let contract_address = Address::p2wsh(&contract_redeemscript, NETWORK);

Transaction {
input: vec![TxIn {
previous_output: input,
Expand All @@ -259,7 +265,7 @@ pub fn create_senders_contract_tx(
script_sig: Script::new(),
}],
output: vec![TxOut {
script_pubkey: contract_address.script_pubkey(),
script_pubkey: redeemscript_to_scriptpubkey(&contract_redeemscript),
value: input_value - 1000,
}],
lock_time: 0,
Expand Down Expand Up @@ -291,8 +297,7 @@ fn is_contract_out_valid(

let redeemscript_from_request =
create_contract_redeemscript(hashlock_pubkey, timelock_pubkey, hashvalue, locktime);
let contract_spk_from_request =
Address::p2wsh(&redeemscript_from_request, NETWORK).script_pubkey();
let contract_spk_from_request = redeemscript_to_scriptpubkey(&redeemscript_from_request);
if contract_output.script_pubkey != contract_spk_from_request {
return Err(Error::Protocol(
"given transaction does not pay to requested contract",
Expand Down Expand Up @@ -370,7 +375,7 @@ pub fn find_funding_output<'a>(
funding_tx: &'a Transaction,
multisig_redeemscript: &Script,
) -> Option<(u32, &'a TxOut)> {
let multisig_spk = Address::p2wsh(&multisig_redeemscript, NETWORK).script_pubkey();
let multisig_spk = redeemscript_to_scriptpubkey(&multisig_redeemscript);
funding_tx
.output
.iter()
Expand Down Expand Up @@ -456,7 +461,7 @@ pub fn verify_proof_of_funding(

//check that the provided contract matches the scriptpubkey from the
//cache which was populated when the signsendercontracttx message arrived
let contract_spk = Address::p2wsh(&funding_info.contract_redeemscript, NETWORK).script_pubkey();
let contract_spk = redeemscript_to_scriptpubkey(&funding_info.contract_redeemscript);

if !wallet.does_prevout_match_cached_contract(
&OutPoint {
Expand Down Expand Up @@ -501,7 +506,7 @@ pub fn validate_contract_tx(
return Err(Error::Protocol("not spending the funding outpoint"));
}
if receivers_contract_tx.output[0].script_pubkey
!= Address::p2wsh(&contract_redeemscript, NETWORK).script_pubkey()
!= redeemscript_to_scriptpubkey(&contract_redeemscript)
{
return Err(Error::Protocol("doesnt pay to requested contract"));
}
Expand Down Expand Up @@ -911,8 +916,8 @@ mod test {
let multisig_reedemscript = Script::from(Vec::from_hex("5221032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af21039b6347398505f5ec93826dc61c19f47c66c0283ee9be980e29ce325a0f4679ef52ae").unwrap());
let another_script = Script::from(Vec::from_hex("020000000156944c5d3f98413ef45cf54545538103cc9f298e0575820ad3591376e2e0f65d2a0000000000000000014871000000000000220020dad1b452caf4a0f26aecf1cc43aaae9b903a043c34f75ad9a36c86317b22236800000000").unwrap());

let multi_script_pubkey = Address::p2wsh(&multisig_reedemscript, NETWORK).script_pubkey();
let another_script_pubkey = Address::p2wsh(&another_script, NETWORK).script_pubkey();
let multi_script_pubkey = redeemscript_to_scriptpubkey(&multisig_reedemscript);
let another_script_pubkey = redeemscript_to_scriptpubkey(&another_script);

// Create the funding transaction
let funding_tx = Transaction {
Expand Down Expand Up @@ -1034,7 +1039,7 @@ mod test {
// Change contract transaction to pay into wrong output
let mut contract_tx_err2 = contract_tx.clone();
let multisig_redeemscript = Script::from(Vec::from_hex("5221032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af21039b6347398505f5ec93826dc61c19f47c66c0283ee9be980e29ce325a0f4679ef52ae").unwrap());
let multi_script_pubkey = Address::p2wsh(&multisig_redeemscript, NETWORK).script_pubkey();
let multi_script_pubkey = redeemscript_to_scriptpubkey(&multisig_redeemscript);
contract_tx_err2.output[0] = TxOut {
script_pubkey: multi_script_pubkey,
value: 3000,
Expand Down Expand Up @@ -1065,7 +1070,7 @@ mod test {
let funding_outpoint_script =
crate::wallet_sync::create_multisig_redeemscript(&pub1, &pub2);

let funding_spk = Address::p2sh(&funding_outpoint_script, NETWORK).script_pubkey();
let funding_spk = redeemscript_to_scriptpubkey(&funding_outpoint_script);

let funding_tx = Transaction {
input: vec![TxIn {
Expand Down
6 changes: 3 additions & 3 deletions src/direct_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bitcoincore_rpc::Client;

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

#[derive(Debug)]
pub enum SendAmount {
Expand Down Expand Up @@ -179,8 +179,8 @@ impl Wallet {
//so a.network is always testnet even if the address is signet
let testnet_signet_type = (a.network == Network::Testnet
|| a.network == Network::Signet)
&& (NETWORK == Network::Testnet || NETWORK == Network::Signet);
if a.network != NETWORK && !testnet_signet_type {
&& (self.network == Network::Testnet || self.network == Network::Signet);
if a.network != self.network && !testnet_signet_type {
panic!("wrong address network type (e.g. mainnet, testnet, regtest, signet)");
}
a
Expand Down
Loading

0 comments on commit 4b8581e

Please sign in to comment.