mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-30 14:17:56 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -19,37 +19,35 @@
|
||||
//! Cryptographic utilities.
|
||||
// end::description[]
|
||||
|
||||
use crate::{sr25519, ed25519};
|
||||
use sp_std::hash::Hash;
|
||||
use sp_std::vec::Vec;
|
||||
use sp_std::str;
|
||||
#[cfg(feature = "std")]
|
||||
use sp_std::convert::TryInto;
|
||||
use sp_std::convert::TryFrom;
|
||||
use crate::hexdisplay::HexDisplay;
|
||||
use crate::{ed25519, sr25519};
|
||||
#[cfg(feature = "std")]
|
||||
use base58::{FromBase58, ToBase58};
|
||||
use codec::{Decode, Encode, MaxEncodedLen};
|
||||
#[cfg(feature = "std")]
|
||||
use parking_lot::Mutex;
|
||||
#[cfg(feature = "std")]
|
||||
use rand::{RngCore, rngs::OsRng};
|
||||
use codec::{Encode, Decode, MaxEncodedLen};
|
||||
use rand::{rngs::OsRng, RngCore};
|
||||
#[cfg(feature = "std")]
|
||||
use regex::Regex;
|
||||
#[cfg(feature = "std")]
|
||||
use base58::{FromBase58, ToBase58};
|
||||
#[cfg(feature = "std")]
|
||||
use crate::hexdisplay::HexDisplay;
|
||||
#[doc(hidden)]
|
||||
pub use sp_std::ops::Deref;
|
||||
use sp_runtime_interface::pass_by::PassByInner;
|
||||
/// Trait to zeroize a memory buffer.
|
||||
pub use zeroize::Zeroize;
|
||||
/// Trait for accessing reference to `SecretString`.
|
||||
pub use secrecy::ExposeSecret;
|
||||
/// A store for sensitive data.
|
||||
#[cfg(feature = "std")]
|
||||
pub use secrecy::SecretString;
|
||||
use sp_runtime_interface::pass_by::PassByInner;
|
||||
#[cfg(feature = "std")]
|
||||
use sp_std::convert::TryInto;
|
||||
#[doc(hidden)]
|
||||
pub use sp_std::ops::Deref;
|
||||
use sp_std::{convert::TryFrom, hash::Hash, str, vec::Vec};
|
||||
/// Trait to zeroize a memory buffer.
|
||||
pub use zeroize::Zeroize;
|
||||
|
||||
/// The root phrase for our publicly known keys.
|
||||
pub const DEV_PHRASE: &str = "bottom drive obey lake curtain smoke basket hold race lonely fit walk";
|
||||
pub const DEV_PHRASE: &str =
|
||||
"bottom drive obey lake curtain smoke basket hold race lonely fit walk";
|
||||
|
||||
/// The address of the associated root phrase for our publicly known keys.
|
||||
pub const DEV_ADDRESS: &str = "5DfhGyQdFobKM8NsWvEeAKk5EQQgYe9AydgJ7rMB6E1EqRzV";
|
||||
@@ -118,22 +116,28 @@ pub enum DeriveJunction {
|
||||
#[cfg(feature = "full_crypto")]
|
||||
impl DeriveJunction {
|
||||
/// Consume self to return a soft derive junction with the same chain code.
|
||||
pub fn soften(self) -> Self { DeriveJunction::Soft(self.unwrap_inner()) }
|
||||
pub fn soften(self) -> Self {
|
||||
DeriveJunction::Soft(self.unwrap_inner())
|
||||
}
|
||||
|
||||
/// Consume self to return a hard derive junction with the same chain code.
|
||||
pub fn harden(self) -> Self { DeriveJunction::Hard(self.unwrap_inner()) }
|
||||
pub fn harden(self) -> Self {
|
||||
DeriveJunction::Hard(self.unwrap_inner())
|
||||
}
|
||||
|
||||
/// Create a new soft (vanilla) DeriveJunction from a given, encodable, value.
|
||||
///
|
||||
/// If you need a hard junction, use `hard()`.
|
||||
pub fn soft<T: Encode>(index: T) -> Self {
|
||||
let mut cc: [u8; JUNCTION_ID_LEN] = Default::default();
|
||||
index.using_encoded(|data| if data.len() > JUNCTION_ID_LEN {
|
||||
let hash_result = blake2_rfc::blake2b::blake2b(JUNCTION_ID_LEN, &[], data);
|
||||
let hash = hash_result.as_bytes();
|
||||
cc.copy_from_slice(hash);
|
||||
} else {
|
||||
cc[0..data.len()].copy_from_slice(data);
|
||||
index.using_encoded(|data| {
|
||||
if data.len() > JUNCTION_ID_LEN {
|
||||
let hash_result = blake2_rfc::blake2b::blake2b(JUNCTION_ID_LEN, &[], data);
|
||||
let hash = hash_result.as_bytes();
|
||||
cc.copy_from_slice(hash);
|
||||
} else {
|
||||
cc[0..data.len()].copy_from_slice(data);
|
||||
}
|
||||
});
|
||||
DeriveJunction::Soft(cc)
|
||||
}
|
||||
@@ -174,11 +178,8 @@ impl DeriveJunction {
|
||||
impl<T: AsRef<str>> From<T> for DeriveJunction {
|
||||
fn from(j: T) -> DeriveJunction {
|
||||
let j = j.as_ref();
|
||||
let (code, hard) = if let Some(stripped) = j.strip_prefix('/') {
|
||||
(stripped, true)
|
||||
} else {
|
||||
(j, false)
|
||||
};
|
||||
let (code, hard) =
|
||||
if let Some(stripped) = j.strip_prefix('/') { (stripped, true) } else { (j, false) };
|
||||
|
||||
let res = if let Ok(n) = str::parse::<u64>(code) {
|
||||
// number
|
||||
@@ -231,12 +232,11 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
|
||||
/// Some if the string is a properly encoded SS58Check address.
|
||||
#[cfg(feature = "std")]
|
||||
fn from_ss58check(s: &str) -> Result<Self, PublicError> {
|
||||
Self::from_ss58check_with_version(s)
|
||||
.and_then(|(r, v)| match v {
|
||||
v if !v.is_custom() => Ok(r),
|
||||
v if v == *DEFAULT_VERSION.lock() => Ok(r),
|
||||
_ => Err(PublicError::UnknownVersion),
|
||||
})
|
||||
Self::from_ss58check_with_version(s).and_then(|(r, v)| match v {
|
||||
v if !v.is_custom() => Ok(r),
|
||||
v if v == *DEFAULT_VERSION.lock() => Ok(r),
|
||||
_ => Err(PublicError::UnknownVersion),
|
||||
})
|
||||
}
|
||||
|
||||
/// Some if the string is a properly encoded SS58Check address.
|
||||
@@ -249,7 +249,9 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
|
||||
let body_len = res.as_mut().len();
|
||||
|
||||
let data = s.from_base58().map_err(|_| PublicError::BadBase58)?;
|
||||
if data.len() < 2 { return Err(PublicError::BadLength); }
|
||||
if data.len() < 2 {
|
||||
return Err(PublicError::BadLength)
|
||||
}
|
||||
let (prefix_len, ident) = match data[0] {
|
||||
0..=63 => (1, data[0] as u16),
|
||||
64..=127 => {
|
||||
@@ -261,18 +263,22 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
|
||||
let lower = (data[0] << 2) | (data[1] >> 6);
|
||||
let upper = data[1] & 0b00111111;
|
||||
(2, (lower as u16) | ((upper as u16) << 8))
|
||||
}
|
||||
},
|
||||
_ => return Err(PublicError::UnknownVersion),
|
||||
};
|
||||
if data.len() != prefix_len + body_len + CHECKSUM_LEN { return Err(PublicError::BadLength) }
|
||||
if data.len() != prefix_len + body_len + CHECKSUM_LEN {
|
||||
return Err(PublicError::BadLength)
|
||||
}
|
||||
let format = ident.try_into().map_err(|_: ()| PublicError::UnknownVersion)?;
|
||||
if !Self::format_is_allowed(format) { return Err(PublicError::FormatNotAllowed) }
|
||||
if !Self::format_is_allowed(format) {
|
||||
return Err(PublicError::FormatNotAllowed)
|
||||
}
|
||||
|
||||
let hash = ss58hash(&data[0..body_len + prefix_len]);
|
||||
let checksum = &hash.as_bytes()[0..CHECKSUM_LEN];
|
||||
if data[body_len + prefix_len..body_len + prefix_len + CHECKSUM_LEN] != *checksum {
|
||||
// Invalid checksum.
|
||||
return Err(PublicError::InvalidChecksum);
|
||||
return Err(PublicError::InvalidChecksum)
|
||||
}
|
||||
res.as_mut().copy_from_slice(&data[prefix_len..body_len + prefix_len]);
|
||||
Ok((res, format))
|
||||
@@ -282,12 +288,11 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
|
||||
/// a derivation path following.
|
||||
#[cfg(feature = "std")]
|
||||
fn from_string(s: &str) -> Result<Self, PublicError> {
|
||||
Self::from_string_with_version(s)
|
||||
.and_then(|(r, v)| match v {
|
||||
v if !v.is_custom() => Ok(r),
|
||||
v if v == *DEFAULT_VERSION.lock() => Ok(r),
|
||||
_ => Err(PublicError::UnknownVersion),
|
||||
})
|
||||
Self::from_string_with_version(s).and_then(|(r, v)| match v {
|
||||
v if !v.is_custom() => Ok(r),
|
||||
v if v == *DEFAULT_VERSION.lock() => Ok(r),
|
||||
_ => Err(PublicError::UnknownVersion),
|
||||
})
|
||||
}
|
||||
|
||||
/// Return the ss58-check string for this key.
|
||||
@@ -304,7 +309,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
|
||||
// lower bits of the upper byte in the low pos
|
||||
let second = ((ident >> 8) as u8) | ((ident & 0b0000_0000_0000_0011) as u8) << 6;
|
||||
vec![first | 0b01000000, second]
|
||||
}
|
||||
},
|
||||
_ => unreachable!("masked out the upper two bits; qed"),
|
||||
};
|
||||
v.extend(self.as_ref());
|
||||
@@ -315,7 +320,9 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
|
||||
|
||||
/// Return the ss58-check string for this key.
|
||||
#[cfg(feature = "std")]
|
||||
fn to_ss58check(&self) -> String { self.to_ss58check_with_version(*DEFAULT_VERSION.lock()) }
|
||||
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.
|
||||
@@ -331,7 +338,7 @@ pub trait Derive: Sized {
|
||||
///
|
||||
/// Will be `None` for public keys if there are any hard junctions in there.
|
||||
#[cfg(feature = "std")]
|
||||
fn derive<Iter: Iterator<Item=DeriveJunction>>(&self, _path: Iter) -> Option<Self> {
|
||||
fn derive<Iter: Iterator<Item = DeriveJunction>>(&self, _path: Iter) -> Option<Self> {
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -629,9 +636,7 @@ lazy_static::lazy_static! {
|
||||
impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Default + Derive> Ss58Codec for T {
|
||||
fn from_string(s: &str) -> Result<Self, PublicError> {
|
||||
let cap = SS58_REGEX.captures(s).ok_or(PublicError::InvalidFormat)?;
|
||||
let s = cap.name("ss58")
|
||||
.map(|r| r.as_str())
|
||||
.unwrap_or(DEV_ADDRESS);
|
||||
let s = cap.name("ss58").map(|r| r.as_str()).unwrap_or(DEV_ADDRESS);
|
||||
let addr = if let Some(stripped) = s.strip_prefix("0x") {
|
||||
let d = hex::decode(stripped).map_err(|_| PublicError::InvalidFormat)?;
|
||||
let mut r = Self::default();
|
||||
@@ -647,28 +652,23 @@ impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Default + Derive> Ss58Codec for T {
|
||||
if cap["path"].is_empty() {
|
||||
Ok(addr)
|
||||
} else {
|
||||
let path = JUNCTION_REGEX.captures_iter(&cap["path"])
|
||||
.map(|f| DeriveJunction::from(&f[1]));
|
||||
addr.derive(path)
|
||||
.ok_or(PublicError::InvalidPath)
|
||||
let path =
|
||||
JUNCTION_REGEX.captures_iter(&cap["path"]).map(|f| DeriveJunction::from(&f[1]));
|
||||
addr.derive(path).ok_or(PublicError::InvalidPath)
|
||||
}
|
||||
}
|
||||
|
||||
fn from_string_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> {
|
||||
let cap = SS58_REGEX.captures(s).ok_or(PublicError::InvalidFormat)?;
|
||||
let (addr, v) = Self::from_ss58check_with_version(
|
||||
cap.name("ss58")
|
||||
.map(|r| r.as_str())
|
||||
.unwrap_or(DEV_ADDRESS)
|
||||
cap.name("ss58").map(|r| r.as_str()).unwrap_or(DEV_ADDRESS),
|
||||
)?;
|
||||
if cap["path"].is_empty() {
|
||||
Ok((addr, v))
|
||||
} else {
|
||||
let path = JUNCTION_REGEX.captures_iter(&cap["path"])
|
||||
.map(|f| DeriveJunction::from(&f[1]));
|
||||
addr.derive(path)
|
||||
.ok_or(PublicError::InvalidPath)
|
||||
.map(|a| (a, v))
|
||||
let path =
|
||||
JUNCTION_REGEX.captures_iter(&cap["path"]).map(|f| DeriveJunction::from(&f[1]));
|
||||
addr.derive(path).ok_or(PublicError::InvalidPath).map(|a| (a, v))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -694,10 +694,14 @@ pub trait Public:
|
||||
fn from_slice(data: &[u8]) -> Self;
|
||||
|
||||
/// Return a `Vec<u8>` filled with raw data.
|
||||
fn to_raw_vec(&self) -> Vec<u8> { self.as_slice().to_vec() }
|
||||
fn to_raw_vec(&self) -> Vec<u8> {
|
||||
self.as_slice().to_vec()
|
||||
}
|
||||
|
||||
/// Return a slice filled with raw data.
|
||||
fn as_slice(&self) -> &[u8] { self.as_ref() }
|
||||
fn as_slice(&self) -> &[u8] {
|
||||
self.as_ref()
|
||||
}
|
||||
/// Return `CryptoTypePublicPair` from public key.
|
||||
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair;
|
||||
}
|
||||
@@ -809,14 +813,20 @@ impl sp_std::fmt::Debug for AccountId32 {
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl serde::Serialize for AccountId32 {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
serializer.serialize_str(&self.to_ss58check())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de> serde::Deserialize<'de> for AccountId32 {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
Ss58Codec::from_ss58check(&String::deserialize(deserializer)?)
|
||||
.map_err(|e| serde::de::Error::custom(format!("{:?}", e)))
|
||||
}
|
||||
@@ -851,11 +861,13 @@ mod dummy {
|
||||
pub struct Dummy;
|
||||
|
||||
impl AsRef<[u8]> for Dummy {
|
||||
fn as_ref(&self) -> &[u8] { &b""[..] }
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
&b""[..]
|
||||
}
|
||||
}
|
||||
|
||||
impl AsMut<[u8]> for Dummy {
|
||||
fn as_mut(&mut self) -> &mut[u8] {
|
||||
fn as_mut(&mut self) -> &mut [u8] {
|
||||
unsafe {
|
||||
#[allow(mutable_transmutes)]
|
||||
sp_std::mem::transmute::<_, &'static mut [u8]>(&b""[..])
|
||||
@@ -878,14 +890,18 @@ mod dummy {
|
||||
impl Derive for Dummy {}
|
||||
|
||||
impl Public for Dummy {
|
||||
fn from_slice(_: &[u8]) -> Self { Self }
|
||||
fn from_slice(_: &[u8]) -> Self {
|
||||
Self
|
||||
}
|
||||
#[cfg(feature = "std")]
|
||||
fn to_raw_vec(&self) -> Vec<u8> { vec![] }
|
||||
fn as_slice(&self) -> &[u8] { b"" }
|
||||
fn to_raw_vec(&self) -> Vec<u8> {
|
||||
vec![]
|
||||
}
|
||||
fn as_slice(&self) -> &[u8] {
|
||||
b""
|
||||
}
|
||||
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
|
||||
CryptoTypePublicPair(
|
||||
CryptoTypeId(*b"dumm"), Public::to_raw_vec(self)
|
||||
)
|
||||
CryptoTypePublicPair(CryptoTypeId(*b"dumm"), Public::to_raw_vec(self))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -895,23 +911,41 @@ mod dummy {
|
||||
type Signature = Dummy;
|
||||
type DeriveError = ();
|
||||
#[cfg(feature = "std")]
|
||||
fn generate_with_phrase(_: Option<&str>) -> (Self, String, Self::Seed) { Default::default() }
|
||||
fn generate_with_phrase(_: Option<&str>) -> (Self, String, Self::Seed) {
|
||||
Default::default()
|
||||
}
|
||||
#[cfg(feature = "std")]
|
||||
fn from_phrase(_: &str, _: Option<&str>)
|
||||
-> Result<(Self, Self::Seed), SecretStringError>
|
||||
{
|
||||
fn from_phrase(_: &str, _: Option<&str>) -> Result<(Self, Self::Seed), SecretStringError> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
fn derive<
|
||||
Iter: Iterator<Item=DeriveJunction>,
|
||||
>(&self, _: Iter, _: Option<Dummy>) -> Result<(Self, Option<Dummy>), Self::DeriveError> { Ok((Self, None)) }
|
||||
fn from_seed(_: &Self::Seed) -> Self { Self }
|
||||
fn from_seed_slice(_: &[u8]) -> Result<Self, SecretStringError> { Ok(Self) }
|
||||
fn sign(&self, _: &[u8]) -> Self::Signature { Self }
|
||||
fn verify<M: AsRef<[u8]>>(_: &Self::Signature, _: M, _: &Self::Public) -> bool { true }
|
||||
fn verify_weak<P: AsRef<[u8]>, M: AsRef<[u8]>>(_: &[u8], _: M, _: P) -> bool { true }
|
||||
fn public(&self) -> Self::Public { Self }
|
||||
fn to_raw_vec(&self) -> Vec<u8> { vec![] }
|
||||
fn derive<Iter: Iterator<Item = DeriveJunction>>(
|
||||
&self,
|
||||
_: Iter,
|
||||
_: Option<Dummy>,
|
||||
) -> Result<(Self, Option<Dummy>), Self::DeriveError> {
|
||||
Ok((Self, None))
|
||||
}
|
||||
fn from_seed(_: &Self::Seed) -> Self {
|
||||
Self
|
||||
}
|
||||
fn from_seed_slice(_: &[u8]) -> Result<Self, SecretStringError> {
|
||||
Ok(Self)
|
||||
}
|
||||
fn sign(&self, _: &[u8]) -> Self::Signature {
|
||||
Self
|
||||
}
|
||||
fn verify<M: AsRef<[u8]>>(_: &Self::Signature, _: M, _: &Self::Public) -> bool {
|
||||
true
|
||||
}
|
||||
fn verify_weak<P: AsRef<[u8]>, M: AsRef<[u8]>>(_: &[u8], _: M, _: P) -> bool {
|
||||
true
|
||||
}
|
||||
fn public(&self) -> Self::Public {
|
||||
Self
|
||||
}
|
||||
fn to_raw_vec(&self) -> Vec<u8> {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -956,10 +990,14 @@ pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static {
|
||||
|
||||
/// Returns the KeyPair from the English BIP39 seed `phrase`, or `None` if it's invalid.
|
||||
#[cfg(feature = "std")]
|
||||
fn from_phrase(phrase: &str, password: Option<&str>) -> Result<(Self, Self::Seed), SecretStringError>;
|
||||
fn from_phrase(
|
||||
phrase: &str,
|
||||
password: Option<&str>,
|
||||
) -> Result<(Self, Self::Seed), SecretStringError>;
|
||||
|
||||
/// Derive a child key from a series of given junctions.
|
||||
fn derive<Iter: Iterator<Item=DeriveJunction>>(&self,
|
||||
fn derive<Iter: Iterator<Item = DeriveJunction>>(
|
||||
&self,
|
||||
path: Iter,
|
||||
seed: Option<Self::Seed>,
|
||||
) -> Result<(Self, Option<Self::Seed>), Self::DeriveError>;
|
||||
@@ -1018,19 +1056,20 @@ pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static {
|
||||
///
|
||||
/// `None` is returned if no matches are found.
|
||||
#[cfg(feature = "std")]
|
||||
fn from_string_with_seed(s: &str, password_override: Option<&str>)
|
||||
-> Result<(Self, Option<Self::Seed>), SecretStringError>
|
||||
{
|
||||
fn from_string_with_seed(
|
||||
s: &str,
|
||||
password_override: Option<&str>,
|
||||
) -> Result<(Self, Option<Self::Seed>), SecretStringError> {
|
||||
let cap = SECRET_PHRASE_REGEX.captures(s).ok_or(SecretStringError::InvalidFormat)?;
|
||||
|
||||
let path = JUNCTION_REGEX.captures_iter(&cap["path"])
|
||||
.map(|f| DeriveJunction::from(&f[1]));
|
||||
let path = JUNCTION_REGEX.captures_iter(&cap["path"]).map(|f| DeriveJunction::from(&f[1]));
|
||||
|
||||
let phrase = cap.name("phrase").map(|r| r.as_str()).unwrap_or(DEV_PHRASE);
|
||||
let password = password_override.or_else(|| cap.name("password").map(|m| m.as_str()));
|
||||
|
||||
let (root, seed) = if let Some(stripped) = phrase.strip_prefix("0x") {
|
||||
hex::decode(stripped).ok()
|
||||
hex::decode(stripped)
|
||||
.ok()
|
||||
.and_then(|seed_vec| {
|
||||
let mut seed = Self::Seed::default();
|
||||
if seed.as_ref().len() == seed_vec.len() {
|
||||
@@ -1042,8 +1081,7 @@ pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static {
|
||||
})
|
||||
.ok_or(SecretStringError::InvalidSeed)?
|
||||
} else {
|
||||
Self::from_phrase(phrase, password)
|
||||
.map_err(|_| SecretStringError::InvalidPhrase)?
|
||||
Self::from_phrase(phrase, password).map_err(|_| SecretStringError::InvalidPhrase)?
|
||||
};
|
||||
root.derive(path, Some(seed)).map_err(|_| SecretStringError::InvalidPath)
|
||||
}
|
||||
@@ -1074,19 +1112,25 @@ pub trait Wraps: Sized {
|
||||
type Inner: IsWrappedBy<Self>;
|
||||
}
|
||||
|
||||
impl<T, Outer> IsWrappedBy<Outer> for T where
|
||||
impl<T, Outer> IsWrappedBy<Outer> for T
|
||||
where
|
||||
Outer: AsRef<Self> + AsMut<Self> + From<Self>,
|
||||
T: From<Outer>,
|
||||
{
|
||||
/// Get a reference to the inner from the outer.
|
||||
fn from_ref(outer: &Outer) -> &Self { outer.as_ref() }
|
||||
fn from_ref(outer: &Outer) -> &Self {
|
||||
outer.as_ref()
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the inner from the outer.
|
||||
fn from_mut(outer: &mut Outer) -> &mut Self { outer.as_mut() }
|
||||
fn from_mut(outer: &mut Outer) -> &mut Self {
|
||||
outer.as_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Inner, Outer, T> UncheckedFrom<T> for Outer where
|
||||
Outer: Wraps<Inner=Inner>,
|
||||
impl<Inner, Outer, T> UncheckedFrom<T> for Outer
|
||||
where
|
||||
Outer: Wraps<Inner = Inner>,
|
||||
Inner: IsWrappedBy<Outer> + UncheckedFrom<T>,
|
||||
{
|
||||
fn unchecked_from(t: T) -> Self {
|
||||
@@ -1110,8 +1154,18 @@ pub trait CryptoType {
|
||||
/// Values whose first character is `_` are reserved for private use and won't conflict with any
|
||||
/// public modules.
|
||||
#[derive(
|
||||
Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, Decode, PassByInner,
|
||||
crate::RuntimeDebug
|
||||
Copy,
|
||||
Clone,
|
||||
Default,
|
||||
PartialEq,
|
||||
Eq,
|
||||
PartialOrd,
|
||||
Ord,
|
||||
Hash,
|
||||
Encode,
|
||||
Decode,
|
||||
PassByInner,
|
||||
crate::RuntimeDebug,
|
||||
)]
|
||||
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct KeyTypeId(pub [u8; 4]);
|
||||
@@ -1134,7 +1188,7 @@ impl<'a> TryFrom<&'a str> for KeyTypeId {
|
||||
fn try_from(x: &'a str) -> Result<Self, ()> {
|
||||
let b = x.as_bytes();
|
||||
if b.len() != 4 {
|
||||
return Err(());
|
||||
return Err(())
|
||||
}
|
||||
let mut res = KeyTypeId::default();
|
||||
res.0.copy_from_slice(&b[0..4]);
|
||||
@@ -1159,7 +1213,7 @@ impl sp_std::fmt::Display for CryptoTypePublicPair {
|
||||
Ok(id) => id.to_string(),
|
||||
Err(_) => {
|
||||
format!("{:#?}", self.0)
|
||||
}
|
||||
},
|
||||
};
|
||||
write!(f, "{}-{}", id, HexDisplay::from(&self.1))
|
||||
}
|
||||
@@ -1195,16 +1249,16 @@ pub mod key_types {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::DeriveJunction;
|
||||
use hex_literal::hex;
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone, Eq, PartialEq, Debug)]
|
||||
enum TestPair {
|
||||
Generated,
|
||||
GeneratedWithPhrase,
|
||||
GeneratedFromPhrase{phrase: String, password: Option<String>},
|
||||
Standard{phrase: String, password: Option<String>, path: Vec<DeriveJunction>},
|
||||
GeneratedFromPhrase { phrase: String, password: Option<String> },
|
||||
Standard { phrase: String, password: Option<String>, path: Vec<DeriveJunction> },
|
||||
Seed(Vec<u8>),
|
||||
}
|
||||
impl Default for TestPair {
|
||||
@@ -1250,9 +1304,7 @@ mod tests {
|
||||
vec![]
|
||||
}
|
||||
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
|
||||
CryptoTypePublicPair(
|
||||
CryptoTypeId(*b"dumm"), self.to_raw_vec(),
|
||||
)
|
||||
CryptoTypePublicPair(CryptoTypeId(*b"dumm"), self.to_raw_vec())
|
||||
}
|
||||
}
|
||||
impl Pair for TestPair {
|
||||
@@ -1261,41 +1313,68 @@ mod tests {
|
||||
type Signature = [u8; 0];
|
||||
type DeriveError = ();
|
||||
|
||||
fn generate() -> (Self, <Self as Pair>::Seed) { (TestPair::Generated, [0u8; 8]) }
|
||||
fn generate() -> (Self, <Self as Pair>::Seed) {
|
||||
(TestPair::Generated, [0u8; 8])
|
||||
}
|
||||
fn generate_with_phrase(_password: Option<&str>) -> (Self, String, <Self as Pair>::Seed) {
|
||||
(TestPair::GeneratedWithPhrase, "".into(), [0u8; 8])
|
||||
}
|
||||
fn from_phrase(phrase: &str, password: Option<&str>)
|
||||
-> Result<(Self, <Self as Pair>::Seed), SecretStringError>
|
||||
{
|
||||
Ok((TestPair::GeneratedFromPhrase {
|
||||
phrase: phrase.to_owned(),
|
||||
password: password.map(Into::into)
|
||||
}, [0u8; 8]))
|
||||
fn from_phrase(
|
||||
phrase: &str,
|
||||
password: Option<&str>,
|
||||
) -> Result<(Self, <Self as Pair>::Seed), SecretStringError> {
|
||||
Ok((
|
||||
TestPair::GeneratedFromPhrase {
|
||||
phrase: phrase.to_owned(),
|
||||
password: password.map(Into::into),
|
||||
},
|
||||
[0u8; 8],
|
||||
))
|
||||
}
|
||||
fn derive<Iter: Iterator<Item=DeriveJunction>>(&self, path_iter: Iter, _: Option<[u8; 8]>)
|
||||
-> Result<(Self, Option<[u8; 8]>), Self::DeriveError>
|
||||
{
|
||||
Ok((match self.clone() {
|
||||
TestPair::Standard {phrase, password, path} =>
|
||||
TestPair::Standard { phrase, password, path: path.into_iter().chain(path_iter).collect() },
|
||||
TestPair::GeneratedFromPhrase {phrase, password} =>
|
||||
TestPair::Standard { phrase, password, path: path_iter.collect() },
|
||||
x => if path_iter.count() == 0 { x } else { return Err(()) },
|
||||
}, None))
|
||||
fn derive<Iter: Iterator<Item = DeriveJunction>>(
|
||||
&self,
|
||||
path_iter: Iter,
|
||||
_: Option<[u8; 8]>,
|
||||
) -> Result<(Self, Option<[u8; 8]>), Self::DeriveError> {
|
||||
Ok((
|
||||
match self.clone() {
|
||||
TestPair::Standard { phrase, password, path } => TestPair::Standard {
|
||||
phrase,
|
||||
password,
|
||||
path: path.into_iter().chain(path_iter).collect(),
|
||||
},
|
||||
TestPair::GeneratedFromPhrase { phrase, password } =>
|
||||
TestPair::Standard { phrase, password, path: path_iter.collect() },
|
||||
x =>
|
||||
if path_iter.count() == 0 {
|
||||
x
|
||||
} else {
|
||||
return Err(())
|
||||
},
|
||||
},
|
||||
None,
|
||||
))
|
||||
}
|
||||
fn from_seed(_seed: &<TestPair as Pair>::Seed) -> Self {
|
||||
TestPair::Seed(_seed.as_ref().to_owned())
|
||||
}
|
||||
fn sign(&self, _message: &[u8]) -> Self::Signature {
|
||||
[]
|
||||
}
|
||||
fn verify<M: AsRef<[u8]>>(_: &Self::Signature, _: M, _: &Self::Public) -> bool {
|
||||
true
|
||||
}
|
||||
fn from_seed(_seed: &<TestPair as Pair>::Seed) -> Self { TestPair::Seed(_seed.as_ref().to_owned()) }
|
||||
fn sign(&self, _message: &[u8]) -> Self::Signature { [] }
|
||||
fn verify<M: AsRef<[u8]>>(_: &Self::Signature, _: M, _: &Self::Public) -> bool { true }
|
||||
fn verify_weak<P: AsRef<[u8]>, M: AsRef<[u8]>>(
|
||||
_sig: &[u8],
|
||||
_message: M,
|
||||
_pubkey: P
|
||||
) -> bool { true }
|
||||
fn public(&self) -> Self::Public { TestPublic }
|
||||
fn from_seed_slice(seed: &[u8])
|
||||
-> Result<Self, SecretStringError>
|
||||
{
|
||||
_pubkey: P,
|
||||
) -> bool {
|
||||
true
|
||||
}
|
||||
fn public(&self) -> Self::Public {
|
||||
TestPublic
|
||||
}
|
||||
fn from_seed_slice(seed: &[u8]) -> Result<Self, SecretStringError> {
|
||||
Ok(TestPair::Seed(seed.to_owned()))
|
||||
}
|
||||
fn to_raw_vec(&self) -> Vec<u8> {
|
||||
@@ -1327,43 +1406,83 @@ mod tests {
|
||||
fn interpret_std_secret_string_should_work() {
|
||||
assert_eq!(
|
||||
TestPair::from_string("hello world", None),
|
||||
Ok(TestPair::Standard{phrase: "hello world".to_owned(), password: None, path: vec![]})
|
||||
Ok(TestPair::Standard {
|
||||
phrase: "hello world".to_owned(),
|
||||
password: None,
|
||||
path: vec![]
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
TestPair::from_string("hello world/1", None),
|
||||
Ok(TestPair::Standard{phrase: "hello world".to_owned(), password: None, path: vec![DeriveJunction::soft(1)]})
|
||||
Ok(TestPair::Standard {
|
||||
phrase: "hello world".to_owned(),
|
||||
password: None,
|
||||
path: vec![DeriveJunction::soft(1)]
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
TestPair::from_string("hello world/DOT", None),
|
||||
Ok(TestPair::Standard{phrase: "hello world".to_owned(), password: None, path: vec![DeriveJunction::soft("DOT")]})
|
||||
Ok(TestPair::Standard {
|
||||
phrase: "hello world".to_owned(),
|
||||
password: None,
|
||||
path: vec![DeriveJunction::soft("DOT")]
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
TestPair::from_string("hello world//1", None),
|
||||
Ok(TestPair::Standard{phrase: "hello world".to_owned(), password: None, path: vec![DeriveJunction::hard(1)]})
|
||||
Ok(TestPair::Standard {
|
||||
phrase: "hello world".to_owned(),
|
||||
password: None,
|
||||
path: vec![DeriveJunction::hard(1)]
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
TestPair::from_string("hello world//DOT", None),
|
||||
Ok(TestPair::Standard{phrase: "hello world".to_owned(), password: None, path: vec![DeriveJunction::hard("DOT")]})
|
||||
Ok(TestPair::Standard {
|
||||
phrase: "hello world".to_owned(),
|
||||
password: None,
|
||||
path: vec![DeriveJunction::hard("DOT")]
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
TestPair::from_string("hello world//1/DOT", None),
|
||||
Ok(TestPair::Standard{phrase: "hello world".to_owned(), password: None, path: vec![DeriveJunction::hard(1), DeriveJunction::soft("DOT")]})
|
||||
Ok(TestPair::Standard {
|
||||
phrase: "hello world".to_owned(),
|
||||
password: None,
|
||||
path: vec![DeriveJunction::hard(1), DeriveJunction::soft("DOT")]
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
TestPair::from_string("hello world//DOT/1", None),
|
||||
Ok(TestPair::Standard{phrase: "hello world".to_owned(), password: None, path: vec![DeriveJunction::hard("DOT"), DeriveJunction::soft(1)]})
|
||||
Ok(TestPair::Standard {
|
||||
phrase: "hello world".to_owned(),
|
||||
password: None,
|
||||
path: vec![DeriveJunction::hard("DOT"), DeriveJunction::soft(1)]
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
TestPair::from_string("hello world///password", None),
|
||||
Ok(TestPair::Standard{phrase: "hello world".to_owned(), password: Some("password".to_owned()), path: vec![]})
|
||||
Ok(TestPair::Standard {
|
||||
phrase: "hello world".to_owned(),
|
||||
password: Some("password".to_owned()),
|
||||
path: vec![]
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
TestPair::from_string("hello world//1/DOT///password", None),
|
||||
Ok(TestPair::Standard{phrase: "hello world".to_owned(), password: Some("password".to_owned()), path: vec![DeriveJunction::hard(1), DeriveJunction::soft("DOT")]})
|
||||
Ok(TestPair::Standard {
|
||||
phrase: "hello world".to_owned(),
|
||||
password: Some("password".to_owned()),
|
||||
path: vec![DeriveJunction::hard(1), DeriveJunction::soft("DOT")]
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
TestPair::from_string("hello world/1//DOT///password", None),
|
||||
Ok(TestPair::Standard{phrase: "hello world".to_owned(), password: Some("password".to_owned()), path: vec![DeriveJunction::soft(1), DeriveJunction::hard("DOT")]})
|
||||
Ok(TestPair::Standard {
|
||||
phrase: "hello world".to_owned(),
|
||||
password: Some("password".to_owned()),
|
||||
path: vec![DeriveJunction::soft(1), DeriveJunction::hard("DOT")]
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1371,25 +1490,40 @@ mod tests {
|
||||
fn accountid_32_from_str_works() {
|
||||
use std::str::FromStr;
|
||||
assert!(AccountId32::from_str("5G9VdMwXvzza9pS8qE8ZHJk3CheHW9uucBn9ngW4C1gmmzpv").is_ok());
|
||||
assert!(AccountId32::from_str("5c55177d67b064bb5d189a3e1ddad9bc6646e02e64d6e308f5acbb1533ac430d").is_ok());
|
||||
assert!(AccountId32::from_str("0x5c55177d67b064bb5d189a3e1ddad9bc6646e02e64d6e308f5acbb1533ac430d").is_ok());
|
||||
assert!(AccountId32::from_str(
|
||||
"5c55177d67b064bb5d189a3e1ddad9bc6646e02e64d6e308f5acbb1533ac430d"
|
||||
)
|
||||
.is_ok());
|
||||
assert!(AccountId32::from_str(
|
||||
"0x5c55177d67b064bb5d189a3e1ddad9bc6646e02e64d6e308f5acbb1533ac430d"
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
assert_eq!(
|
||||
AccountId32::from_str("99G9VdMwXvzza9pS8qE8ZHJk3CheHW9uucBn9ngW4C1gmmzpv").unwrap_err(),
|
||||
"invalid ss58 address.",
|
||||
);
|
||||
assert_eq!(
|
||||
AccountId32::from_str("gc55177d67b064bb5d189a3e1ddad9bc6646e02e64d6e308f5acbb1533ac430d").unwrap_err(),
|
||||
AccountId32::from_str(
|
||||
"gc55177d67b064bb5d189a3e1ddad9bc6646e02e64d6e308f5acbb1533ac430d"
|
||||
)
|
||||
.unwrap_err(),
|
||||
"invalid hex address.",
|
||||
);
|
||||
assert_eq!(
|
||||
AccountId32::from_str("0xgc55177d67b064bb5d189a3e1ddad9bc6646e02e64d6e308f5acbb1533ac430d").unwrap_err(),
|
||||
AccountId32::from_str(
|
||||
"0xgc55177d67b064bb5d189a3e1ddad9bc6646e02e64d6e308f5acbb1533ac430d"
|
||||
)
|
||||
.unwrap_err(),
|
||||
"invalid hex address.",
|
||||
);
|
||||
|
||||
// valid hex but invalid length will be treated as ss58.
|
||||
assert_eq!(
|
||||
AccountId32::from_str("55c55177d67b064bb5d189a3e1ddad9bc6646e02e64d6e308f5acbb1533ac430d").unwrap_err(),
|
||||
AccountId32::from_str(
|
||||
"55c55177d67b064bb5d189a3e1ddad9bc6646e02e64d6e308f5acbb1533ac430d"
|
||||
)
|
||||
.unwrap_err(),
|
||||
"invalid ss58 address.",
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user