Skip to content

Commit

Permalink
use u8 for forkid
Browse files Browse the repository at this point in the history
  • Loading branch information
mariocynicys committed Apr 24, 2024
1 parent d36f43d commit 0845256
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 69 deletions.
2 changes: 1 addition & 1 deletion mm2src/coins/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ pub struct UtxoCoinConf {
/// Address and privkey checksum type
pub checksum_type: ChecksumType,
/// Fork id used in sighash
pub fork_id: u32,
pub fork_id: u8,
/// Signature version
pub signature_version: SignatureVersion,
pub required_confirmations: AtomicU64,
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/qtum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ impl UtxoSignerOps for QtumCoin {
})
}

fn fork_id(&self) -> u32 { self.utxo_arc.conf.fork_id }
fn fork_id(&self) -> u8 { self.utxo_arc.conf.fork_id }

fn branch_id(&self) -> u32 { self.utxo_arc.conf.consensus_branch_id }

Expand Down
18 changes: 10 additions & 8 deletions mm2src/coins/utxo/utxo_builder/utxo_conf_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub enum UtxoConfError {
InvalidVersionGroupId(String),
InvalidAddressFormat(String),
InvalidDecimals(String),
#[display(fmt = "Invalid fork id: {}, valid fork ids are values between 0x00 - 0xff", _0)]
InvalidForkId(String),
}

