Call manager contract to verify that user is allowed to sign up

This commit is contained in:
silverpill 2021-10-21 11:55:27 +00:00
parent a9fb874cb3
commit 324ff20480
5 changed files with 42 additions and 1 deletions

View file

@ -5,6 +5,7 @@ Federated social network with smart contracts.
- Built on [ActivityPub](https://activitypub.rocks/) protocol. - Built on [ActivityPub](https://activitypub.rocks/) protocol.
- Lightweight. - Lightweight.
- Sign-in with Ethereum. - Sign-in with Ethereum.
- Proving membership with a token.
- Converting posts into NFTs. - Converting posts into NFTs.
- More crypto features in the future. - More crypto features in the future.

31
src/ethereum/gate.rs Normal file
View file

@ -0,0 +1,31 @@
use web3::contract::{Contract, Options};
use crate::config::Config;
use super::api::connect;
use super::nft::{MANAGER, load_abi, EthereumError};
use super::utils::parse_address;
pub async fn is_allowed_user(
config: &Config,
user_address: &str,
) -> Result<bool, EthereumError> {
let json_rpc_url = config.ethereum_json_rpc_url.as_ref()
.ok_or(EthereumError::ImproperlyConfigured)?;
let web3 = connect(json_rpc_url)?;
let ethereum_config = config.ethereum_contract.as_ref()
.ok_or(EthereumError::ImproperlyConfigured)?;
let manager_abi = load_abi(&config.contract_dir, MANAGER)?;
let manager_address = parse_address(&ethereum_config.address)?;
let manager = Contract::from_json(
web3.eth(),
manager_address,
&manager_abi,
)?;
let user_address = parse_address(user_address)?;
let result: bool = manager.query(
"isAllowedUser", (user_address,),
None, Options::default(), None,
).await?;
Ok(result)
}

View file

@ -1,3 +1,4 @@
mod api; mod api;
pub mod gate;
pub mod nft; pub mod nft;
pub mod utils; pub mod utils;

View file

@ -71,7 +71,7 @@ pub enum EthereumError {
SigError(#[from] SignatureError), SigError(#[from] SignatureError),
} }
fn load_abi( pub fn load_abi(
contract_dir: &PathBuf, contract_dir: &PathBuf,
contract_name: &str, contract_name: &str,
) -> Result<Vec<u8>, EthereumError> { ) -> Result<Vec<u8>, EthereumError> {

View file

@ -6,6 +6,7 @@ use actix_web::{
use crate::config::Config; use crate::config::Config;
use crate::database::{Pool, get_database_client}; use crate::database::{Pool, get_database_client};
use crate::errors::{HttpError, ValidationError}; use crate::errors::{HttpError, ValidationError};
use crate::ethereum::gate::is_allowed_user;
use crate::mastodon_api::accounts::types::{Account, AccountCreateData}; use crate::mastodon_api::accounts::types::{Account, AccountCreateData};
use crate::models::users::queries::{ use crate::models::users::queries::{
is_valid_invite_code, is_valid_invite_code,
@ -35,6 +36,13 @@ pub async fn create_user_view(
Err(ValidationError("invalid invite code"))?; Err(ValidationError("invalid invite code"))?;
} }
} }
if config.ethereum_contract.is_some() {
let is_allowed = is_allowed_user(&config, &user_data.wallet_address).await
.map_err(|_| HttpError::InternalError)?;
if !is_allowed {
Err(ValidationError("not allowed to sign up"))?;
}
}
// Hash password and generate private key // Hash password and generate private key
let password_hash = hash_password(&user_data.password) let password_hash = hash_password(&user_data.password)
.map_err(|_| HttpError::InternalError)?; .map_err(|_| HttpError::InternalError)?;