Skip to content

Commit a6d9915

Browse files
committed
chore: Switch to using an EthereumAddress
1 parent d77568f commit a6d9915

File tree

1 file changed

+28
-37
lines changed

1 file changed

+28
-37
lines changed

src/sqlite.rs

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,34 @@ use thiserror::Error;
88
use alloy::primitives::{Address, keccak256};
99
use std::str::FromStr;
1010

11+
#[derive(Debug, Clone, Copy)]
12+
struct EthereumAddress(Address);
13+
14+
impl ToSql for EthereumAddress {
15+
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
16+
Ok(ToSqlOutput::from(self.0.as_slice()))
17+
}
18+
}
19+
20+
// Convenience methods to convert between Address (Alloy) and EthereumAddress (Used for ToSql)
21+
impl From<Address> for EthereumAddress {
22+
fn from(addr: Address) -> Self {
23+
EthereumAddress(addr)
24+
}
25+
}
26+
27+
impl From<EthereumAddress> for Address {
28+
fn from(addr: EthereumAddress) -> Self {
29+
addr.0
30+
}
31+
}
32+
1133
#[derive(Debug)]
1234
struct Transactions {
1335
id: i32,
14-
// The sender of a transaction. This is the user who signed and authorized a
15-
// transaction, not the message sender that eventually sequenced the
16-
// transaction on the metabased chain
1736
sender: EthereumAddress,
1837
transaction_type: TransactionType,
19-
// Signed TX data as bytes
2038
data: Vec<u8>,
21-
// Fetched from the metabased chain. Used to derive the block number
2239
timestamp: i64,
2340
}
2441

@@ -42,34 +59,6 @@ impl ToSql for TransactionType {
4259
}
4360
}
4461

45-
#[derive(Debug, Clone, PartialEq)]
46-
pub struct EthereumAddress([u8; 20]);
47-
48-
impl EthereumAddress {
49-
pub fn new(hex_string: &str) -> Result<Self, Box<dyn std::error::Error>> {
50-
let hex = hex_string.strip_prefix("0x").unwrap_or(hex_string);
51-
let bytes = hex::decode(hex)?;
52-
if bytes.len() != 20 {
53-
return Err("Invalid Ethereum address length".into());
54-
}
55-
let mut address = [0u8; 20];
56-
address.copy_from_slice(&bytes);
57-
Ok(EthereumAddress(address))
58-
}
59-
60-
pub fn to_hex_string(&self) -> String {
61-
format!("0x{}", hex::encode(self.0))
62-
}
63-
}
64-
65-
// Automatically convert EthereumAddress to a BLOB by getting the first element
66-
// of the tuple
67-
impl ToSql for EthereumAddress {
68-
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
69-
Ok(ToSqlOutput::from(&self.0[..]))
70-
}
71-
}
72-
7362
#[derive(Debug)]
7463
struct Contracts {
7564
id: i32,
@@ -110,7 +99,9 @@ fn initialize_db() -> Result<Connection, DatabaseError> {
11099
// Using a fixed deployer address and init code for this example
111100
// In production, these should be parameters or configured constants
112101
// TODO: Change to sender of bridge address
113-
let deployer = EthereumAddress::from_str("0x0000000000000000000000000000000000000010").unwrap();
102+
let deployer = EthereumAddress::from(
103+
Address::from_str("0x4000000000000000000000000000000000000000").unwrap()
104+
);
114105

115106
// This should be your actual contract init code
116107
// TODO: Change to ERC-721/20/1155 init code
@@ -122,15 +113,15 @@ fn initialize_db() -> Result<Connection, DatabaseError> {
122113
// Prepare the CREATE2 input buffer
123114
let mut buffer = Vec::with_capacity(85); // 1 + 20 + 32 + 32
124115
buffer.push(0xff);
125-
buffer.extend_from_slice(&deployer.0);
116+
buffer.extend_from_slice(deployer.0.as_slice());
126117

127118
// Use transaction_id as salt, padded to 32 bytes
128119
let mut salt = [0u8; 32];
129120
// We want to pad the address to the right so that transaction ID comes at the end
130121
salt[24..32].copy_from_slice(&transaction_id.to_be_bytes());
131122
buffer.extend_from_slice(&salt);
132123

133-
buffer.extend_from_slice(&init_code_hash);
124+
buffer.extend_from_slice(init_code_hash.as_slice());
134125

135126
// Calculate final hash and take last 20 bytes for the address
136127
let address_bytes = &keccak256(&buffer)[12..];
@@ -215,7 +206,7 @@ mod tests {
215206
let mut conn = initialize_db().unwrap();
216207
let transaction = Transactions {
217208
id: 0,
218-
sender: EthereumAddress::new("0x0000000000000000000000000000000000000001").unwrap(),
209+
sender: EthereumAddress::from(Address::from_str("0x0000000000000000000000000000000000000001").unwrap()),
219210
transaction_type: TransactionType::CreateToken,
220211
data: "0x".as_bytes().to_vec(),
221212
timestamp: 1715136000,

0 commit comments

Comments
 (0)