Skip to content

Commit

Permalink
fix(sidecar): use compressed hash bytes20 instead of bytes32
Browse files Browse the repository at this point in the history
  • Loading branch information
thedevbirb committed Oct 30, 2024
1 parent 1831be7 commit 4a32a06
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
22 changes: 17 additions & 5 deletions bolt-sidecar/src/chain_io/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ impl BoltManager {
status.pubkeyHash
);
} else if status.operator != commitment_signer_pubkey {
bail!(
"mismatch between commitment signer public key and authorized operator address for validator with public key hash {:?} in Bolt.\n - commitment signer public key: {:?}\n - authorized operator address: {:?}",
bail!(generate_operator_keys_mismatch_error(
status.pubkeyHash,
commitment_signer_pubkey,
status.operator
);
));
}
}

Expand Down Expand Up @@ -101,21 +100,34 @@ impl BoltManager {
}
}

fn generate_operator_keys_mismatch_error(
pubkey_hash: CompressedHash,
commitment_signer_pubkey: Address,
operator: Address,
) -> String {
format!(
"mismatch between commitment signer public key and authorized operator address for validator with public key hash {:?} in Bolt.\n - commitment signer public key: {:?}\n - authorized operator address: {:?}",
pubkey_hash,
commitment_signer_pubkey,
operator
)
}

sol! {
#[allow(missing_docs)]
#[sol(rpc)]
interface BoltManagerContract {
#[derive(Debug, Default, Serialize)]
struct ProposerStatus {
bytes32 pubkeyHash;
bytes20 pubkeyHash;
bool active;
address operator;
string operatorRPC;
address[] collaterals;
uint256[] amounts;
}

function getProposerStatuses(bytes32[] calldata pubkeyHashes) public view returns (ProposerStatus[] memory statuses);
function getProposerStatuses(bytes20[] calldata pubkeyHashes) public view returns (ProposerStatus[] memory statuses);

function isOperator(address operator) external view returns (bool isOperator);

Expand Down
32 changes: 28 additions & 4 deletions bolt-sidecar/src/chain_io/utils.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
use alloy::primitives::{B256, B512};
use alloy::primitives::{FixedBytes, B512};
use ethereum_consensus::primitives::BlsPublicKey;
use reth_primitives::keccak256;

/// A 20-byte compressed hash of a BLS public key.
///
/// Reference: https://github.com/chainbound/bolt/blob/lore/feat/holesky-launch/bolt-contracts/script/holesky/validators/registervalidators.s.sol#l65-l69.
type CompressedHash = FixedBytes<20>;

/// Hash the public keys of the proposers. This follows the same
/// implementation done on-chain in the BoltValidators contract.
pub fn pubkey_hashes(keys: &[BlsPublicKey]) -> Vec<B256> {
pub fn pubkey_hashes(keys: &[BlsPublicKey]) -> Vec<CompressedHash> {
keys.iter().map(pubkey_hash).collect()
}

/// Hash the public key of the proposer. This follows the same
/// implementation done on-chain in the BoltValidators contract.
pub fn pubkey_hash(key: &BlsPublicKey) -> B256 {
///
/// Reference: https://github.com/chainbound/bolt/blob/lore/feat/holesky-launch/bolt-contracts/script/holesky/validators/registervalidators.s.sol#l65-l69
pub fn pubkey_hash(key: &BlsPublicKey) -> CompressedHash {
let digest = pubkey_hash_digest(key);
keccak256(digest)
let hash = keccak256(digest);
FixedBytes::<20>::from_slice(hash.get(0..20).expect("hash is longer than 20 bytes"))
}

fn pubkey_hash_digest(key: &BlsPublicKey) -> B512 {
Expand All @@ -26,3 +34,19 @@ fn pubkey_hash_digest(key: &BlsPublicKey) -> B512 {
onchain_pubkey_repr[16..].copy_from_slice(key);
onchain_pubkey_repr
}

#[cfg(test)]
mod tests {
use alloy::hex;
use ethereum_consensus::primitives::BlsPublicKey;

use super::pubkey_hash;

#[test]
fn test_public_key_hash() {
let bytes = hex!("87cbbfe6f08a0fd424507726cfcf5b9df2b2fd6b78a65a3d7bb6db946dca3102eb8abae32847d5a9a27e414888414c26").as_ref();
let bls_public_key = BlsPublicKey::try_from(bytes).expect("valid bls public key");
let hash = pubkey_hash(&bls_public_key);
assert_eq!(hex::encode(hash.as_slice()), "cf44d8bca49d695164be6796108cf788d8d056e1");
}
}

0 comments on commit 4a32a06

Please sign in to comment.