f27c4a541d Added push_x_only_key(..) and its test. (mpls)

Pull request description:

  **Issue**

  I can not use [`XOnlyPublicKey`](ae985dd191/src/key.rs (L973)) in my Scripts which prevents me from working with Taproot.

  **Cause**

  The current version of [`script::Builder`](0a2d45de09/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`](0a2d45de09/src/blockdata/script.rs (L914)) function. I also augmented an [existing test](0a2d45de09/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 f27c4a541d
  sanket1729:
    utACK f27c4a541d. Thanks a lot for keeping up the iterations with prompt responses

Tree-SHA512: 064958d49edc1d3636a21e428d62c2e9bcd9b13bd226c5821db9e04ce78663a11fcf601c7667b564f88e845207219a052e1c7413f50e5d27c79003e8129825ed
This commit is contained in:
Dr. Maxim Orlovsky 2022-04-01 19:30:29 +03:00
commit 3f04c04b3d
No known key found for this signature in database
GPG Key ID: AF91255DEA466640
1 changed files with 17 additions and 1 deletions

View File

@ -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.
@ -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());
@ -1117,6 +1122,17 @@ mod test {
script = script.push_opcode(opcodes::all::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(&script[..], &comp[..]);
}
#[test]
fn script_x_only_key() {
// 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.
// 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();
let script = Builder::new().push_x_only_key(&x_only_key);
assert_eq!(script.0, Vec::from_hex(keystr).unwrap());
}
#[test]
fn script_builder() {
// from txid 3bb5e6434c11fb93f64574af5d116736510717f2c595eb45b52c28e31622dfff which was in my mempool when I wrote the test