Skip to content

Commit

Permalink
Switch to using ss58-registry crate (paritytech#9755)
Browse files Browse the repository at this point in the history
* Switch to using ss58-registry crate
* Custom(42) is now eq to Substrate

Co-authored-by: Bastian Köcher <[email protected]>

Co-authored-by: André Silva <[email protected]>
  • Loading branch information
gilescope and andresilva authored Oct 12, 2021
1 parent 1403b37 commit cafe12e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 302 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

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

11 changes: 7 additions & 4 deletions client/cli/src/commands/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ use crate::{
};
use serde_json::json;
use sp_core::{
crypto::{ExposeSecret, SecretString, Ss58AddressFormat, Ss58Codec, Zeroize},
crypto::{
unwrap_or_default_ss58_version, ExposeSecret, SecretString, Ss58AddressFormat, Ss58Codec,
Zeroize,
},
hexdisplay::HexDisplay,
Pair,
};
Expand Down Expand Up @@ -72,7 +75,7 @@ pub fn print_from_uri<Pair>(
let password = password.as_ref().map(|s| s.expose_secret().as_str());
if let Ok((pair, seed)) = Pair::from_phrase(uri, password.clone()) {
let public_key = pair.public();
let network_override = network_override.unwrap_or_default();
let network_override = unwrap_or_default_ss58_version(network_override);

match output {
OutputType::Json => {
Expand Down Expand Up @@ -108,7 +111,7 @@ pub fn print_from_uri<Pair>(
}
} else if let Ok((pair, seed)) = Pair::from_string_with_seed(uri, password.clone()) {
let public_key = pair.public();
let network_override = network_override.unwrap_or_default();
let network_override = unwrap_or_default_ss58_version(network_override);

match output {
OutputType::Json => {
Expand Down Expand Up @@ -198,7 +201,7 @@ where
let public_key = Pair::Public::try_from(&public)
.map_err(|_| "Failed to construct public key from given hex")?;

let network_override = network_override.unwrap_or_default();
let network_override = unwrap_or_default_ss58_version(network_override);

match output {
OutputType::Json => {
Expand Down
20 changes: 14 additions & 6 deletions client/cli/src/commands/vanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
error, utils, with_crypto_scheme, CryptoSchemeFlag, NetworkSchemeFlag, OutputTypeFlag,
};
use rand::{rngs::OsRng, RngCore};
use sp_core::crypto::{Ss58AddressFormat, Ss58Codec};
use sp_core::crypto::{unwrap_or_default_ss58_version, Ss58AddressFormat, Ss58Codec};
use sp_runtime::traits::IdentifyAccount;
use structopt::StructOpt;
use utils::print_from_uri;
Expand Down Expand Up @@ -53,7 +53,10 @@ impl VanityCmd {
pub fn run(&self) -> error::Result<()> {
let formated_seed = with_crypto_scheme!(
self.crypto_scheme.scheme,
generate_key(&self.pattern, self.network_scheme.network.clone().unwrap_or_default()),
generate_key(
&self.pattern,
unwrap_or_default_ss58_version(self.network_scheme.network)
),
)?;

with_crypto_scheme!(
Expand Down Expand Up @@ -159,7 +162,10 @@ fn assert_non_empty_string(pattern: &str) -> Result<String, &'static str> {
#[cfg(test)]
mod tests {
use super::*;
use sp_core::{crypto::Ss58Codec, sr25519, Pair};
use sp_core::{
crypto::{default_ss58_version, Ss58AddressFormatRegistry, Ss58Codec},
sr25519, Pair,
};
use structopt::StructOpt;
#[cfg(feature = "bench")]
use test::Bencher;
Expand All @@ -172,7 +178,7 @@ mod tests {

#[test]
fn test_generation_with_single_char() {
let seed = generate_key::<sr25519::Pair>("ab", Default::default()).unwrap();
let seed = generate_key::<sr25519::Pair>("ab", default_ss58_version()).unwrap();
assert!(sr25519::Pair::from_seed_slice(&hex::decode(&seed[2..]).unwrap())
.unwrap()
.public()
Expand All @@ -182,11 +188,13 @@ mod tests {

#[test]
fn generate_key_respects_network_override() {
let seed = generate_key::<sr25519::Pair>("ab", Ss58AddressFormat::PolkadotAccount).unwrap();
let seed =
generate_key::<sr25519::Pair>("ab", Ss58AddressFormatRegistry::PolkadotAccount.into())
.unwrap();
assert!(sr25519::Pair::from_seed_slice(&hex::decode(&seed[2..]).unwrap())
.unwrap()
.public()
.to_ss58check_with_version(Ss58AddressFormat::PolkadotAccount)
.to_ss58check_with_version(Ss58AddressFormatRegistry::PolkadotAccount.into())
.contains("ab"));
}

Expand Down
2 changes: 1 addition & 1 deletion primitives/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ hex = { version = "0.4", default-features = false, optional = true }
twox-hash = { version = "1.6.1", default-features = false, optional = true }
libsecp256k1 = { version = "0.6", default-features = false, features = ["hmac", "static-context"], optional = true }
merlin = { version = "2.0", default-features = false, optional = true }

ss58-registry = "1.0.0"
sp-runtime-interface = { version = "4.0.0-dev", default-features = false, path = "../runtime-interface" }

[dev-dependencies]
Expand Down
Loading

0 comments on commit cafe12e

Please sign in to comment.