Skip to content

Commit 191192d

Browse files
committed
add bech32m string encoding for common::PublicKey and DkgPublicKey
1 parent e592f97 commit 191192d

5 files changed

Lines changed: 33 additions & 55 deletions

File tree

apps/src/lib/config/genesis.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,29 +77,11 @@ pub mod genesis_config {
7777
#[derive(Error, Debug)]
7878
pub enum HexKeyError {
7979
#[error("Invalid hex string: {0:?}")]
80-
InvalidHexString(data_encoding::DecodeError),
80+
InvalidHexString(#[from] data_encoding::DecodeError),
8181
#[error("Invalid sha256 checksum: {0}")]
82-
InvalidSha256(TryFromSliceError),
82+
InvalidSha256(#[from] TryFromSliceError),
8383
#[error("Invalid public key: {0}")]
84-
InvalidPublicKey(ParsePublicKeyError),
85-
}
86-
87-
impl From<data_encoding::DecodeError> for HexKeyError {
88-
fn from(err: data_encoding::DecodeError) -> Self {
89-
Self::InvalidHexString(err)
90-
}
91-
}
92-
93-
impl From<ParsePublicKeyError> for HexKeyError {
94-
fn from(err: ParsePublicKeyError) -> Self {
95-
Self::InvalidPublicKey(err)
96-
}
97-
}
98-
99-
impl From<TryFromSliceError> for HexKeyError {
100-
fn from(err: TryFromSliceError) -> Self {
101-
Self::InvalidSha256(err)
102-
}
84+
InvalidPublicKey(#[from] common::DecodeError),
10385
}
10486

10587
#[derive(Clone, Debug, Deserialize, Serialize)]

core/src/types/key/common.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use super::{
1414
ParseSignatureError, RefTo, SchemeType, SigScheme as SigSchemeTrait,
1515
VerifySigError,
1616
};
17+
use crate::impl_display_and_from_str_via_format;
18+
use crate::types::string_encoding;
1719

1820
/// Public key
1921
#[derive(
@@ -66,24 +68,23 @@ impl super::PublicKey for PublicKey {
6668
}
6769
}
6870

69-
impl Display for PublicKey {
70-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
71-
write!(f, "{}", HEXLOWER.encode(&self.try_to_vec().unwrap()))
72-
}
73-
}
71+
/// String decoding error
72+
pub type DecodeError = string_encoding::DecodeError;
7473

75-
impl FromStr for PublicKey {
76-
type Err = ParsePublicKeyError;
74+
impl string_encoding::Format for PublicKey {
75+
const HRP: &'static str = string_encoding::COMMON_PK_HRP;
7776

78-
fn from_str(str: &str) -> Result<Self, Self::Err> {
79-
let vec = HEXLOWER
80-
.decode(str.as_ref())
81-
.map_err(ParsePublicKeyError::InvalidHex)?;
82-
Self::try_from_slice(vec.as_slice())
83-
.map_err(ParsePublicKeyError::InvalidEncoding)
77+
fn to_bytes(&self) -> Vec<u8> {
78+
BorshSerialize::try_to_vec(self).unwrap()
79+
}
80+
81+
fn decode_bytes(bytes: &[u8]) -> Result<Self, std::io::Error> {
82+
BorshDeserialize::try_from_slice(bytes)
8483
}
8584
}
8685

86+
impl_display_and_from_str_via_format!(PublicKey);
87+
8788
/// Secret key
8889
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, BorshSchema)]
8990
#[allow(clippy::large_enum_variant)]

core/src/types/key/dkg_session_keys.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
//! Utilities around the DKG session keys
22
33
use std::cmp::Ordering;
4-
use std::fmt::Display;
54
use std::io::{Error, ErrorKind};
6-
use std::str::FromStr;
75

86
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
97
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
10-
use data_encoding::HEXLOWER;
118
use serde::{Deserialize, Serialize};
129

10+
use crate::impl_display_and_from_str_via_format;
1311
use crate::types::address::Address;
14-
use crate::types::key::ParsePublicKeyError;
1512
use crate::types::storage::{DbKeySeg, Key, KeySeg};
13+
use crate::types::string_encoding;
1614
use crate::types::transaction::EllipticCurve;
1715

1816
/// A keypair used in the DKG protocol
@@ -138,27 +136,21 @@ impl BorshSchema for DkgPublicKey {
138136
}
139137
}
140138

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;
149141

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+
}
152146

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)
159149
}
160150
}
161151

152+
impl_display_and_from_str_via_format!(DkgPublicKey);
153+
162154
/// Obtain a storage key for user's public dkg session key.
163155
pub fn dkg_pk_key(owner: &Address) -> Key {
164156
Key::from(owner.to_db_key())

core/src/types/key/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ pub trait PublicKey:
187187
+ Display
188188
+ Debug
189189
+ PartialOrd
190-
+ FromStr<Err = ParsePublicKeyError>
191190
+ Hash
192191
+ Send
193192
+ Sync

core/src/types/string_encoding.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ pub const MASP_PAYMENT_ADDRESS_HRP: &str = "patest";
2828
pub const MASP_PINNED_PAYMENT_ADDRESS_HRP: &str = "ppatest";
2929
/// MASP extended spending key human-readable part
3030
pub const MASP_EXT_SPENDING_KEY_HRP: &str = "xsktest";
31+
/// `common::PublicKey` human-readable part
32+
pub const COMMON_PK_HRP: &str = "pktest";
33+
/// `DkgPublicKey` human-readable part
34+
pub const DKG_PK_HRP: &str = "dpktest";
3135

3236
#[allow(missing_docs)]
3337
#[derive(Error, Debug)]

0 commit comments

Comments
 (0)