Skip to content

Commit

Permalink
PubKey updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptoquick committed Mar 2, 2024
1 parent 2607728 commit e33f76b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
29 changes: 24 additions & 5 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ pub struct EncodeInfo {
/// i.e., Encoded(encoded_bytes, bao_hash, encode_info)
pub struct Encoded(pub Vec<u8>, 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;
Expand All @@ -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<u8> {
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()
}
Expand Down
7 changes: 7 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ pub fn bech32_decode(bech32_str: &str) -> Result<(String, Vec<u8>), CarbonadoErr
let (hrp, words) = decode(bech32_str)?;
Ok((hrp.to_string(), words))
}

pub fn pub_keyed_hash(ss: &str, bytes: &[u8]) -> Result<String, CarbonadoError> {
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)
}

0 comments on commit e33f76b

Please sign in to comment.