@@ -8,17 +8,34 @@ use thiserror::Error;
8
8
use alloy:: primitives:: { Address , keccak256} ;
9
9
use std:: str:: FromStr ;
10
10
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
+
11
33
#[ derive( Debug ) ]
12
34
struct Transactions {
13
35
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
17
36
sender : EthereumAddress ,
18
37
transaction_type : TransactionType ,
19
- // Signed TX data as bytes
20
38
data : Vec < u8 > ,
21
- // Fetched from the metabased chain. Used to derive the block number
22
39
timestamp : i64 ,
23
40
}
24
41
@@ -42,34 +59,6 @@ impl ToSql for TransactionType {
42
59
}
43
60
}
44
61
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
-
73
62
#[ derive( Debug ) ]
74
63
struct Contracts {
75
64
id : i32 ,
@@ -110,7 +99,9 @@ fn initialize_db() -> Result<Connection, DatabaseError> {
110
99
// Using a fixed deployer address and init code for this example
111
100
// In production, these should be parameters or configured constants
112
101
// 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
+ ) ;
114
105
115
106
// This should be your actual contract init code
116
107
// TODO: Change to ERC-721/20/1155 init code
@@ -122,15 +113,15 @@ fn initialize_db() -> Result<Connection, DatabaseError> {
122
113
// Prepare the CREATE2 input buffer
123
114
let mut buffer = Vec :: with_capacity ( 85 ) ; // 1 + 20 + 32 + 32
124
115
buffer. push ( 0xff ) ;
125
- buffer. extend_from_slice ( & deployer. 0 ) ;
116
+ buffer. extend_from_slice ( deployer. 0 . as_slice ( ) ) ;
126
117
127
118
// Use transaction_id as salt, padded to 32 bytes
128
119
let mut salt = [ 0u8 ; 32 ] ;
129
120
// We want to pad the address to the right so that transaction ID comes at the end
130
121
salt[ 24 ..32 ] . copy_from_slice ( & transaction_id. to_be_bytes ( ) ) ;
131
122
buffer. extend_from_slice ( & salt) ;
132
123
133
- buffer. extend_from_slice ( & init_code_hash) ;
124
+ buffer. extend_from_slice ( init_code_hash. as_slice ( ) ) ;
134
125
135
126
// Calculate final hash and take last 20 bytes for the address
136
127
let address_bytes = & keccak256 ( & buffer) [ 12 ..] ;
@@ -215,7 +206,7 @@ mod tests {
215
206
let mut conn = initialize_db ( ) . unwrap ( ) ;
216
207
let transaction = Transactions {
217
208
id : 0 ,
218
- sender : EthereumAddress :: new ( "0x0000000000000000000000000000000000000001" ) . unwrap ( ) ,
209
+ sender : EthereumAddress :: from ( Address :: from_str ( "0x0000000000000000000000000000000000000001" ) . unwrap ( ) ) ,
219
210
transaction_type : TransactionType :: CreateToken ,
220
211
data : "0x" . as_bytes ( ) . to_vec ( ) ,
221
212
timestamp : 1715136000 ,
0 commit comments