|
1 | 1 | //! Utilities around the DKG session keys |
2 | 2 |
|
3 | 3 | use std::cmp::Ordering; |
4 | | -use std::fmt::Display; |
5 | 4 | use std::io::{Error, ErrorKind}; |
6 | | -use std::str::FromStr; |
7 | 5 |
|
8 | 6 | use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; |
9 | 7 | use borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; |
10 | | -use data_encoding::HEXLOWER; |
11 | 8 | use serde::{Deserialize, Serialize}; |
12 | 9 |
|
| 10 | +use crate::impl_display_and_from_str_via_format; |
13 | 11 | use crate::types::address::Address; |
14 | | -use crate::types::key::ParsePublicKeyError; |
15 | 12 | use crate::types::storage::{DbKeySeg, Key, KeySeg}; |
| 13 | +use crate::types::string_encoding; |
16 | 14 | use crate::types::transaction::EllipticCurve; |
17 | 15 |
|
18 | 16 | /// A keypair used in the DKG protocol |
@@ -138,27 +136,21 @@ impl BorshSchema for DkgPublicKey { |
138 | 136 | } |
139 | 137 | } |
140 | 138 |
|
141 | | -impl Display for DkgPublicKey { |
142 | | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
143 | | - let vec = self |
144 | | - .try_to_vec() |
145 | | - .expect("Encoding public key shouldn't fail"); |
146 | | - write!(f, "{}", HEXLOWER.encode(&vec)) |
147 | | - } |
148 | | -} |
| 139 | +impl string_encoding::Format for DkgPublicKey { |
| 140 | + const HRP: &'static str = string_encoding::DKG_PK_HRP; |
149 | 141 |
|
150 | | -impl FromStr for DkgPublicKey { |
151 | | - type Err = ParsePublicKeyError; |
| 142 | + fn to_bytes(&self) -> Vec<u8> { |
| 143 | + self.try_to_vec() |
| 144 | + .expect("Encoding public key shouldn't fail") |
| 145 | + } |
152 | 146 |
|
153 | | - fn from_str(s: &str) -> Result<Self, Self::Err> { |
154 | | - let vec = HEXLOWER |
155 | | - .decode(s.as_ref()) |
156 | | - .map_err(ParsePublicKeyError::InvalidHex)?; |
157 | | - BorshDeserialize::try_from_slice(&vec) |
158 | | - .map_err(ParsePublicKeyError::InvalidEncoding) |
| 147 | + fn decode_bytes(bytes: &[u8]) -> Result<Self, std::io::Error> { |
| 148 | + BorshDeserialize::try_from_slice(bytes) |
159 | 149 | } |
160 | 150 | } |
161 | 151 |
|
| 152 | +impl_display_and_from_str_via_format!(DkgPublicKey); |
| 153 | + |
162 | 154 | /// Obtain a storage key for user's public dkg session key. |
163 | 155 | pub fn dkg_pk_key(owner: &Address) -> Key { |
164 | 156 | Key::from(owner.to_db_key()) |
|
0 commit comments