adjust subxt signer static once locks

This commit is contained in:
Tadeo hepperle
2024-02-14 15:51:34 +01:00
parent 2b38a0fce1
commit 64987fe44c
4 changed files with 28 additions and 39 deletions
Generated
+1 -17
View File
@@ -1076,12 +1076,6 @@ dependencies = [
"itertools 0.10.5",
]
[[package]]
name = "critical-section"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7059fff8937831a9ae6f0fe4d658ffabf58f2ca96aa9dec1c889f936f705f216"
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
@@ -2738,10 +2732,6 @@ name = "once_cell"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
dependencies = [
"critical-section",
"portable-atomic",
]
[[package]]
name = "oorandom"
@@ -2989,12 +2979,6 @@ dependencies = [
"universal-hash",
]
[[package]]
name = "portable-atomic"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
@@ -4638,11 +4622,11 @@ name = "subxt-signer"
version = "0.34.0"
dependencies = [
"bip39",
"cfg-if",
"derive_more",
"getrandom",
"hex",
"hmac 0.12.1",
"once_cell",
"parity-scale-codec",
"pbkdf2 0.12.2",
"regex",
+1 -2
View File
@@ -37,6 +37,7 @@ web = ["getrandom/js"]
subxt-core = { workspace = true, optional = true, default-features = false }
regex = { workspace = true, default-features = false }
hex = { workspace = true }
cfg-if = { workspace = true }
codec = { package = "parity-scale-codec", workspace = true, features = ["derive"] }
sp-core-hashing = { workspace = true, default-features = false }
derive_more = { workspace = true }
@@ -49,8 +50,6 @@ schnorrkel = { workspace = true, optional = true, default-features = false }
secp256k1 = { workspace = true, optional = true, default-features = false, features = ["alloc", "recovery"] } # features = ["recovery", "global-context"],
secrecy = { workspace = true }
# We only pull this in because `std::sync::OnceLock` is not available in no-std contexts.
once_cell = { workspace = true, default-features = false, features = ["alloc", "critical-section"] }
# We only pull this in to enable the JS flag for schnorrkel to use.
getrandom = { workspace = true, optional = true }
+5 -14
View File
@@ -9,19 +9,9 @@ use crate::crypto::{seed_from_entropy, DeriveJunction, SecretUri};
use core::str::FromStr;
use derive_more::{Display, From};
use hex::FromHex;
use secp256k1::{ecdsa::RecoverableSignature, All, Message, Secp256k1, SecretKey};
use secp256k1::{ecdsa::RecoverableSignature, Message, Secp256k1, SecretKey};
use secrecy::ExposeSecret;
struct GlobalSecp256K1Context;
impl core::ops::Deref for GlobalSecp256K1Context {
type Target = Secp256k1<All>;
fn deref(&self) -> &Self::Target {
static ONCE: once_cell::sync::OnceCell<Secp256k1<All>> = once_cell::sync::OnceCell::new();
ONCE.get_or_init(|| Secp256k1::new())
}
}
const SEED_LENGTH: usize = 32;
/// Seed bytes used to generate a key pair.
@@ -122,7 +112,7 @@ impl Keypair {
pub fn from_seed(seed: Seed) -> Result<Self, Error> {
let secret = SecretKey::from_slice(&seed).map_err(|_| Error::InvalidSeed)?;
Ok(Self(secp256k1::Keypair::from_secret_key(
&GlobalSecp256K1Context,
&Secp256k1::signing_only(),
&secret,
)))
}
@@ -175,7 +165,7 @@ impl Keypair {
// From sp_core::ecdsa::sign_prehashed:
let wrapped = Message::from_digest_slice(&message_hash).expect("Message is 32 bytes; qed");
let recsig: RecoverableSignature =
GlobalSecp256K1Context.sign_ecdsa_recoverable(&wrapped, &self.0.secret_key());
Secp256k1::signing_only().sign_ecdsa_recoverable(&wrapped, &self.0.secret_key());
// From sp_core::ecdsa's `impl From<RecoverableSignature> for Signature`:
let (recid, sig): (_, [u8; 64]) = recsig.serialize_compact();
let mut signature_bytes: [u8; 65] = [0; 65];
@@ -206,7 +196,8 @@ pub fn verify<M: AsRef<[u8]>>(sig: &Signature, message: M, pubkey: &PublicKey) -
};
let message_hash = sp_core_hashing::blake2_256(message.as_ref());
let wrapped = Message::from_digest_slice(&message_hash).expect("Message is 32 bytes; qed");
GlobalSecp256K1Context
Secp256k1::verification_only()
.verify_ecdsa(&wrapped, &signature, &public)
.is_ok()
}
+21 -6
View File
@@ -17,10 +17,19 @@
macro_rules! once_static {
($($(#[$attr:meta])* $vis:vis fn $name:ident() -> $ty:ty { $expr:expr } )+) => {
$(
$(#[$attr])*
$vis fn $name() -> &'static $ty {
static VAR: once_cell::sync::OnceCell<$ty> = once_cell::sync::OnceCell::new();
VAR.get_or_init(|| { $expr })
cfg_if::cfg_if! {
if #[cfg(feature = "std")] {
$(#[$attr])*
$vis fn $name() -> &'static $ty {
static VAR: std::sync::OnceLock<$ty> = std::sync::OnceLock::new();
VAR.get_or_init(|| { $expr })
}
} else {
$(#[$attr])*
$vis fn $name() -> $ty {
$expr
}
}
}
)+
};
@@ -33,8 +42,14 @@ macro_rules! once_static_cloned {
$(
$(#[$attr])*
$vis fn $name() -> $ty {
static VAR: once_cell::sync::OnceCell<$ty> = once_cell::sync::OnceCell::new();
VAR.get_or_init(|| { $expr }).clone()
cfg_if::cfg_if! {
if #[cfg(feature = "std")] {
static VAR: std::sync::OnceLock<$ty> = std::sync::OnceLock::new();
VAR.get_or_init(|| { $expr }).clone()
} else {
{ $expr }
}
}
}
)+
};