From 64987fe44ceae86be156a72f405ab474d6e3e93e Mon Sep 17 00:00:00 2001 From: Tadeo hepperle Date: Wed, 14 Feb 2024 15:51:34 +0100 Subject: [PATCH] adjust subxt signer static once locks --- Cargo.lock | 18 +----------------- signer/Cargo.toml | 3 +-- signer/src/ecdsa.rs | 19 +++++-------------- signer/src/utils.rs | 27 +++++++++++++++++++++------ 4 files changed, 28 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2b28f2d1fd..8cf73fb235 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/signer/Cargo.toml b/signer/Cargo.toml index f60ad4e942..9efd2e1ca7 100644 --- a/signer/Cargo.toml +++ b/signer/Cargo.toml @@ -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 } diff --git a/signer/src/ecdsa.rs b/signer/src/ecdsa.rs index 6946446f60..549f9f30c2 100644 --- a/signer/src/ecdsa.rs +++ b/signer/src/ecdsa.rs @@ -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; - - fn deref(&self) -> &Self::Target { - static ONCE: once_cell::sync::OnceCell> = 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 { 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 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>(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() } diff --git a/signer/src/utils.rs b/signer/src/utils.rs index 52a935a1fd..756d050a0a 100644 --- a/signer/src/utils.rs +++ b/signer/src/utils.rs @@ -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 } + } + } } )+ };