added p2wkh, p2sh-p2wkh tests

This commit is contained in:
evgeniy.scherbina 2018-09-26 11:51:14 +03:00
parent 45140a3251
commit bc41772f89
1 changed files with 89 additions and 1 deletions

View File

@ -100,14 +100,102 @@ impl SighashComponents {
#[cfg(test)]
mod tests {
use blockdata::script::Script;
use blockdata::transaction::Transaction;
use consensus::encode::deserialize;
use network::constants::Network;
use util::misc::hex_bytes;
use util::address::Address;
use hex;
use secp256k1::{Secp256k1, PublicKey};
use super::*;
fn p2pkh_hex(pk: &str) -> Script {
let ctx = Secp256k1::new();
let pk = hex::decode(pk).unwrap();
let pk = PublicKey::from_slice(&ctx, pk.as_slice()).unwrap();
let witness_script = Address::p2pkh(&pk, Network::Bitcoin).script_pubkey();
witness_script
}
#[test]
fn bip143_sig() {
fn bip143_p2wpkh() {
let tx = deserialize::<Transaction>(
&hex_bytes(
"0100000002fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f000000\
0000eeffffffef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a01000000\
00ffffffff02202cb206000000001976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac9093\
510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac11000000",
).unwrap()[..],
).unwrap();
let witness_script = p2pkh_hex("025476c2e83188368da1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee6357");
let value = 600_000_000;
let comp = SighashComponents::new(&tx);
assert_eq!(
comp,
SighashComponents {
tx_version: 1,
tx_locktime: 17,
hash_prevouts: hex_hash!(
"96b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd37"
),
hash_sequence: hex_hash!(
"52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b"
),
hash_outputs: hex_hash!(
"863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5"
),
}
);
assert_eq!(
comp.sighash_all(&tx.input[1], &witness_script, value),
hex_hash!("c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670")
);
}
#[test]
fn bip143_p2wpkh_nested_in_p2sh() {
let tx = deserialize::<Transaction>(
&hex_bytes(
"0100000001db6b1b20aa0fd7b23880be2ecbd4a98130974cf4748fb66092ac4d3ceb1a5477010000\
0000feffffff02b8b4eb0b000000001976a914a457b684d7f0d539a46a45bbc043f35b59d0d96388ac00\
08af2f000000001976a914fd270b1ee6abcaea97fea7ad0402e8bd8ad6d77c88ac92040000",
).unwrap()[..],
).unwrap();
let witness_script = p2pkh_hex("03ad1d8e89212f0b92c74d23bb710c00662ad1470198ac48c43f7d6f93a2a26873");
let value = 1_000_000_000;
let comp = SighashComponents::new(&tx);
assert_eq!(
comp,
SighashComponents {
tx_version: 1,
tx_locktime: 1170,
hash_prevouts: hex_hash!(
"b0287b4a252ac05af83d2dcef00ba313af78a3e9c329afa216eb3aa2a7b4613a"
),
hash_sequence: hex_hash!(
"18606b350cd8bf565266bc352f0caddcf01e8fa789dd8a15386327cf8cabe198"
),
hash_outputs: hex_hash!(
"de984f44532e2173ca0d64314fcefe6d30da6f8cf27bafa706da61df8a226c83"
),
}
);
assert_eq!(
comp.sighash_all(&tx.input[0], &witness_script, value),
hex_hash!("64f3b0f4dd2bb3aa1ce8566d220cc74dda9df97d8490cc81d89d735c92e59fb6")
);
}
#[test]
fn bip143_p2wsh_nested_in_p2sh() {
let tx = deserialize::<Transaction>(
&hex_bytes(
"010000000136641869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e0100000000\