Skip to content

Commit

Permalink
Added push_x_only_key(..) and its test.
Browse files Browse the repository at this point in the history
  • Loading branch information
mplsgrant committed Mar 31, 2022
1 parent 8d602b8 commit 8a042dd
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/blockdata/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use OutPoint;
use util::key::PublicKey;
use util::address::WitnessVersion;
use util::taproot::{LeafVersion, TapBranchHash, TapLeafHash};
use secp256k1::{Secp256k1, Verification};
use secp256k1::{Secp256k1, Verification, XOnlyPublicKey};
use schnorr::{TapTweak, TweakedPublicKey, UntweakedPublicKey};

/// A Bitcoin script.
Expand Down Expand Up @@ -919,6 +919,11 @@ impl Builder {
}
}

/// Adds instructions to push an XOnly public key onto the stack.
pub fn push_x_only_key(self, x_only_key: &XOnlyPublicKey) -> Builder {
self.push_slice(&x_only_key.serialize())
}

/// Adds a single opcode to the script.
pub fn push_opcode(mut self, data: opcodes::All) -> Builder {
self.0.push(data.into_u8());
Expand Down Expand Up @@ -1117,6 +1122,20 @@ mod test {
script = script.push_opcode(opcodes::all::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(&script[..], &comp[..]);
}

#[test]
fn script_x_only_key() {
let mut comp = vec![];
let mut script = Builder::new();
assert_eq!(&script[..], &comp[..]);
// Notice the "20" which prepends the 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.
// To mimic the Builder's automatic insertion of the 32 opcode, we manually add it to the Vec and slice it out in the from_str.
// From: https://github.com/bitcoin-core/btcdeb/blob/e8c2750c4a4702768c52d15640ed03bf744d2601/doc/tapscript-example.md?plain=1#L43
let keystr = "209997a497d964fc1a62885b05a51166a65a90df00492c8d7cf61d6accf54803be";
let x_only_key = XOnlyPublicKey::from_str(&keystr[2..]).unwrap();
script = script.push_x_only_key(&x_only_key); comp.extend(Vec::from_hex(keystr).unwrap().iter().cloned()); assert_eq!(&script[..], &comp[..]);
}

#[test]
fn script_builder() {
// from txid 3bb5e6434c11fb93f64574af5d116736510717f2c595eb45b52c28e31622dfff which was in my mempool when I wrote the test
Expand Down

0 comments on commit 8a042dd

Please sign in to comment.