keyring: remove lazy_static public keys hash maps (#2387)

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 removes public/private key hash-maps and replaces them with
simple static byte arrays.

`&T` versions of `AsRef/Deref/From` traits implementation were removed.

Little const helper for converting hex strings into array during compile
time was also added. (somewhat similar to _hex_literal_).

---------

Co-authored-by: command-bot <>
Co-authored-by: Koute <koute@users.noreply.github.com>
This commit is contained in:
Michal Kucharczyk
2023-12-11 15:21:20 +01:00
committed by GitHub
parent 1e878780a5
commit f6548aee31
7 changed files with 266 additions and 122 deletions
+38 -38
View File
@@ -17,14 +17,13 @@
//! Support code for the runtime. A set of test accounts.
use lazy_static::lazy_static;
pub use sp_core::sr25519;
use sp_core::{
hex2array,
sr25519::{Pair, Public, Signature},
ByteArray, Pair as PairT, H256,
};
use sp_runtime::AccountId32;
use std::{collections::HashMap, ops::Deref};
/// Set of test accounts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum::Display, strum::EnumIter)]
@@ -93,7 +92,7 @@ impl Keyring {
}
pub fn public(self) -> Public {
self.pair().public()
Public::from(self)
}
pub fn to_seed(self) -> String {
@@ -165,13 +164,6 @@ impl std::str::FromStr for Keyring {
}
}
lazy_static! {
static ref PRIVATE_KEYS: HashMap<Keyring, Pair> =
Keyring::iter().map(|i| (i, i.pair())).collect();
static ref PUBLIC_KEYS: HashMap<Keyring, Public> =
PRIVATE_KEYS.iter().map(|(&name, pair)| (name, pair.public())).collect();
}
impl From<Keyring> for AccountId32 {
fn from(k: Keyring) -> Self {
k.to_account_id()
@@ -180,7 +172,7 @@ impl From<Keyring> for AccountId32 {
impl From<Keyring> for Public {
fn from(k: Keyring) -> Self {
*(*PUBLIC_KEYS).get(&k).unwrap()
Public::from_raw(k.into())
}
}
@@ -192,38 +184,42 @@ impl From<Keyring> for Pair {
impl From<Keyring> for [u8; 32] {
fn from(k: Keyring) -> Self {
*(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref()
match k {
Keyring::Alice =>
hex2array!("d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"),
Keyring::Bob =>
hex2array!("8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48"),
Keyring::Charlie =>
hex2array!("90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22"),
Keyring::Dave =>
hex2array!("306721211d5404bd9da88e0204360a1a9ab8b87c66c1bc2fcdd37f3c2222cc20"),
Keyring::Eve =>
hex2array!("e659a7a1628cdd93febc04a4e0646ea20e9f5f0ce097d9a05290d4a9e054df4e"),
Keyring::Ferdie =>
hex2array!("1cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c"),
Keyring::AliceStash =>
hex2array!("be5ddb1579b72e84524fc29e78609e3caf42e85aa118ebfe0b0ad404b5bdd25f"),
Keyring::BobStash =>
hex2array!("fe65717dad0447d715f660a0a58411de509b42e6efb8375f562f58a554d5860e"),
Keyring::CharlieStash =>
hex2array!("1e07379407fecc4b89eb7dbd287c2c781cfb1907a96947a3eb18e4f8e7198625"),
Keyring::DaveStash =>
hex2array!("e860f1b1c7227f7c22602f53f15af80747814dffd839719731ee3bba6edc126c"),
Keyring::EveStash =>
hex2array!("8ac59e11963af19174d0b94d5d78041c233f55d2e19324665bafdfb62925af2d"),
Keyring::FerdieStash =>
hex2array!("101191192fc877c24d725b337120fa3edc63d227bbc92705db1e2cb65f56981a"),
Keyring::One =>
hex2array!("ac859f8a216eeb1b320b4c76d118da3d7407fa523484d0a980126d3b4d0d220a"),
Keyring::Two =>
hex2array!("1254f7017f0b8347ce7ab14f96d818802e7e9e0c0d1b7c9acb3c726b080e7a03"),
}
}
}
impl From<Keyring> for H256 {
fn from(k: Keyring) -> Self {
(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref().into()
}
}
impl From<Keyring> for &'static [u8; 32] {
fn from(k: Keyring) -> Self {
(*PUBLIC_KEYS).get(&k).unwrap().as_array_ref()
}
}
impl AsRef<[u8; 32]> for Keyring {
fn as_ref(&self) -> &[u8; 32] {
(*PUBLIC_KEYS).get(self).unwrap().as_array_ref()
}
}
impl AsRef<Public> for Keyring {
fn as_ref(&self) -> &Public {
(*PUBLIC_KEYS).get(self).unwrap()
}
}
impl Deref for Keyring {
type Target = [u8; 32];
fn deref(&self) -> &[u8; 32] {
(*PUBLIC_KEYS).get(self).unwrap().as_array_ref()
k.into()
}
}
@@ -250,4 +246,8 @@ mod tests {
&Keyring::Bob.public(),
));
}
#[test]
fn verify_static_public_keys() {
assert!(Keyring::iter().all(|k| { k.pair().public().as_ref() == <[u8; 32]>::from(k) }));
}
}