Skip to content

Commit 1951034

Browse files
refactor(wasm-utxo): move sighash_fork_id to Network, drop wrapper
Move SIGHASH_FORKID lookup from fixed_script_wallet::get_sighash_fork_id to Network::sighash_fork_id as the single source of truth. Inline call sites in bitgo_psbt and drop the redundant wrapper and its re-export. Refs: T1-3672
1 parent 3e864cf commit 1951034

3 files changed

Lines changed: 38 additions & 45 deletions

File tree

packages/wasm-utxo/src/fixed_script_wallet/bitgo_psbt/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub use propkv::{
2121
find_kv, get_zec_consensus_branch_id, BitGoKeyValue, ProprietaryKeySubtype,
2222
WasmUtxoVersionInfo, BITGO,
2323
};
24-
pub use sighash::{get_sighash_fork_id, validate_sighash_type};
24+
pub use sighash::validate_sighash_type;
2525
pub use zcash_psbt::{
2626
decode_zcash_transaction_meta, ZcashBitGoPsbt, ZcashTransactionMeta,
2727
ZCASH_SAPLING_VERSION_GROUP_ID,
@@ -703,7 +703,7 @@ impl BitGoPsbt {
703703
input: &FixedScriptInput,
704704
) -> Result<(), String> {
705705
let ctx = SighashContext::Bitcoin {
706-
fork_id: sighash::get_sighash_fork_id(self.network()),
706+
fork_id: self.network().sighash_fork_id(),
707707
};
708708
input.apply_signatures(self.psbt_mut(), index, &ctx)
709709
}
@@ -1653,7 +1653,7 @@ impl BitGoPsbt {
16531653
return Ok(());
16541654
}
16551655

1656-
let fork_id = sighash::get_sighash_fork_id(*network);
1656+
let fork_id = (*network).sighash_fork_id();
16571657

16581658
// Finalize with fork_id support for FORKID networks
16591659
psbt.finalize_inp_mut_with_fork_id(secp, input_index, fork_id)
@@ -1671,7 +1671,7 @@ impl BitGoPsbt {
16711671
return Ok(());
16721672
}
16731673

1674-
let fork_id = sighash::get_sighash_fork_id(*network);
1674+
let fork_id = (*network).sighash_fork_id();
16751675

16761676
// Finalize with fork_id support for FORKID networks
16771677
psbt.finalize_inp_mut_with_fork_id(secp, input_index, fork_id)
@@ -2766,7 +2766,7 @@ impl BitGoPsbt {
27662766
.map(|(_, v)| v)
27672767
.unwrap_or(miniscript::bitcoin::Amount::ZERO);
27682768

2769-
let fork_id = sighash::get_sighash_fork_id(network);
2769+
let fork_id = network.sighash_fork_id();
27702770

27712771
// Compute sighash based on network type
27722772
let mut cache = SighashCache::new(&psbt.unsigned_tx);
@@ -2972,7 +2972,7 @@ impl BitGoPsbt {
29722972
};
29732973
}
29742974

2975-
let fork_id = sighash::get_sighash_fork_id(network);
2975+
let fork_id = network.sighash_fork_id();
29762976

29772977
let message = if let Some(fork_id) = fork_id {
29782978
// BCH-style BIP143 sighash with FORKID
@@ -3062,7 +3062,7 @@ impl BitGoPsbt {
30623062
}
30633063
}
30643064

3065-
let fork_id = sighash::get_sighash_fork_id(*network);
3065+
let fork_id = (*network).sighash_fork_id();
30663066

30673067
// Fall back to ECDSA signature verification for legacy/SegWit inputs
30683068
psbt_wallet_input::verify_ecdsa_signature(
@@ -3113,7 +3113,7 @@ impl BitGoPsbt {
31133113
}
31143114
}
31153115

3116-
let fork_id = sighash::get_sighash_fork_id(*network);
3116+
let fork_id = (*network).sighash_fork_id();
31173117
psbt_wallet_input::verify_ecdsa_signature(
31183118
secp,
31193119
psbt,

packages/wasm-utxo/src/fixed_script_wallet/bitgo_psbt/sighash.rs

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,6 @@ const SIGHASH_NONE: u32 = 0x02;
1515
const SIGHASH_SINGLE: u32 = 0x03;
1616
const SIGHASH_ANYONECANPAY: u32 = 0x80;
1717

18-
/// Returns the SIGHASH_FORKID value for networks that use it.
19-
///
20-
/// Different networks use different fork IDs in their sighash computation:
21-
/// - Bitcoin Cash, Ecash, Bitcoin SV: fork_id = 0
22-
/// - Bitcoin Gold: fork_id = 79
23-
/// - All other networks: None (don't use SIGHASH_FORKID)
24-
///
25-
/// # Arguments
26-
///
27-
/// * `network` - The network to get the fork ID for
28-
///
29-
/// # Returns
30-
///
31-
/// `Some(fork_id)` for FORKID networks, `None` otherwise
32-
pub fn get_sighash_fork_id(network: Network) -> Option<u32> {
33-
match network.mainnet() {
34-
Network::BitcoinCash | Network::Ecash | Network::BitcoinSV => Some(0),
35-
Network::BitcoinGold => Some(79),
36-
_ => None,
37-
}
38-
}
39-
4018
/// Validates a sighash type for a given network
4119
///
4220
/// Different networks have different valid sighash types:
@@ -103,26 +81,26 @@ mod tests {
10381
use super::*;
10482

10583
#[test]
106-
fn test_get_sighash_fork_id() {
84+
fn test_sighash_fork_id() {
10785
// Networks with fork_id = 0
108-
assert_eq!(get_sighash_fork_id(Network::BitcoinCash), Some(0));
109-
assert_eq!(get_sighash_fork_id(Network::BitcoinCashTestnet), Some(0));
110-
assert_eq!(get_sighash_fork_id(Network::Ecash), Some(0));
111-
assert_eq!(get_sighash_fork_id(Network::EcashTestnet), Some(0));
112-
assert_eq!(get_sighash_fork_id(Network::BitcoinSV), Some(0));
113-
assert_eq!(get_sighash_fork_id(Network::BitcoinSVTestnet), Some(0));
86+
assert_eq!(Network::BitcoinCash.sighash_fork_id(), Some(0));
87+
assert_eq!(Network::BitcoinCashTestnet.sighash_fork_id(), Some(0));
88+
assert_eq!(Network::Ecash.sighash_fork_id(), Some(0));
89+
assert_eq!(Network::EcashTestnet.sighash_fork_id(), Some(0));
90+
assert_eq!(Network::BitcoinSV.sighash_fork_id(), Some(0));
91+
assert_eq!(Network::BitcoinSVTestnet.sighash_fork_id(), Some(0));
11492

11593
// Bitcoin Gold has fork_id = 79
116-
assert_eq!(get_sighash_fork_id(Network::BitcoinGold), Some(79));
117-
assert_eq!(get_sighash_fork_id(Network::BitcoinGoldTestnet), Some(79));
94+
assert_eq!(Network::BitcoinGold.sighash_fork_id(), Some(79));
95+
assert_eq!(Network::BitcoinGoldTestnet.sighash_fork_id(), Some(79));
11896

11997
// Standard networks don't use FORKID
120-
assert_eq!(get_sighash_fork_id(Network::Bitcoin), None);
121-
assert_eq!(get_sighash_fork_id(Network::BitcoinTestnet3), None);
122-
assert_eq!(get_sighash_fork_id(Network::Litecoin), None);
123-
assert_eq!(get_sighash_fork_id(Network::Dogecoin), None);
124-
assert_eq!(get_sighash_fork_id(Network::Dash), None);
125-
assert_eq!(get_sighash_fork_id(Network::Zcash), None);
98+
assert_eq!(Network::Bitcoin.sighash_fork_id(), None);
99+
assert_eq!(Network::BitcoinTestnet3.sighash_fork_id(), None);
100+
assert_eq!(Network::Litecoin.sighash_fork_id(), None);
101+
assert_eq!(Network::Dogecoin.sighash_fork_id(), None);
102+
assert_eq!(Network::Dash.sighash_fork_id(), None);
103+
assert_eq!(Network::Zcash.sighash_fork_id(), None);
126104
}
127105

128106
#[test]

packages/wasm-utxo/src/networks.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,21 @@ impl Network {
330330
)
331331
}
332332

333+
/// Returns the SIGHASH_FORKID value for networks that use it (`Psbt::sign_forkid` /
334+
/// `finalize_mut_with_fork_id`'s `fork_id` parameter), or `None` for plain-sighash networks
335+
/// (`Psbt::sign` / plain finalize).
336+
///
337+
/// - Bitcoin Cash, Ecash, Bitcoin SV: fork_id = 0
338+
/// - Bitcoin Gold: fork_id = 79
339+
/// - All other networks: `None`
340+
pub fn sighash_fork_id(self) -> Option<u32> {
341+
match self.mainnet() {
342+
Network::BitcoinCash | Network::Ecash | Network::BitcoinSV => Some(0),
343+
Network::BitcoinGold => Some(79),
344+
_ => None,
345+
}
346+
}
347+
333348
/// Convert to bitcoin crate Network type for address encoding
334349
pub fn to_bitcoin_network(self) -> crate::bitcoin::Network {
335350
use crate::bitcoin::Network as BitcoinNetwork;

0 commit comments

Comments
 (0)