diff --git a/src/ethereum/contracts.rs b/src/ethereum/contracts.rs index 376efb6..241d973 100644 --- a/src/ethereum/contracts.rs +++ b/src/ethereum/contracts.rs @@ -16,10 +16,9 @@ use super::sync::{ }; use super::utils::parse_address; -pub const ADAPTER: &str = "IAdapter"; -pub const SUBSCRIPTION: &str = "ISubscription"; -pub const ERC20: &str = "IERC20"; -pub const ERC721: &str = "IERC721Metadata"; +const ADAPTER: &str = "IAdapter"; +const SUBSCRIPTION: &str = "ISubscription"; +const ERC721: &str = "IERC721Metadata"; #[derive(thiserror::Error, Debug)] pub enum ArtifactError { @@ -33,7 +32,7 @@ pub enum ArtifactError { KeyError, } -pub fn load_abi( +fn load_abi( contract_dir: &Path, contract_name: &str, ) -> Result, ArtifactError> { @@ -47,14 +46,13 @@ pub fn load_abi( Ok(contract_abi) } +#[derive(Clone)] pub struct ContractSet { pub web3: Web3, // Last synced block pub current_block: u64, - #[allow(dead_code)] pub adapter: Contract, - pub collectible: Contract, pub subscription: Contract, } diff --git a/src/ethereum/gate.rs b/src/ethereum/gate.rs index 3519243..e5cfd9e 100644 --- a/src/ethereum/gate.rs +++ b/src/ethereum/gate.rs @@ -1,25 +1,15 @@ -use web3::contract::{Contract, Options}; +use web3::contract::Options; -use crate::config::BlockchainConfig; -use super::api::connect; -use super::contracts::{ADAPTER, load_abi}; +use super::contracts::ContractSet; use super::errors::EthereumError; use super::utils::parse_address; pub async fn is_allowed_user( - config: &BlockchainConfig, + contract_set: &ContractSet, user_address: &str, ) -> Result { - let web3 = connect(&config.api_url)?; - let adapter_abi = load_abi(&config.contract_dir, ADAPTER)?; - let adapter_address = parse_address(&config.contract_address)?; - let adapter = Contract::from_json( - web3.eth(), - adapter_address, - &adapter_abi, - )?; let user_address = parse_address(user_address)?; - let result: bool = adapter.query( + let result: bool = contract_set.adapter.query( "isAllowedUser", (user_address,), None, Options::default(), None, ).await?; diff --git a/src/main.rs b/src/main.rs index cbbd520..61f8d26 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,16 +51,15 @@ async fn main() -> std::io::Result<()> { config.environment, ); - let maybe_contract_set = if let Some(blockchain_config) = &config.blockchain { + let maybe_blockchain = if let Some(blockchain_config) = &config.blockchain { // Create blockchain interface get_contracts(blockchain_config, &config.storage_dir).await - .map_err(|err| log::error!("{}", err)) - .ok() + .map(Some).unwrap() } else { None }; - scheduler::run(config.clone(), maybe_contract_set, db_pool.clone()); + scheduler::run(config.clone(), maybe_blockchain.clone(), db_pool.clone()); log::info!("scheduler started"); let http_socket_addr = format!( @@ -117,6 +116,7 @@ async fn main() -> std::io::Result<()> { .app_data(web::JsonConfig::default().limit(MAX_UPLOAD_SIZE)) .app_data(web::Data::new(config.clone())) .app_data(web::Data::new(db_pool.clone())) + .app_data(web::Data::new(maybe_blockchain.clone())) .app_data(web::Data::clone(&inbox_mutex)) .service(actix_files::Files::new( "/media", diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index e07299b..e513d54 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -11,6 +11,7 @@ use crate::activitypub::deliverer::deliver_activity; use crate::config::Config; use crate::database::{Pool, get_database_client}; use crate::errors::{DatabaseError, HttpError, ValidationError}; +use crate::ethereum::contracts::ContractSet; use crate::ethereum::eip4361::verify_eip4361_signature; use crate::ethereum::gate::is_allowed_user; use crate::ethereum::identity::{ @@ -68,6 +69,7 @@ use super::types::{ pub async fn create_account( config: web::Data, db_pool: web::Data, + maybe_blockchain: web::Data>, account_data: web::Json, ) -> Result { let db_client = &mut **get_database_client(&db_pool).await?; @@ -103,16 +105,19 @@ pub async fn create_account( }; assert!(password_hash.is_some() || wallet_address.is_some()); - if let Some(blockchain_config) = config.blockchain.as_ref() { + if let Some(contract_set) = maybe_blockchain.as_ref() { // Wallet address is required if blockchain integration is enabled let wallet_address = wallet_address.as_ref() .ok_or(ValidationError("wallet address is required"))?; - let is_allowed = is_allowed_user(blockchain_config, wallet_address).await + let is_allowed = is_allowed_user(contract_set, wallet_address).await .map_err(|_| HttpError::InternalError)?; if !is_allowed { return Err(ValidationError("not allowed to sign up").into()); - } - } + }; + } else { + assert!(config.blockchain.is_none()); + }; + // Generate RSA private key for actor let private_key = match web::block(generate_private_key).await { Ok(Ok(private_key)) => private_key,