Support for keyring in runtimes (#2044)

This functionality is required for #1984.

This PR enables
[`sp-keyring`](https://github.com/paritytech/polkadot-sdk/blob/21d36b7b4229c4d5225944f197918cde23fda4ea/substrate/primitives/keyring/src/sr25519.rs#L31-L40)
in `no-std` environments, allowing to generate the public key (e.g.
`AccountKeyring::Alice.public().to_ss58check()`), which can be later
used in the any of built-in [_runtime-genesis-config_
variant](https://github.com/paritytech/polkadot-sdk/blob/21d36b7b4229c4d5225944f197918cde23fda4ea/polkadot/node/service/src/chain_spec.rs#L1066-L1073).


The proposal is as follows:
- expose [`core::Pair`
trait](https://github.com/paritytech/polkadot-sdk/blob/d6f15306282e3de848a09c9aa9cba6f95a7811f0/substrate/primitives/core/src/crypto.rs#L832)
in `no-std`,
- `full_crypto` feature enables `sign` method,
- `std` feature enables `generate_with_phrase` and `generate` methods
(randomness is required),
- All other functionality, currently gated by `full_crypto` will be
available unconditionally (`no-std`):
-- `from_string`
-- `from_string_with_seed`
-- `from seed`
-- `from_seed_slice`
-- `from_phrase`
-- `derive`
-- `verify`

---

Depends on https://github.com/rust-bitcoin/rust-bip39/pull/57

---------

Co-authored-by: command-bot <>
Co-authored-by: Davide Galassi <davxy@datawok.net>
This commit is contained in:
Michal Kucharczyk
2024-03-12 12:43:31 +01:00
committed by GitHub
parent 1ead59773e
commit a756baf3b2
30 changed files with 248 additions and 233 deletions
@@ -18,14 +18,21 @@
//! A set of well-known keys used for testing.
pub use sp_core::bandersnatch;
#[cfg(feature = "std")]
use sp_core::bandersnatch::Signature;
use sp_core::{
bandersnatch::{Pair, Public, Signature},
bandersnatch::{Pair, Public},
crypto::UncheckedFrom,
hex2array, ByteArray, Pair as PairT,
};
extern crate alloc;
use alloc::{fmt, format, str::FromStr, string::String, vec::Vec};
/// Set of test accounts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter)]
#[derive(
Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter, Ord, PartialOrd,
)]
pub enum Keyring {
Alice,
Bob,
@@ -56,6 +63,7 @@ impl Keyring {
Public::from(self).to_raw_vec()
}
#[cfg(feature = "std")]
pub fn sign(self, msg: &[u8]) -> Signature {
Pair::from(self).sign(msg)
}
@@ -102,16 +110,16 @@ impl From<Keyring> for &'static str {
#[derive(Debug)]
pub struct ParseKeyringError;
impl std::fmt::Display for ParseKeyringError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for ParseKeyringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ParseKeyringError")
}
}
impl std::str::FromStr for Keyring {
impl FromStr for Keyring {
type Err = ParseKeyringError;
fn from_str(s: &str) -> Result<Self, <Self as std::str::FromStr>::Err> {
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
match s {
"Alice" => Ok(Keyring::Alice),
"Bob" => Ok(Keyring::Bob),
+10 -2
View File
@@ -18,14 +18,21 @@
//! Support code for the runtime. A set of test accounts.
pub use sp_core::ed25519;
#[cfg(feature = "std")]
use sp_core::ed25519::Signature;
use sp_core::{
ed25519::{Pair, Public, Signature},
ed25519::{Pair, Public},
hex2array, ByteArray, Pair as PairT, H256,
};
use sp_runtime::AccountId32;
extern crate alloc;
use alloc::{format, string::String, vec::Vec};
/// Set of test accounts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter)]
#[derive(
Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter, Ord, PartialOrd,
)]
pub enum Keyring {
Alice,
Bob,
@@ -76,6 +83,7 @@ impl Keyring {
self.to_raw_public().into()
}
#[cfg(feature = "std")]
pub fn sign(self, msg: &[u8]) -> Signature {
Pair::from(self).sign(msg)
}
+2
View File
@@ -17,6 +17,8 @@
//! Support code for the runtime. A set of test accounts.
#![cfg_attr(not(feature = "std"), no_std)]
/// Test account crypto for sr25519.
pub mod sr25519;
+14 -6
View File
@@ -18,15 +18,22 @@
//! Support code for the runtime. A set of test accounts.
pub use sp_core::sr25519;
#[cfg(feature = "std")]
use sp_core::sr25519::Signature;
use sp_core::{
hex2array,
sr25519::{Pair, Public, Signature},
sr25519::{Pair, Public},
ByteArray, Pair as PairT, H256,
};
use sp_runtime::AccountId32;
extern crate alloc;
use alloc::{fmt, format, str::FromStr, string::String, vec::Vec};
/// Set of test accounts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter)]
#[derive(
Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter, Ord, PartialOrd,
)]
pub enum Keyring {
Alice,
Bob,
@@ -77,6 +84,7 @@ impl Keyring {
self.to_raw_public().into()
}
#[cfg(feature = "std")]
pub fn sign(self, msg: &[u8]) -> Signature {
Pair::from(self).sign(msg)
}
@@ -140,16 +148,16 @@ impl From<Keyring> for sp_runtime::MultiSigner {
#[derive(Debug)]
pub struct ParseKeyringError;
impl std::fmt::Display for ParseKeyringError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for ParseKeyringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ParseKeyringError")
}
}
impl std::str::FromStr for Keyring {
impl FromStr for Keyring {
type Err = ParseKeyringError;
fn from_str(s: &str) -> Result<Self, <Self as std::str::FromStr>::Err> {
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
match s {
"alice" => Ok(Keyring::Alice),
"bob" => Ok(Keyring::Bob),