From 67e4959e340280f0589641938f26a2c290d7143f Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Tue, 29 Nov 2022 21:29:17 +0000 Subject: [PATCH] change user address commands --- web3_proxy/src/bin/web3_proxy.rs | 2 +- .../bin/web3_proxy_cli/change_user_address.rs | 56 ++++++++++++++++ .../change_user_address_by_key.rs | 67 +++++++++++++++++++ .../src/bin/web3_proxy_cli/check_config.rs | 2 +- web3_proxy/src/bin/web3_proxy_cli/main.rs | 14 ++++ 5 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 web3_proxy/src/bin/web3_proxy_cli/change_user_address.rs create mode 100644 web3_proxy/src/bin/web3_proxy_cli/change_user_address_by_key.rs diff --git a/web3_proxy/src/bin/web3_proxy.rs b/web3_proxy/src/bin/web3_proxy.rs index 56f2a526..5ddb5da7 100644 --- a/web3_proxy/src/bin/web3_proxy.rs +++ b/web3_proxy/src/bin/web3_proxy.rs @@ -288,7 +288,7 @@ mod tests { public_requests_per_period: Some(1_000_000), response_cache_max_bytes: 10_usize.pow(7), redirect_public_url: Some("example.com/".to_string()), - redirect_rpc_key_url: Some("example.com/{rpc_key_id}".to_string()), + redirect_rpc_key_url: Some("example.com/{{rpc_key_id}}".to_string()), ..Default::default() }, balanced_rpcs: HashMap::from([ diff --git a/web3_proxy/src/bin/web3_proxy_cli/change_user_address.rs b/web3_proxy/src/bin/web3_proxy_cli/change_user_address.rs new file mode 100644 index 00000000..d2f02c6c --- /dev/null +++ b/web3_proxy/src/bin/web3_proxy_cli/change_user_address.rs @@ -0,0 +1,56 @@ +use anyhow::Context; +use argh::FromArgs; +use entities::user; +use ethers::types::Address; +use log::{debug, info}; +use migration::sea_orm::{ + self, ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, IntoActiveModel, + QueryFilter, +}; + +/// change a user's address. +#[derive(FromArgs, PartialEq, Eq, Debug)] +#[argh(subcommand, name = "change_user_address")] +pub struct ChangeUserAddressCommand { + /// the address of the user you want to change + #[argh(positional)] + old_address: String, + + /// the address of the user you want to change + #[argh(positional)] + new_address: String, +} + +impl ChangeUserAddressCommand { + pub async fn main(self, db_conn: &DatabaseConnection) -> anyhow::Result<()> { + let old_address: Address = self.old_address.parse()?; + let new_address: Address = self.new_address.parse()?; + + let old_address: Vec = old_address.to_fixed_bytes().into(); + let new_address: Vec = new_address.to_fixed_bytes().into(); + + let u = user::Entity::find() + .filter(user::Column::Address.eq(old_address)) + .one(db_conn) + .await? + .context("No user found with that address")?; + + debug!("initial user: {:#?}", u); + + if u.address == new_address { + info!("user already has this address"); + } else { + let mut u = u.into_active_model(); + + u.address = sea_orm::Set(new_address); + + let u = u.save(db_conn).await?; + + info!("changed user address"); + + debug!("updated user: {:#?}", u); + } + + Ok(()) + } +} diff --git a/web3_proxy/src/bin/web3_proxy_cli/change_user_address_by_key.rs b/web3_proxy/src/bin/web3_proxy_cli/change_user_address_by_key.rs new file mode 100644 index 00000000..c234c5a6 --- /dev/null +++ b/web3_proxy/src/bin/web3_proxy_cli/change_user_address_by_key.rs @@ -0,0 +1,67 @@ +use anyhow::Context; +use argh::FromArgs; +use entities::{rpc_key, user}; +use ethers::types::Address; +use log::{debug, info}; +use migration::sea_orm::{ + self, ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, IntoActiveModel, + QueryFilter, +}; +use uuid::Uuid; +use web3_proxy::frontend::authorization::RpcSecretKey; + +/// change a user's tier. +#[derive(FromArgs, PartialEq, Eq, Debug)] +#[argh(subcommand, name = "change_user_address_by_key")] +pub struct ChangeUserAddressByKeyCommand { + #[argh(positional)] + /// the RPC key owned by the user you want to change. + rpc_secret_key: RpcSecretKey, + + /// the new address for the user. + #[argh(positional)] + new_address: String, +} + +impl ChangeUserAddressByKeyCommand { + pub async fn main(self, db_conn: &DatabaseConnection) -> anyhow::Result<()> { + let rpc_secret_key: Uuid = self.rpc_secret_key.into(); + + let new_address: Address = self.new_address.parse()?; + + let new_address: Vec = new_address.to_fixed_bytes().into(); + + let uk = rpc_key::Entity::find() + .filter(rpc_key::Column::SecretKey.eq(rpc_secret_key)) + .one(db_conn) + .await? + .context("No key found")?; + + debug!("user key: {:#?}", uk); + + // use the rpc secret key to get the user + // TODO: get this with a join on rpc_key + let u = user::Entity::find_by_id(uk.user_id) + .one(db_conn) + .await? + .context("No user found with that key")?; + + debug!("user: {:#?}", u); + + if u.address == new_address { + info!("user already has that address"); + } else { + let mut u = u.into_active_model(); + + u.address = sea_orm::Set(new_address); + + let u = u.save(db_conn).await?; + + debug!("user: {:#?}", u); + + info!("user's address changed"); + } + + Ok(()) + } +} diff --git a/web3_proxy/src/bin/web3_proxy_cli/check_config.rs b/web3_proxy/src/bin/web3_proxy_cli/check_config.rs index f0972274..189d1cb3 100644 --- a/web3_proxy/src/bin/web3_proxy_cli/check_config.rs +++ b/web3_proxy/src/bin/web3_proxy_cli/check_config.rs @@ -73,7 +73,7 @@ impl CheckConfigSubCommand { warn!("app.redirect_user_url is None. Registered users will get an error page instead of a redirect") } Some(x) => { - if !x.contains("{rpc_key_id}") { + if !x.contains("{{rpc_key_id}}") { num_errors += 1; error!("redirect_user_url user url must contain \"{{rpc_key_id}}\"") } diff --git a/web3_proxy/src/bin/web3_proxy_cli/main.rs b/web3_proxy/src/bin/web3_proxy_cli/main.rs index 28560dda..a3a89dea 100644 --- a/web3_proxy/src/bin/web3_proxy_cli/main.rs +++ b/web3_proxy/src/bin/web3_proxy_cli/main.rs @@ -1,3 +1,5 @@ +mod change_user_address; +mod change_user_address_by_key; mod change_user_tier; mod change_user_tier_by_key; mod check_config; @@ -37,6 +39,8 @@ pub struct CliConfig { #[derive(FromArgs, PartialEq, Debug)] #[argh(subcommand)] enum SubCommand { + ChangeUserAddress(change_user_address::ChangeUserAddressCommand), + ChangeUserAddressByKey(change_user_address_by_key::ChangeUserAddressByKeyCommand), ChangeUserTier(change_user_tier::ChangeUserTierCommand), ChangeUserTierByKey(change_user_tier_by_key::ChangeUserTierByKeyCommand), CheckConfig(check_config::CheckConfigSubCommand), @@ -80,6 +84,16 @@ async fn main() -> anyhow::Result<()> { }; match cli_config.sub_command { + SubCommand::ChangeUserAddress(x) => { + let db_conn = get_db(cli_config.db_url, 1, 1).await?; + + x.main(&db_conn).await + } + SubCommand::ChangeUserAddressByKey(x) => { + let db_conn = get_db(cli_config.db_url, 1, 1).await?; + + x.main(&db_conn).await + } SubCommand::ChangeUserTier(x) => { let db_conn = get_db(cli_config.db_url, 1, 1).await?;