Skip to content

Commit

Permalink
crypto: lazy_static removed, light parser for address URI added (#2250
Browse files Browse the repository at this point in the history
)

The `lazy_static` package does not work well in `no-std`: it requires
`spin_no_std` feature, which also will propagate into `std` if enabled.
This is not what we want.

This PR provides simple address uri parser which allows to get rid of
_regex_ which was used to parse the address uri, what in turns allows to
remove lazy_static.

Three regular expressions
(`SS58_REGEX`,`SECRET_PHRASE_REGEX`,`JUNCTION_REGEX`) were replaced with
the parser which unifies all of them.

The new parser does not support Unicode, it is ASCII only.

Related to: #2044

---------

Co-authored-by: Bastian Köcher <[email protected]>
Co-authored-by: Koute <[email protected]>
Co-authored-by: command-bot <>
  • Loading branch information
3 people authored Nov 17, 2023
1 parent 3ab2bc9 commit 5007e2d
Show file tree
Hide file tree
Showing 10 changed files with 601 additions and 67 deletions.
45 changes: 36 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ members = [
"substrate/primitives/consensus/sassafras",
"substrate/primitives/consensus/slots",
"substrate/primitives/core",
"substrate/primitives/core/fuzz",
"substrate/primitives/core/hashing",
"substrate/primitives/core/hashing/proc-macro",
"substrate/primitives/crypto/ec-utils",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
|
= help: the following other types implement trait `WrapperTypeDecode`:
Box<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Arc<T>
= note: required for `Bar` to implement `Decode`
= note: required for `Bar` to implement `FullCodec`
Expand Down Expand Up @@ -44,8 +44,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeEncode` is not satisfied
bytes::bytes::Bytes
Cow<'a, T>
parity_scale_codec::Ref<'a, T, U>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Arc<T>
Vec<T>
and $N others
Expand Down Expand Up @@ -81,8 +81,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
|
= help: the following other types implement trait `WrapperTypeDecode`:
Box<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Arc<T>
= note: required for `Bar` to implement `Decode`
= note: required for `Bar` to implement `FullCodec`
Expand Down Expand Up @@ -119,8 +119,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeEncode` is not satisfied
bytes::bytes::Bytes
Cow<'a, T>
parity_scale_codec::Ref<'a, T, U>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Arc<T>
Vec<T>
and $N others
Expand All @@ -137,8 +137,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
|
= help: the following other types implement trait `WrapperTypeDecode`:
Box<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Arc<T>
= note: required for `Bar` to implement `Decode`
= note: required for `Bar` to implement `FullCodec`
Expand Down Expand Up @@ -177,8 +177,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeEncode` is not satisfied
bytes::bytes::Bytes
Cow<'a, T>
parity_scale_codec::Ref<'a, T, U>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Arc<T>
Vec<T>
and $N others
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
|
= help: the following other types implement trait `WrapperTypeDecode`:
Box<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Arc<T>
= note: required for `Bar` to implement `Decode`
= note: required for `Bar` to implement `FullCodec`
Expand Down Expand Up @@ -44,8 +44,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeEncode` is not satisfied
bytes::bytes::Bytes
Cow<'a, T>
parity_scale_codec::Ref<'a, T, U>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Arc<T>
Vec<T>
and $N others
Expand Down Expand Up @@ -81,8 +81,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
|
= help: the following other types implement trait `WrapperTypeDecode`:
Box<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Arc<T>
= note: required for `Bar` to implement `Decode`
= note: required for `Bar` to implement `FullCodec`
Expand Down Expand Up @@ -119,8 +119,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeEncode` is not satisfied
bytes::bytes::Bytes
Cow<'a, T>
parity_scale_codec::Ref<'a, T, U>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Arc<T>
Vec<T>
and $N others
Expand All @@ -137,8 +137,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeDecode` is not satisfied
|
= help: the following other types implement trait `WrapperTypeDecode`:
Box<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Arc<T>
= note: required for `Bar` to implement `Decode`
= note: required for `Bar` to implement `FullCodec`
Expand Down Expand Up @@ -177,8 +177,8 @@ error[E0277]: the trait bound `Bar: WrapperTypeEncode` is not satisfied
bytes::bytes::Bytes
Cow<'a, T>
parity_scale_codec::Ref<'a, T, U>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Rc<T>
frame_support::sp_runtime::sp_application_crypto::sp_core::Bytes
Arc<T>
Vec<T>
and $N others
Expand Down
6 changes: 2 additions & 4 deletions substrate/primitives/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ bs58 = { version = "0.5.0", default-features = false, optional = true }
rand = { version = "0.8.5", features = ["small_rng"], optional = true }
substrate-bip39 = { version = "0.4.4", optional = true }
bip39 = { version = "2.0.0", default-features = false }
regex = { version = "1.6.0", optional = true }
zeroize = { version = "1.4.3", default-features = false }
secrecy = { version = "0.8.0", default-features = false }
lazy_static = { version = "1.4.0", default-features = false, optional = true }
parking_lot = { version = "0.12.1", optional = true }
ss58-registry = { version = "1.34.0", default-features = false }
sp-std = { path = "../std", default-features = false}
Expand Down Expand Up @@ -63,6 +61,8 @@ bandersnatch_vrfs = { git = "https://github.com/w3f/ring-vrf", rev = "cbc342e",
[dev-dependencies]
criterion = "0.4.0"
serde_json = "1.0.108"
lazy_static = "1.4.0"
regex = "1.6.0"
sp-core-hashing-proc-macro = { path = "hashing/proc-macro" }

[[bench]]
Expand Down Expand Up @@ -92,7 +92,6 @@ std = [
"hash256-std-hasher/std",
"impl-serde/std",
"itertools",
"lazy_static",
"libsecp256k1/std",
"log/std",
"merlin/std",
Expand All @@ -102,7 +101,6 @@ std = [
"primitive-types/serde",
"primitive-types/std",
"rand",
"regex",
"scale-info/std",
"schnorrkel/std",
"secp256k1/global-context",
Expand Down
20 changes: 20 additions & 0 deletions substrate/primitives/core/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "sp-core-fuzz"
version = "0.0.0"
publish = false

[package.metadata]
cargo-fuzz = true

[dependencies]
lazy_static = "1.4.0"
libfuzzer-sys = "0.4"
regex = "1.10.2"

sp-core = { path = ".." }

[[bin]]
name = "fuzz_address_uri"
path = "fuzz_targets/fuzz_address_uri.rs"
test = false
doc = false
53 changes: 53 additions & 0 deletions substrate/primitives/core/fuzz/fuzz_targets/fuzz_address_uri.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![no_main]

extern crate libfuzzer_sys;
extern crate regex;
extern crate sp_core;

use libfuzzer_sys::fuzz_target;
use regex::Regex;
use sp_core::crypto::AddressUri;

lazy_static::lazy_static! {
static ref SECRET_PHRASE_REGEX: Regex = Regex::new(r"^(?P<phrase>[a-zA-Z0-9 ]+)?(?P<path>(//?[^/]+)*)(///(?P<password>.*))?$")
.expect("constructed from known-good static value; qed");
}

fuzz_target!(|input: &str| {
let regex_result = SECRET_PHRASE_REGEX.captures(input);
let manual_result = AddressUri::parse(input);
assert_eq!(regex_result.is_some(), manual_result.is_ok());
if manual_result.is_err() {
let _ = format!("{}", manual_result.as_ref().err().unwrap());
}
if let (Some(regex_result), Ok(manual_result)) = (regex_result, manual_result) {
assert_eq!(regex_result.name("phrase").map(|p| p.as_str()), manual_result.phrase);

let manual_paths = manual_result
.paths
.iter()
.map(|s| "/".to_string() + s)
.collect::<Vec<_>>()
.join("");

assert_eq!(regex_result.name("path").unwrap().as_str().to_string(), manual_paths);
assert_eq!(regex_result.name("password").map(|pass| pass.as_str()), manual_result.pass);
}
});
Loading

0 comments on commit 5007e2d

Please sign in to comment.