From c3413fdea3b233f499a27e7d56290878d6f77e21 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sat, 21 Dec 2019 06:34:36 -0800 Subject: [PATCH] Clean up definition for custom ss58 address formats (#4470) * Clearer definition for custom ss58 address formats * Fix subkey compile --- substrate/Cargo.lock | 1 + substrate/bin/utils/subkey/Cargo.toml | 1 + substrate/bin/utils/subkey/src/main.rs | 36 +++++++++++++++---------- substrate/primitives/core/src/crypto.rs | 34 ++++++++++++----------- 4 files changed, 42 insertions(+), 30 deletions(-) diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index f68ae16202..67fa81af0e 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -6669,6 +6669,7 @@ dependencies = [ "frame-system 2.0.0", "hex 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "hex-literal 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "node-primitives 2.0.0", "node-runtime 2.0.0", "pallet-balances 2.0.0", diff --git a/substrate/bin/utils/subkey/Cargo.toml b/substrate/bin/utils/subkey/Cargo.toml index 2fcd2aa473..6d687ce910 100644 --- a/substrate/bin/utils/subkey/Cargo.toml +++ b/substrate/bin/utils/subkey/Cargo.toml @@ -21,6 +21,7 @@ frame-system = { version = "2.0.0", path = "../../../frame/system" } pallet-balances = { version = "2.0.0", path = "../../../frame/balances" } pallet-transaction-payment = { version = "2.0.0", path = "../../../frame/transaction-payment" } rpassword = "4.0.1" +itertools = "0.8.2" [features] bench = [] diff --git a/substrate/bin/utils/subkey/src/main.rs b/substrate/bin/utils/subkey/src/main.rs index e89e0466ea..9526fa1b52 100644 --- a/substrate/bin/utils/subkey/src/main.rs +++ b/substrate/bin/utils/subkey/src/main.rs @@ -22,6 +22,7 @@ use bip39::{Language, Mnemonic, MnemonicType}; use clap::{App, ArgMatches, SubCommand}; use codec::{Decode, Encode}; use hex_literal::hex; +use itertools::Itertools; use node_primitives::{Balance, Hash, Index, AccountId, Signature}; use node_runtime::{BalancesCall, Call, Runtime, SignedPayload, UncheckedExtrinsic, VERSION}; use sp_core::{ @@ -155,20 +156,25 @@ impl PublicT for sr25519::Public { fn into_runtime(self) -> AccountPublic { self impl PublicT for ed25519::Public { fn into_runtime(self) -> AccountPublic { self.into() } } impl PublicT for ecdsa::Public { fn into_runtime(self) -> AccountPublic { self.into() } } -fn get_app<'a, 'b>() -> App<'a, 'b> { +fn get_usage() -> String { + let networks = Ss58AddressFormat::all().iter().cloned().map(String::from).join("/"); + let default_network = String::from(Ss58AddressFormat::default()); + format!(" + -e, --ed25519 'Use Ed25519/BIP39 cryptography' + -k, --secp256k1 'Use SECP256k1/ECDSA/BIP39 cryptography' + -s, --sr25519 'Use Schnorr/Ristretto x25519/BIP39 cryptography' + [network] -n, --network 'Specify a network. One of {}. Default is {}' + [password] -p, --password 'The password for the key' + --password-interactive 'You will be prompted for the password for the key.' + ", networks, default_network) +} + +fn get_app<'a, 'b>(usage: &'a str) -> App<'a, 'b> { App::new("subkey") .author("Parity Team ") .about("Utility for generating and restoring with Substrate keys") .version(env!("CARGO_PKG_VERSION")) - .args_from_usage(" - -e, --ed25519 'Use Ed25519/BIP39 cryptography' - -k, --secp256k1 'Use SECP256k1/ECDSA/BIP39 cryptography' - -s, --sr25519 'Use Schnorr/Ristretto x25519/BIP39 cryptography' - [network] -n, --network 'Specify a network. One of substrate \ - (default), polkadot, kusama, dothereum, edgeware, or kulupu' - [password] -p, --password 'The password for the key' - --password-interactive 'You will be prompted for the password for the key.' - ") + .args_from_usage(usage) .subcommands(vec![ SubCommand::with_name("generate") .about("Generate a random account") @@ -226,7 +232,8 @@ fn get_app<'a, 'b>() -> App<'a, 'b> { } fn main() { - let matches = get_app().get_matches(); + let usage = get_usage(); + let matches = get_app(&usage).get_matches(); if matches.is_present("ed25519") { return execute::(matches) @@ -260,7 +267,7 @@ where let maybe_network: Option = matches.value_of("network").map(|network| { network .try_into() - .expect("Invalid network name: must be polkadot/substrate/kusama/dothereum/edgeware") + .expect("Invalid network name. See --help for available networks.") }); if let Some(network) = maybe_network { set_default_ss58_version(network); @@ -553,7 +560,8 @@ mod tests { SignatureOf: SignatureT, PublicOf: PublicT, { - let app = get_app(); + let usage = get_usage(); + let app = get_app(&usage); let password = None; // Generate public key and seed. @@ -581,7 +589,7 @@ mod tests { // Verify the previous signature. let arg_vec = vec!["subkey", "verify", &signature[..], &public_key[..]]; - let matches = get_app().get_matches_from(arg_vec); + let matches = get_app(&usage).get_matches_from(arg_vec); let matches = matches.subcommand().1.unwrap(); assert!(do_verify::(matches, message)); } diff --git a/substrate/primitives/core/src/crypto.rs b/substrate/primitives/core/src/crypto.rs index fe3a53c83a..c5a42243dc 100644 --- a/substrate/primitives/core/src/crypto.rs +++ b/substrate/primitives/core/src/crypto.rs @@ -264,11 +264,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default { fn from_ss58check(s: &str) -> Result { Self::from_ss58check_with_version(s) .and_then(|(r, v)| match v { - Ss58AddressFormat::SubstrateAccountDirect => Ok(r), - Ss58AddressFormat::PolkadotAccountDirect => Ok(r), - Ss58AddressFormat::KusamaAccountDirect => Ok(r), - Ss58AddressFormat::DothereumAccountDirect => Ok(r), - Ss58AddressFormat::EdgewareAccountDirect => Ok(r), + v if !v.is_custom() => Ok(r), v if v == *DEFAULT_VERSION.lock() => Ok(r), _ => Err(PublicError::UnknownVersion), }) @@ -298,11 +294,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default { fn from_string(s: &str) -> Result { Self::from_string_with_version(s) .and_then(|(r, v)| match v { - Ss58AddressFormat::SubstrateAccountDirect => Ok(r), - Ss58AddressFormat::PolkadotAccountDirect => Ok(r), - Ss58AddressFormat::KusamaAccountDirect => Ok(r), - Ss58AddressFormat::DothereumAccountDirect => Ok(r), - Ss58AddressFormat::EdgewareAccountDirect => Ok(r), + v if !v.is_custom() => Ok(r), v if v == *DEFAULT_VERSION.lock() => Ok(r), _ => Err(PublicError::UnknownVersion), }) @@ -377,6 +369,14 @@ macro_rules! ss58_address_format { pub fn all() -> &'static [Ss58AddressFormat] { &ALL_SS58_ADDRESS_FORMATS } + + /// Whether the address is custom. + pub fn is_custom(&self) -> bool { + match self { + Self::Custom(_) => true, + _ => false, + } + } } impl From for u8 { @@ -410,6 +410,13 @@ macro_rules! ss58_address_format { } } + #[cfg(feature = "std")] + impl Default for Ss58AddressFormat { + fn default() -> Self { + *DEFAULT_VERSION.lock() + } + } + #[cfg(feature = "std")] impl From for String { fn from(x: Ss58AddressFormat) -> String { @@ -442,12 +449,7 @@ ss58_address_format!( /// typically used not just to encode format/version but also network identity) that is used for /// encoding and decoding SS58 addresses. If an unknown version is provided then it fails. /// -/// Current known "versions" are: -/// - 0 direct (payload) checksum for 32-byte *25519 Polkadot addresses. -/// - 2 direct (payload) checksum for 32-byte *25519 Kusama addresses. -/// - 7 direct (payload) checksum for 32-byte *25519 Edgeware addresses. -/// - 20 direct (payload) checksum for 32-byte *25519 Dothereum addresses. -/// - 42 direct (payload) checksum for 32-byte *25519 addresses on any Substrate-based network. +/// See `ss58_address_format!` for all current known "versions". #[cfg(feature = "std")] pub fn set_default_ss58_version(version: Ss58AddressFormat) { *DEFAULT_VERSION.lock() = version