-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
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
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,67 @@ | ||
use std::sync::Arc; | ||
|
||
use ain_macros::ocean_endpoint; | ||
use axum::{routing::post, Extension, Json, Router}; | ||
use defichain_rpc::RpcApi; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::{response::Response, AppContext}; | ||
use crate::{ | ||
error::{ApiError, Error}, | ||
Result, | ||
}; | ||
|
||
#[derive(Serialize, Deserialize, Default, Clone)] | ||
#[serde(rename_all = "camelCase")] | ||
struct RpcDto { | ||
method: String, | ||
params: Vec<serde_json::Value>, | ||
} | ||
|
||
fn method_whitelist(method: &str) -> Result<()> { | ||
let methods = [ | ||
"getblockchaininfo", | ||
"getblockhash", | ||
"getblockcount", | ||
"getblock", | ||
"getblockstats", | ||
"getgov", | ||
"validateaddress", | ||
"listcommunitybalances", | ||
"getaccounthistory", | ||
"getfutureswapblock", | ||
"getpendingfutureswaps", | ||
"sendrawtransaction", | ||
"getrawtransaction", | ||
"getgovproposal", | ||
"listgovproposals", | ||
"listgovproposalvotes", | ||
"vmmap", | ||
"gettxout", | ||
]; | ||
|
||
if !methods.contains(&method) { | ||
log::debug!("forbidden"); | ||
return Err(Error::Forbidden { | ||
method: method.to_owned(), | ||
}); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[ocean_endpoint] | ||
async fn rpc( | ||
Extension(ctx): Extension<Arc<AppContext>>, | ||
Json(body): Json<RpcDto>, | ||
) -> Result<Response<serde_json::Value>> { | ||
method_whitelist(&body.method)?; | ||
|
||
let res: serde_json::Value = ctx.client.call(&body.method, &body.params).await?; | ||
|
||
Ok(Response::new(res)) | ||
} | ||
|
||
pub fn router(ctx: Arc<AppContext>) -> Router { | ||
Router::new().route("/", post(rpc)).layer(Extension(ctx)) | ||
} |
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