Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
5 changes: 4 additions & 1 deletion primitives/core/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
// Invalid length.
return Err(PublicError::BadLength);
}
let ver = d[0].try_into().map_err(|_: ()| PublicError::UnknownVersion)?;
let ver = d[0]
.try_into()
.or_else(|_| d[0].to_string().as_str().try_into())
Comment thread
atenjin marked this conversation as resolved.
Outdated
.map_err(|_: ()| PublicError::UnknownVersion)?;

if d[len + 1..len + 3] != ss58hash(&d[0..len + 1]).as_bytes()[0..2] {
// Invalid checksum.
Expand Down
16 changes: 15 additions & 1 deletion primitives/core/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,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 @@ -648,6 +648,20 @@ mod test {
assert_eq!(cmp, public);
}

#[test]
fn ss58check_custom_format_works() {
use crate::crypto::Ss58AddressFormat;
let default_format = Ss58AddressFormat::default();
set_default_ss58_version(Ss58AddressFormat::try_from("200").expect(""));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect better have some descriptive error

// custom
let addr = "2X64kMNEWAW5KLZMSKcGKEc96MyuaRsRUku7vomuYxKgqjVCRj";
let _ = Public::from_ss58check(&addr).unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also maybe replace unwrap with expect with some error description?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I fix it

set_default_ss58_version(default_format);
// normal
let addr = "KWAfgC2aRG5UVD6CpbPQXCx4YZZUhvWqqAJE6qcYc9Rtr6g5C";
let _ = Public::from_ss58check(&addr).unwrap();
}

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