use crate::{ed25519, sr25519};
#[cfg(feature = "std")]
use bip39::{Language, Mnemonic, MnemonicType};
use codec::{Decode, Encode, MaxEncodedLen};
#[cfg(feature = "std")]
use rand::{rngs::OsRng, RngCore};
#[cfg(feature = "std")]
use regex::Regex;
use scale_info::TypeInfo;
#[cfg(feature = "std")]
pub use secrecy::{ExposeSecret, SecretString};
use sp_runtime_interface::pass_by::PassByInner;
#[doc(hidden)]
pub use sp_std::ops::Deref;
#[cfg(all(not(feature = "std"), feature = "serde"))]
use sp_std::{
alloc::{format, string::String},
vec,
};
use sp_std::{hash::Hash, str, vec::Vec};
pub use ss58_registry::{from_known_address_format, Ss58AddressFormat, Ss58AddressFormatRegistry};
pub use zeroize::Zeroize;
pub const DEV_PHRASE: &str =
"bottom drive obey lake curtain smoke basket hold race lonely fit walk";
pub const DEV_ADDRESS: &str = "5DfhGyQdFobKM8NsWvEeAKk5EQQgYe9AydgJ7rMB6E1EqRzV";
pub const JUNCTION_ID_LEN: usize = 32;
pub trait UncheckedFrom<T> {
fn unchecked_from(t: T) -> Self;
}
pub trait UncheckedInto<T> {
fn unchecked_into(self) -> T;
}
impl<S, T: UncheckedFrom<S>> UncheckedInto<T> for S {
fn unchecked_into(self) -> T {
T::unchecked_from(self)
}
}
#[cfg_attr(feature = "std", derive(thiserror::Error))]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg(feature = "full_crypto")]
pub enum SecretStringError {
#[cfg_attr(feature = "std", error("Invalid format"))]
InvalidFormat,
#[cfg_attr(feature = "std", error("Invalid phrase"))]
InvalidPhrase,
#[cfg_attr(feature = "std", error("Invalid password"))]
InvalidPassword,
#[cfg_attr(feature = "std", error("Invalid seed"))]
InvalidSeed,
#[cfg_attr(feature = "std", error("Invalid seed length"))]
InvalidSeedLength,
#[cfg_attr(feature = "std", error("Invalid path"))]
InvalidPath,
}
#[cfg_attr(feature = "std", derive(thiserror::Error))]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg(feature = "full_crypto")]
pub enum DeriveError {
#[cfg_attr(feature = "std", error("Soft key in path"))]
SoftKeyInPath,
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Encode, Decode)]
#[cfg(any(feature = "full_crypto", feature = "serde"))]
pub enum DeriveJunction {
Soft([u8; JUNCTION_ID_LEN]),
Hard([u8; JUNCTION_ID_LEN]),
}
#[cfg(any(feature = "full_crypto", feature = "serde"))]
impl DeriveJunction {
pub fn soften(self) -> Self {
DeriveJunction::Soft(self.unwrap_inner())
}
pub fn harden(self) -> Self {
DeriveJunction::Hard(self.unwrap_inner())
}
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 {
cc.copy_from_slice(&sp_core_hashing::blake2_256(data));
} else {
cc[0..data.len()].copy_from_slice(data);
}
});
DeriveJunction::Soft(cc)
}
pub fn hard<T: Encode>(index: T) -> Self {
Self::soft(index).harden()
}
pub fn unwrap_inner(self) -> [u8; JUNCTION_ID_LEN] {
match self {
DeriveJunction::Hard(c) | DeriveJunction::Soft(c) => c,
}
}
pub fn inner(&self) -> &[u8; JUNCTION_ID_LEN] {
match self {
DeriveJunction::Hard(ref c) | DeriveJunction::Soft(ref c) => c,
}
}
pub fn is_soft(&self) -> bool {
matches!(*self, DeriveJunction::Soft(_))
}
pub fn is_hard(&self) -> bool {
matches!(*self, DeriveJunction::Hard(_))
}
}
#[cfg(any(feature = "full_crypto", feature = "serde"))]
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 res = if let Ok(n) = str::parse::<u64>(code) {
DeriveJunction::soft(n)
} else {
DeriveJunction::soft(code)
};
if hard {
res.harden()
} else {
res
}
}
}
#[cfg_attr(feature = "std", derive(thiserror::Error))]
#[cfg_attr(not(feature = "std"), derive(Debug))]
#[derive(Clone, Copy, Eq, PartialEq)]
#[allow(missing_docs)]
#[cfg(any(feature = "full_crypto", feature = "serde"))]
pub enum PublicError {
#[cfg_attr(feature = "std", error("Base 58 requirement is violated"))]
BadBase58,
#[cfg_attr(feature = "std", error("Length is bad"))]
BadLength,
#[cfg_attr(
feature = "std",
error(
"Unknown SS58 address format `{}`. ` \
`To support this address format, you need to call `set_default_ss58_version` at node start up.",
_0
)
)]
UnknownSs58AddressFormat(Ss58AddressFormat),
#[cfg_attr(feature = "std", error("Invalid checksum"))]
InvalidChecksum,
#[cfg_attr(feature = "std", error("Invalid SS58 prefix byte."))]
InvalidPrefix,
#[cfg_attr(feature = "std", error("Invalid SS58 format."))]
InvalidFormat,
#[cfg_attr(feature = "std", error("Invalid derivation path."))]
InvalidPath,
#[cfg_attr(feature = "std", error("Disallowed SS58 Address Format for this datatype."))]
FormatNotAllowed,
}
#[cfg(feature = "std")]
impl sp_std::fmt::Debug for PublicError {
fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
write!(f, "{}", self)
}
}
pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray {
fn format_is_allowed(f: Ss58AddressFormat) -> bool {
!f.is_reserved()
}
#[cfg(feature = "serde")]
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_ss58_version() => Ok(r),
v => Err(PublicError::UnknownSs58AddressFormat(v)),
})
}
#[cfg(feature = "serde")]
fn from_ss58check_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> {
const CHECKSUM_LEN: usize = 2;
let body_len = Self::LEN;
let data = bs58::decode(s).into_vec().map_err(|_| PublicError::BadBase58)?;
if data.len() < 2 {
return Err(PublicError::BadLength)
}
let (prefix_len, ident) = match data[0] {
0..=63 => (1, data[0] as u16),
64..=127 => {
let lower = (data[0] << 2) | (data[1] >> 6);
let upper = data[1] & 0b00111111;
(2, (lower as u16) | ((upper as u16) << 8))
},
_ => return Err(PublicError::InvalidPrefix),
};
if data.len() != prefix_len + body_len + CHECKSUM_LEN {
return Err(PublicError::BadLength)
}
let format = ident.into();
if !Self::format_is_allowed(format) {
return Err(PublicError::FormatNotAllowed)
}
let hash = ss58hash(&data[0..body_len + prefix_len]);
let checksum = &hash[0..CHECKSUM_LEN];
if data[body_len + prefix_len..body_len + prefix_len + CHECKSUM_LEN] != *checksum {
return Err(PublicError::InvalidChecksum)
}
let result = Self::from_slice(&data[prefix_len..body_len + prefix_len])
.map_err(|()| PublicError::BadLength)?;
Ok((result, format))
}
#[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_ss58_version() => Ok(r),
v => Err(PublicError::UnknownSs58AddressFormat(v)),
})
}
#[cfg(feature = "serde")]
fn to_ss58check_with_version(&self, version: Ss58AddressFormat) -> String {
let ident: u16 = u16::from(version) & 0b0011_1111_1111_1111;
let mut v = match ident {
0..=63 => vec![ident as u8],
64..=16_383 => {
let first = ((ident & 0b0000_0000_1111_1100) as u8) >> 2;
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());
let r = ss58hash(&v);
v.extend(&r[0..2]);
bs58::encode(v).into_string()
}
#[cfg(feature = "serde")]
fn to_ss58check(&self) -> String {
self.to_ss58check_with_version(default_ss58_version())
}
#[cfg(feature = "std")]
fn from_string_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> {
Self::from_ss58check_with_version(s)
}
}
pub trait Derive: Sized {
#[cfg(feature = "serde")]
fn derive<Iter: Iterator<Item = DeriveJunction>>(&self, _path: Iter) -> Option<Self> {
None
}
}
#[cfg(feature = "serde")]
const PREFIX: &[u8] = b"SS58PRE";
#[cfg(feature = "serde")]
fn ss58hash(data: &[u8]) -> Vec<u8> {
use blake2::{Blake2b512, Digest};
let mut ctx = Blake2b512::new();
ctx.update(PREFIX);
ctx.update(data);
ctx.finalize().to_vec()
}
#[cfg(feature = "serde")]
static DEFAULT_VERSION: core::sync::atomic::AtomicU16 = core::sync::atomic::AtomicU16::new(
from_known_address_format(Ss58AddressFormatRegistry::SubstrateAccount),
);
#[cfg(feature = "serde")]
pub fn default_ss58_version() -> Ss58AddressFormat {
DEFAULT_VERSION.load(core::sync::atomic::Ordering::Relaxed).into()
}
#[cfg(feature = "serde")]
pub fn unwrap_or_default_ss58_version(network: Option<Ss58AddressFormat>) -> Ss58AddressFormat {
network.unwrap_or_else(default_ss58_version)
}
#[cfg(feature = "serde")]
pub fn set_default_ss58_version(new_default: Ss58AddressFormat) {
DEFAULT_VERSION.store(new_default.into(), core::sync::atomic::Ordering::Relaxed);
}
#[cfg(feature = "std")]
lazy_static::lazy_static! {
static ref SS58_REGEX: Regex = Regex::new(r"^(?P<ss58>[\w\d ]+)?(?P<path>(//?[^/]+)*)$")
.expect("constructed from known-good static value; qed");
static ref SECRET_PHRASE_REGEX: Regex = Regex::new(r"^(?P<phrase>[\d\w ]+)?(?P<path>(//?[^/]+)*)(///(?P<password>.*))?$")
.expect("constructed from known-good static value; qed");
static ref JUNCTION_REGEX: Regex = Regex::new(r"/(/?[^/]+)")
.expect("constructed from known-good static value; qed");
}
#[cfg(feature = "std")]
impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Public + 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 addr = if let Some(stripped) = s.strip_prefix("0x") {
let d = array_bytes::hex2bytes(stripped).map_err(|_| PublicError::InvalidFormat)?;
Self::from_slice(&d).map_err(|()| PublicError::BadLength)?
} else {
Self::from_ss58check(s)?
};
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)
}
}
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),
)?;
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))
}
}
}
#[cfg(all(not(feature = "std"), feature = "serde"))]
impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Public + Derive> Ss58Codec for T {}
pub trait ByteArray: AsRef<[u8]> + AsMut<[u8]> + for<'a> TryFrom<&'a [u8], Error = ()> {
const LEN: usize;
fn from_slice(data: &[u8]) -> Result<Self, ()> {
Self::try_from(data)
}
fn to_raw_vec(&self) -> Vec<u8> {
self.as_slice().to_vec()
}
fn as_slice(&self) -> &[u8] {
self.as_ref()
}
}
pub trait Public: CryptoType + ByteArray + Derive + PartialEq + Eq + Clone + Send + Sync {}
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, MaxEncodedLen, TypeInfo)]
#[cfg_attr(feature = "std", derive(Hash))]
pub struct AccountId32([u8; 32]);
impl AccountId32 {
pub const fn new(inner: [u8; 32]) -> Self {
Self(inner)
}
}
impl UncheckedFrom<crate::hash::H256> for AccountId32 {
fn unchecked_from(h: crate::hash::H256) -> Self {
AccountId32(h.into())
}
}
impl ByteArray for AccountId32 {
const LEN: usize = 32;
}
#[cfg(feature = "serde")]
impl Ss58Codec for AccountId32 {}
impl AsRef<[u8]> for AccountId32 {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
impl AsMut<[u8]> for AccountId32 {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0[..]
}
}
impl AsRef<[u8; 32]> for AccountId32 {
fn as_ref(&self) -> &[u8; 32] {
&self.0
}
}
impl AsMut<[u8; 32]> for AccountId32 {
fn as_mut(&mut self) -> &mut [u8; 32] {
&mut self.0
}
}
impl From<[u8; 32]> for AccountId32 {
fn from(x: [u8; 32]) -> Self {
Self::new(x)
}
}
impl<'a> TryFrom<&'a [u8]> for AccountId32 {
type Error = ();
fn try_from(x: &'a [u8]) -> Result<AccountId32, ()> {
if x.len() == 32 {
let mut data = [0; 32];
data.copy_from_slice(x);
Ok(AccountId32(data))
} else {
Err(())
}
}
}
impl From<AccountId32> for [u8; 32] {
fn from(x: AccountId32) -> [u8; 32] {
x.0
}
}
impl From<sr25519::Public> for AccountId32 {
fn from(k: sr25519::Public) -> Self {
k.0.into()
}
}
impl From<ed25519::Public> for AccountId32 {
fn from(k: ed25519::Public) -> Self {
k.0.into()
}
}
#[cfg(feature = "std")]
impl std::fmt::Display for AccountId32 {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.to_ss58check())
}
}
impl sp_std::fmt::Debug for AccountId32 {
#[cfg(feature = "std")]
fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
let s = self.to_ss58check();
write!(f, "{} ({}...)", crate::hexdisplay::HexDisplay::from(&self.0), &s[0..8])
}
#[cfg(not(feature = "std"))]
fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
Ok(())
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for AccountId32 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.to_ss58check())
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for AccountId32 {
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)))
}
}
#[cfg(feature = "std")]
impl sp_std::str::FromStr for AccountId32 {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let hex_or_ss58_without_prefix = s.trim_start_matches("0x");
if hex_or_ss58_without_prefix.len() == 64 {
array_bytes::hex_n_into(hex_or_ss58_without_prefix).map_err(|_| "invalid hex address.")
} else {
Self::from_ss58check(s).map_err(|_| "invalid ss58 address.")
}
}
}
#[cfg(feature = "std")]
pub use self::dummy::*;
#[cfg(feature = "std")]
mod dummy {
use super::*;
#[derive(Clone, Hash, Default, Eq, PartialEq)]
pub struct Dummy;
impl AsRef<[u8]> for Dummy {
fn as_ref(&self) -> &[u8] {
&b""[..]
}
}
impl AsMut<[u8]> for Dummy {
fn as_mut(&mut self) -> &mut [u8] {
unsafe {
#[allow(mutable_transmutes)]
sp_std::mem::transmute::<_, &'static mut [u8]>(&b""[..])
}
}
}
impl<'a> TryFrom<&'a [u8]> for Dummy {
type Error = ();
fn try_from(_: &'a [u8]) -> Result<Self, ()> {
Ok(Self)
}
}
impl CryptoType for Dummy {
type Pair = Dummy;
}
impl Derive for Dummy {}
impl ByteArray for Dummy {
const LEN: usize = 0;
fn from_slice(_: &[u8]) -> Result<Self, ()> {
Ok(Self)
}
#[cfg(feature = "std")]
fn to_raw_vec(&self) -> Vec<u8> {
vec![]
}
fn as_slice(&self) -> &[u8] {
b""
}
}
impl Public for Dummy {}
impl Pair for Dummy {
type Public = Dummy;
type Seed = Dummy;
type Signature = Dummy;
#[cfg(feature = "std")]
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> {
Ok(Default::default())
}
fn derive<Iter: Iterator<Item = DeriveJunction>>(
&self,
_: Iter,
_: Option<Dummy>,
) -> Result<(Self, Option<Dummy>), DeriveError> {
Ok((Self, None))
}
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 public(&self) -> Self::Public {
Self
}
fn to_raw_vec(&self) -> Vec<u8> {
vec![]
}
}
}
#[cfg(feature = "std")]
pub struct SecretUri {
pub phrase: SecretString,
pub password: Option<SecretString>,
pub junctions: Vec<DeriveJunction>,
}
#[cfg(feature = "std")]
impl sp_std::str::FromStr for SecretUri {
type Err = SecretStringError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let cap = SECRET_PHRASE_REGEX.captures(s).ok_or(SecretStringError::InvalidFormat)?;
let junctions = JUNCTION_REGEX
.captures_iter(&cap["path"])
.map(|f| DeriveJunction::from(&f[1]))
.collect::<Vec<_>>();
let phrase = cap.name("phrase").map(|r| r.as_str()).unwrap_or(DEV_PHRASE);
let password = cap.name("password");
Ok(Self {
phrase: SecretString::from_str(phrase).expect("Returns infallible error; qed"),
password: password.map(|v| {
SecretString::from_str(v.as_str()).expect("Returns infallible error; qed")
}),
junctions,
})
}
}
#[cfg(feature = "full_crypto")]
pub trait Pair: CryptoType + Sized {
type Public: Public + Hash;
type Seed: Default + AsRef<[u8]> + AsMut<[u8]> + Clone;
type Signature: AsRef<[u8]>;
#[cfg(feature = "std")]
fn generate() -> (Self, Self::Seed) {
let mut seed = Self::Seed::default();
OsRng.fill_bytes(seed.as_mut());
(Self::from_seed(&seed), seed)
}
#[cfg(feature = "std")]
fn generate_with_phrase(password: Option<&str>) -> (Self, String, Self::Seed) {
let mnemonic = Mnemonic::new(MnemonicType::Words12, Language::English);
let phrase = mnemonic.phrase();
let (pair, seed) = Self::from_phrase(phrase, password)
.expect("All phrases generated by Mnemonic are valid; qed");
(pair, phrase.to_owned(), seed)
}
#[cfg(feature = "std")]
fn from_phrase(
phrase: &str,
password: Option<&str>,
) -> Result<(Self, Self::Seed), SecretStringError> {
let mnemonic = Mnemonic::from_phrase(phrase, Language::English)
.map_err(|_| SecretStringError::InvalidPhrase)?;
let big_seed =
substrate_bip39::seed_from_entropy(mnemonic.entropy(), password.unwrap_or(""))
.map_err(|_| SecretStringError::InvalidSeed)?;
let mut seed = Self::Seed::default();
let seed_slice = seed.as_mut();
let seed_len = seed_slice.len();
debug_assert!(seed_len <= big_seed.len());
seed_slice[..seed_len].copy_from_slice(&big_seed[..seed_len]);
Self::from_seed_slice(seed_slice).map(|x| (x, seed))
}
fn derive<Iter: Iterator<Item = DeriveJunction>>(
&self,
path: Iter,
seed: Option<Self::Seed>,
) -> Result<(Self, Option<Self::Seed>), DeriveError>;
fn from_seed(seed: &Self::Seed) -> Self {
Self::from_seed_slice(seed.as_ref()).expect("seed has valid length; qed")
}
fn from_seed_slice(seed: &[u8]) -> Result<Self, SecretStringError>;
fn sign(&self, message: &[u8]) -> Self::Signature;
fn verify<M: AsRef<[u8]>>(sig: &Self::Signature, message: M, pubkey: &Self::Public) -> bool;
fn public(&self) -> Self::Public;
#[cfg(feature = "std")]
fn from_string_with_seed(
s: &str,
password_override: Option<&str>,
) -> Result<(Self, Option<Self::Seed>), SecretStringError> {
use sp_std::str::FromStr;
let SecretUri { junctions, phrase, password } = SecretUri::from_str(s)?;
let password =
password_override.or_else(|| password.as_ref().map(|p| p.expose_secret().as_str()));
let (root, seed) = if let Some(stripped) = phrase.expose_secret().strip_prefix("0x") {
array_bytes::hex2bytes(stripped)
.ok()
.and_then(|seed_vec| {
let mut seed = Self::Seed::default();
if seed.as_ref().len() == seed_vec.len() {
seed.as_mut().copy_from_slice(&seed_vec);
Some((Self::from_seed(&seed), seed))
} else {
None
}
})
.ok_or(SecretStringError::InvalidSeed)?
} else {
Self::from_phrase(phrase.expose_secret().as_str(), password)
.map_err(|_| SecretStringError::InvalidPhrase)?
};
root.derive(junctions.into_iter(), Some(seed))
.map_err(|_| SecretStringError::InvalidPath)
}
#[cfg(feature = "std")]
fn from_string(s: &str, password_override: Option<&str>) -> Result<Self, SecretStringError> {
Self::from_string_with_seed(s, password_override).map(|x| x.0)
}
fn to_raw_vec(&self) -> Vec<u8>;
}
pub trait IsWrappedBy<Outer>: From<Outer> + Into<Outer> {
fn from_ref(outer: &Outer) -> &Self;
fn from_mut(outer: &mut Outer) -> &mut Self;
}
pub trait Wraps: Sized {
type Inner: IsWrappedBy<Self>;
fn as_inner_ref(&self) -> &Self::Inner {
Self::Inner::from_ref(self)
}
}
impl<T, Outer> IsWrappedBy<Outer> for T
where
Outer: AsRef<Self> + AsMut<Self> + From<Self>,
T: From<Outer>,
{
fn from_ref(outer: &Outer) -> &Self {
outer.as_ref()
}
fn from_mut(outer: &mut Outer) -> &mut Self {
outer.as_mut()
}
}
impl<Inner, Outer, T> UncheckedFrom<T> for Outer
where
Outer: Wraps<Inner = Inner>,
Inner: IsWrappedBy<Outer> + UncheckedFrom<T>,
{
fn unchecked_from(t: T) -> Self {
let inner: Inner = t.unchecked_into();
inner.into()
}
}
pub trait CryptoType {
#[cfg(feature = "full_crypto")]
type Pair: Pair;
}
#[derive(
Copy,
Clone,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Encode,
Decode,
PassByInner,
crate::RuntimeDebug,
TypeInfo,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct KeyTypeId(pub [u8; 4]);
impl From<u32> for KeyTypeId {
fn from(x: u32) -> Self {
Self(x.to_le_bytes())
}
}
impl From<KeyTypeId> for u32 {
fn from(x: KeyTypeId) -> Self {
u32::from_le_bytes(x.0)
}
}
impl<'a> TryFrom<&'a str> for KeyTypeId {
type Error = ();
fn try_from(x: &'a str) -> Result<Self, ()> {
let b = x.as_bytes();
if b.len() != 4 {
return Err(())
}
let mut res = KeyTypeId::default();
res.0.copy_from_slice(&b[0..4]);
Ok(res)
}
}
pub trait VrfCrypto {
type VrfInput;
type VrfOutput;
type VrfSignData;
type VrfSignature;
}
pub trait VrfSecret: VrfCrypto {
fn vrf_output(&self, data: &Self::VrfInput) -> Self::VrfOutput;
fn vrf_sign(&self, input: &Self::VrfSignData) -> Self::VrfSignature;
}
pub trait VrfPublic: VrfCrypto {
fn vrf_verify(&self, data: &Self::VrfSignData, signature: &Self::VrfSignature) -> bool;
}
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, Decode)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CryptoTypeId(pub [u8; 4]);
pub mod key_types {
use super::KeyTypeId;
pub const BABE: KeyTypeId = KeyTypeId(*b"babe");
pub const GRANDPA: KeyTypeId = KeyTypeId(*b"gran");
pub const ACCOUNT: KeyTypeId = KeyTypeId(*b"acco");
pub const AURA: KeyTypeId = KeyTypeId(*b"aura");
pub const BEEFY: KeyTypeId = KeyTypeId(*b"beef");
pub const IM_ONLINE: KeyTypeId = KeyTypeId(*b"imon");
pub const AUTHORITY_DISCOVERY: KeyTypeId = KeyTypeId(*b"audi");
pub const STAKING: KeyTypeId = KeyTypeId(*b"stak");
pub const STATEMENT: KeyTypeId = KeyTypeId(*b"stmt");
pub const DUMMY: KeyTypeId = KeyTypeId(*b"dumy");
}
pub trait FromEntropy: Sized {
fn from_entropy(input: &mut impl codec::Input) -> Result<Self, codec::Error>;
}
impl FromEntropy for bool {
fn from_entropy(input: &mut impl codec::Input) -> Result<Self, codec::Error> {
Ok(input.read_byte()? % 2 == 1)
}
}
macro_rules! impl_from_entropy {
($type:ty , $( $others:tt )*) => {
impl_from_entropy!($type);
impl_from_entropy!($( $others )*);
};
($type:ty) => {
impl FromEntropy for $type {
fn from_entropy(input: &mut impl codec::Input) -> Result<Self, codec::Error> {
<Self as codec::Decode>::decode(input)
}
}
}
}
macro_rules! impl_from_entropy_base {
($type:ty , $( $others:tt )*) => {
impl_from_entropy_base!($type);
impl_from_entropy_base!($( $others )*);
};
($type:ty) => {
impl_from_entropy!($type,
[$type; 1], [$type; 2], [$type; 3], [$type; 4], [$type; 5], [$type; 6], [$type; 7], [$type; 8],
[$type; 9], [$type; 10], [$type; 11], [$type; 12], [$type; 13], [$type; 14], [$type; 15], [$type; 16],
[$type; 17], [$type; 18], [$type; 19], [$type; 20], [$type; 21], [$type; 22], [$type; 23], [$type; 24],
[$type; 25], [$type; 26], [$type; 27], [$type; 28], [$type; 29], [$type; 30], [$type; 31], [$type; 32],
[$type; 36], [$type; 40], [$type; 44], [$type; 48], [$type; 56], [$type; 64], [$type; 72], [$type; 80],
[$type; 96], [$type; 112], [$type; 128], [$type; 160], [$type; 192], [$type; 224], [$type; 256]
);
}
}
impl_from_entropy_base!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128);
#[cfg(test)]
mod tests {
use super::*;
use crate::DeriveJunction;
#[derive(Clone, Eq, PartialEq, Debug)]
enum TestPair {
Generated,
GeneratedWithPhrase,
GeneratedFromPhrase { phrase: String, password: Option<String> },
Standard { phrase: String, password: Option<String>, path: Vec<DeriveJunction> },
Seed(Vec<u8>),
}
impl Default for TestPair {
fn default() -> Self {
TestPair::Generated
}
}
impl CryptoType for TestPair {
type Pair = Self;
}
#[derive(Clone, PartialEq, Eq, Hash, Default)]
struct TestPublic;
impl AsRef<[u8]> for TestPublic {
fn as_ref(&self) -> &[u8] {
&[]
}
}
impl AsMut<[u8]> for TestPublic {
fn as_mut(&mut self) -> &mut [u8] {
&mut []
}
}
impl<'a> TryFrom<&'a [u8]> for TestPublic {
type Error = ();
fn try_from(data: &'a [u8]) -> Result<Self, ()> {
Self::from_slice(data)
}
}
impl CryptoType for TestPublic {
type Pair = TestPair;
}
impl Derive for TestPublic {}
impl ByteArray for TestPublic {
const LEN: usize = 0;
fn from_slice(bytes: &[u8]) -> Result<Self, ()> {
if bytes.is_empty() {
Ok(Self)
} else {
Err(())
}
}
fn as_slice(&self) -> &[u8] {
&[]
}
fn to_raw_vec(&self) -> Vec<u8> {
vec![]
}
}
impl Public for TestPublic {}
impl Pair for TestPair {
type Public = TestPublic;
type Seed = [u8; 8];
type Signature = [u8; 0];
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 derive<Iter: Iterator<Item = DeriveJunction>>(
&self,
path_iter: Iter,
_: Option<[u8; 8]>,
) -> Result<(Self, Option<[u8; 8]>), 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(DeriveError::SoftKeyInPath)
},
},
None,
))
}
fn sign(&self, _message: &[u8]) -> Self::Signature {
[]
}
fn verify<M: AsRef<[u8]>>(_: &Self::Signature, _: M, _: &Self::Public) -> 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> {
vec![]
}
}
#[test]
fn interpret_std_seed_should_work() {
assert_eq!(
TestPair::from_string("0x0123456789abcdef", None),
Ok(TestPair::Seed(array_bytes::hex2bytes_unchecked("0123456789abcdef")))
);
}
#[test]
fn password_override_should_work() {
assert_eq!(
TestPair::from_string("hello world///password", None),
TestPair::from_string("hello world", Some("password")),
);
assert_eq!(
TestPair::from_string("hello world///password", None),
TestPair::from_string("hello world///other password", Some("password")),
);
}
#[test]
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![]
})
);
assert_eq!(
TestPair::from_string("hello world/1", None),
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")]
})
);
assert_eq!(
TestPair::from_string("hello world/0123456789012345678901234567890123456789", None),
Ok(TestPair::Standard {
phrase: "hello world".to_owned(),
password: None,
path: vec![DeriveJunction::soft("0123456789012345678901234567890123456789")]
})
);
assert_eq!(
TestPair::from_string("hello world//1", None),
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")]
})
);
assert_eq!(
TestPair::from_string("hello world//0123456789012345678901234567890123456789", None),
Ok(TestPair::Standard {
phrase: "hello world".to_owned(),
password: None,
path: vec![DeriveJunction::hard("0123456789012345678901234567890123456789")]
})
);
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")]
})
);
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)]
})
);
assert_eq!(
TestPair::from_string("hello world///password", None),
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")]
})
);
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")]
})
);
}
#[test]
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_eq!(
AccountId32::from_str("99G9VdMwXvzza9pS8qE8ZHJk3CheHW9uucBn9ngW4C1gmmzpv").unwrap_err(),
"invalid ss58 address.",
);
assert_eq!(
AccountId32::from_str(
"gc55177d67b064bb5d189a3e1ddad9bc6646e02e64d6e308f5acbb1533ac430d"
)
.unwrap_err(),
"invalid hex address.",
);
assert_eq!(
AccountId32::from_str(
"0xgc55177d67b064bb5d189a3e1ddad9bc6646e02e64d6e308f5acbb1533ac430d"
)
.unwrap_err(),
"invalid hex address.",
);
assert_eq!(
AccountId32::from_str(
"55c55177d67b064bb5d189a3e1ddad9bc6646e02e64d6e308f5acbb1533ac430d"
)
.unwrap_err(),
"invalid ss58 address.",
);
}
}