Skip to content

Commit

Permalink
feat(jstz_crypto): get hash of public keys
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Dec 31, 2024
1 parent 2770eb8 commit c47fc90
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion crates/jstz_crypto/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use crate::{error::Result, Error};
use std::fmt::{self, Display};

use serde::{Deserialize, Serialize};
use tezos_crypto_rs::hash::{PublicKeyEd25519, PublicKeyP256, PublicKeySecp256k1};
use tezos_crypto_rs::{
hash::{PublicKeyEd25519, PublicKeyP256, PublicKeySecp256k1},
PublicKeyWithHash,
};
use utoipa::ToSchema;

// FIXME: https://linear.app/tezos/issue/JSTZ-169/support-bls-in-risc-v
Expand Down Expand Up @@ -40,6 +43,14 @@ impl PublicKey {
}
}

pub fn hash(&self) -> String {
match self {
PublicKey::Ed25519(pk) => pk.pk_hash().to_string(),
PublicKey::Secp256k1(pk) => pk.pk_hash().to_string(),
PublicKey::P256(pk) => pk.pk_hash().to_string(),
}
}

pub fn from_base58(data: &str) -> Result<Self> {
match &data[..4] {
"edpk" => {
Expand Down Expand Up @@ -105,4 +116,20 @@ mod test {
assert_eq!(PublicKey::from_base58(TZ2).unwrap().to_string(), TZ2);
assert_eq!(PublicKey::from_base58(TZ3).unwrap().to_string(), TZ3);
}

#[test]
fn hash() {
assert_eq!(
PublicKey::from_base58(TZ1).unwrap().hash(),
"tz1cD5CuvAALcxgypqBXcBQEA8dkLJivoFjU"
);
assert_eq!(
PublicKey::from_base58(TZ2).unwrap().hash(),
"tz2KDvEL9fuvytRfe1cVVDo1QfDfaBktGNkh"
);
assert_eq!(
PublicKey::from_base58(TZ3).unwrap().hash(),
"tz3QxNCB8HgxJyp5V9ZmCVGcTm6BzYc14k9C"
);
}
}

0 comments on commit c47fc90

Please sign in to comment.