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

feat(sidecar): commit-boost integration #203

Merged
merged 19 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
82 changes: 80 additions & 2 deletions bolt-sidecar/src/client/commit_boost.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{str::FromStr, sync::Arc};

use alloy::{rpc::types::beacon::BlsSignature, signers::Signature};
use cb_common::{
Expand All @@ -13,7 +13,13 @@ use parking_lot::RwLock;
use thiserror::Error;
use tracing::{debug, error, info};

use crate::crypto::{bls::SignerBLS, ecdsa::SignerECDSA};
use crate::{
crypto::{
bls::{SignerBLS, BLS_DST_PREFIX},
ecdsa::SignerECDSA,
},
primitives::commitment::ECDSASignatureExt,
};

/// A client for interacting with CommitBoost.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -67,6 +73,40 @@ impl CommitBoostSigner {

Ok(client)
}

/// Get the consensus public key from the Commit-Boost signer.
pub fn get_consensus_pubkey(&self) -> CBBlsPublicKey {
*self.pubkeys.read().first().expect("consensus pubkey loaded")
}

/// Get the proxy ECDSA public key from the Commit-Boost signer.
pub fn get_proxy_ecdsa_pubkey(&self) -> EcdsaPublicKey {
*self.proxy_ecdsa.read().first().expect("proxy ecdsa key loaded")
}

/// Verify the BLS signature of the object with the given public key.
///
/// Note: The default implementation should be used where possible.
pub fn verify_bls(
&self,
data: &[u8; 32],
sig: &blst::min_pk::Signature,
pubkey: &blst::min_pk::PublicKey,
) -> bool {
sig.verify(false, data, BLS_DST_PREFIX, &[], pubkey, true) == blst::BLST_ERROR::BLST_SUCCESS
}

/// Verify the ECDSA signature of the object with the given public key.
///
/// Note: The default implementation should be used where possible.
pub fn verify_ecdsa(&self, data: &[u8; 32], sig: &Signature, pubkey: &EcdsaPublicKey) -> bool {
let sig = secp256k1::ecdsa::Signature::from_str(&sig.to_hex()).expect("signature is valid");
let pubkey =
secp256k1::PublicKey::from_slice(pubkey.as_ref()).expect("public key is valid");
secp256k1::Secp256k1::new()
.verify_ecdsa(&secp256k1::Message::from_digest(*data), &sig, &pubkey)
.is_ok()
}
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -101,3 +141,41 @@ impl SignerECDSA for CommitBoostSigner {
Ok(alloy_sig)
}
}

#[cfg(test)]
mod test {
use super::*;
use rand::Rng;

#[tokio::test]
async fn test_bls_commit_boost_signer() {
let signer =
CommitBoostSigner::new("http://localhost:19551".to_string(), "jwt_hex").await.unwrap();

namn-grg marked this conversation as resolved.
Show resolved Hide resolved
// Generate random data for the test
let mut rng = rand::thread_rng();
let mut data = [0u8; 32];
rng.fill(&mut data);

let signature = signer.sign(&data).await.unwrap();
let sig = blst::min_pk::Signature::from_bytes(signature.as_ref()).unwrap();
let pubkey = signer.get_consensus_pubkey();
let bls_pubkey = blst::min_pk::PublicKey::from_bytes(pubkey.as_ref()).unwrap();
assert!(signer.verify_bls(&data, &sig, &bls_pubkey));
}

#[tokio::test]
async fn test_ecdsa_commit_boost_signer() {
let signer =
CommitBoostSigner::new("http://localhost:19551".to_string(), "jwt_hex").await.unwrap();
let pubkey = signer.get_proxy_ecdsa_pubkey();

// Generate random data for the test
let mut rng = rand::thread_rng();
let mut data = [0u8; 32];
rng.fill(&mut data);

let signature = signer.sign_hash(&data).await.unwrap();
assert!(signer.verify_ecdsa(&data, &signature, &pubkey));
}
}
2 changes: 1 addition & 1 deletion bolt-sidecar/src/crypto/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub trait SignableBLS {
}
}

/// A generic signing trait to generate BLS signatures asynchronously.
/// A generic signing trait to generate BLS signatures.
#[async_trait::async_trait]
pub trait SignerBLS: Send + Debug {
/// Sign the given data and return the signature.
Expand Down
4 changes: 2 additions & 2 deletions bolt-sidecar/src/crypto/ecdsa.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Debug;

use alloy::signers::{local::PrivateKeySigner, Signature as AlloySignature};
use alloy::signers::{local::PrivateKeySigner, Signature as AlloySignature, Signer};
use secp256k1::{ecdsa::Signature, Message, PublicKey, SecretKey};

/// Trait for any types that can be signed and verified with ECDSA.
Expand Down Expand Up @@ -64,7 +64,7 @@ pub trait SignerECDSA: Send + Debug {
#[async_trait::async_trait]
impl SignerECDSA for PrivateKeySigner {
async fn sign_hash(&self, hash: &[u8; 32]) -> eyre::Result<AlloySignature> {
Ok(alloy::signers::Signer::sign_hash(self, hash.into()).await?)
Ok(Signer::sign_hash(self, hash.into()).await?)
}
}

Expand Down
Loading