Skip to content

Commit

Permalink
better import for libsecp256k1
Browse files Browse the repository at this point in the history
  • Loading branch information
rockcoolsaint committed May 11, 2024
1 parent f951edd commit b39684d
Show file tree
Hide file tree
Showing 9 changed files with 1,029 additions and 26 deletions.
4 changes: 2 additions & 2 deletions benches/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ repository = "https://github.com/stratum-mining/stratum"

[dependencies]
bitcoin = {version="0.29.1",optional=true}
secp256k1 = { version = "0.28.2", default-features = false, features =["alloc","rand","rand-std"] }
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
//! and manage the shared dependencies and utils across stratum crates.
#[cfg(feature = "bitcoin")]
pub use bitcoin;
pub use secp256k1;
1 change: 0 additions & 1 deletion roles/jd-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@ serde_json = { version = "1.0", default-features = false, features = ["alloc","r
serde = { version = "1.0.89", features = ["derive", "alloc"], default-features = false }
hashbrown = { version = "0.11", default-features = false, features = ["ahash", "serde"] }
key-utils = { version = "^1.0.0", path = "../../utils/key-utils" }
secp256k1 = { version = "0.28.2", default-features = false, features =["alloc","rand","rand-std"] }
rpc_sv2 = { version = "1.0.0", path = "../roles-utils/rpc" }
hex = "0.4.3"
13 changes: 3 additions & 10 deletions roles/jd-server/src/lib/job_declarator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use async_channel::{Receiver, Sender};
use binary_sv2::{B0255, U256};
use codec_sv2::{Frame, HandshakeRole, Responder};
use error_handling::handle_result;
use key_utils::{Secp256k1PublicKey, Secp256k1SecretKey};
use key_utils::{Secp256k1PublicKey, Secp256k1SecretKey, SignatureService};
use network_helpers_sv2::noise_connection_tokio::Connection;
use nohash_hasher::BuildNoHashHasher;
use roles_logic_sv2::{
Expand All @@ -14,7 +14,6 @@ use roles_logic_sv2::{
parsers::{JobDeclaration, PoolMessages as JdsMessages},
utils::{Id, Mutex},
};
use secp256k1::{Keypair, Message as SecpMessage, Secp256k1};
use std::{collections::HashMap, convert::TryInto, sync::Arc};
use tokio::{net::TcpListener, time::Duration};
use tracing::{debug, error, info};
Expand Down Expand Up @@ -395,15 +394,9 @@ pub fn signed_token(
_pub_key: &Secp256k1PublicKey,
prv_key: &Secp256k1SecretKey,
) -> B0255<'static> {
let secp = Secp256k1::signing_only();
let secp = SignatureService::new();

// Create the SecretKey and PublicKey instances
let secret_key = prv_key.0;
let kp = Keypair::from_secret_key(&secp, &secret_key);

let message: Vec<u8> = tx_hash_list_hash.to_vec();

let signature = secp.sign_schnorr(&SecpMessage::from_digest_slice(&message).unwrap(), &kp);
let signature = secp.sign(tx_hash_list_hash.to_vec(), prv_key.0);

// Sign message
signature.as_ref().to_vec().try_into().unwrap()
Expand Down
1 change: 0 additions & 1 deletion roles/pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ async-recursion = "1.0.0"
error_handling = { version = "1.0.0", path = "../../utils/error-handling" }
nohash-hasher = "0.2.0"
key-utils = { version = "^1.0.0", path = "../../utils/key-utils" }
secp256k1 = { version = "0.28.2", default-features = false, features =["alloc","rand","rand-std"] }

[dev-dependencies]
hex = "0.4.3"
Expand Down
16 changes: 5 additions & 11 deletions roles/pool/src/lib/mining_pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use async_channel::{Receiver, Sender};
use binary_sv2::U256;
use codec_sv2::{Frame, HandshakeRole, Responder, StandardEitherFrame, StandardSv2Frame};
use error_handling::handle_result;
use key_utils::{Secp256k1PublicKey, Secp256k1SecretKey};
use key_utils::{Secp256k1PublicKey, Secp256k1SecretKey, SignatureService};
use network_helpers_sv2::noise_connection_tokio::Connection;
use nohash_hasher::BuildNoHashHasher;
use roles_logic_sv2::{
Expand All @@ -29,6 +29,7 @@ use std::{
sync::Arc,
};
use stratum_common::bitcoin::{Script, TxOut};
use stratum_common::secp256k1;
use tokio::{net::TcpListener, task};
use tracing::{debug, error, info, warn};

Expand Down Expand Up @@ -283,18 +284,11 @@ pub fn verify_token(
signature: secp256k1::schnorr::Signature,
pub_key: key_utils::Secp256k1PublicKey,
) -> Result<(), secp256k1::Error> {
let secp = secp256k1::Secp256k1::verification_only();
// Create PublicKey instance
let x_only_public_key = pub_key.0;

let message: Vec<u8> = tx_hash_list_hash.to_vec();

// Verify signature
let is_verified = secp.verify_schnorr(
&signature,
&secp256k1::Message::from_digest_slice(&message)?,
&x_only_public_key,
);
let secp = SignatureService::new();

let is_verified = secp.verify(tx_hash_list_hash.to_vec(), signature, pub_key.0);

// debug
debug!("Message: {}", std::str::from_utf8(&message).unwrap());
Expand Down
Loading

0 comments on commit b39684d

Please sign in to comment.