From b1b8346db933aab77192c49d85c4bf32fbf6410b Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Thu, 22 Jun 2023 12:56:19 -0700 Subject: [PATCH] #123 role to rpc keys (#142) * added roles to rpc keys * dont unwrap --------- Co-authored-by: yenicelik --- .../101-balance-referral-stats.sh | 6 +- scripts/manual-tests/123-get-key-roles.sh | 4 + web3_proxy/src/frontend/users/rpc_keys.rs | 73 +++++++++++++++---- 3 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 scripts/manual-tests/123-get-key-roles.sh diff --git a/scripts/manual-tests/101-balance-referral-stats.sh b/scripts/manual-tests/101-balance-referral-stats.sh index d0bc3096..5260d931 100644 --- a/scripts/manual-tests/101-balance-referral-stats.sh +++ b/scripts/manual-tests/101-balance-referral-stats.sh @@ -140,4 +140,8 @@ curl -X GET \ "http://localhost:8544/user/stats/aggregate?query_start=1686236378&query_window_seconds=3600" curl -X GET \ -"http://localhost:8544/user/stats/aggregate?query_start=1686772800&query_window_seconds=3600" \ No newline at end of file +"http://localhost:8544/user/stats/aggregate?query_start=1686772800&query_window_seconds=3600" + +curl \ +-H "Authorization: Bearer 01H2D5DN564M4Q2T6PETEZY83Q" \ +-X GET "127.0.0.1:8544/user/referral/stats/used-codes" \ No newline at end of file diff --git a/scripts/manual-tests/123-get-key-roles.sh b/scripts/manual-tests/123-get-key-roles.sh new file mode 100644 index 00000000..650b0836 --- /dev/null +++ b/scripts/manual-tests/123-get-key-roles.sh @@ -0,0 +1,4 @@ + +curl \ +-H "Authorization: Bearer 01H2D5CAQJF7P80222P4ZAFQ26" \ +-X GET "127.0.0.1:8544/user/keys" \ No newline at end of file diff --git a/web3_proxy/src/frontend/users/rpc_keys.rs b/web3_proxy/src/frontend/users/rpc_keys.rs index ece913d8..1dc45b2c 100644 --- a/web3_proxy/src/frontend/users/rpc_keys.rs +++ b/web3_proxy/src/frontend/users/rpc_keys.rs @@ -19,7 +19,7 @@ use itertools::Itertools; use migration::sea_orm::{ self, ActiveModelTrait, ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter, TryIntoModel, }; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use serde_json::json; use std::sync::Arc; @@ -35,11 +35,47 @@ pub async fn rpc_keys_get( .db_replica() .web3_context("db_replica is required to fetch a user's keys")?; - let uks = rpc_key::Entity::find() + // This is basically completely copied from sea-orm. Not optimal, but it keeps the format identical to before (while adding the final key) + // We could also pack the below stuff into it's subfield, but then we would destroy the format. Both options are fine for now though + #[derive(Serialize)] + struct ReturnType<'a> { + id: u64, + user_id: u64, + secret_key: RpcSecretKey, + description: Option, + private_txs: bool, + active: bool, + allowed_ips: Option, + allowed_origins: Option, + allowed_referers: Option, + allowed_user_agents: Option, + log_revert_chance: f64, + // Addition + // role is optional only to handle an inconsistent database. it should always be set + role: Option<&'a Role>, + } + + let uks: Vec = rpc_key::Entity::find() .filter(rpc_key::Column::UserId.eq(user.id)) .all(db_replica.as_ref()) .await - .web3_context("failed loading user's key")?; + .web3_context("failed loading user's key")? + .into_iter() + .map(|x| ReturnType { + id: x.id, + user_id: x.user_id, + secret_key: x.secret_key.into(), + description: x.description, + private_txs: x.private_txs, + active: x.active, + allowed_ips: x.allowed_ips, + allowed_origins: x.allowed_origins, + allowed_referers: x.allowed_referers, + allowed_user_agents: x.allowed_user_agents, + log_revert_chance: x.log_revert_chance, + role: Some(&Role::Owner), + }) + .collect::>(); let secondary_user_entities = secondary_user::Entity::find() .filter(secondary_user::Column::UserId.eq(user.id)) @@ -50,25 +86,36 @@ pub async fn rpc_keys_get( .collect::>(); // Now return a list of all subusers (their wallets) - let rpc_key_entities: Vec = rpc_key::Entity::find() + let secondary_rpc_key_entities: Vec = rpc_key::Entity::find() .filter( - rpc_key::Column::Id.is_in( - secondary_user_entities - .iter() - .map(|(x, _)| *x) - .collect::>(), - ), + rpc_key::Column::Id.is_in(secondary_user_entities.keys().copied().collect::>()), ) .all(db_replica.as_ref()) - .await?; + .await? + .into_iter() + .map(|x| ReturnType { + id: x.id, + user_id: x.user_id, + secret_key: x.secret_key.into(), + description: x.description, + private_txs: x.private_txs, + active: x.active, + allowed_ips: x.allowed_ips, + allowed_origins: x.allowed_origins, + allowed_referers: x.allowed_referers, + allowed_user_agents: x.allowed_user_agents, + log_revert_chance: x.log_revert_chance, + role: secondary_user_entities.get(&x.id).map(|x| &x.role), + }) + .collect::>(); let response_json = json!({ "user_id": user.id, "user_rpc_keys": uks .into_iter() .map(|uk| (uk.id, uk)) - .chain(rpc_key_entities.into_iter().map(|sk| (sk.id, sk))) - .collect::>(), + .chain(secondary_rpc_key_entities.into_iter().map(|sk| (sk.id, sk))) + .collect::>() }); Ok(Json(response_json).into_response())