Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
9 changes: 5 additions & 4 deletions primitives/core/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
};
let ver = identifier.try_into().map_err(|_: ()| PublicError::UnknownVersion)?;

if d[len + prefix..len + prefix + 2] != ss58hash(&d[0..len + 1]).as_bytes()[0..2] {
if d[len + prefix..len + prefix + 2] != ss58hash(&d[0..len + prefix]).as_bytes()[0..2] {
// Invalid checksum.
return Err(PublicError::InvalidChecksum);
}
res.as_mut().copy_from_slice(&d[1..len + 1]);
res.as_mut().copy_from_slice(&d[prefix..len + prefix]);
Ok((res, ver))
}
/// Some if the string is a properly encoded SS58Check address, optionally with
Expand Down Expand Up @@ -430,14 +430,15 @@ macro_rules! ss58_address_format {
match x {
$($number => Ok(Ss58AddressFormat::$identifier)),*,
_ => {
#[cfg(feature = "std")]
/* #[cfg(feature = "std")]
match Ss58AddressFormat::default() {
Ss58AddressFormat::Custom(n) if n == x => Ok(Ss58AddressFormat::Custom(n)),
_ => Err(()),
}

#[cfg(not(feature = "std"))]
Err(())
Err(())*/
Ok(Ss58AddressFormat::Custom(x))
},
}
}
Expand Down
27 changes: 26 additions & 1 deletion primitives/core/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,24 @@ mod test {
assert_eq!(cmp, public);
}

#[test]
fn ss58check_full_roundtrip_works() {
use crate::crypto::Ss58AddressFormat;
let pair = Pair::from_seed(b"12345678901234567890123456789012");
let public = pair.public();
let format = Ss58AddressFormat::PolkadotAccount;
let s = public.to_ss58check_with_version(format);
let (k, f) = Public::from_ss58check_with_version(&s).unwrap();
assert_eq!(k, public);
assert_eq!(f, format);

let format = Ss58AddressFormat::Custom(64);
let s = public.to_ss58check_with_version(format);
let (k, f) = Public::from_ss58check_with_version(&s).unwrap();
assert_eq!(k, public);
assert_eq!(f, format);
}

#[test]
fn ss58check_custom_format_works() {
// We need to run this test in its own process to not interfere with other tests running in
Expand All @@ -685,10 +703,17 @@ mod test {
// temp save default format version
let default_format = Ss58AddressFormat::default();
// set current ss58 version is custom "200" `Ss58AddressFormat::Custom(200)`

let pair = Pair::from_seed(b"12345678901234567890123456789012");
let public = pair.public();
let s = public.to_ss58check_with_version(Ss58AddressFormat::Custom(200));
println!("{}", s);

set_default_ss58_version(Ss58AddressFormat::Custom(200));
// custom addr encoded by version 200
let addr = "2X64kMNEWAW5KLZMSKcGKEc96MyuaRsRUku7vomuYxKgqjVCRj";
let addr = "4pbsSkWcBaYoFHrKJZp5fDVUKbqSYD9dhZZGvpp3vQ5ysVs5ybV";
Public::from_ss58check(&addr).unwrap();

set_default_ss58_version(default_format);
// set current ss58 version to default version
let addr = "KWAfgC2aRG5UVD6CpbPQXCx4YZZUhvWqqAJE6qcYc9Rtr6g5C";
Expand Down