Skip to content

Commit

Permalink
Created a separate test for push_x_only_key.
Browse files Browse the repository at this point in the history
  • Loading branch information
mplsgrant committed Mar 30, 2022
1 parent 04bbf73 commit 10e3f06
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/blockdata/script.rs
Expand Up @@ -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.
Expand All @@ -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[..]);
Expand Down

0 comments on commit 10e3f06

Please sign in to comment.