Skip to content

Commit e16c17c

Browse files
committed
Merge rust-bitcoin/rust-bitcoin#920: Push key xonly
f27c4a5 Added push_x_only_key(..) and its test. (mpls) Pull request description: **Issue** I can not use [`XOnlyPublicKey`](https://github.com/rust-bitcoin/rust-secp256k1/blob/ae985dd1911183c22bab9c9a199aca7b154c40f9/src/key.rs#L973) in my Scripts which prevents me from working with Taproot. **Cause** The current version of [`script::Builder`](https://github.com/rust-bitcoin/rust-bitcoin/blob/0a2d45de094c4c0a4861dc19314568684ac4d91c/src/blockdata/script.rs#L121) does not accept `XOnlyPublicKey`s. **Solution** So, I created a function `push_xkey(self, key: &XOnlyPublicKey)` based on the existing [`push_key`](https://github.com/rust-bitcoin/rust-bitcoin/blob/0a2d45de094c4c0a4861dc19314568684ac4d91c/src/blockdata/script.rs#L914) function. I also augmented an [existing test](https://github.com/rust-bitcoin/rust-bitcoin/blob/0a2d45de094c4c0a4861dc19314568684ac4d91c/src/blockdata/script.rs#L1108) in an attempt to reach testing parity with existing code. After toying around with `push_xkey`, it seems to work on my end. ACKs for top commit: dr-orlovsky: ACK f27c4a5 sanket1729: utACK f27c4a5. Thanks a lot for keeping up the iterations with prompt responses Tree-SHA512: 064958d49edc1d3636a21e428d62c2e9bcd9b13bd226c5821db9e04ce78663a11fcf601c7667b564f88e845207219a052e1c7413f50e5d27c79003e8129825ed
2 parents 580e89c + 3501214 commit e16c17c

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)