Skip to content

Commit

Permalink
CCCP-295, fix: use right encode
Browse files Browse the repository at this point in the history
  • Loading branch information
alstjd0921 committed Dec 17, 2024
1 parent f556126 commit c34a01a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
16 changes: 2 additions & 14 deletions client/src/eth/handlers/roundup_relay_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{sync::Arc, time::Duration};

use alloy::{
dyn_abi::DynSolValue,
network::{primitives::ReceiptResponse as _, AnyNetwork},
primitives::{Address, PrimitiveSignature, B256, U256},
providers::{fillers::TxFiller, Provider, WalletProvider},
Expand All @@ -25,7 +24,7 @@ use br_primitives::{
},
eth::{BootstrapState, RoundUpEventStatus},
tx::{TxRequestMetadata, VSPPhase2Metadata},
utils::{recover_message, sub_display_format},
utils::{encode_roundup_param, recover_message, sub_display_format},
};

use crate::eth::{
Expand Down Expand Up @@ -197,17 +196,6 @@ where
Ok(log.log_decode::<RoundUp>()?.inner.data)
}

/// Encodes the given round and new relayers to bytes.
fn encode_relayer_array(&self, round: U256, new_relayers: &[Address]) -> Vec<u8> {
DynSolValue::Tuple(vec![
DynSolValue::Uint(round, 256),
DynSolValue::Array(
new_relayers.iter().map(|address| DynSolValue::Address(*address)).collect(),
),
])
.abi_encode()
}

/// Get the submitted signatures of the updated round.
async fn get_sorted_signatures(
&self,
Expand All @@ -225,7 +213,7 @@ where

let mut signature_vec = Vec::<PrimitiveSignature>::from(signatures);
signature_vec
.sort_by_key(|k| recover_message(*k, &self.encode_relayer_array(round, new_relayers)));
.sort_by_key(|k| recover_message(*k, &encode_roundup_param(round, &new_relayers)));

Ok(Signatures::from(signature_vec))
}
Expand Down
11 changes: 2 additions & 9 deletions periodic/src/roundup_emitter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use alloy::{
dyn_abi::DynSolValue,
network::AnyNetwork,
primitives::{Address, U256},
providers::{fillers::TxFiller, Provider, WalletProvider},
Expand All @@ -25,7 +24,7 @@ use br_primitives::{
},
eth::{BootstrapState, RoundUpEventStatus},
tx::{TxRequestMetadata, VSPPhase1Metadata},
utils::sub_display_format,
utils::{encode_roundup_param, sub_display_format},
};
use eyre::Result;

Expand Down Expand Up @@ -157,13 +156,7 @@ where
round: U256,
new_relayers: Vec<Address>,
) -> Result<TransactionRequest> {
let encoded_msg = DynSolValue::Tuple(vec![
DynSolValue::Uint(round, 256),
DynSolValue::Array(
new_relayers.iter().map(|address| DynSolValue::Address(*address)).collect(),
),
])
.abi_encode();
let encoded_msg = encode_roundup_param(round, &new_relayers);

let sigs = Signatures::from(self.client.sign_message(&encoded_msg).await?);
let round_up_submit = Round_Up_Submit { round, new_relayers, sigs };
Expand Down
16 changes: 15 additions & 1 deletion primitives/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use alloy::primitives::{keccak256, Address, PrimitiveSignature, B256};
use alloy::{
dyn_abi::DynSolValue,
primitives::{keccak256, Address, PrimitiveSignature, B256, U256},
};
use k256::{ecdsa::VerifyingKey, elliptic_curve::sec1::ToEncodedPoint};
use rand::Rng as _;
use sha3::{Digest, Keccak256};
Expand All @@ -13,6 +16,17 @@ pub fn generate_delay() -> u64 {
rand::thread_rng().gen_range(0..=12000)
}

/// Encodes the given round and new relayers to bytes.
pub fn encode_roundup_param(round: U256, new_relayers: &[Address]) -> Vec<u8> {
DynSolValue::Tuple(vec![
DynSolValue::Uint(round, 256),
DynSolValue::Array(
new_relayers.iter().map(|address| DynSolValue::Address(*address)).collect(),
),
])
.abi_encode_params()
}

impl From<PrimitiveSignature> for EthereumSignature {
fn from(signature: PrimitiveSignature) -> Self {
let sig: [u8; 65] = signature.into();
Expand Down

0 comments on commit c34a01a

Please sign in to comment.