radix_common/crypto/
public_key_hash.rs

1use crate::internal_prelude::*;
2
3//===============
4// TRAITS + UTILS
5//===============
6
7pub trait HasPublicKeyHash {
8    type TypedPublicKeyHash: IsPublicKeyHash;
9
10    fn get_hash(&self) -> Self::TypedPublicKeyHash;
11
12    fn signature_proof(&self) -> NonFungibleGlobalId {
13        NonFungibleGlobalId::from_public_key_hash(self.get_hash())
14    }
15}
16
17impl<T: HasPublicKeyHash> HasPublicKeyHash for &T {
18    type TypedPublicKeyHash = T::TypedPublicKeyHash;
19
20    fn get_hash(&self) -> Self::TypedPublicKeyHash {
21        <T as HasPublicKeyHash>::get_hash(self)
22    }
23}
24
25pub trait IsPublicKeyHash: Copy + HasPublicKeyHash {
26    fn get_hash_bytes(&self) -> &[u8; NodeId::RID_LENGTH];
27    fn into_enum(self) -> PublicKeyHash;
28}
29
30pub fn hash_public_key_bytes<T: AsRef<[u8]>>(key_bytes: T) -> [u8; NodeId::RID_LENGTH] {
31    hash(key_bytes).lower_bytes()
32}
33
34//===============
35// ENUM TYPE
36//===============
37
38/// The hash of a given public key.
39///
40/// In particular, it is the last 29 bytes of Blake2b-256 hash of the public key in the Radix canonical encoding.
41#[cfg_attr(feature = "fuzzing", derive(::arbitrary::Arbitrary))]
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Categorize, Encode, Decode, BasicDescribe)]
43pub enum PublicKeyHash {
44    Secp256k1(Secp256k1PublicKeyHash),
45    Ed25519(Ed25519PublicKeyHash),
46}
47
48impl Describe<ScryptoCustomTypeKind> for PublicKeyHash {
49    const TYPE_ID: RustTypeId =
50        RustTypeId::WellKnown(well_known_scrypto_custom_types::PUBLIC_KEY_HASH_TYPE);
51
52    fn type_data() -> ScryptoTypeData<RustTypeId> {
53        well_known_scrypto_custom_types::public_key_hash_type_data()
54    }
55}
56
57impl From<Secp256k1PublicKeyHash> for PublicKeyHash {
58    fn from(public_key: Secp256k1PublicKeyHash) -> Self {
59        Self::Secp256k1(public_key)
60    }
61}
62
63impl From<Ed25519PublicKeyHash> for PublicKeyHash {
64    fn from(public_key: Ed25519PublicKeyHash) -> Self {
65        Self::Ed25519(public_key)
66    }
67}
68
69impl PublicKeyHash {
70    pub fn new_from_public_key(public_key: &PublicKey) -> Self {
71        match public_key {
72            PublicKey::Secp256k1(public_key) => {
73                PublicKeyHash::Secp256k1(Secp256k1PublicKeyHash::new_from_public_key(public_key))
74            }
75            PublicKey::Ed25519(public_key) => {
76                PublicKeyHash::Ed25519(Ed25519PublicKeyHash::new_from_public_key(public_key))
77            }
78        }
79    }
80}
81
82impl IsPublicKeyHash for PublicKeyHash {
83    fn get_hash_bytes(&self) -> &[u8; NodeId::RID_LENGTH] {
84        match self {
85            PublicKeyHash::Secp256k1(value) => value.get_hash_bytes(),
86            PublicKeyHash::Ed25519(value) => value.get_hash_bytes(),
87        }
88    }
89
90    fn into_enum(self) -> PublicKeyHash {
91        self
92    }
93}
94
95impl HasPublicKeyHash for PublicKeyHash {
96    type TypedPublicKeyHash = Self;
97
98    fn get_hash(&self) -> Self::TypedPublicKeyHash {
99        *self
100    }
101}