Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
13 changes: 11 additions & 2 deletions primitives/core/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,16 @@ macro_rules! ss58_address_format {
fn try_from(x: u8) -> Result<Ss58AddressFormat, ()> {
match x {
$($number => Ok(Ss58AddressFormat::$identifier)),*,
_ => Err(()),
_ => {
#[cfg(feature = "std")]
match Ss58AddressFormat::default() {
Ss58AddressFormat::Custom(n) if n == x => Ok(Ss58AddressFormat::Custom(x)),
_ => Err(()),
}

#[cfg(not(feature = "std"))]
Err(())
},
}
}
}
Expand All @@ -377,7 +386,7 @@ macro_rules! ss58_address_format {
fn try_from(x: &'a str) -> Result<Ss58AddressFormat, ()> {
match x {
$($name => Ok(Ss58AddressFormat::$identifier)),*,
a => a.parse::<u8>().map(Ss58AddressFormat::Custom).map_err(|_| ()),
a => a.parse::<u8>().map_err(|_| ()).and_then(TryFrom::try_from),
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion primitives/core/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ impl CryptoType for Pair {
mod test {
use super::*;
use hex_literal::hex;
use crate::crypto::DEV_PHRASE;
use crate::crypto::{DEV_PHRASE, set_default_ss58_version};
use serde_json;

#[test]
Expand Down Expand Up @@ -676,6 +676,22 @@ mod test {
assert_eq!(cmp, public);
}

#[test]
fn ss58check_custom_format_works() {
use crate::crypto::Ss58AddressFormat;
// temp save default format version
let default_format = Ss58AddressFormat::default();
// set current ss58 version is custom "200" `Ss58AddressFormat::Custom(200)`
set_default_ss58_version(Ss58AddressFormat::Custom(200));
// custom addr encoded by version 200
let addr = "2X64kMNEWAW5KLZMSKcGKEc96MyuaRsRUku7vomuYxKgqjVCRj";
Public::from_ss58check(&addr).unwrap();
set_default_ss58_version(default_format);
// set current ss58 version to default version
let addr = "KWAfgC2aRG5UVD6CpbPQXCx4YZZUhvWqqAJE6qcYc9Rtr6g5C";
Public::from_ss58check(&addr).unwrap();
}

#[test]
fn signature_serialization_works() {
let pair = Pair::from_seed(b"12345678901234567890123456789012");
Expand Down