impl From<Bip32Error> for UtxoConfError {
Expand Down Expand Up @@ -91,8 +93,8 @@ impl<'a> UtxoConfBuilder<'a> {
let tx_fee_volatility_percent = self.tx_fee_volatility_percent();
let version_group_id = self.version_group_id(tx_version, overwintered)?;
let consensus_branch_id = self.consensus_branch_id(tx_version)?;
let signature_version = self.signature_version();
let fork_id = self.fork_id();
let signature_version = self.signature_version()?;
let fork_id = self.fork_id()?;

// should be sufficient to detect zcash by overwintered flag
let zcash = overwintered;
Expand Down Expand Up @@ -244,23 +246,23 @@ impl<'a> UtxoConfBuilder<'a> {
Ok(consensus_branch_id)
}

fn signature_version(&self) -> SignatureVersion {
let default_signature_version = if self.ticker == "BCH" || self.fork_id() != 0 {
fn signature_version(&self) -> UtxoConfResult<SignatureVersion> {
let default_signature_version = if self.ticker == "BCH" || self.fork_id()? != 0 {
SignatureVersion::ForkId
} else {
SignatureVersion::Base
};
json::from_value(self.conf["signature_version"].clone()).unwrap_or(default_signature_version)
Ok(json::from_value(self.conf["signature_version"].clone()).unwrap_or(default_signature_version))
}

fn fork_id(&self) -> u32 {
fn fork_id(&self) -> UtxoConfResult<u8> {
let default_fork_id = match self.ticker {
"BCH" => "0x40",
_ => "0x0",
};
let hex_string = self.conf["fork_id"].as_str().unwrap_or(default_fork_id);
let fork_id = u32::from_str_radix(hex_string.trim_start_matches("0x"), 16).unwrap();
fork_id
u8::from_str_radix(hex_string.trim_start_matches("0x"), 16)
.map_to_mm(|e| UtxoConfError::InvalidForkId(e.to_string()))
}

fn required_confirmations(&self) -> u64 {
Expand Down
10 changes: 5 additions & 5 deletions mm2src/coins/utxo/utxo_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1506,13 +1506,13 @@ pub async fn sign_and_send_taker_funding_spend<T: UtxoCommonOps>(
SIGHASH_ALL,
coin.as_ref().conf.fork_id
));
let sig_hash_all_fork_id = (SIGHASH_ALL | coin.as_ref().conf.fork_id) as u8;
let sig_hash_all_fork_id = SIGHASH_ALL | coin.as_ref().conf.fork_id;

let mut maker_signature_with_sighash = preimage.signature.to_vec();
maker_signature_with_sighash.push(sig_hash_all_fork_id);
drop_mutability!(maker_signature_with_sighash);

let mut taker_signature_with_sighash: Vec<u8> = taker_signature.take();
let mut taker_signature_with_sighash = taker_signature.take();
taker_signature_with_sighash.push(sig_hash_all_fork_id);
drop_mutability!(taker_signature_with_sighash);

Expand Down Expand Up @@ -1752,14 +1752,14 @@ pub async fn sign_and_broadcast_taker_payment_spend<T: UtxoCommonOps>(
));
let mut taker_signature_with_sighash = preimage.signature.to_vec();
let taker_sig_hash = match gen_args.dex_fee {
DexFee::Standard(_) => (SIGHASH_SINGLE | coin.as_ref().conf.fork_id) as u8,
DexFee::WithBurn { .. } => (SIGHASH_ALL | coin.as_ref().conf.fork_id) as u8,
DexFee::Standard(_) => SIGHASH_SINGLE | coin.as_ref().conf.fork_id,
DexFee::WithBurn { .. } => SIGHASH_ALL | coin.as_ref().conf.fork_id,
};

taker_signature_with_sighash.push(taker_sig_hash);
drop_mutability!(taker_signature_with_sighash);

let sig_hash_all_fork_id = (SIGHASH_ALL | coin.as_ref().conf.fork_id) as u8;
let sig_hash_all_fork_id = SIGHASH_ALL | coin.as_ref().conf.fork_id;
let mut maker_signature_with_sighash: Vec<u8> = maker_signature.take();
maker_signature_with_sighash.push(sig_hash_all_fork_id);
drop_mutability!(maker_signature_with_sighash);
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/utxo_standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ impl UtxoSignerOps for UtxoStandardCoin {
})
}

fn fork_id(&self) -> u32 { self.utxo_arc.conf.fork_id }
fn fork_id(&self) -> u8 { self.utxo_arc.conf.fork_id }

fn branch_id(&self) -> u32 { self.utxo_arc.conf.consensus_branch_id }

Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo_signer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub trait UtxoSignerOps {

fn trezor_coin(&self) -> UtxoSignTxResult<String>;

fn fork_id(&self) -> u32;
fn fork_id(&self) -> u8;

fn branch_id(&self) -> u32;

Expand Down
16 changes: 8 additions & 8 deletions mm2src/coins/utxo_signer/src/sign_common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Signature;
use crate::{with_key_pair::SIGHASH_ALL, Signature};
use chain::{Transaction as UtxoTx, TransactionInput};
use keys::bytes::Bytes;
use keys::Public as PublicKey;
Expand Down Expand Up @@ -31,7 +31,7 @@ pub(crate) fn complete_tx(unsigned: TransactionInputSigner, signed_inputs: Vec<T

pub(crate) fn p2pk_spend_with_signature(
unsigned_input: &UnsignedTransactionInput,
fork_id: u32,
fork_id: u8,
signature: Signature,
) -> TransactionInput {
let script_sig = script_sig(signature, fork_id);
Expand All @@ -47,7 +47,7 @@ pub(crate) fn p2pk_spend_with_signature(
pub(crate) fn p2pkh_spend_with_signature(
unsigned_input: &UnsignedTransactionInput,
public_key: &PublicKey,
fork_id: u32,
fork_id: u8,
signature: Signature,
) -> TransactionInput {
let script_sig = script_sig_with_pub(public_key, fork_id, signature);
Expand All @@ -64,7 +64,7 @@ pub(crate) fn p2sh_spend_with_signature(
unsigned_input: &UnsignedTransactionInput,
redeem_script: Script,
script_data: Script,
fork_id: u32,
fork_id: u8,
signature: Signature,
) -> TransactionInput {
let script_sig = script_sig(signature, fork_id);
Expand All @@ -88,7 +88,7 @@ pub(crate) fn p2sh_spend_with_signature(
pub(crate) fn p2wpkh_spend_with_signature(
unsigned_input: &UnsignedTransactionInput,
public_key: &PublicKey,
fork_id: u32,
fork_id: u8,
signature: Signature,
) -> TransactionInput {
let script_sig = script_sig(signature, fork_id);
Expand All @@ -101,7 +101,7 @@ pub(crate) fn p2wpkh_spend_with_signature(
}
}

pub(crate) fn script_sig_with_pub(public_key: &PublicKey, fork_id: u32, signature: Signature) -> Bytes {
pub(crate) fn script_sig_with_pub(public_key: &PublicKey, fork_id: u8, signature: Signature) -> Bytes {
let script_sig = script_sig(signature, fork_id);
let builder = Builder::default();
builder
Expand All @@ -110,12 +110,12 @@ pub(crate) fn script_sig_with_pub(public_key: &PublicKey, fork_id: u32, signatur
.into_bytes()
}

pub(crate) fn script_sig(mut signature: Signature, fork_id: u32) -> Bytes {
pub(crate) fn script_sig(mut signature: Signature, fork_id: u8) -> Bytes {
let mut sig_script = Bytes::default();

sig_script.append(&mut signature);
// Using SIGHASH_ALL only for now
sig_script.append(&mut Bytes::from(vec![1 | fork_id as u8]));
sig_script.append(&mut Bytes::from(vec![SIGHASH_ALL | fork_id]));

sig_script
}
24 changes: 12 additions & 12 deletions mm2src/coins/utxo_signer/src/with_key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use mm2_err_handle::prelude::*;
use primitives::hash::H256;
use script::{Builder, Script, SignatureVersion, TransactionInputSigner, UnsignedTransactionInput};

pub const SIGHASH_ALL: u32 = 1;
pub const _SIGHASH_NONE: u32 = 2;
pub const SIGHASH_SINGLE: u32 = 3;
pub const SIGHASH_ALL: u8 = 1;
pub const _SIGHASH_NONE: u8 = 2;
pub const SIGHASH_SINGLE: u8 = 3;

pub type UtxoSignWithKeyPairResult<T> = Result<T, MmError<UtxoSignWithKeyPairError>>;

Expand Down Expand Up @@ -43,7 +43,7 @@ pub fn sign_tx(
key_pair: &KeyPair,
prev_script: Script,
signature_version: SignatureVersion,
fork_id: u32,
fork_id: u8,
) -> UtxoSignWithKeyPairResult<UtxoTx> {
let mut signed_inputs = vec![];
match signature_version {
Expand Down Expand Up @@ -81,7 +81,7 @@ pub fn p2pk_spend(
input_index: usize,
key_pair: &KeyPair,
signature_version: SignatureVersion,
fork_id: u32,
fork_id: u8,
) -> UtxoSignWithKeyPairResult<TransactionInput> {
let unsigned_input = get_input(signer, input_index)?;

Expand All @@ -105,7 +105,7 @@ pub fn p2pkh_spend(
key_pair: &KeyPair,
prev_script: Script,
signature_version: SignatureVersion,
fork_id: u32,
fork_id: u8,
) -> UtxoSignWithKeyPairResult<TransactionInput> {
let unsigned_input = get_input(signer, input_index)?;

Expand Down Expand Up @@ -143,7 +143,7 @@ pub fn p2sh_spend(
script_data: Script,
redeem_script: Script,
signature_version: SignatureVersion,
fork_id: u32,
fork_id: u8,
) -> UtxoSignWithKeyPairResult<TransactionInput> {
let unsigned_input = get_input(signer, input_index)?;

Expand Down Expand Up @@ -172,7 +172,7 @@ pub fn p2wpkh_spend(
key_pair: &KeyPair,
prev_script: Script,
signature_version: SignatureVersion,
fork_id: u32,
fork_id: u8,
) -> UtxoSignWithKeyPairResult<TransactionInput> {
let unsigned_input = get_input(signer, input_index)?;

Expand Down Expand Up @@ -210,8 +210,8 @@ pub fn calc_and_sign_sighash(
output_script: &Script,
key_pair: &KeyPair,
signature_version: SignatureVersion,
sighash_type: u32,
fork_id: u32,
sighash_type: u8,
fork_id: u8,
) -> UtxoSignWithKeyPairResult<Signature> {
let sighash = signature_hash_to_sign(
signer,
Expand All @@ -229,8 +229,8 @@ pub fn signature_hash_to_sign(
input_index: usize,
output_script: &Script,
signature_version: SignatureVersion,
sighash_type: u32,
fork_id: u32,
sighash_type: u8,
fork_id: u8,
) -> UtxoSignWithKeyPairResult<H256> {
let input_amount = get_input(signer, input_index)?.amount;

Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo_signer/src/with_trezor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct TrezorTxSigner<'a, TxP> {
pub tx_provider: TxP,
pub trezor_coin: String,
pub params: UtxoSignTxParams,
pub fork_id: u32,
pub fork_id: u8,
pub branch_id: u32,
}

Expand Down
Loading

0 comments on commit 0845256

Please sign in to comment.