Skip to content

Commit

Permalink
primitives: add InternalPk constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 4, 2023
1 parent b775ecd commit d962c2a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions primitives/src/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ pub const MIDSTATE_TAPTWEAK: [u8; 8] = *b"TapTweak";
pub const MIDSTATE_TAPSIGHASH: [u8; 10] = *b"TapSighash";
// f504a425d7f8783b1363868ae3e556586eee945dbc7888dd02a6e2c31873fe9f

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error)]
#[display("invalid public key")]
pub struct InvalidPubkey;

#[derive(Wrapper, WrapperMut, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)]
#[wrapper(Deref, LowerHex, Display, FromStr)]
#[wrapper_mut(DerefMut)]
Expand All @@ -66,6 +70,14 @@ pub const MIDSTATE_TAPSIGHASH: [u8; 10] = *b"TapSighash";
pub struct InternalPk(XOnlyPublicKey);

impl InternalPk {
pub fn from_byte_array(data: [u8; 32]) -> Result<Self, InvalidPubkey> {
XOnlyPublicKey::from_slice(data.as_ref())
.map(Self)
.map_err(|_| InvalidPubkey)
}

pub fn to_byte_array(&self) -> [u8; 32] { self.0.serialize() }

pub fn to_output_key(&self, merkle_root: Option<impl IntoTapHash>) -> XOnlyPublicKey {
let mut engine = Sha256::from_tag(MIDSTATE_TAPTWEAK);
// always hash the key
Expand Down

0 comments on commit d962c2a

Please sign in to comment.