Make Regex in ss58codec and secret phrase crypto static (#8117) (#8177)

This commit is contained in:
Robin Syihab
2021-02-23 04:37:48 +07:00
committed by GitHub
parent d25229bc89
commit e7ac265649
+16 -18
View File
@@ -595,14 +595,20 @@ pub fn set_default_ss58_version(version: Ss58AddressFormat) {
*DEFAULT_VERSION.lock() = version *DEFAULT_VERSION.lock() = version
} }
#[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")] #[cfg(feature = "std")]
impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Default + Derive> Ss58Codec for T { impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Default + Derive> Ss58Codec for T {
fn from_string(s: &str) -> Result<Self, PublicError> { fn from_string(s: &str) -> Result<Self, PublicError> {
let re = Regex::new(r"^(?P<ss58>[\w\d ]+)?(?P<path>(//?[^/]+)*)$") let cap = SS58_REGEX.captures(s).ok_or(PublicError::InvalidFormat)?;
.expect("constructed from known-good static value; qed");
let cap = re.captures(s).ok_or(PublicError::InvalidFormat)?;
let re_junction = Regex::new(r"/(/?[^/]+)")
.expect("constructed from known-good static value; qed");
let s = cap.name("ss58") let s = cap.name("ss58")
.map(|r| r.as_str()) .map(|r| r.as_str())
.unwrap_or(DEV_ADDRESS); .unwrap_or(DEV_ADDRESS);
@@ -621,7 +627,7 @@ impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Default + Derive> Ss58Codec for T {
if cap["path"].is_empty() { if cap["path"].is_empty() {
Ok(addr) Ok(addr)
} else { } else {
let path = re_junction.captures_iter(&cap["path"]) let path = JUNCTION_REGEX.captures_iter(&cap["path"])
.map(|f| DeriveJunction::from(&f[1])); .map(|f| DeriveJunction::from(&f[1]));
addr.derive(path) addr.derive(path)
.ok_or(PublicError::InvalidPath) .ok_or(PublicError::InvalidPath)
@@ -629,11 +635,7 @@ impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Default + Derive> Ss58Codec for T {
} }
fn from_string_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> { fn from_string_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> {
let re = Regex::new(r"^(?P<ss58>[\w\d ]+)?(?P<path>(//?[^/]+)*)$") let cap = SS58_REGEX.captures(s).ok_or(PublicError::InvalidFormat)?;
.expect("constructed from known-good static value; qed");
let cap = re.captures(s).ok_or(PublicError::InvalidFormat)?;
let re_junction = Regex::new(r"/(/?[^/]+)")
.expect("constructed from known-good static value; qed");
let (addr, v) = Self::from_ss58check_with_version( let (addr, v) = Self::from_ss58check_with_version(
cap.name("ss58") cap.name("ss58")
.map(|r| r.as_str()) .map(|r| r.as_str())
@@ -642,7 +644,7 @@ impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Default + Derive> Ss58Codec for T {
if cap["path"].is_empty() { if cap["path"].is_empty() {
Ok((addr, v)) Ok((addr, v))
} else { } else {
let path = re_junction.captures_iter(&cap["path"]) let path = JUNCTION_REGEX.captures_iter(&cap["path"])
.map(|f| DeriveJunction::from(&f[1])); .map(|f| DeriveJunction::from(&f[1]));
addr.derive(path) addr.derive(path)
.ok_or(PublicError::InvalidPath) .ok_or(PublicError::InvalidPath)
@@ -999,13 +1001,9 @@ pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static {
fn from_string_with_seed(s: &str, password_override: Option<&str>) fn from_string_with_seed(s: &str, password_override: Option<&str>)
-> Result<(Self, Option<Self::Seed>), SecretStringError> -> Result<(Self, Option<Self::Seed>), SecretStringError>
{ {
let re = Regex::new(r"^(?P<phrase>[\d\w ]+)?(?P<path>(//?[^/]+)*)(///(?P<password>.*))?$") let cap = SECRET_PHRASE_REGEX.captures(s).ok_or(SecretStringError::InvalidFormat)?;
.expect("constructed from known-good static value; qed");
let cap = re.captures(s).ok_or(SecretStringError::InvalidFormat)?;
let re_junction = Regex::new(r"/(/?[^/]+)") let path = JUNCTION_REGEX.captures_iter(&cap["path"])
.expect("constructed from known-good static value; qed");
let path = re_junction.captures_iter(&cap["path"])
.map(|f| DeriveJunction::from(&f[1])); .map(|f| DeriveJunction::from(&f[1]));
let phrase = cap.name("phrase").map(|r| r.as_str()).unwrap_or(DEV_PHRASE); let phrase = cap.name("phrase").map(|r| r.as_str()).unwrap_or(DEV_PHRASE);