Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize secp256k1 context only once #535

Merged
merged 1 commit into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ jsonrpc-core = "18.0.0"
log = "0.4.6"
parking_lot = "0.11.0"
rlp = "0.5"
secp256k1 = { version = "0.20", features = ["recovery"], optional = true }
serde = { version = "1.0.90", features = ["derive"] }
serde_json = "1.0.39"
tiny-keccak = { version = "2.0.1", features = ["keccak"] }
pin-project = "1.0"
# Optional deps
secp256k1 = { version = "0.20", features = ["recovery"], optional = true }
once_cell = { version = "1.8.0", optional = true }

## HTTP
base64 = { version = "0.13", optional = true }
bytes = { version = "1.0", optional = true }
Expand Down Expand Up @@ -73,7 +75,7 @@ http = ["_http_base"]
http-tls = ["http", "reqwest/default-tls"]
http-native-tls = ["http", "reqwest/native-tls"]
http-rustls-tls = ["http", "reqwest/rustls-tls"]
signing = ["secp256k1"]
signing = ["secp256k1", "once_cell"]
ws-tokio = ["soketto", "url", "tokio", "tokio-util"]
ws-async-std = ["soketto", "url", "async-std"]
ws-tls-tokio = ["async-native-tls", "async-native-tls/runtime-tokio", "ws-tokio"]
Expand Down
7 changes: 5 additions & 2 deletions src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ pub use feature_gated::*;
mod feature_gated {
use super::*;
use crate::types::Address;
use once_cell::sync::Lazy;
pub(crate) use secp256k1::SecretKey;
use secp256k1::{
recovery::{RecoverableSignature, RecoveryId},
Message, PublicKey, Secp256k1,
Message, PublicKey, Secp256k1, VerifyOnly,
};
use std::ops::Deref;

static CONTEXT: Lazy<Secp256k1<VerifyOnly>> = Lazy::new(Secp256k1::verification_only);

/// A trait representing ethereum-compatible key with signing capabilities.
///
/// The purpose of this trait is to prevent leaking `secp256k1::SecretKey` struct
Expand Down Expand Up @@ -125,7 +128,7 @@ mod feature_gated {
let recovery_id = RecoveryId::from_i32(recovery_id).map_err(|_| RecoveryError::InvalidSignature)?;
let signature =
RecoverableSignature::from_compact(&signature, recovery_id).map_err(|_| RecoveryError::InvalidSignature)?;
let public_key = Secp256k1::verification_only()
let public_key = CONTEXT
.recover(&message, &signature)
.map_err(|_| RecoveryError::InvalidSignature)?;

Expand Down