Skip to content

Commit e03a0e2

Browse files
committed
Add unit test for sighash single bug
When signing a transaction will result in the sighash single bug being exploitable we should return the 'one array' (equivalent to 1 as a uint256) as the signature hash. Add a unit test to verify we return uint256 1 value when use of SIGHASH_SINGLE is invalid.
1 parent 44c0790 commit e03a0e2

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/blockdata/transaction.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ impl ::std::error::Error for SigHashTypeParseError {}
835835

836836
#[cfg(test)]
837837
mod tests {
838-
use super::{OutPoint, ParseOutPointError, Transaction, TxIn, NonStandardSigHashType};
838+
use super::*;
839839

840840
use core::str::FromStr;
841841
use blockdata::constants::WITNESS_SCALE_FACTOR;
@@ -1147,6 +1147,29 @@ mod tests {
11471147
assert_eq!(EcdsaSigHashType::from_u32_standard(nonstandard_hashtype), Err(NonStandardSigHashType(0x04)));
11481148
}
11491149

1150+
#[test]
1151+
fn sighash_single_bug() {
1152+
const SIGHASH_SINGLE: u32 = 3;
1153+
// We need a tx with more inputs than outputs.
1154+
let mut input = Vec::new();
1155+
input.push(TxIn::default());
1156+
input.push(TxIn::default());
1157+
let mut output = Vec::new();
1158+
output.push(TxOut::default());
1159+
1160+
let tx = Transaction {
1161+
version: 1,
1162+
lock_time: 0,
1163+
input: input,
1164+
output: output, // TODO: Use Vec::from([TxOut]) once we bump MSRV.
1165+
};
1166+
let script = Script::new();
1167+
let got = tx.signature_hash(1, &script, SIGHASH_SINGLE);
1168+
let want = SigHash::from_slice(&UINT256_ONE).unwrap();
1169+
1170+
assert_eq!(got, want)
1171+
}
1172+
11501173
fn run_test_sighash(tx: &str, script: &str, input_index: usize, hash_type: i32, expected_result: &str) {
11511174
let tx: Transaction = deserialize(&Vec::from_hex(tx).unwrap()[..]).unwrap();
11521175
let script = Script::from(Vec::from_hex(script).unwrap());

0 commit comments

Comments
 (0)