Skip to content

Commit

Permalink
add bech32m string encoding for common::PublicKey and DkgPublicKey
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Nov 30, 2022
1 parent 90c5784 commit 7933bec
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 55 deletions.
24 changes: 3 additions & 21 deletions apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,11 @@ pub mod genesis_config {
#[derive(Error, Debug)]
pub enum HexKeyError {
#[error("Invalid hex string: {0:?}")]
InvalidHexString(data_encoding::DecodeError),
InvalidHexString(#[from] data_encoding::DecodeError),
#[error("Invalid sha256 checksum: {0}")]
InvalidSha256(TryFromSliceError),
InvalidSha256(#[from] TryFromSliceError),
#[error("Invalid public key: {0}")]
InvalidPublicKey(ParsePublicKeyError),
}

impl From<data_encoding::DecodeError> for HexKeyError {
fn from(err: data_encoding::DecodeError) -> Self {
Self::InvalidHexString(err)
}
}

impl From<ParsePublicKeyError> for HexKeyError {
fn from(err: ParsePublicKeyError) -> Self {
Self::InvalidPublicKey(err)
}
}

impl From<TryFromSliceError> for HexKeyError {
fn from(err: TryFromSliceError) -> Self {
Self::InvalidSha256(err)
}
InvalidPublicKey(#[from] common::DecodeError),
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down
27 changes: 14 additions & 13 deletions core/src/types/key/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use super::{
ParseSignatureError, RefTo, SchemeType, SigScheme as SigSchemeTrait,
VerifySigError,
};
use crate::impl_display_and_from_str_via_format;
use crate::types::string_encoding;

/// Public key
#[derive(
Expand Down Expand Up @@ -66,24 +68,23 @@ impl super::PublicKey for PublicKey {
}
}

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

impl FromStr for PublicKey {
type Err = ParsePublicKeyError;
impl string_encoding::Format for PublicKey {
const HRP: &'static str = string_encoding::COMMON_PK_HRP;

fn from_str(str: &str) -> Result<Self, Self::Err> {
let vec = HEXLOWER
.decode(str.as_ref())
.map_err(ParsePublicKeyError::InvalidHex)?;
Self::try_from_slice(vec.as_slice())
.map_err(ParsePublicKeyError::InvalidEncoding)
fn to_bytes(&self) -> Vec<u8> {
BorshSerialize::try_to_vec(self).unwrap()
}

fn decode_bytes(bytes: &[u8]) -> Result<Self, std::io::Error> {
BorshDeserialize::try_from_slice(bytes)
}
}

impl_display_and_from_str_via_format!(PublicKey);

/// Secret key
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, BorshSchema)]
#[allow(clippy::large_enum_variant)]
Expand Down
32 changes: 12 additions & 20 deletions core/src/types/key/dkg_session_keys.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
//! Utilities around the DKG session keys

use std::cmp::Ordering;
use std::fmt::Display;
use std::io::{Error, ErrorKind};
use std::str::FromStr;

use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use data_encoding::HEXLOWER;
use serde::{Deserialize, Serialize};

use crate::impl_display_and_from_str_via_format;
use crate::types::address::Address;
use crate::types::key::ParsePublicKeyError;
use crate::types::storage::{DbKeySeg, Key, KeySeg};
use crate::types::string_encoding;
use crate::types::transaction::EllipticCurve;

/// A keypair used in the DKG protocol
Expand Down Expand Up @@ -138,27 +136,21 @@ impl BorshSchema for DkgPublicKey {
}
}

impl Display for DkgPublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let vec = self
.try_to_vec()
.expect("Encoding public key shouldn't fail");
write!(f, "{}", HEXLOWER.encode(&vec))
}
}
impl string_encoding::Format for DkgPublicKey {
const HRP: &'static str = string_encoding::DKG_PK_HRP;

impl FromStr for DkgPublicKey {
type Err = ParsePublicKeyError;
fn to_bytes(&self) -> Vec<u8> {
self.try_to_vec()
.expect("Encoding public key shouldn't fail")
}

fn from_str(s: &str) -> Result<Self, Self::Err> {
let vec = HEXLOWER
.decode(s.as_ref())
.map_err(ParsePublicKeyError::InvalidHex)?;
BorshDeserialize::try_from_slice(&vec)
.map_err(ParsePublicKeyError::InvalidEncoding)
fn decode_bytes(bytes: &[u8]) -> Result<Self, std::io::Error> {
BorshDeserialize::try_from_slice(bytes)
}
}

impl_display_and_from_str_via_format!(DkgPublicKey);

/// Obtain a storage key for user's public dkg session key.
pub fn dkg_pk_key(owner: &Address) -> Key {
Key::from(owner.to_db_key())
Expand Down
1 change: 0 additions & 1 deletion core/src/types/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ pub trait PublicKey:
+ Display
+ Debug
+ PartialOrd
+ FromStr<Err = ParsePublicKeyError>
+ Hash
+ Send
+ Sync
Expand Down
4 changes: 4 additions & 0 deletions core/src/types/string_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ pub const MASP_PAYMENT_ADDRESS_HRP: &str = "patest";
pub const MASP_PINNED_PAYMENT_ADDRESS_HRP: &str = "ppatest";
/// MASP extended spending key human-readable part
pub const MASP_EXT_SPENDING_KEY_HRP: &str = "xsktest";
/// `common::PublicKey` human-readable part
pub const COMMON_PK_HRP: &str = "pktest";
/// `DkgPublicKey` human-readable part
pub const DKG_PK_HRP: &str = "dpktest";

#[allow(missing_docs)]
#[derive(Error, Debug)]
Expand Down

0 comments on commit 7933bec

Please sign in to comment.