sui_sdk_types/crypto/
secp256r1.rs1#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
13#[cfg_attr(
14 feature = "serde",
15 derive(serde_derive::Serialize, serde_derive::Deserialize)
16)]
17#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
18pub struct Secp256r1PublicKey(
19 #[cfg_attr(
20 feature = "serde",
21 serde(
22 with = "::serde_with::As::<::serde_with::IfIsHumanReadable<super::Base64Array33, [::serde_with::Same; 33]>>"
23 )
24 )]
25 [u8; Self::LENGTH],
26);
27
28impl Secp256r1PublicKey {
29 pub const LENGTH: usize = 33;
31
32 pub const fn new(bytes: [u8; Self::LENGTH]) -> Self {
33 Self(bytes)
34 }
35
36 #[cfg(feature = "rand")]
37 #[cfg_attr(doc_cfg, doc(cfg(feature = "rand")))]
38 pub fn generate<R>(mut rng: R) -> Self
39 where
40 R: rand_core::RngCore + rand_core::CryptoRng,
41 {
42 let mut buf: [u8; Self::LENGTH] = [0; Self::LENGTH];
43 rng.fill_bytes(&mut buf);
44 Self::new(buf)
45 }
46
47 pub const fn into_inner(self) -> [u8; Self::LENGTH] {
49 self.0
50 }
51
52 pub const fn inner(&self) -> &[u8; Self::LENGTH] {
53 &self.0
54 }
55
56 pub const fn as_bytes(&self) -> &[u8] {
57 &self.0
58 }
59
60 pub fn from_bytes<T: AsRef<[u8]>>(bytes: T) -> Result<Self, std::array::TryFromSliceError> {
61 <[u8; Self::LENGTH]>::try_from(bytes.as_ref()).map(Self)
62 }
63}
64
65impl std::str::FromStr for Secp256r1PublicKey {
66 type Err = base64ct::Error;
67
68 fn from_str(s: &str) -> Result<Self, Self::Err> {
69 super::Base64FromStr33::from_str(s).map(|a| Self::new(a.0))
70 }
71}
72
73impl AsRef<[u8]> for Secp256r1PublicKey {
74 fn as_ref(&self) -> &[u8] {
75 &self.0
76 }
77}
78
79impl AsRef<[u8; Self::LENGTH]> for Secp256r1PublicKey {
80 fn as_ref(&self) -> &[u8; Self::LENGTH] {
81 &self.0
82 }
83}
84
85impl From<Secp256r1PublicKey> for [u8; Secp256r1PublicKey::LENGTH] {
86 fn from(public_key: Secp256r1PublicKey) -> Self {
87 public_key.into_inner()
88 }
89}
90
91impl From<[u8; Self::LENGTH]> for Secp256r1PublicKey {
92 fn from(public_key: [u8; Self::LENGTH]) -> Self {
93 Self::new(public_key)
94 }
95}
96
97impl std::fmt::Display for Secp256r1PublicKey {
98 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99 std::fmt::Display::fmt(&super::Base64Display33(&self.0), f)
100 }
101}
102
103impl std::fmt::Debug for Secp256r1PublicKey {
104 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105 f.debug_tuple("Secp256r1PublicKey")
106 .field(&format_args!("\"{}\"", self))
107 .finish()
108 }
109}
110
111#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
121#[cfg_attr(
122 feature = "serde",
123 derive(serde_derive::Serialize, serde_derive::Deserialize)
124)]
125#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
126pub struct Secp256r1Signature(
127 #[cfg_attr(
128 feature = "serde",
129 serde(
130 with = "::serde_with::As::<::serde_with::IfIsHumanReadable<super::Base64Array64, [::serde_with::Same; 64]>>"
131 )
132 )]
133 [u8; Self::LENGTH],
134);
135
136impl Secp256r1Signature {
137 pub const LENGTH: usize = 64;
139
140 pub const fn new(bytes: [u8; Self::LENGTH]) -> Self {
141 Self(bytes)
142 }
143
144 #[cfg(feature = "rand")]
145 #[cfg_attr(doc_cfg, doc(cfg(feature = "rand")))]
146 pub fn generate<R>(mut rng: R) -> Self
147 where
148 R: rand_core::RngCore + rand_core::CryptoRng,
149 {
150 let mut buf: [u8; Self::LENGTH] = [0; Self::LENGTH];
151 rng.fill_bytes(&mut buf);
152 Self::new(buf)
153 }
154
155 pub const fn into_inner(self) -> [u8; Self::LENGTH] {
157 self.0
158 }
159
160 pub const fn inner(&self) -> &[u8; Self::LENGTH] {
161 &self.0
162 }
163
164 pub const fn as_bytes(&self) -> &[u8] {
165 &self.0
166 }
167
168 pub fn from_bytes<T: AsRef<[u8]>>(bytes: T) -> Result<Self, std::array::TryFromSliceError> {
169 <[u8; Self::LENGTH]>::try_from(bytes.as_ref()).map(Self)
170 }
171}
172
173impl std::str::FromStr for Secp256r1Signature {
174 type Err = base64ct::Error;
175
176 fn from_str(s: &str) -> Result<Self, Self::Err> {
177 super::Base64FromStr64::from_str(s).map(|a| Self::new(a.0))
178 }
179}
180
181impl AsRef<[u8]> for Secp256r1Signature {
182 fn as_ref(&self) -> &[u8] {
183 &self.0
184 }
185}
186
187impl AsRef<[u8; Self::LENGTH]> for Secp256r1Signature {
188 fn as_ref(&self) -> &[u8; Self::LENGTH] {
189 &self.0
190 }
191}
192
193impl From<Secp256r1Signature> for [u8; Secp256r1Signature::LENGTH] {
194 fn from(signature: Secp256r1Signature) -> Self {
195 signature.into_inner()
196 }
197}
198
199impl From<[u8; Self::LENGTH]> for Secp256r1Signature {
200 fn from(signature: [u8; Self::LENGTH]) -> Self {
201 Self::new(signature)
202 }
203}
204
205impl std::fmt::Display for Secp256r1Signature {
206 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
207 std::fmt::Display::fmt(&super::Base64Display64(&self.0), f)
208 }
209}
210
211impl std::fmt::Debug for Secp256r1Signature {
212 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
213 f.debug_tuple("Secp256r1Signature")
214 .field(&format_args!("\"{}\"", self))
215 .finish()
216 }
217}