diff --git a/Cargo.lock b/Cargo.lock index c01b21c..e012cef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3248,6 +3248,7 @@ dependencies = [ "runit", "serde", "serde_json", + "sha256", "sqlx", "test-log", "tokio", @@ -4602,6 +4603,19 @@ dependencies = [ "digest 0.10.7", ] +[[package]] +name = "sha256" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18278f6a914fa3070aa316493f7d2ddfb9ac86ebc06fa3b83bffda487e9065b0" +dependencies = [ + "async-trait", + "bytes", + "hex", + "sha2", + "tokio", +] + [[package]] name = "sha3-asm" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index e00b771..08ae6bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -73,6 +73,7 @@ alloy-primitives = { git = "https://github.com/alloy-rs/core", rev = "7574bfc", alloy-sol-macro = { git = "https://github.com/alloy-rs/core", rev = "7574bfc", features = ["json"] } url = "2.5.0" futures = "0.3.30" +sha256 = "1.5.0" [patch.crates-io] diff --git a/src/p2p.rs b/src/p2p.rs index 948353d..f1484ba 100644 --- a/src/p2p.rs +++ b/src/p2p.rs @@ -11,6 +11,7 @@ use libp2p::kad::Addresses; use libp2p::multiaddr::Protocol; use libp2p::swarm::{ConnectionId, NetworkBehaviour, NetworkInfo, SwarmEvent}; use libp2p::{gossipsub, kad, noise, tcp, yamux, Multiaddr, PeerId}; +use sha256::digest; use std::hash::{DefaultHasher, Hash, Hasher}; use std::time::Duration; use tokio::select; @@ -85,12 +86,6 @@ impl SwarmController { )? .with_dns()? .with_behaviour(|key| { - let message_id_fn = |message: &gossipsub::Message| { - let mut s = DefaultHasher::new(); - message.data.hash(&mut s); - gossipsub::MessageId::from(s.finish().to_string()) - }; - let mut b = kad::Behaviour::new(peer_id, MemoryStore::new(key.public().to_peer_id())); b.set_mode(Some(kad::Mode::Server)); @@ -98,7 +93,7 @@ impl SwarmController { .heartbeat_interval(Duration::from_secs(10)) .validation_mode(gossipsub::ValidationMode::Strict) .protocol_id("/mintpool/0.1.0", Version::V1_1) - .message_id_fn(message_id_fn) + .message_id_fn(gossipsub_message_id) .build() .expect("valid config"); @@ -156,7 +151,7 @@ impl SwarmController { } async fn handle_command(&mut self, command: SwarmCommand) { - tracing::info!("Received command: {:?}", command); + tracing::debug!("Received command: {:?}", command); match command { SwarmCommand::ConnectToPeer { address } => match address.parse() { Ok(addr) => { @@ -432,6 +427,24 @@ impl SwarmController { } } +fn gossipsub_message_id(message: &gossipsub::Message) -> gossipsub::MessageId { + if message.topic == announce_topic().hash() { + let s = String::from_utf8_lossy(&message.data); + let hash = digest(s.to_string()); + gossipsub::MessageId::from(hash) + } else { + let s = String::from_utf8_lossy(&message.data); + match PremintTypes::from_json(s.to_string()) { + Ok(premint) => { + let metadata = premint.metadata(); + let hash = digest(metadata.id); + gossipsub::MessageId::from(hash) + } + Err(_) => gossipsub::MessageId::from("likely_spam".to_string()), + } + } +} + #[derive(Debug)] pub struct NetworkState { pub local_peer_id: PeerId, diff --git a/src/premints/zora_premint_v2/rules.rs b/src/premints/zora_premint_v2/rules.rs index a050afa..d3e6f3f 100644 --- a/src/premints/zora_premint_v2/rules.rs +++ b/src/premints/zora_premint_v2/rules.rs @@ -1,3 +1,4 @@ +use alloy::hex; use std::str::FromStr; use alloy_primitives::Signature; @@ -89,7 +90,6 @@ pub async fn is_valid_signature( ) -> eyre::Result { // * if contract exists, check if the signer is the contract admin // * if contract does not exist, check if the signer is the proposed contract admin - let signature = Signature::from_str(premint.signature.as_str())?; let domain = premint.eip712_domain(); @@ -98,8 +98,9 @@ pub async fn is_valid_signature( if signer != premint.collection.contractAdmin { reject!( - "Invalid signature for contract admin {}", - premint.collection.contractAdmin + "Invalid signature for contract admin {} vs recovered {}", + premint.collection.contractAdmin, + signer ) } else { Ok(Accept) diff --git a/src/stdin.rs b/src/stdin.rs index 1233f71..9c0720b 100644 --- a/src/stdin.rs +++ b/src/stdin.rs @@ -10,6 +10,7 @@ Supported commands: /peers - list all connected peers /node - shows node info /announce - announce self to the network to connect to all available peers + /list - list all premints in the database - send a premint to the network "#; diff --git a/src/types.rs b/src/types.rs index 5be04f1..ea4d802 100644 --- a/src/types.rs +++ b/src/types.rs @@ -12,7 +12,7 @@ pub struct PremintName(pub String); impl PremintName { pub fn msg_topic(&self) -> gossipsub::IdentTopic { - gossipsub::IdentTopic::new(format!("mintpool::{:?}", self)) + gossipsub::IdentTopic::new(format!("mintpool::premint::{:?}", self)) } } diff --git a/tests/e2e_test.rs b/tests/e2e_test.rs index bdf3a79..4699c7e 100644 --- a/tests/e2e_test.rs +++ b/tests/e2e_test.rs @@ -15,13 +15,10 @@ use alloy_sol_types::{SolCall, SolValue}; use alloy_transport::TransportErrorKind; use mintpool::config::{ChainInclusionMode, Config}; use mintpool::controller::{ControllerCommands, DBQuery}; -use mintpool::premints::zora_premint_v2::rules::is_valid_signature; use mintpool::premints::zora_premint_v2::types::IZoraPremintV2::MintArguments; use mintpool::premints::zora_premint_v2::types::{ IZoraPremintV2, ZoraPremintV2, PREMINT_FACTORY_ADDR, }; -use mintpool::rules::Evaluation::Accept; -use mintpool::rules::RuleContext; use mintpool::run; use mintpool::types::PremintTypes; use std::env;