cardano_sdk/cardano/crypto/ed25519/
verification_key.rs1use crate::{
6 Address, Credential, NetworkId, PlutusData, Signature, address::kind, pallas::ed25519,
7};
8use std::{cmp, fmt, str::FromStr};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12#[repr(transparent)]
13pub struct VerificationKey(ed25519::PublicKey);
14
15impl fmt::Display for VerificationKey {
16 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
17 self.0.fmt(f)
18 }
19}
20
21impl PartialOrd for VerificationKey {
22 fn partial_cmp(&self, rhs: &Self) -> Option<cmp::Ordering> {
23 Some(self.cmp(rhs))
24 }
25}
26
27impl Ord for VerificationKey {
28 fn cmp(&self, rhs: &Self) -> cmp::Ordering {
29 let lhs = self.as_ref();
30 let rhs = rhs.as_ref();
31 lhs.cmp(rhs)
32 }
33}
34
35impl VerificationKey {
38 pub const SIZE: usize = ed25519::PublicKey::SIZE;
39
40 pub fn verify<T>(&self, message: T, signature: &Signature) -> bool
43 where
44 T: AsRef<[u8]>,
45 {
46 self.0
47 .verify(message, <&ed25519::Signature>::from(signature))
48 }
49}
50
51impl From<[u8; 32]> for VerificationKey {
54 fn from(bytes: [u8; 32]) -> Self {
55 Self(ed25519::PublicKey::from(bytes))
56 }
57}
58
59impl TryFrom<Vec<u8>> for VerificationKey {
60 type Error = Vec<u8>;
61
62 fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
63 Ok(Self::from(<[u8; 32]>::try_from(value)?))
64 }
65}
66
67impl From<ed25519::PublicKey> for VerificationKey {
68 fn from(key: ed25519::PublicKey) -> Self {
69 Self(key)
70 }
71}
72
73impl FromStr for VerificationKey {
74 type Err = anyhow::Error;
75
76 fn from_str(s: &str) -> anyhow::Result<Self> {
77 Ok(VerificationKey(ed25519::PublicKey::from_str(s)?))
78 }
79}
80
81impl<'a> TryFrom<&PlutusData<'a>> for VerificationKey {
82 type Error = anyhow::Error;
83
84 fn try_from(data: &PlutusData<'a>) -> anyhow::Result<Self> {
85 let array =
86 <&'_ [u8; 32]>::try_from(data).map_err(|e| e.context("invalid verification key"))?;
87 Ok(Self(ed25519::PublicKey::from(*array)))
88 }
89}
90
91impl<'a> TryFrom<PlutusData<'a>> for VerificationKey {
92 type Error = anyhow::Error;
93
94 fn try_from(data: PlutusData<'a>) -> anyhow::Result<Self> {
95 Self::try_from(&data)
96 }
97}
98
99impl VerificationKey {
102 pub fn to_address(&self, network_id: NetworkId) -> Address<kind::Shelley> {
103 Address::new(network_id, self.to_credential())
104 }
105
106 pub fn to_credential(&self) -> Credential {
107 Credential::from(self)
108 }
109
110 pub fn as_plutus_data<'a>(&'a self) -> PlutusData<'a> {
111 PlutusData::from(self)
112 }
113
114 pub fn as_slice(&self) -> &[u8] {
115 self.as_ref()
116 }
117
118 pub fn into_bytes(self) -> [u8; 32] {
119 <[u8; 32]>::from(self)
120 }
121}
122
123impl AsRef<[u8]> for VerificationKey {
124 fn as_ref(&self) -> &[u8] {
125 self.0.as_ref()
126 }
127}
128
129impl From<VerificationKey> for [u8; 32] {
130 fn from(key: VerificationKey) -> Self {
131 key.0.into()
132 }
133}
134
135impl From<VerificationKey> for ed25519::PublicKey {
136 fn from(key: VerificationKey) -> Self {
137 key.0
138 }
139}
140
141impl<'a> From<&'a VerificationKey> for &'a ed25519::PublicKey {
142 fn from(key: &'a VerificationKey) -> Self {
143 &key.0
144 }
145}
146
147impl<'a> From<&'a VerificationKey> for PlutusData<'a> {
148 fn from(key: &'a VerificationKey) -> Self {
149 Self::bytes(key.0)
150 }
151}