forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move walletconnect rpc to mm2_main rpc
- Loading branch information
1 parent
5457d80
commit 9d12079
Showing
12 changed files
with
168 additions
and
14 deletions.
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
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
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,30 @@ | ||
use kdf_walletconnect::{session::delete::send_session_delete_request, WalletConnectCtx}; | ||
use mm2_core::mm_ctx::MmArc; | ||
use mm2_err_handle::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::WalletConnectRpcError; | ||
|
||
#[derive(Debug, PartialEq, Serialize)] | ||
pub struct DeleteConnectionResponse { | ||
pub successful: bool, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct DeleteConnectionRequest { | ||
topic: String, | ||
} | ||
|
||
/// `delete connection` RPC command implementation. | ||
pub async fn delete_connection( | ||
ctx: MmArc, | ||
req: DeleteConnectionRequest, | ||
) -> MmResult<DeleteConnectionResponse, WalletConnectRpcError> { | ||
let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx) | ||
.mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?; | ||
send_session_delete_request(&ctx, &req.topic.into()) | ||
.await | ||
.mm_err(|err| WalletConnectRpcError::SessionRequestError(err.to_string()))?; | ||
|
||
Ok(DeleteConnectionResponse { successful: true }) | ||
} |
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,20 @@ | ||
use kdf_walletconnect::WalletConnectCtx; | ||
use mm2_core::mm_ctx::MmArc; | ||
use mm2_err_handle::prelude::*; | ||
use serde::Serialize; | ||
|
||
use super::{EmptyRpcRequst, WalletConnectRpcError}; | ||
|
||
#[derive(Debug, PartialEq, Serialize)] | ||
pub struct GetChainIdResponse { | ||
pub chain_id: String, | ||
} | ||
|
||
/// `delete connection` RPC command implementation. | ||
pub async fn get_chain_id(ctx: MmArc, _req: EmptyRpcRequst) -> MmResult<GetChainIdResponse, WalletConnectRpcError> { | ||
let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx) | ||
.mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?; | ||
let chain_id = ctx.get_active_chain_id().await; | ||
|
||
Ok(GetChainIdResponse { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use kdf_walletconnect::{session::Session, WalletConnectCtx}; | ||
use mm2_core::mm_ctx::MmArc; | ||
use mm2_err_handle::prelude::*; | ||
use serde::Serialize; | ||
|
||
use super::{EmptyRpcRequst, WalletConnectRpcError}; | ||
|
||
#[derive(Debug, PartialEq, Serialize)] | ||
pub struct GetSessionResponse { | ||
pub session: Option<Session>, | ||
} | ||
|
||
/// `delete connection` RPC command implementation. | ||
pub async fn get_session(ctx: MmArc, _req: EmptyRpcRequst) -> MmResult<GetSessionResponse, WalletConnectRpcError> { | ||
let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx) | ||
.mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?; | ||
let session = ctx.get_session().await; | ||
|
||
Ok(GetSessionResponse { session }) | ||
} |
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,26 @@ | ||
use kdf_walletconnect::WalletConnectCtx; | ||
use mm2_core::mm_ctx::MmArc; | ||
use mm2_err_handle::prelude::*; | ||
use serde::Serialize; | ||
|
||
use super::{EmptyRpcRequst, WalletConnectRpcError}; | ||
|
||
#[derive(Debug, PartialEq, Serialize)] | ||
pub struct CreateConnectionResponse { | ||
pub url: String, | ||
} | ||
|
||
/// `new_connection` RPC command implementation. | ||
pub async fn new_connection( | ||
ctx: MmArc, | ||
_req: EmptyRpcRequst, | ||
) -> MmResult<CreateConnectionResponse, WalletConnectRpcError> { | ||
let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx) | ||
.mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?; | ||
let url = ctx | ||
.new_connection(None) | ||
.await | ||
.mm_err(|err| WalletConnectRpcError::SessionRequestError(err.to_string()))?; | ||
|
||
Ok(CreateConnectionResponse { url }) | ||
} |
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,27 @@ | ||
use kdf_walletconnect::{session::ping::send_session_ping_request, WalletConnectCtx}; | ||
use mm2_core::mm_ctx::MmArc; | ||
use mm2_err_handle::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::WalletConnectRpcError; | ||
|
||
#[derive(Debug, PartialEq, Serialize)] | ||
pub struct SessionPingResponse { | ||
pub successful: bool, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct SessionPingRequest { | ||
topic: String, | ||
} | ||
|
||
/// `ping session` RPC command implementation. | ||
pub async fn ping_session(ctx: MmArc, req: SessionPingRequest) -> MmResult<SessionPingResponse, WalletConnectRpcError> { | ||
let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx) | ||
.mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?; | ||
send_session_ping_request(&ctx, &req.topic.into()) | ||
.await | ||
.mm_err(|err| WalletConnectRpcError::SessionRequestError(err.to_string()))?; | ||
|
||
Ok(SessionPingResponse { successful: true }) | ||
} |
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,37 @@ | ||
mod delete_connection; | ||
mod get_chain_id; | ||
mod get_session; | ||
mod new_connection; | ||
mod ping; | ||
|
||
use common::HttpStatusCode; | ||
pub use delete_connection::delete_connection; | ||
use derive_more::Display; | ||
pub use get_chain_id::get_chain_id; | ||
pub use get_session::get_session; | ||
use http::StatusCode; | ||
pub use new_connection::new_connection; | ||
pub use ping::ping_session; | ||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize)] | ||
pub struct EmptyRpcRequst {} | ||
|
||
#[derive(Serialize, Display, SerializeErrorType)] | ||
#[serde(tag = "error_type", content = "error_data")] | ||
pub enum WalletConnectRpcError { | ||
InternalError(String), | ||
InitializationError(String), | ||
SessionRequestError(String), | ||
} | ||
|
||
impl HttpStatusCode for WalletConnectRpcError { | ||
fn status_code(&self) -> StatusCode { | ||
match self { | ||
WalletConnectRpcError::InitializationError(_) => StatusCode::BAD_REQUEST, | ||
WalletConnectRpcError::SessionRequestError(_) | WalletConnectRpcError::InternalError(_) => { | ||
StatusCode::INTERNAL_SERVER_ERROR | ||
}, | ||
} | ||
} | ||
} |