-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
259 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
multichain-aggregator/multichain-aggregator-entity/src/api_keys.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0 | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] | ||
#[sea_orm(table_name = "api_keys")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: i32, | ||
#[sea_orm(unique)] | ||
pub key: Uuid, | ||
pub chain_id: i64, | ||
pub created_at: DateTime, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation { | ||
#[sea_orm( | ||
belongs_to = "super::chains::Entity", | ||
from = "Column::ChainId", | ||
to = "super::chains::Column::Id", | ||
on_update = "NoAction", | ||
on_delete = "NoAction" | ||
)] | ||
Chains, | ||
} | ||
|
||
impl Related<super::chains::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::Chains.def() | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
multichain-aggregator/multichain-aggregator-entity/src/prelude.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0 | ||
pub use super::{ | ||
addresses::Entity as Addresses, block_ranges::Entity as BlockRanges, chains::Entity as Chains, | ||
dapps::Entity as Dapps, hashes::Entity as Hashes, | ||
addresses::Entity as Addresses, api_keys::Entity as ApiKeys, | ||
block_ranges::Entity as BlockRanges, chains::Entity as Chains, dapps::Entity as Dapps, | ||
hashes::Entity as Hashes, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
multichain-aggregator/multichain-aggregator-logic/src/api_key_manager.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use crate::{ | ||
repository::api_keys, | ||
types::api_keys::{ApiKey, ApiKeyError}, | ||
}; | ||
use sea_orm::DatabaseConnection; | ||
|
||
pub struct ApiKeyManager { | ||
db: DatabaseConnection, | ||
} | ||
|
||
impl ApiKeyManager { | ||
pub fn new(db: DatabaseConnection) -> Self { | ||
Self { db } | ||
} | ||
|
||
pub async fn validate_api_key(&self, api_key: ApiKey) -> Result<(), ApiKeyError> { | ||
let api_key = | ||
api_keys::find_by_key_and_chain_id(&self.db, api_key.key, api_key.chain_id).await?; | ||
if api_key.is_none() { | ||
return Err(ApiKeyError::InvalidToken("Invalid API key".to_string())); | ||
} | ||
Ok(()) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
multichain-aggregator/multichain-aggregator-logic/src/error.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use crate::types::api_keys::ApiKeyError; | ||
use alloy_primitives::hex::FromHexError; | ||
use sea_orm::{sqlx::types::uuid, DbErr}; | ||
use std::num::ParseIntError; | ||
use thiserror::Error; | ||
use tonic::Code; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum ServiceError { | ||
#[error("api key error: {0}")] | ||
ApiKey(#[from] ApiKeyError), | ||
#[error(transparent)] | ||
Convert(#[from] ParseError), | ||
#[error("internal error: {0}")] | ||
Internal(#[from] anyhow::Error), | ||
#[error("db error: {0}")] | ||
Db(#[from] DbErr), | ||
#[error("not found: {0}")] | ||
NotFound(String), | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum ParseError { | ||
#[error("parse error: invalid integer")] | ||
ParseInt(#[from] ParseIntError), | ||
#[error("parse error: invalid hex")] | ||
ParseHex(#[from] FromHexError), | ||
#[error("parse error: invalid uuid")] | ||
ParseUuid(#[from] uuid::Error), | ||
} | ||
|
||
impl From<ServiceError> for tonic::Status { | ||
fn from(err: ServiceError) -> Self { | ||
let code = match &err { | ||
ServiceError::ApiKey(err) => map_api_key_code(err), | ||
ServiceError::Convert(_) => Code::InvalidArgument, | ||
ServiceError::Internal(_) => Code::Internal, | ||
ServiceError::NotFound(_) => Code::NotFound, | ||
ServiceError::Db(_) => Code::Internal, | ||
}; | ||
tonic::Status::new(code, err.to_string()) | ||
} | ||
} | ||
|
||
fn map_api_key_code(err: &ApiKeyError) -> Code { | ||
match err { | ||
ApiKeyError::InvalidToken(_) => Code::PermissionDenied, | ||
ApiKeyError::Db(_) => Code::Internal, | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
multichain-aggregator/multichain-aggregator-logic/src/import.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
pub mod api_key_manager; | ||
pub mod error; | ||
mod import; | ||
pub mod repository; | ||
mod types; | ||
|
||
pub use import::batch_import; | ||
pub use types::batch_import_request::BatchImportRequest; | ||
pub use types::{api_keys::ApiKey, batch_import_request::BatchImportRequest}; |
4 changes: 2 additions & 2 deletions
4
multichain-aggregator/multichain-aggregator-logic/src/repository/addresses.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
multichain-aggregator/multichain-aggregator-logic/src/repository/api_keys.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use crate::types::api_keys::ApiKey; | ||
use entity::api_keys::{Column, Entity}; | ||
use sea_orm::{prelude::Uuid, ColumnTrait, DatabaseConnection, DbErr, EntityTrait, QueryFilter}; | ||
|
||
pub async fn find_by_key_and_chain_id( | ||
db: &DatabaseConnection, | ||
key: Uuid, | ||
chain_id: i64, | ||
) -> Result<Option<ApiKey>, DbErr> { | ||
let api_key = Entity::find() | ||
.filter(Column::Key.eq(key)) | ||
.filter(Column::ChainId.eq(chain_id)) | ||
.one(db) | ||
.await? | ||
.map(ApiKey::from); | ||
|
||
Ok(api_key) | ||
} |
4 changes: 2 additions & 2 deletions
4
multichain-aggregator/multichain-aggregator-logic/src/repository/block_ranges.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
multichain-aggregator/multichain-aggregator-logic/src/repository/chains.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
multichain-aggregator/multichain-aggregator-logic/src/repository/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod addresses; | ||
pub mod api_keys; | ||
pub mod block_ranges; | ||
pub mod chains; | ||
pub mod hashes; |
39 changes: 39 additions & 0 deletions
39
multichain-aggregator/multichain-aggregator-logic/src/types/api_keys.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use super::ChainId; | ||
use crate::error::{ParseError, ServiceError}; | ||
use entity::api_keys::Model; | ||
use sea_orm::{entity::prelude::Uuid, DbErr}; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum ApiKeyError { | ||
#[error("invalid token: {0}")] | ||
InvalidToken(String), | ||
#[error("db error: {0}")] | ||
Db(#[from] DbErr), | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ApiKey { | ||
pub key: Uuid, | ||
pub chain_id: ChainId, | ||
} | ||
|
||
impl TryFrom<(&str, &str)> for ApiKey { | ||
type Error = ServiceError; | ||
|
||
fn try_from(v: (&str, &str)) -> Result<Self, Self::Error> { | ||
Ok(Self { | ||
key: Uuid::parse_str(v.0).map_err(ParseError::from)?, | ||
chain_id: v.1.parse().map_err(ParseError::from)?, | ||
}) | ||
} | ||
} | ||
|
||
impl From<Model> for ApiKey { | ||
fn from(v: Model) -> Self { | ||
Self { | ||
key: v.key, | ||
chain_id: v.chain_id, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
multichain-aggregator/multichain-aggregator-logic/src/types/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod addresses; | ||
pub mod api_keys; | ||
pub mod batch_import_request; | ||
pub mod block_ranges; | ||
pub mod chains; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.