radix_common/crypto/
public_key.rs

1use crate::internal_prelude::*;
2
3/// Represents any natively supported public key.
4#[cfg_attr(feature = "fuzzing", derive(::arbitrary::Arbitrary))]
5#[cfg_attr(
6    feature = "serde",
7    derive(::serde::Serialize, ::serde::Deserialize),
8    serde(tag = "type", content = "public_key")
9)]
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Categorize, Encode, Decode, BasicDescribe)]
11pub enum PublicKey {
12    Secp256k1(Secp256k1PublicKey),
13    Ed25519(Ed25519PublicKey),
14}
15
16impl Describe<ScryptoCustomTypeKind> for PublicKey {
17    const TYPE_ID: RustTypeId =
18        RustTypeId::WellKnown(well_known_scrypto_custom_types::PUBLIC_KEY_TYPE);
19
20    fn type_data() -> ScryptoTypeData<RustTypeId> {
21        well_known_scrypto_custom_types::public_key_type_data()
22    }
23}
24
25impl From<Secp256k1PublicKey> for PublicKey {
26    fn from(public_key: Secp256k1PublicKey) -> Self {
27        Self::Secp256k1(public_key)
28    }
29}
30
31impl From<Ed25519PublicKey> for PublicKey {
32    fn from(public_key: Ed25519PublicKey) -> Self {
33        Self::Ed25519(public_key)
34    }
35}
36
37impl HasPublicKeyHash for PublicKey {
38    type TypedPublicKeyHash = PublicKeyHash;
39
40    fn get_hash(&self) -> Self::TypedPublicKeyHash {
41        PublicKeyHash::new_from_public_key(self)
42    }
43}