Skip to content

Commit

Permalink
Implement FromStr for Ss58AddressFormat (paritytech#7068)
Browse files Browse the repository at this point in the history
* Implement `FromStr` for `Ss58AddressFormat`

* Update primitives/core/src/crypto.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
  • Loading branch information
2 people authored and Andrew Plaza committed Nov 10, 2020
1 parent fbf7421 commit 017a604
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions primitives/core/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
}

/// Return the ss58-check string for this key.

#[cfg(feature = "std")]
fn to_ss58check_with_version(&self, version: Ss58AddressFormat) -> String {
let mut v = vec![version.into()];
Expand All @@ -274,9 +273,11 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
v.extend(&r.as_bytes()[0..2]);
v.to_base58()
}

/// Return the ss58-check string for this key.
#[cfg(feature = "std")]
fn to_ss58check(&self) -> String { self.to_ss58check_with_version(*DEFAULT_VERSION.lock()) }

/// Some if the string is a properly encoded SS58Check address, optionally with
/// a derivation path following.
#[cfg(feature = "std")]
Expand Down Expand Up @@ -317,8 +318,7 @@ lazy_static::lazy_static! {
macro_rules! ss58_address_format {
( $( $identifier:tt => ($number:expr, $name:expr, $desc:tt) )* ) => (
/// A known address (sub)format/network ID for SS58.
#[derive(Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Copy, Clone, PartialEq, Eq, crate::RuntimeDebug)]
pub enum Ss58AddressFormat {
$(#[doc = $desc] $identifier),*,
/// Use a manually provided numeric value.
Expand All @@ -328,7 +328,13 @@ macro_rules! ss58_address_format {
#[cfg(feature = "std")]
impl std::fmt::Display for Ss58AddressFormat {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
match self {
$(
Ss58AddressFormat::$identifier => write!(f, "{}", $name),
)*
Ss58AddressFormat::Custom(x) => write!(f, "{}", x),
}

}
}

Expand All @@ -337,6 +343,12 @@ macro_rules! ss58_address_format {
];

impl Ss58AddressFormat {
/// names of all address formats
pub fn all_names() -> &'static [&'static str] {
&[
$($name),*,
]
}
/// All known address formats.
pub fn all() -> &'static [Ss58AddressFormat] {
&ALL_SS58_ADDRESS_FORMATS
Expand Down Expand Up @@ -372,25 +384,46 @@ macro_rules! ss58_address_format {
Ss58AddressFormat::Custom(n) if n == x => Ok(Ss58AddressFormat::Custom(x)),
_ => Err(()),
}

#[cfg(not(feature = "std"))]
Err(())
},
}
}
}

/// Error encountered while parsing `Ss58AddressFormat` from &'_ str
/// unit struct for now.
#[derive(Copy, Clone, PartialEq, Eq, crate::RuntimeDebug)]
pub struct ParseError;

impl<'a> TryFrom<&'a str> for Ss58AddressFormat {
type Error = ();
type Error = ParseError;

fn try_from(x: &'a str) -> Result<Ss58AddressFormat, ()> {
fn try_from(x: &'a str) -> Result<Ss58AddressFormat, Self::Error> {
match x {
$($name => Ok(Ss58AddressFormat::$identifier)),*,
a => a.parse::<u8>().map_err(|_| ()).and_then(TryFrom::try_from),
a => a.parse::<u8>().map(Ss58AddressFormat::Custom).map_err(|_| ParseError),
}
}
}

#[cfg(feature = "std")]
impl std::str::FromStr for Ss58AddressFormat {
type Err = ParseError;

fn from_str(data: &str) -> Result<Self, Self::Err> {
Self::try_from(data)
}
}

#[cfg(feature = "std")]
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "failed to parse network value as u8")
}
}

#[cfg(feature = "std")]
impl Default for Ss58AddressFormat {
fn default() -> Self {
Expand All @@ -401,10 +434,7 @@ macro_rules! ss58_address_format {
#[cfg(feature = "std")]
impl From<Ss58AddressFormat> for String {
fn from(x: Ss58AddressFormat) -> String {
match x {
$(Ss58AddressFormat::$identifier => $name.into()),*,
Ss58AddressFormat::Custom(x) => x.to_string(),
}
x.to_string()
}
}
)
Expand Down Expand Up @@ -446,8 +476,16 @@ ss58_address_format!(
(18, "darwinia", "Darwinia Chain mainnet, standard account (*25519).")
StafiAccount =>
(20, "stafi", "Stafi mainnet, standard account (*25519).")
DockTestAccount =>
(21, "dock-testnet", "Dock testnet, standard account (*25519).")
DockMainAccount =>
(22, "dock-mainnet", "Dock mainnet, standard account (*25519).")
ShiftNrg =>
(23, "shift", "ShiftNrg mainnet, standard account (*25519).")
SubsocialAccount =>
(28, "subsocial", "Subsocial network, standard account (*25519).")
PhalaAccount =>
(30, "phala", "Phala Network, standard account (*25519).")
RobonomicsAccount =>
(32, "robonomics", "Any Robonomics network standard account (*25519).")
DataHighwayAccount =>
Expand Down

0 comments on commit 017a604

Please sign in to comment.