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
+39 -39
View File
@@ -17,14 +17,12 @@
//! Support code for the runtime. A set of test accounts.
use lazy_static::lazy_static;
pub use sp_core::ed25519;
use sp_core::{
ed25519::{Pair, Public, Signature},
ByteArray, Pair as PairT, H256,
hex2array, 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 +91,7 @@ impl Keyring {
}
pub fn public(self) -> Public {
self.pair().public()
Public::from(self)
}
pub fn to_seed(self) -> String {
@@ -128,16 +126,9 @@ impl From<Keyring> for sp_runtime::MultiSigner {
}
}
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 Public {
fn from(k: Keyring) -> Self {
*(*PUBLIC_KEYS).get(&k).unwrap()
Public::from_raw(k.into())
}
}
@@ -155,38 +146,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!("88dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee"),
Keyring::Bob =>
hex2array!("d17c2d7823ebf260fd138f2d7e27d114c0145d968b5ff5006125f2414fadae69"),
Keyring::Charlie =>
hex2array!("439660b36c6c03afafca027b910b4fecf99801834c62a5e6006f27d978de234f"),
Keyring::Dave =>
hex2array!("5e639b43e0052c47447dac87d6fd2b6ec50bdd4d0f614e4299c665249bbd09d9"),
Keyring::Eve =>
hex2array!("1dfe3e22cc0d45c70779c1095f7489a8ef3cf52d62fbd8c2fa38c9f1723502b5"),
Keyring::Ferdie =>
hex2array!("568cb4a574c6d178feb39c27dfc8b3f789e5f5423e19c71633c748b9acf086b5"),
Keyring::AliceStash =>
hex2array!("451781cd0c5504504f69ceec484cc66e4c22a2b6a9d20fb1a426d91ad074a2a8"),
Keyring::BobStash =>
hex2array!("292684abbb28def63807c5f6e84e9e8689769eb37b1ab130d79dbfbf1b9a0d44"),
Keyring::CharlieStash =>
hex2array!("dd6a6118b6c11c9c9e5a4f34ed3d545e2c74190f90365c60c230fa82e9423bb9"),
Keyring::DaveStash =>
hex2array!("1d0432d75331ab299065bee79cdb1bdc2497c597a3087b4d955c67e3c000c1e2"),
Keyring::EveStash =>
hex2array!("c833bdd2e1a7a18acc1c11f8596e2e697bb9b42d6b6051e474091a1d43a294d7"),
Keyring::FerdieStash =>
hex2array!("199d749dbf4b8135cb1f3c8fd697a390fc0679881a8a110c1d06375b3b62cd09"),
Keyring::One =>
hex2array!("16f97016bbea8f7b45ae6757b49efc1080accc175d8f018f9ba719b60b0815e4"),
Keyring::Two =>
hex2array!("5079bcd20fd97d7d2f752c4607012600b401950260a91821f73e692071c82bf5"),
}
}
}
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()
}
}
@@ -213,4 +208,9 @@ 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) }));
}
}