@@ -43,7 +43,7 @@ use OutPoint;
43
43
use util:: key:: PublicKey ;
44
44
use util:: address:: WitnessVersion ;
45
45
use util:: taproot:: { LeafVersion , TapBranchHash , TapLeafHash } ;
46
- use secp256k1:: { Secp256k1 , Verification } ;
46
+ use secp256k1:: { Secp256k1 , Verification , XOnlyPublicKey } ;
47
47
use schnorr:: { TapTweak , TweakedPublicKey , UntweakedPublicKey } ;
48
48
49
49
/// A Bitcoin script.
@@ -919,6 +919,11 @@ impl Builder {
919
919
}
920
920
}
921
921
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
+
922
927
/// Adds a single opcode to the script.
923
928
pub fn push_opcode ( mut self , data : opcodes:: All ) -> Builder {
924
929
self . 0 . push ( data. into_u8 ( ) ) ;
@@ -1117,6 +1122,17 @@ mod test {
1117
1122
script = script. push_opcode ( opcodes:: all:: OP_CHECKSIG ) ; comp. push ( 0xACu8 ) ; assert_eq ! ( & script[ ..] , & comp[ ..] ) ;
1118
1123
}
1119
1124
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
+
1120
1136
#[ test]
1121
1137
fn script_builder ( ) {
1122
1138
// from txid 3bb5e6434c11fb93f64574af5d116736510717f2c595eb45b52c28e31622dfff which was in my mempool when I wrote the test
0 commit comments