Skip to content

Commit

Permalink
Refactor Into Library and Binary Crates (#7)
Browse files Browse the repository at this point in the history
This PR refactors `deadbeef` into separate binary and library crates.
This makes producing additional `deadbeef` targets (for WASM for
example, see #5) possible.
  • Loading branch information
Nicholas Rodrigues Lordello authored Jun 30, 2023
1 parent ff7896b commit 4bb4c51
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 85 deletions.
12 changes: 10 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 5 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
[package]
name = "deadbeef"
version = "0.1.0"
edition = "2021"
publish = false
license = "GPL-3.0-or-later"

[dependencies]
clap = { version = "4", features = ["derive"] }
hex = "0.4"
hex-literal = "0.4"
num_cpus = "1"
rand = { version = "0.8", features = ["small_rng"] }
tiny-keccak = { version = "2", features = ["keccak"] }
[workspace]
members = [
"cli",
"core",
]
12 changes: 12 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "deadbeef"
version = "0.1.1"
edition = "2021"
publish = false
license = "GPL-3.0-or-later"

[dependencies]
clap = { version = "4", features = ["derive"] }
deadbeef-core = { version = "0.1.0", path = "../core" }
hex = "0.4"
num_cpus = "1"
46 changes: 44 additions & 2 deletions src/chain.rs → cli/src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Module for chain-specific data.
use crate::{address::address, safe::Contracts};
use hex_literal::hex;
use deadbeef_core::{address, hex, Contracts};
use std::{
fmt::{self, Display, Formatter},
num::ParseIntError,
Expand Down Expand Up @@ -80,3 +79,46 @@ impl FromStr for Chain {
Ok(Self(value))
}
}

#[cfg(test)]
mod tests {
use super::*;
use deadbeef_core::Safe;

#[test]
fn proxy_init_code_digest() {
assert_eq!(
Chain::default()
.contracts()
.unwrap()
.proxy_init_code_digest(),
hex!("76733d705f71b79841c0ee960a0ca880f779cde7ef446c989e6d23efc0a4adfb"),
);
}

#[test]
fn compute_address() {
// <https://etherscan.io/tx/0xdac58edb65c2af3f86f03586eeec7caa7ee245d6d06679a913e5dda16617658e>
let mut safe = Safe::new(
Chain::ethereum().contracts().unwrap(),
vec![
address!("34f845773D4364999f2fbC7AA26ABDeE902cBb46"),
address!("E2Df39d8c1c393BDe653D96a09852508CA2816e5"),
address!("000000000dD7Bc0bcCE4392698dc3e11004F20eB"),
address!("Cbd6073f486714E6641bf87c22A9CEc25aCf5804"),
],
2,
);
safe.update_salt_nonce(|n| {
n.copy_from_slice(&hex!(
"c437564b491906978ae4396733fbc0835f87e6b2578193331caa87645ebe9bdc"
))
});

let address = safe.creation_address();
assert_eq!(
address,
address!("000000000034065b3a94C2118CFe5B4C0067B615")
);
}
}
24 changes: 6 additions & 18 deletions src/main.rs → cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
mod address;
mod chain;
mod create2;
mod safe;

use crate::{address::Address, safe::Safe};
use chain::Chain;
use self::chain::Chain;
use clap::Parser;
use deadbeef_core::{Address, Contracts, Safe};
use hex::FromHexError;
use rand::{rngs::SmallRng, Rng as _, SeedableRng as _};
use safe::{Contracts, Info};
use std::{process, str::FromStr, sync::mpsc, thread};

/// Generate vanity addresses for Safe deployments.
Expand Down Expand Up @@ -103,7 +98,10 @@ fn main() {
let safe = Safe::new(contracts.clone(), args.owners.clone(), args.threshold);
let prefix = args.prefix.0.clone();
let result = sender.clone();
move || search_vanity_safe(safe, &prefix, result)
move || {
let safe = deadbeef_core::search(safe, &prefix);
let _ = result.send(safe);
}
})
})
.collect::<Vec<_>>();
Expand All @@ -128,13 +126,3 @@ fn main() {
let _ = threads;
process::exit(0);
}

fn search_vanity_safe(mut safe: Safe, prefix: &[u8], result: mpsc::Sender<Info>) {
let mut rng = SmallRng::from_entropy();

while !safe.creation_address().0.starts_with(prefix) {
safe.update_salt_nonce(|n| rng.fill(n));
}

let _ = result.send(safe.info());
}
12 changes: 12 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "deadbeef-core"
version = "0.1.0"
edition = "2021"
publish = false
license = "GPL-3.0-or-later"

[dependencies]
hex = "0.4"
hex-literal = "0.4"
rand = { version = "0.8", features = ["small_rng"] }
tiny-keccak = { version = "2", features = ["keccak"] }
5 changes: 2 additions & 3 deletions src/address.rs → core/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,13 @@ impl FromStr for Address {
}
}

#[macro_export]
macro_rules! address {
($s:literal) => {
$crate::address::Address(::hex_literal::hex!($s))
$crate::Address($crate::hex!($s))
};
}

pub(crate) use address;

#[cfg(test)]
mod tests {
use super::*;
Expand Down
1 change: 0 additions & 1 deletion src/create2.rs → core/src/create2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ impl Create2 {
#[cfg(test)]
mod tests {
use super::*;
use crate::address::address;
use hex_literal::hex;

#[test]
Expand Down
20 changes: 20 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[macro_use]
mod address;
mod create2;
mod safe;

pub use self::{
address::Address,
safe::{Contracts, Safe, Transaction},
};
pub use hex_literal::hex;
use rand::{rngs::SmallRng, Rng as _, SeedableRng as _};

/// For the specified Safe parameters
pub fn search(mut safe: Safe, prefix: &[u8]) -> Transaction {
let mut rng = SmallRng::from_entropy();
while !safe.creation_address().0.starts_with(prefix) {
safe.update_salt_nonce(|n| rng.fill(n));
}
safe.transaction()
}
Loading

0 comments on commit 4bb4c51

Please sign in to comment.