Skip to content

Commit

Permalink
feat: Add Contracts alongside Ethereum Address
Browse files Browse the repository at this point in the history
  • Loading branch information
WillPapper committed Jan 5, 2025
1 parent ba3cd69 commit 09da40b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
hex = "0.4.3"
# `bundled` causes us to automatically compile and link in an up to date
# version of SQLite for you. This avoids many common build issues, and
# avoids depending on the version of SQLite on the users system (or your
Expand Down
41 changes: 41 additions & 0 deletions src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rusqlite::{Connection, Result, ToSql};
use rusqlite::types::ToSqlOutput;
use serde::{Serialize, Deserialize};
use thiserror::Error;
use hex;

#[derive(Debug)]
struct Transactions {
Expand Down Expand Up @@ -36,6 +37,34 @@ impl ToSql for TransactionType {
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct EthereumAddress([u8; 20]);

impl EthereumAddress {
pub fn new(hex_string: &str) -> Result<Self, Box<dyn std::error::Error>> {
let hex = hex_string.strip_prefix("0x").unwrap_or(hex_string);
let bytes = hex::decode(hex)?;
if bytes.len() != 20 {
return Err("Invalid Ethereum address length".into());
}
let mut address = [0u8; 20];
address.copy_from_slice(&bytes);
Ok(EthereumAddress(address))
}

pub fn to_hex_string(&self) -> String {
format!("0x{}", hex::encode(self.0))
}
}

#[derive(Debug)]
struct Contracts {
id: i32,
address: EthereumAddress,
signers: Vec<EthereumAddress>,
transaction_id: i32,
}

#[derive(Debug, thiserror::Error)]
pub enum DatabaseError {
#[error("Database error: {0}")]
Expand Down Expand Up @@ -67,6 +96,18 @@ fn initialize_db() -> Result<Connection, DatabaseError> {
(), // empty list of parameters.
)?;

// Create a table for contract addresses
// Contract addresses are unique. Transactions and contracts are 1:1 and also unique
conn.execute(
"CREATE TABLE contracts(
id INTEGER PRIMARY KEY AUTOINCREMENT,
address BLOB NOT NULL UNIQUE,
signers BLOB,
transaction_id INTEGER NOT NULL UNIQUE
)",
(),
)?;

Ok(conn)
}

Expand Down

0 comments on commit 09da40b

Please sign in to comment.