From f2e04fc97c4e80524443e4b9cde3d3d61754c2e2 Mon Sep 17 00:00:00 2001 From: Naman Garg <0708ng@gmail.com> Date: Wed, 25 Sep 2024 14:15:15 +0530 Subject: [PATCH 1/3] test(sidecar): generate data --- bolt-sidecar/src/crypto/bls.rs | 106 ++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/bolt-sidecar/src/crypto/bls.rs b/bolt-sidecar/src/crypto/bls.rs index 618f113d..03a3cbed 100644 --- a/bolt-sidecar/src/crypto/bls.rs +++ b/bolt-sidecar/src/crypto/bls.rs @@ -102,10 +102,11 @@ fn sign_with_prefix(key: &BlsSecretKey, data: &[u8]) -> Signature { #[cfg(test)] mod tests { use crate::{ - crypto::bls::{SignableBLS, Signer, SignerBLS}, - test_util::{test_bls_secret_key, TestSignableData}, + crypto::bls::{SignableBLS, Signer, SignerBLS}, primitives::{ConstraintsMessage, DelegationMessage, FullTransaction, InclusionRequest, RevocationMessage, SignedConstraints, SignedDelegation, SignedRevocation}, test_util::{test_bls_secret_key, TestSignableData} }; + use blst::min_pk::SecretKey; + use ethereum_consensus::crypto::{PublicKey, Signature}; use rand::Rng; #[tokio::test] @@ -124,4 +125,105 @@ mod tests { let sig = blst::min_pk::Signature::from_bytes(signature.as_ref()).unwrap(); assert!(signer.verify(&msg, &sig, &pubkey)); } + + fn random_constraints(count: usize) -> Vec { + // Random inclusion request + let json_req = r#"{ + "slot": 10, + "txs": ["0x02f86c870c72dd9d5e883e4d0183408f2382520894d2e2adf7177b7a8afddbc12d1634cf23ea1a71020180c001a08556dcfea479b34675db3fe08e29486fe719c2b22f6b0c1741ecbbdce4575cc6a01cd48009ccafd6b9f1290bbe2ceea268f94101d1d322c787018423ebcbc87ab4"] + }"#; + + let req: InclusionRequest = serde_json::from_str(json_req).unwrap(); + + (0..count).map(|_| req.txs.first().unwrap().clone()).collect() + } + + #[tokio::test] + async fn generate_test_data() { + let sk = test_bls_secret_key(); + let pk = sk.sk_to_pk(); + let signer = Signer::new(sk); + + println!("Validator Public Key: {}", hex::encode(pk.to_bytes())); + + // Generate a delegatee's BLS secret key and public key + let delegatee_ikm: [u8; 32] = rand::thread_rng().gen(); + let delegatee_sk = SecretKey::key_gen(&delegatee_ikm, &[]).expect("Failed to generate delegatee secret key"); + let delegatee_pk = delegatee_sk.sk_to_pk(); + + // Prepare a Delegation message + let delegation_msg = DelegationMessage { + validator_pubkey: PublicKey::try_from(pk.to_bytes().as_slice()).expect("Failed to convert validator public key"), + delegatee_pubkey: PublicKey::try_from(delegatee_pk.to_bytes().as_slice()).expect("Failed to convert delegatee public key"), + }; + + let delegation_bytes = serde_json::to_vec(&delegation_msg).unwrap(); + let temp = delegation_bytes.as_slice(); + let mut data = [0u8; 32]; + data.copy_from_slice(&temp[..32]); + let msg = TestSignableData { data }; + + // Sign the Delegation message + let delegation_signature = SignerBLS::sign(&signer, &msg.digest()).await.unwrap(); + + // Create SignedDelegation + let signed_delegation = SignedDelegation { + message: delegation_msg, + signature: Signature::try_from(delegation_signature.as_ref()).expect("Failed to convert delegation signature"), + }; + + // Output SignedDelegation + println!("{}", serde_json::to_string_pretty(&signed_delegation).unwrap()); + + // Prepare a revocation message + let revocation_msg = RevocationMessage { + validator_pubkey: PublicKey::try_from(pk.to_bytes().as_slice()).expect("Failed to convert validator public key"), + delegatee_pubkey: PublicKey::try_from(delegatee_pk.to_bytes().as_slice()).expect("Failed to convert delegatee public key"), + }; + + let revocation_bytes = serde_json::to_vec(&revocation_msg).unwrap(); + let temp = revocation_bytes.as_slice(); + let mut data = [0u8; 32]; + data.copy_from_slice(&temp[..32]); + let msg = TestSignableData { data }; + + // Sign the Revocation message + let revocation_signature = SignerBLS::sign(&signer, &msg.digest()).await.unwrap(); + + // Create SignedRevocation + let signed_revocation = SignedRevocation { + message: revocation_msg, + signature: Signature::try_from(revocation_signature.as_ref()).expect("Failed to convert revocation signature"), + }; + + // Output SignedRevocation + println!("{}", serde_json::to_string_pretty(&signed_revocation).unwrap()); + + let constraints = random_constraints(2); + + // Prepare a ConstraintsMessage + let constraints_msg = ConstraintsMessage { + pubkey: PublicKey::try_from(pk.to_bytes().as_slice()).expect("Failed to convert validator public key"), + slot: 1, + top: true, + constraints, + }; + + let constraints_bytes = serde_json::to_vec(&constraints_msg).unwrap(); + let temp = constraints_bytes.as_slice(); + let mut data = [0u8; 32]; + data.copy_from_slice(&temp[..32]); + + // Sign the ConstraintsMessage + let constraints_signature = SignerBLS::sign(&signer, &data).await.unwrap(); + + // Create SignedConstraints + let signed_constraints = SignedConstraints { + message: constraints_msg, + signature: constraints_signature, + }; + + // Output SignedConstraints + println!("{}", serde_json::to_string_pretty(&signed_constraints).unwrap()); + } } From fca43bcf23b431eb3a64e13305c275d96409c0fa Mon Sep 17 00:00:00 2001 From: Naman Garg <0708ng@gmail.com> Date: Wed, 25 Sep 2024 15:51:10 +0530 Subject: [PATCH 2/3] fix(sidecar): constraints -> transactions --- bolt-sidecar/src/builder/template.rs | 18 ++++++------- bolt-sidecar/src/crypto/bls.rs | 31 ++++++++--------------- bolt-sidecar/src/driver.rs | 2 +- bolt-sidecar/src/primitives/constraint.rs | 18 ++++++------- bolt-sidecar/src/primitives/mod.rs | 27 +++++++++++++++++++- 5 files changed, 55 insertions(+), 41 deletions(-) diff --git a/bolt-sidecar/src/builder/template.rs b/bolt-sidecar/src/builder/template.rs index b9d8df82..6d3b5735 100644 --- a/bolt-sidecar/src/builder/template.rs +++ b/bolt-sidecar/src/builder/template.rs @@ -42,7 +42,7 @@ impl BlockTemplate { /// Returns the cloned list of transactions from the constraints. #[inline] pub fn transactions(&self) -> Vec { - self.signed_constraints_list.iter().flat_map(|sc| sc.message.constraints.clone()).collect() + self.signed_constraints_list.iter().flat_map(|sc| sc.message.transactions.clone()).collect() } /// Converts the list of signed constraints into a list of signed transactions. Use this when @@ -52,7 +52,7 @@ impl BlockTemplate { self.signed_constraints_list .iter() .flat_map(|sc| { - sc.message.constraints.iter().map(|c| c.clone().into_inner().into_transaction()) + sc.message.transactions.iter().map(|c| c.clone().into_inner().into_transaction()) }) .collect() } @@ -64,7 +64,7 @@ impl BlockTemplate { let (commitments, proofs, blobs) = self.signed_constraints_list .iter() - .flat_map(|sc| sc.message.constraints.iter()) + .flat_map(|sc| sc.message.transactions.iter()) .filter_map(|c| c.blob_sidecar()) .fold( (Vec::new(), Vec::new(), Vec::new()), @@ -90,14 +90,14 @@ impl BlockTemplate { /// Returns the length of the transactions in the block template. #[inline] pub fn transactions_len(&self) -> usize { - self.signed_constraints_list.iter().fold(0, |acc, sc| acc + sc.message.constraints.len()) + self.signed_constraints_list.iter().fold(0, |acc, sc| acc + sc.message.transactions.len()) } /// Returns the committed gas in the block template. #[inline] pub fn committed_gas(&self) -> u64 { self.signed_constraints_list.iter().fold(0, |acc, sc| { - acc + sc.message.constraints.iter().fold(0, |acc, c| acc + c.gas_limit()) + acc + sc.message.transactions.iter().fold(0, |acc, c| acc + c.gas_limit()) }) } @@ -105,7 +105,7 @@ impl BlockTemplate { #[inline] pub fn blob_count(&self) -> usize { self.signed_constraints_list.iter().fold(0, |mut acc, sc| { - acc += sc.message.constraints.iter().fold(0, |acc, c| { + acc += sc.message.transactions.iter().fold(0, |acc, c| { acc + c.as_eip4844().map(|tx| tx.blob_versioned_hashes.len()).unwrap_or(0) }); @@ -115,7 +115,7 @@ impl BlockTemplate { /// Adds a list of constraints to the block template and updates the state diff. pub fn add_constraints(&mut self, constraints: SignedConstraints) { - for constraint in constraints.message.constraints.iter() { + for constraint in constraints.message.transactions.iter() { let max_cost = max_transaction_cost(constraint); self.state_diff .diffs @@ -134,7 +134,7 @@ impl BlockTemplate { fn remove_constraints_at_index(&mut self, index: usize) { let constraints = self.signed_constraints_list.remove(index); - for constraint in constraints.message.constraints.iter() { + for constraint in constraints.message.transactions.iter() { self.state_diff .diffs .entry(*constraint.sender().expect("recovered sender")) @@ -155,7 +155,7 @@ impl BlockTemplate { .signed_constraints_list .iter() .enumerate() - .map(|(idx, c)| (idx, &c.message.constraints)) + .map(|(idx, c)| (idx, &c.message.transactions)) .filter(|(_idx, c)| c.iter().any(|c| c.sender().expect("recovered sender") == &address)) .map(|(idx, c)| { ( diff --git a/bolt-sidecar/src/crypto/bls.rs b/bolt-sidecar/src/crypto/bls.rs index 03a3cbed..e1b82182 100644 --- a/bolt-sidecar/src/crypto/bls.rs +++ b/bolt-sidecar/src/crypto/bls.rs @@ -157,14 +157,10 @@ mod tests { delegatee_pubkey: PublicKey::try_from(delegatee_pk.to_bytes().as_slice()).expect("Failed to convert delegatee public key"), }; - let delegation_bytes = serde_json::to_vec(&delegation_msg).unwrap(); - let temp = delegation_bytes.as_slice(); - let mut data = [0u8; 32]; - data.copy_from_slice(&temp[..32]); - let msg = TestSignableData { data }; + let digest = SignableBLS::digest(&delegation_msg); // Sign the Delegation message - let delegation_signature = SignerBLS::sign(&signer, &msg.digest()).await.unwrap(); + let delegation_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); // Create SignedDelegation let signed_delegation = SignedDelegation { @@ -181,14 +177,10 @@ mod tests { delegatee_pubkey: PublicKey::try_from(delegatee_pk.to_bytes().as_slice()).expect("Failed to convert delegatee public key"), }; - let revocation_bytes = serde_json::to_vec(&revocation_msg).unwrap(); - let temp = revocation_bytes.as_slice(); - let mut data = [0u8; 32]; - data.copy_from_slice(&temp[..32]); - let msg = TestSignableData { data }; + let digest = SignableBLS::digest(&revocation_msg); // Sign the Revocation message - let revocation_signature = SignerBLS::sign(&signer, &msg.digest()).await.unwrap(); + let revocation_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); // Create SignedRevocation let signed_revocation = SignedRevocation { @@ -199,23 +191,20 @@ mod tests { // Output SignedRevocation println!("{}", serde_json::to_string_pretty(&signed_revocation).unwrap()); - let constraints = random_constraints(2); + let transactions = random_constraints(2); // Prepare a ConstraintsMessage let constraints_msg = ConstraintsMessage { pubkey: PublicKey::try_from(pk.to_bytes().as_slice()).expect("Failed to convert validator public key"), - slot: 1, - top: true, - constraints, + slot: 2, + top: false, + transactions, }; - let constraints_bytes = serde_json::to_vec(&constraints_msg).unwrap(); - let temp = constraints_bytes.as_slice(); - let mut data = [0u8; 32]; - data.copy_from_slice(&temp[..32]); + let digest = SignableBLS::digest(&constraints_msg); // Sign the ConstraintsMessage - let constraints_signature = SignerBLS::sign(&signer, &data).await.unwrap(); + let constraints_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); // Create SignedConstraints let signed_constraints = SignedConstraints { diff --git a/bolt-sidecar/src/driver.rs b/bolt-sidecar/src/driver.rs index 6cd3520a..99a37912 100644 --- a/bolt-sidecar/src/driver.rs +++ b/bolt-sidecar/src/driver.rs @@ -231,7 +231,7 @@ impl SidecarDriver, + pub transactions: Vec, } impl ConstraintsMessage { /// Builds a constraints message from an inclusion request and metadata pub fn build(pubkey: BlsPublicKey, request: InclusionRequest) -> Self { - let constraints = request.txs; + let transactions = request.txs; - Self { pubkey, slot: request.slot, top: false, constraints } + Self { pubkey, slot: request.slot, top: false, transactions } } } @@ -79,7 +79,7 @@ impl SignableBLS for ConstraintsMessage { hasher.update(self.slot.to_le_bytes()); hasher.update((self.top as u8).to_le_bytes()); - for constraint in &self.constraints { + for constraint in &self.transactions { hasher.update(constraint.hash()); } @@ -114,10 +114,10 @@ mod tests { let pubkey = BlsPublicKey::default(); let slot = 0; let top = false; - let constraints = random_constraints(1); // Generate 'n' random constraints + let transactions = random_constraints(1); // Generate 'n' random constraints // Create a random `ConstraintsMessage` - let message = ConstraintsMessage { pubkey, slot, top, constraints }; + let message = ConstraintsMessage { pubkey, slot, top, transactions }; // Compute tree hash root let digest = SignableBLS::digest(&message); @@ -134,10 +134,10 @@ mod tests { let pubkey = BlsPublicKey::default(); let slot = random_u64(&mut rng); let top = false; - let constraints = random_constraints(2); // Generate 'n' random constraints + let transactions = random_constraints(2); // Generate 'n' random constraints // Create a random `ConstraintsMessage` - let message = ConstraintsMessage { pubkey, slot, top, constraints }; + let message = ConstraintsMessage { pubkey, slot, top, transactions }; // Serialize the `ConstraintsMessage` to JSON let json = serde_json::to_string(&message).unwrap(); diff --git a/bolt-sidecar/src/primitives/mod.rs b/bolt-sidecar/src/primitives/mod.rs index e705e698..cc459791 100644 --- a/bolt-sidecar/src/primitives/mod.rs +++ b/bolt-sidecar/src/primitives/mod.rs @@ -6,7 +6,10 @@ use std::{ sync::{atomic::AtomicU64, Arc}, }; -use alloy::primitives::{Address, U256}; +use alloy::{ + primitives::{Address, U256}, + signers::k256::sha2::{Digest, Sha256} +}; use ethereum_consensus::{ crypto::KzgCommitment, deneb::{ @@ -36,6 +39,8 @@ pub mod constraint; pub use constraint::{BatchedSignedConstraints, ConstraintsMessage, SignedConstraints}; use tracing::{error, info}; +use crate::crypto::SignableBLS; + /// An alias for a Beacon Chain slot number pub type Slot = u64; @@ -430,6 +435,16 @@ pub struct DelegationMessage { pub delegatee_pubkey: BlsPublicKey, } +impl SignableBLS for DelegationMessage { + fn digest(&self) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(&self.validator_pubkey.to_vec()); + hasher.update(&self.delegatee_pubkey.to_vec()); + + hasher.finalize().into() + } +} + #[derive(Debug, Clone, Serialize)] pub struct SignedRevocation { pub message: RevocationMessage, @@ -441,3 +456,13 @@ pub struct RevocationMessage { pub validator_pubkey: BlsPublicKey, pub delegatee_pubkey: BlsPublicKey, } + +impl SignableBLS for RevocationMessage { + fn digest(&self) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(&self.validator_pubkey.to_vec()); + hasher.update(&self.delegatee_pubkey.to_vec()); + + hasher.finalize().into() + } +} \ No newline at end of file From 166f7649d60334daae544663e0ea35740d44aa9b Mon Sep 17 00:00:00 2001 From: Naman Garg <0708ng@gmail.com> Date: Thu, 26 Sep 2024 14:50:38 +0530 Subject: [PATCH 3/3] chore(sidecar): move test to test util --- bolt-sidecar/src/crypto/bls.rs | 95 +------------------ bolt-sidecar/src/primitives/constraint.rs | 4 +- bolt-sidecar/src/primitives/mod.rs | 8 +- bolt-sidecar/src/test_util.rs | 109 +++++++++++++++++++++- 4 files changed, 115 insertions(+), 101 deletions(-) diff --git a/bolt-sidecar/src/crypto/bls.rs b/bolt-sidecar/src/crypto/bls.rs index e1b82182..618f113d 100644 --- a/bolt-sidecar/src/crypto/bls.rs +++ b/bolt-sidecar/src/crypto/bls.rs @@ -102,11 +102,10 @@ fn sign_with_prefix(key: &BlsSecretKey, data: &[u8]) -> Signature { #[cfg(test)] mod tests { use crate::{ - crypto::bls::{SignableBLS, Signer, SignerBLS}, primitives::{ConstraintsMessage, DelegationMessage, FullTransaction, InclusionRequest, RevocationMessage, SignedConstraints, SignedDelegation, SignedRevocation}, test_util::{test_bls_secret_key, TestSignableData} + crypto::bls::{SignableBLS, Signer, SignerBLS}, + test_util::{test_bls_secret_key, TestSignableData}, }; - use blst::min_pk::SecretKey; - use ethereum_consensus::crypto::{PublicKey, Signature}; use rand::Rng; #[tokio::test] @@ -125,94 +124,4 @@ mod tests { let sig = blst::min_pk::Signature::from_bytes(signature.as_ref()).unwrap(); assert!(signer.verify(&msg, &sig, &pubkey)); } - - fn random_constraints(count: usize) -> Vec { - // Random inclusion request - let json_req = r#"{ - "slot": 10, - "txs": ["0x02f86c870c72dd9d5e883e4d0183408f2382520894d2e2adf7177b7a8afddbc12d1634cf23ea1a71020180c001a08556dcfea479b34675db3fe08e29486fe719c2b22f6b0c1741ecbbdce4575cc6a01cd48009ccafd6b9f1290bbe2ceea268f94101d1d322c787018423ebcbc87ab4"] - }"#; - - let req: InclusionRequest = serde_json::from_str(json_req).unwrap(); - - (0..count).map(|_| req.txs.first().unwrap().clone()).collect() - } - - #[tokio::test] - async fn generate_test_data() { - let sk = test_bls_secret_key(); - let pk = sk.sk_to_pk(); - let signer = Signer::new(sk); - - println!("Validator Public Key: {}", hex::encode(pk.to_bytes())); - - // Generate a delegatee's BLS secret key and public key - let delegatee_ikm: [u8; 32] = rand::thread_rng().gen(); - let delegatee_sk = SecretKey::key_gen(&delegatee_ikm, &[]).expect("Failed to generate delegatee secret key"); - let delegatee_pk = delegatee_sk.sk_to_pk(); - - // Prepare a Delegation message - let delegation_msg = DelegationMessage { - validator_pubkey: PublicKey::try_from(pk.to_bytes().as_slice()).expect("Failed to convert validator public key"), - delegatee_pubkey: PublicKey::try_from(delegatee_pk.to_bytes().as_slice()).expect("Failed to convert delegatee public key"), - }; - - let digest = SignableBLS::digest(&delegation_msg); - - // Sign the Delegation message - let delegation_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); - - // Create SignedDelegation - let signed_delegation = SignedDelegation { - message: delegation_msg, - signature: Signature::try_from(delegation_signature.as_ref()).expect("Failed to convert delegation signature"), - }; - - // Output SignedDelegation - println!("{}", serde_json::to_string_pretty(&signed_delegation).unwrap()); - - // Prepare a revocation message - let revocation_msg = RevocationMessage { - validator_pubkey: PublicKey::try_from(pk.to_bytes().as_slice()).expect("Failed to convert validator public key"), - delegatee_pubkey: PublicKey::try_from(delegatee_pk.to_bytes().as_slice()).expect("Failed to convert delegatee public key"), - }; - - let digest = SignableBLS::digest(&revocation_msg); - - // Sign the Revocation message - let revocation_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); - - // Create SignedRevocation - let signed_revocation = SignedRevocation { - message: revocation_msg, - signature: Signature::try_from(revocation_signature.as_ref()).expect("Failed to convert revocation signature"), - }; - - // Output SignedRevocation - println!("{}", serde_json::to_string_pretty(&signed_revocation).unwrap()); - - let transactions = random_constraints(2); - - // Prepare a ConstraintsMessage - let constraints_msg = ConstraintsMessage { - pubkey: PublicKey::try_from(pk.to_bytes().as_slice()).expect("Failed to convert validator public key"), - slot: 2, - top: false, - transactions, - }; - - let digest = SignableBLS::digest(&constraints_msg); - - // Sign the ConstraintsMessage - let constraints_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); - - // Create SignedConstraints - let signed_constraints = SignedConstraints { - message: constraints_msg, - signature: constraints_signature, - }; - - // Output SignedConstraints - println!("{}", serde_json::to_string_pretty(&signed_constraints).unwrap()); - } } diff --git a/bolt-sidecar/src/primitives/constraint.rs b/bolt-sidecar/src/primitives/constraint.rs index 6c1709aa..a3e0bc37 100644 --- a/bolt-sidecar/src/primitives/constraint.rs +++ b/bolt-sidecar/src/primitives/constraint.rs @@ -79,8 +79,8 @@ impl SignableBLS for ConstraintsMessage { hasher.update(self.slot.to_le_bytes()); hasher.update((self.top as u8).to_le_bytes()); - for constraint in &self.transactions { - hasher.update(constraint.hash()); + for tx in &self.transactions { + hasher.update(tx.hash()); } hasher.finalize().into() diff --git a/bolt-sidecar/src/primitives/mod.rs b/bolt-sidecar/src/primitives/mod.rs index cc459791..d9cfbec7 100644 --- a/bolt-sidecar/src/primitives/mod.rs +++ b/bolt-sidecar/src/primitives/mod.rs @@ -8,7 +8,7 @@ use std::{ use alloy::{ primitives::{Address, U256}, - signers::k256::sha2::{Digest, Sha256} + signers::k256::sha2::{Digest, Sha256}, }; use ethereum_consensus::{ crypto::KzgCommitment, @@ -440,7 +440,7 @@ impl SignableBLS for DelegationMessage { let mut hasher = Sha256::new(); hasher.update(&self.validator_pubkey.to_vec()); hasher.update(&self.delegatee_pubkey.to_vec()); - + hasher.finalize().into() } } @@ -462,7 +462,7 @@ impl SignableBLS for RevocationMessage { let mut hasher = Sha256::new(); hasher.update(&self.validator_pubkey.to_vec()); hasher.update(&self.delegatee_pubkey.to_vec()); - + hasher.finalize().into() } -} \ No newline at end of file +} diff --git a/bolt-sidecar/src/test_util.rs b/bolt-sidecar/src/test_util.rs index c03bad87..ad599b12 100644 --- a/bolt-sidecar/src/test_util.rs +++ b/bolt-sidecar/src/test_util.rs @@ -11,13 +11,18 @@ use alloy::{ }; use alloy_node_bindings::{Anvil, AnvilInstance}; use blst::min_pk::SecretKey; +use ethereum_consensus::crypto::{PublicKey, Signature}; +use rand::Rng; use reth_primitives::PooledTransactionsElement; use secp256k1::Message; use tracing::warn; use crate::{ - crypto::{ecdsa::SignableECDSA, SignableBLS}, - primitives::{CommitmentRequest, FullTransaction, InclusionRequest}, + crypto::{bls::Signer as BlsSigner, ecdsa::SignableECDSA, SignableBLS, SignerBLS}, + primitives::{ + CommitmentRequest, ConstraintsMessage, DelegationMessage, FullTransaction, + InclusionRequest, RevocationMessage, SignedConstraints, SignedDelegation, SignedRevocation, + }, Config, }; @@ -170,3 +175,103 @@ pub(crate) async fn create_signed_commitment_request( Ok(CommitmentRequest::Inclusion(request)) } + +fn random_constraints(count: usize) -> Vec { + // Random inclusion request + let json_req = r#"{ + "slot": 10, + "txs": [ + "0x02f86c870c72dd9d5e883e4d0183408f2382520894d2e2adf7177b7a8afddbc12d1634cf23ea1a71020180c001a08556dcfea479b34675db3fe08e29486fe719c2b22f6b0c1741ecbbdce4575cc6a01cd48009ccafd6b9f1290bbe2ceea268f94101d1d322c787018423ebcbc87ab4", + "0x02f9017b8501a2140cff8303dec685012a05f2008512a05f2000830249f094843669e5220036eddbaca89d8c8b5b82268a0fc580b901040cc7326300000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000022006292538e66f0000000000000000000000005ba38f2c245618e39f6fa067bf1dec304e73ff3c00000000000000000000000092f0ee29e6e1bf0f7c668317ada78f5774a6cb7f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000003fac6482aee49bf58515be2d3fb58378a8497cc9000000000000000000000000c6cc140787b02ae479a10e41169607000c0d44f6c080a00cf74c45dbe9ee1fb923118ec5ce9db8f88cd651196ed3f9d4f8f2a65827e611a04a6bc1d49a7e18b7c92e8f3614cae116b1832ceb311c81d54b2c87de1545f68f", + "0x02f8708501a2140cff82012f800782520894b6c402298fcb88039bbfde70f5ace791f18cfac88707131d70870dc880c080a03aab1b17ecf28f85de43c7733611759b87d25ba885babacb6b4c625d715415eea03fb52cb7744ccb885906e42f6b9cf82e74b47a4b4b4072af2aa52a8dc472236e" + ] + }"#; + + let req: InclusionRequest = serde_json::from_str(json_req).unwrap(); + + req.txs.iter().cloned().take(count).collect() +} + +#[tokio::test] +async fn generate_test_data() { + let sk = test_bls_secret_key(); + let pk = sk.sk_to_pk(); + let signer = BlsSigner::new(sk); + + println!("Validator Public Key: {}", hex::encode(pk.to_bytes())); + + // Generate a delegatee's BLS secret key and public key + let delegatee_ikm: [u8; 32] = rand::thread_rng().gen(); + let delegatee_sk = + SecretKey::key_gen(&delegatee_ikm, &[]).expect("Failed to generate delegatee secret key"); + let delegatee_pk = delegatee_sk.sk_to_pk(); + + // Prepare a Delegation message + let delegation_msg = DelegationMessage { + validator_pubkey: PublicKey::try_from(pk.to_bytes().as_slice()) + .expect("Failed to convert validator public key"), + delegatee_pubkey: PublicKey::try_from(delegatee_pk.to_bytes().as_slice()) + .expect("Failed to convert delegatee public key"), + }; + + let digest = SignableBLS::digest(&delegation_msg); + + // Sign the Delegation message + let delegation_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); + + // Create SignedDelegation + let signed_delegation = SignedDelegation { + message: delegation_msg, + signature: Signature::try_from(delegation_signature.as_ref()) + .expect("Failed to convert delegation signature"), + }; + + // Output SignedDelegation + println!("{}", serde_json::to_string_pretty(&signed_delegation).unwrap()); + + // Prepare a revocation message + let revocation_msg = RevocationMessage { + validator_pubkey: PublicKey::try_from(pk.to_bytes().as_slice()) + .expect("Failed to convert validator public key"), + delegatee_pubkey: PublicKey::try_from(delegatee_pk.to_bytes().as_slice()) + .expect("Failed to convert delegatee public key"), + }; + + let digest = SignableBLS::digest(&revocation_msg); + + // Sign the Revocation message + let revocation_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); + + // Create SignedRevocation + let signed_revocation = SignedRevocation { + message: revocation_msg, + signature: Signature::try_from(revocation_signature.as_ref()) + .expect("Failed to convert revocation signature"), + }; + + // Output SignedRevocation + println!("{}", serde_json::to_string_pretty(&signed_revocation).unwrap()); + + let transactions = random_constraints(1); + + // Prepare a ConstraintsMessage + let constraints_msg = ConstraintsMessage { + pubkey: PublicKey::try_from(pk.to_bytes().as_slice()) + .expect("Failed to convert validator public key"), + slot: 32, + top: true, + transactions, + }; + + let digest = SignableBLS::digest(&constraints_msg); + + // Sign the ConstraintsMessage + let constraints_signature = SignerBLS::sign(&signer, &digest).await.unwrap(); + + // Create SignedConstraints + let signed_constraints = + SignedConstraints { message: constraints_msg, signature: constraints_signature }; + + // Output SignedConstraints + println!("{}", serde_json::to_string_pretty(&signed_constraints).unwrap()); +}