From e33f76b0a0cbe6700a6702fb3d937fc8e8e68eb5 Mon Sep 17 00:00:00 2001 From: Hunter Trujillo Date: Sat, 2 Mar 2024 11:51:42 -0700 Subject: [PATCH] PubKey updates. --- Cargo.toml | 1 + src/structs.rs | 29 ++++++++++++++++++++++++----- src/utils.rs | 7 +++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3b6ac97..64f87d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ include = ["src/**/*", "LICENSE", "README.md"] [dependencies] bao = "0.12.1" bech32 = "0.11.0" +blake3 = "1.5.0" bitmask-enum = "2.1.0" bytes = "1.4.0" ecies = { version = "0.2.6", default-features = false, features = [ diff --git a/src/structs.rs b/src/structs.rs index dbedffa..dba78a5 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -47,7 +47,10 @@ pub struct EncodeInfo { /// i.e., Encoded(encoded_bytes, bao_hash, encode_info) pub struct Encoded(pub Vec, pub bao::Hash, pub EncodeInfo); -pub struct Secp256k1PubKey(pub secp256k1::PublicKey); +pub struct Secp256k1PubKey { + pub pk: secp256k1::PublicKey, + pub x_only_pk: [u8; 32], +} impl TryFrom<&str> for Secp256k1PubKey { type Error = CarbonadoError; @@ -71,27 +74,43 @@ impl TryFrom<&str> for Secp256k1PubKey { _ => return Err(CarbonadoError::IncorrectPubKeyFormat), }; - Ok(Self(pk)) + let (x_only_pk, _) = pk.x_only_public_key(); + let x_only_pk = x_only_pk.serialize(); + + Ok(Self { pk, x_only_pk }) } } impl Display for Secp256k1PubKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let Self(pk) = self; + let Self { pk, .. } = self; f.write_str(&pk.to_string()) } } +impl AsRef<[u8; 32]> for Secp256k1PubKey { + fn as_ref(&self) -> &[u8; 32] { + &self.x_only_pk + } +} + impl Secp256k1PubKey { + pub fn new(pk: secp256k1::PublicKey) -> Self { + let (x_only_pk, _) = pk.x_only_public_key(); + let x_only_pk = x_only_pk.serialize(); + + Self { pk, x_only_pk } + } + pub fn to_bytes(&self) -> Vec { - let Self(pk) = self; + let Self { pk, .. } = self; pk.serialize().to_vec() } pub fn into_inner(&self) -> secp256k1::PublicKey { - let Self(pk) = self; + let Self { pk, .. } = self; pk.to_owned() } diff --git a/src/utils.rs b/src/utils.rs index 6efd3b5..14972b8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -63,3 +63,10 @@ pub fn bech32_decode(bech32_str: &str) -> Result<(String, Vec), CarbonadoErr let (hrp, words) = decode(bech32_str)?; Ok((hrp.to_string(), words)) } + +pub fn pub_keyed_hash(ss: &str, bytes: &[u8]) -> Result { + let key = hex::decode(ss)?; + let key_bytes: [u8; 32] = key[0..32].try_into()?; + let pub_keyed_hash = blake3::keyed_hash(&key_bytes, bytes).to_hex().to_string(); + Ok(pub_keyed_hash) +}