Skip to content

Commit b1af5fa

Browse files
committed
update code to latest flashtestations contract changes
1 parent 7991456 commit b1af5fa

File tree

4 files changed

+60
-64
lines changed

4 files changed

+60
-64
lines changed

crates/op-rbuilder/src/flashtestations/builder_tx.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use crate::{
3838

3939
pub struct FlashtestationsBuilderTxArgs {
4040
pub attestation: Vec<u8>,
41+
pub extra_registration_data: Bytes,
4142
pub tee_service_signer: Signer,
4243
pub funding_key: Signer,
4344
pub funding_amount: U256,
@@ -52,6 +53,8 @@ pub struct FlashtestationsBuilderTxArgs {
5253
pub struct FlashtestationsBuilderTx {
5354
// Attestation for the builder
5455
attestation: Vec<u8>,
56+
// Extra registration data for the builder
57+
extra_registration_data: Bytes,
5558
// TEE service generated key
5659
tee_service_signer: Signer,
5760
// Funding key for the TEE signer
@@ -83,6 +86,7 @@ impl FlashtestationsBuilderTx {
8386
pub fn new(args: FlashtestationsBuilderTxArgs) -> Self {
8487
Self {
8588
attestation: args.attestation,
89+
extra_registration_data: args.extra_registration_data,
8690
tee_service_signer: args.tee_service_signer,
8791
funding_key: args.funding_key,
8892
funding_amount: args.funding_amount,
@@ -128,6 +132,7 @@ impl FlashtestationsBuilderTx {
128132
let quote_bytes = Bytes::from(attestation);
129133
let calldata = IFlashtestationRegistry::registerTEEServiceCall {
130134
rawQuote: quote_bytes,
135+
extendedRegistrationData: self.extra_registration_data.clone(),
131136
}
132137
.abi_encode();
133138

@@ -438,7 +443,7 @@ impl FlashtestationsBuilderTx {
438443
))
439444
}
440445
} else if let Some(FlashtestationRevertReason::FlashtestationRegistry(
441-
FlashtestationRegistryError::TEEServiceAlreadyRegistered(_, _),
446+
FlashtestationRegistryError::TEEServiceAlreadyRegistered(_),
442447
)) = revert_reason
443448
{
444449
Ok((None, true))

crates/op-rbuilder/src/flashtestations/mod.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use alloy_sol_types::{sol, SolError};
44
sol!(
55
#[sol(rpc, abi)]
66
interface IFlashtestationRegistry {
7-
function registerTEEService(bytes calldata rawQuote) external;
7+
function registerTEEService(bytes calldata rawQuote, bytes calldata extendedRegistrationData) external;
88
}
99

1010
#[sol(rpc, abi)]
@@ -21,18 +21,20 @@ sol!(
2121

2222
type WorkloadId is bytes32;
2323

24-
event TEEServiceRegistered(
25-
address teeAddress, WorkloadId workloadId, bytes rawQuote, bytes publicKey, bool alreadyExists
26-
);
24+
event TEEServiceRegistered(address teeAddress, bytes rawQuote, bool alreadyExists);
2725

2826
event BlockBuilderProofVerified(
2927
address caller, WorkloadId workloadId, uint256 blockNumber, uint8 version, bytes32 blockContentHash
3028
);
3129

3230
// FlashtestationRegistry errors
3331
error InvalidQuote(bytes output);
34-
error TEEServiceAlreadyRegistered(address teeAddress, WorkloadId workloadId);
32+
error TEEServiceAlreadyRegistered(address teeAddress);
33+
error InvalidRegistrationDataHash(bytes32 expected, bytes32 received);
3534
error SenderMustMatchTEEAddress(address sender, address teeAddress);
35+
error ByteSizeExceeded(uint256 size);
36+
37+
// QuoteParser errors
3638
error InvalidTEEType(bytes4 teeType);
3739
error InvalidTEEVersion(uint16 version);
3840
error InvalidReportDataLength(uint256 length);
@@ -61,8 +63,12 @@ pub enum FlashtestationRevertReason {
6163
pub enum FlashtestationRegistryError {
6264
#[error("invalid quote: {0}")]
6365
InvalidQuote(Bytes),
64-
#[error("tee address {0} already registered with workload id {1}")]
65-
TEEServiceAlreadyRegistered(Address, B256),
66+
#[error("tee address {0} already registered")]
67+
TEEServiceAlreadyRegistered(Address),
68+
#[error("invalid registration data hash: expected {0}, received {1}")]
69+
InvalidRegistrationDataHash(B256, B256),
70+
#[error("byte size exceeded: {0}")]
71+
ByteSizeExceeded(U256),
6672
#[error("sender address {0} must match quote tee address {1}")]
6773
SenderMustMatchTEEAddress(Address, Address),
6874
#[error("invalid tee type: {0}")]
@@ -95,14 +101,20 @@ impl From<Bytes> for FlashtestationRegistryError {
95101
return FlashtestationRegistryError::InvalidQuote(output);
96102
}
97103

98-
if let Ok(TEEServiceAlreadyRegistered {
99-
teeAddress,
100-
workloadId,
101-
}) = TEEServiceAlreadyRegistered::abi_decode(&value)
104+
if let Ok(TEEServiceAlreadyRegistered { teeAddress }) =
105+
TEEServiceAlreadyRegistered::abi_decode(&value)
102106
{
103-
return FlashtestationRegistryError::TEEServiceAlreadyRegistered(
104-
teeAddress, workloadId,
105-
);
107+
return FlashtestationRegistryError::TEEServiceAlreadyRegistered(teeAddress);
108+
}
109+
110+
if let Ok(InvalidRegistrationDataHash { expected, received }) =
111+
InvalidRegistrationDataHash::abi_decode(&value)
112+
{
113+
return FlashtestationRegistryError::InvalidRegistrationDataHash(expected, received);
114+
}
115+
116+
if let Ok(ByteSizeExceeded { size }) = ByteSizeExceeded::abi_decode(&value) {
117+
return FlashtestationRegistryError::ByteSizeExceeded(size);
106118
}
107119

108120
if let Ok(SenderMustMatchTEEAddress { sender, teeAddress }) =

crates/op-rbuilder/src/flashtestations/service.rs

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use alloy_primitives::{keccak256, Bytes};
12
use reth_node_builder::BuilderContext;
23
use tracing::{info, warn};
34

@@ -51,10 +52,26 @@ where
5152
quote_provider: args.quote_provider,
5253
});
5354

54-
// Prepare report data with public key (64 bytes, no 0x04 prefix)
55+
// Prepare report data:
56+
// - TEE address (20 bytes) at reportData[0:20]
57+
// - Extended registration data hash (32 bytes) at reportData[20:52]
58+
// - Total: 52 bytes, padded to 64 bytes with zeros
59+
60+
// Extract TEE address as 20 bytes
61+
let tee_address_bytes: [u8; 20] = tee_service_signer.address.into();
62+
63+
// Calculate keccak256 hash of empty bytes (32 bytes)
64+
let ext_data = Bytes::from(b"");
65+
let ext_data_hash = keccak256(&ext_data);
66+
67+
// Create 64-byte report data array
5568
let mut report_data = [0u8; 64];
56-
let pubkey_uncompressed = tee_service_signer.pubkey.serialize_uncompressed();
57-
report_data.copy_from_slice(&pubkey_uncompressed[1..65]); // Skip 0x04 prefix
69+
70+
// Copy TEE address (20 bytes) to positions 0-19
71+
report_data[0..20].copy_from_slice(&tee_address_bytes);
72+
73+
// Copy extended registration data hash (32 bytes) to positions 20-51
74+
report_data[20..52].copy_from_slice(ext_data_hash.as_ref());
5875

5976
// Request TDX attestation
6077
info!(target: "flashtestations", "requesting TDX attestation");
@@ -69,7 +86,11 @@ where
6986
);
7087
// Submit report onchain by registering the key of the tee service
7188
match tx_manager
72-
.fund_and_register_tee_service(attestation.clone(), args.funding_amount)
89+
.fund_and_register_tee_service(
90+
attestation.clone(),
91+
ext_data.clone(),
92+
args.funding_amount,
93+
)
7394
.await
7495
{
7596
Ok(_) => (Some(tx_manager), true),
@@ -84,6 +105,7 @@ where
84105

85106
let flashtestations_builder_tx = FlashtestationsBuilderTx::new(FlashtestationsBuilderTxArgs {
86107
attestation,
108+
extra_registration_data: ext_data,
87109
tee_service_signer,
88110
funding_key,
89111
funding_amount: args.funding_amount,
@@ -115,48 +137,3 @@ where
115137

116138
Ok(flashtestations_builder_tx)
117139
}
118-
119-
#[cfg(test)]
120-
mod tests {
121-
use alloy_primitives::Address;
122-
use secp256k1::{PublicKey, Secp256k1, SecretKey};
123-
use sha3::{Digest, Keccak256};
124-
125-
use crate::tx_signer::public_key_to_address;
126-
127-
/// Derives Ethereum address from report data using the same logic as the Solidity contract
128-
fn derive_ethereum_address_from_report_data(pubkey_64_bytes: &[u8]) -> Address {
129-
// This exactly matches the Solidity implementation:
130-
// address(uint160(uint256(keccak256(reportData))))
131-
132-
// Step 1: keccak256(reportData)
133-
let hash = Keccak256::digest(pubkey_64_bytes);
134-
135-
// Step 2: Take last 20 bytes (same as uint256 -> uint160 conversion)
136-
let mut address_bytes = [0u8; 20];
137-
address_bytes.copy_from_slice(&hash[12..32]);
138-
139-
Address::from(address_bytes)
140-
}
141-
142-
#[test]
143-
fn test_address_derivation_matches() {
144-
// Test that our manual derivation is correct
145-
let secp = Secp256k1::new();
146-
let private_key = SecretKey::from_slice(&[0x01; 32]).unwrap();
147-
let public_key = PublicKey::from_secret_key(&secp, &private_key);
148-
149-
// Get address using our implementation
150-
let our_address = public_key_to_address(&public_key);
151-
152-
// Get address using our manual derivation (matching Solidity)
153-
let pubkey_bytes = public_key.serialize_uncompressed();
154-
let report_data = &pubkey_bytes[1..65]; // Skip 0x04 prefix
155-
let manual_address = derive_ethereum_address_from_report_data(report_data);
156-
157-
assert_eq!(
158-
our_address, manual_address,
159-
"Address derivation should match"
160-
);
161-
}
162-
}

crates/op-rbuilder/src/flashtestations/tx_manager.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl TxManager {
102102
pub async fn fund_and_register_tee_service(
103103
&self,
104104
attestation: Vec<u8>,
105+
extra_registration_data: Bytes,
105106
funding_amount: U256,
106107
) -> Result<(), TxManagerError> {
107108
info!(target: "flashtestations", "funding TEE address at {}", self.tee_service_signer.address);
@@ -133,6 +134,7 @@ impl TxManager {
133134

134135
let calldata = IFlashtestationRegistry::registerTEEServiceCall {
135136
rawQuote: quote_bytes,
137+
extendedRegistrationData: extra_registration_data,
136138
}
137139
.abi_encode();
138140
let tx = TransactionRequest {

0 commit comments

Comments
 (0)