From 10e3f06b5e1cdd50c8463d2d0bda8346121f45ff Mon Sep 17 00:00:00 2001 From: mpls Date: Wed, 30 Mar 2022 16:14:39 -0500 Subject: [PATCH] Created a separate test for push_x_only_key. --- src/blockdata/script.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index 98b2b1d5fa..fd8e96d521 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -1117,6 +1117,34 @@ mod test { let key = PublicKey::from_str(&keystr[2..]).unwrap(); script = script.push_key(&key); comp.extend(Vec::from_hex(keystr).unwrap().iter().cloned()); assert_eq!(&script[..], &comp[..]); + // opcodes + script = script.push_opcode(opcodes::all::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(&script[..], &comp[..]); + script = script.push_opcode(opcodes::all::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(&script[..], &comp[..]); + } + + #[test] + fn script_x_only_keys() { + let mut comp = vec![]; + let mut script = Builder::new(); + assert_eq!(&script[..], &comp[..]); + + // small ints + script = script.push_int(1); comp.push(81u8); assert_eq!(&script[..], &comp[..]); + script = script.push_int(0); comp.push(0u8); assert_eq!(&script[..], &comp[..]); + script = script.push_int(4); comp.push(84u8); assert_eq!(&script[..], &comp[..]); + script = script.push_int(-1); comp.push(79u8); assert_eq!(&script[..], &comp[..]); + // forced scriptint + script = script.push_scriptint(4); comp.extend([1u8, 4].iter().cloned()); assert_eq!(&script[..], &comp[..]); + // big ints + script = script.push_int(17); comp.extend([1u8, 17].iter().cloned()); assert_eq!(&script[..], &comp[..]); + script = script.push_int(10000); comp.extend([2u8, 16, 39].iter().cloned()); assert_eq!(&script[..], &comp[..]); + // notice the sign bit set here, hence the extra zero/128 at the end + script = script.push_int(10000000); comp.extend([4u8, 128, 150, 152, 0].iter().cloned()); assert_eq!(&script[..], &comp[..]); + script = script.push_int(-10000000); comp.extend([4u8, 128, 150, 152, 128].iter().cloned()); assert_eq!(&script[..], &comp[..]); + + // data + script = script.push_slice("NRA4VR".as_bytes()); comp.extend([6u8, 78, 82, 65, 52, 86, 82].iter().cloned()); assert_eq!(&script[..], &comp[..]); + // XOnly keys // Notice the "20" which prepends each keystr. That 20 is hexidecimal for "32". The Builder automatically adds the 32 opcode // to our script in order to give a heads up to the script compiler that it should add the next 32 bytes to the stack. @@ -1128,7 +1156,6 @@ mod test { let key = XOnlyPublicKey::from_str(&keystr[2..]).unwrap(); script = script.push_x_only_key(&key); comp.extend(Vec::from_hex(keystr).unwrap().iter().cloned()); assert_eq!(&script[..], &comp[..]); - // opcodes script = script.push_opcode(opcodes::all::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(&script[..], &comp[..]); script = script.push_opcode(opcodes::all::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(&script[..], &comp[..]);