Skip to content

Commit 3501214

Browse files
committed
Added push_x_only_key(..) and its test.
1 parent 6f27bf5 commit 3501214

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/blockdata/script.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use OutPoint;
4343
use util::key::PublicKey;
4444
use util::address::WitnessVersion;
4545
use util::taproot::{LeafVersion, TapBranchHash, TapLeafHash};
46-
use secp256k1::{Secp256k1, Verification};
46+
use secp256k1::{Secp256k1, Verification, XOnlyPublicKey};
4747
use schnorr::{TapTweak, TweakedPublicKey, UntweakedPublicKey};
4848

4949
/// A Bitcoin script.
@@ -919,6 +919,11 @@ impl Builder {
919919
}
920920
}
921921

922+
/// Adds instructions to push an XOnly public key onto the stack.
923+
pub fn push_x_only_key(self, x_only_key: &XOnlyPublicKey) -> Builder {
924+
self.push_slice(&x_only_key.serialize())
925+
}
926+
922927
/// Adds a single opcode to the script.
923928
pub fn push_opcode(mut self, data: opcodes::All) -> Builder {
924929
self.0.push(data.into_u8());
@@ -1117,6 +1122,17 @@ mod test {
11171122
script = script.push_opcode(opcodes::all::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(&script[..], &comp[..]);
11181123
}
11191124

1125+
#[test]
1126+
fn script_x_only_key() {
1127+
// Notice the "20" which prepends the keystr. That 20 is hexidecimal for "32". The Builder automatically adds the 32 opcode
1128+
// 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.
1129+
// From: https://github.com/bitcoin-core/btcdeb/blob/e8c2750c4a4702768c52d15640ed03bf744d2601/doc/tapscript-example.md?plain=1#L43
1130+
let keystr = "209997a497d964fc1a62885b05a51166a65a90df00492c8d7cf61d6accf54803be";
1131+
let x_only_key = XOnlyPublicKey::from_str(&keystr[2..]).unwrap();
1132+
let script = Builder::new().push_x_only_key(&x_only_key);
1133+
assert_eq!(script.0, Vec::from_hex(keystr).unwrap());
1134+
}
1135+
11201136
#[test]
11211137
fn script_builder() {
11221138
// from txid 3bb5e6434c11fb93f64574af5d116736510717f2c595eb45b52c28e31622dfff which was in my mempool when I wrote the test

0 commit comments

Comments
 (0)