Skip to content

Commit

Permalink
Fix stuck #3558525 while syncing blocks (#636)
Browse files Browse the repository at this point in the history
* Fix stuck #3558525 while syncing blocks
The from_ss58check host function is the root cause.

* Format code

Co-authored-by: icodezjb <[email protected]>
Co-authored-by: AAweidai <[email protected]>
  • Loading branch information
3 people authored May 25, 2022
1 parent eda44b8 commit 5c9399a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ pub enum Ss58CheckError {

#[runtime_interface]
pub trait Ss58Codec {
fn from_ss58check(addr: &[u8]) -> Result<AccountId32, Ss58CheckError> {
use sp_core::crypto::{PublicError, Ss58AddressFormat, Ss58Codec};
let s = String::from_utf8_lossy(addr).into_owned();
AccountId32::from_ss58check_with_version(&s)
.map_err(|err| match err {
PublicError::BadBase58 => Ss58CheckError::BadBase58,
PublicError::BadLength => Ss58CheckError::BadLength,
PublicError::UnknownSs58AddressFormat(_) => {
Ss58CheckError::UnknownSs58AddressFormat
}
PublicError::InvalidChecksum => Ss58CheckError::InvalidChecksum,
PublicError::InvalidPrefix => Ss58CheckError::InvalidPrefix,
PublicError::InvalidFormat => Ss58CheckError::InvalidFormat,
PublicError::InvalidPath => Ss58CheckError::InvalidPath,
PublicError::FormatNotAllowed => Ss58CheckError::FormatNotAllowed,
})
.and_then(|(account, ver)| match ver {
ver if ver == Ss58AddressFormat::from(44u16) => Ok(account),
_ => Err(Ss58CheckError::MismatchVersion),
})
}

#[version(2)]
fn from_ss58check(addr: &[u8]) -> Result<AccountId32, Ss58CheckError> {
use sp_core::crypto::{PublicError, Ss58Codec};
let s = String::from_utf8_lossy(addr).into_owned();
Expand Down

0 comments on commit 5c9399a

Please sign in to comment.