Skip to content

Commit 4449772

Browse files
authored
duplicate rpc call with new name (#3425)
1 parent be5762b commit 4449772

File tree

9 files changed

+1016
-0
lines changed

9 files changed

+1016
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use crate::{
2+
error_code::*, methods::omni::PumpxRpcError, server::RpcContext, verify_auth::verify_auth,
3+
Deserialize, ErrorCode,
4+
};
5+
use executor_core::native_task::*;
6+
use executor_primitives::OmniAuth;
7+
use heima_primitives::{Identity, Web2IdentityType};
8+
use jsonrpsee::RpcModule;
9+
use native_task_handler::NativeTaskOk;
10+
use pumpx::types::AddWalletResponse;
11+
use serde::Serialize;
12+
13+
use super::common::{check_omni_api_response, handle_omni_native_task};
14+
15+
#[derive(Debug, Deserialize)]
16+
pub struct AddWalletParams {
17+
pub user_email: String,
18+
pub auth_token: String,
19+
}
20+
21+
#[derive(Serialize, Clone)]
22+
pub struct RPCAddWalletResponse {
23+
pub backend_response: AddWalletResponse,
24+
}
25+
26+
impl From<AddWalletParams> for NativeTaskWrapper<NativeTask> {
27+
fn from(p: AddWalletParams) -> Self {
28+
Self {
29+
task: NativeTask::PumpxAddWallet(Identity::from_web2_account(
30+
p.user_email.as_str(),
31+
Web2IdentityType::Email,
32+
)),
33+
nonce: None,
34+
auth: Some(OmniAuth::AuthToken(p.auth_token)),
35+
}
36+
}
37+
}
38+
39+
pub fn register_add_wallet(module: &mut RpcModule<RpcContext>) {
40+
module
41+
.register_async_method("omni_addWallet", |params, ctx, _| async move {
42+
let params = params.parse::<AddWalletParams>().map_err(|e| {
43+
log::error!("Failed to parse params: {:?}", e);
44+
PumpxRpcError::from_error_code(ErrorCode::ParseError)
45+
})?;
46+
47+
log::debug!("Received omni_addWallet, user_email: {}", params.user_email);
48+
49+
let wrapper: NativeTaskWrapper<NativeTask> = params.into();
50+
51+
if wrapper.task.require_auth() && verify_auth(ctx.clone(), &wrapper).await.is_err() {
52+
return Err(PumpxRpcError::from_error_code(ErrorCode::ServerError(
53+
AUTH_VERIFICATION_FAILED_CODE,
54+
)));
55+
}
56+
57+
handle_omni_native_task(&ctx, wrapper, |task_ok| match task_ok {
58+
NativeTaskOk::PumpxAddWallet(response) => {
59+
check_omni_api_response(response.clone(), "Add wallet".into())?;
60+
Ok(RPCAddWalletResponse { backend_response: response })
61+
},
62+
_ => {
63+
log::error!("Unexpected response type");
64+
Err(PumpxRpcError::from_error_code(ErrorCode::InternalError))
65+
},
66+
})
67+
.await
68+
})
69+
.expect("Failed to register omni_addWallet method");
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
use jsonrpsee::types::{ErrorCode, ErrorObjectOwned};
2+
use parity_scale_codec::Codec;
3+
use pumpx::types::ApiResponse;
4+
use serde::Serialize;
5+
6+
use crate::{error_code::*, oneshot, server::RpcContext, Decode};
7+
use executor_core::native_task::*;
8+
use native_task_handler::{NativeTaskError, NativeTaskOk, NativeTaskResponse};
9+
10+
#[derive(Serialize, Debug)]
11+
pub struct PumpxRpcError {
12+
pub code: i32,
13+
pub message: String,
14+
#[serde(skip_serializing_if = "Option::is_none")]
15+
pub data: Option<PumpxRpcErrorData>,
16+
}
17+
18+
#[derive(Serialize, Debug)]
19+
pub struct PumpxRpcErrorData {
20+
#[serde(skip_serializing_if = "Option::is_none")]
21+
pub backend_response: Option<PumpxRpcErrorBackendResponse>,
22+
}
23+
24+
#[derive(Serialize, Debug)]
25+
pub struct PumpxRpcErrorBackendResponse {
26+
pub code: i32,
27+
pub message: String,
28+
}
29+
30+
impl PumpxRpcError {
31+
pub fn from_code_and_message(code: i32, message: String) -> Self {
32+
Self { code, message, data: None }
33+
}
34+
35+
pub fn from_error_code(error_code: ErrorCode) -> Self {
36+
Self { code: error_code.code(), message: error_code.message().to_string(), data: None }
37+
}
38+
39+
pub fn from_api_response<T>(api_response: ApiResponse<T>) -> Self
40+
where
41+
T: Codec,
42+
{
43+
Self {
44+
code: ErrorCode::InternalError.code(),
45+
message: ErrorCode::InternalError.message().to_string(),
46+
data: Some(PumpxRpcErrorData {
47+
backend_response: Some(PumpxRpcErrorBackendResponse {
48+
code: api_response.code as i32,
49+
message: api_response.message,
50+
}),
51+
}),
52+
}
53+
}
54+
}
55+
56+
impl From<PumpxRpcError> for ErrorObjectOwned {
57+
fn from(error: PumpxRpcError) -> Self {
58+
ErrorObjectOwned::owned(error.code, error.message, error.data)
59+
}
60+
}
61+
62+
/// Process native task and handle response
63+
pub async fn handle_omni_native_task<F, R>(
64+
ctx: &RpcContext,
65+
wrapper: NativeTaskWrapper<NativeTask>,
66+
task_ok_handler: F,
67+
) -> Result<R, PumpxRpcError>
68+
where
69+
F: FnOnce(NativeTaskOk) -> Result<R, PumpxRpcError>,
70+
{
71+
// Create channel for response
72+
let (response_sender, response_receiver) = oneshot::channel();
73+
74+
// Send task to executor
75+
ctx.native_task_sender.send((wrapper, response_sender)).await.map_err(|_| {
76+
log::error!("Failed to send request to native call executor");
77+
PumpxRpcError::from_error_code(ErrorCode::InternalError)
78+
})?;
79+
80+
// Receive response
81+
let response = response_receiver.await.map_err(|e| {
82+
log::error!("Failed to receive response from native call handler: {:?}", e);
83+
PumpxRpcError::from_error_code(ErrorCode::InternalError)
84+
})?;
85+
86+
// Decode response
87+
let native_task_response: NativeTaskResponse = Decode::decode(&mut response.as_slice())
88+
.map_err(|_| {
89+
log::error!("Failed to decode native task response");
90+
PumpxRpcError::from_error_code(ErrorCode::InternalError)
91+
})?;
92+
93+
// Process response
94+
match native_task_response {
95+
Ok(task_ok) => task_ok_handler(task_ok),
96+
Err(NativeTaskError::InternalError) => {
97+
log::error!("Internal error in native task");
98+
Err(PumpxRpcError::from_error_code(ErrorCode::InternalError))
99+
},
100+
Err(native_task_error) => {
101+
log::error!("Failed to execute native task: {:?}", native_task_error);
102+
Err(PumpxRpcError::from_error_code(ErrorCode::ServerError(get_native_task_error_code(
103+
&native_task_error,
104+
))))
105+
},
106+
}
107+
}
108+
109+
pub fn check_omni_api_response<T>(
110+
response: ApiResponse<T>,
111+
name: String,
112+
) -> Result<(), PumpxRpcError>
113+
where
114+
T: Codec,
115+
{
116+
if response.code != 10000 {
117+
log::error!("{} failed: code={}, message={}", name, response.code, response.message);
118+
return Err(PumpxRpcError::from_api_response(response));
119+
}
120+
Ok(())
121+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
use crate::{
2+
error_code::*, methods::omni::PumpxRpcError, server::RpcContext, verify_auth::verify_auth,
3+
Deserialize, ErrorCode,
4+
};
5+
use ethers::types::Bytes;
6+
use executor_core::native_task::*;
7+
use executor_crypto::aes256::{aes_encrypt_default, Aes256Key, SerdeAesOutput};
8+
use executor_primitives::OmniAuth;
9+
use heima_primitives::{Identity, Web2IdentityType};
10+
use jsonrpsee::RpcModule;
11+
use native_task_handler::NativeTaskOk;
12+
use rsa::Oaep;
13+
use sha2::Sha256;
14+
15+
use super::common::handle_omni_native_task;
16+
17+
#[derive(Debug, Deserialize)]
18+
pub struct ExportWalletParams {
19+
pub user_email: String,
20+
pub key: Bytes, // RSA-encrypted AES key to encrypt the wallet private key, in 0x-hex-string
21+
pub google_code: String,
22+
pub chain_id: PumpxChainId,
23+
pub wallet_index: PumxWalletIndex,
24+
pub wallet_address: String,
25+
pub email_code: String,
26+
}
27+
28+
impl From<ExportWalletParams> for NativeTaskWrapper<NativeTask> {
29+
fn from(p: ExportWalletParams) -> Self {
30+
Self {
31+
task: NativeTask::PumpxExportWallet(
32+
Identity::from_web2_account(p.user_email.as_str(), Web2IdentityType::Pumpx),
33+
p.google_code,
34+
p.chain_id,
35+
p.wallet_index,
36+
p.wallet_address,
37+
),
38+
nonce: None,
39+
auth: Some(OmniAuth::Email(p.user_email, p.email_code)),
40+
}
41+
}
42+
}
43+
44+
pub fn register_export_wallet(module: &mut RpcModule<RpcContext>) {
45+
module
46+
.register_async_method("omni_exportWallet", |params, ctx, _| async move {
47+
let params = params.parse::<ExportWalletParams>().map_err(|e| {
48+
log::error!("Failed to parse params: {:?}", e);
49+
PumpxRpcError::from_error_code(ErrorCode::ParseError)
50+
})?;
51+
52+
log::debug!("Received omni_exportWallet, user_email: {}, chain_id: {}, wallet_index: {}, expected_wallet_address: {}", params.user_email, params.chain_id, params.wallet_index, params.wallet_address);
53+
54+
let aes_key = ctx
55+
.shielding_key
56+
.private_key()
57+
.decrypt(Oaep::new::<Sha256>(), &params.key)
58+
.map_err(|e| {
59+
log::error!("Failed to decrypt shielded value: {:?}", e);
60+
PumpxRpcError::from_code_and_message(
61+
DECRYPT_REQUEST_FAILED_CODE,
62+
"Shielded value decryption failed".into(),
63+
)
64+
})?;
65+
let aes_key: Aes256Key = aes_key.try_into().map_err(|_| {
66+
log::error!("Failed to convert AesKey");
67+
PumpxRpcError::from_code_and_message(
68+
AES_KEY_CONVERT_FAILED_CODE,
69+
"AesKey convert failed".into(),
70+
)
71+
})?;
72+
73+
let wrapper: NativeTaskWrapper<NativeTask> = params.into();
74+
75+
if wrapper.task.require_auth() && verify_auth(ctx.clone(), &wrapper).await.is_err() {
76+
return Err(PumpxRpcError::from_error_code(ErrorCode::ServerError(
77+
AUTH_VERIFICATION_FAILED_CODE,
78+
)));
79+
}
80+
81+
handle_omni_native_task(&ctx, wrapper, |task_ok| match task_ok {
82+
NativeTaskOk::PumpxExportWallet(wallet) => {
83+
let encrypted_wallet: SerdeAesOutput =
84+
aes_encrypt_default(&aes_key, &wallet).into();
85+
Ok(encrypted_wallet)
86+
},
87+
_ => {
88+
log::error!("Unexpected response type");
89+
Err(PumpxRpcError::from_error_code(ErrorCode::InternalError))
90+
},
91+
})
92+
.await
93+
})
94+
.expect("Failed to register omni_exportWallet method");
95+
}

tee-worker/omni-executor/rpc-server/src/methods/omni/mod.rs

+32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use crate::server::RpcContext;
22
use jsonrpsee::RpcModule;
33

4+
mod common;
5+
use common::*;
6+
47
mod get_health;
58
use get_health::*;
69

@@ -19,11 +22,40 @@ use request_email_verification_code::*;
1922
mod submit_native_task;
2023
use submit_native_task::*;
2124

25+
mod add_wallet;
26+
use add_wallet::*;
27+
28+
mod export_wallet;
29+
use export_wallet::*;
30+
31+
mod notify_limit_order_result;
32+
use notify_limit_order_result::*;
33+
34+
mod request_jwt;
35+
use request_jwt::*;
36+
37+
mod sign_limit_order;
38+
use sign_limit_order::*;
39+
40+
mod submit_swap_order;
41+
use submit_swap_order::*;
42+
43+
mod transfer_widthdraw;
44+
use transfer_widthdraw::*;
45+
2246
pub fn register_omni(module: &mut RpcModule<RpcContext>) {
2347
register_get_health(module);
2448
register_get_next_intent_id(module);
2549
register_get_shielding_key(module);
2650
register_submit_native_task(module);
2751
register_request_email_verification_code(module);
2852
register_get_oauth2_google_authorization_url(module);
53+
54+
register_request_jwt(module);
55+
register_export_wallet(module);
56+
register_add_wallet(module);
57+
register_transfer_withdraw(module);
58+
register_submit_swap_order(module);
59+
register_sign_limit_order_params(module);
60+
register_notify_limit_order_result(module);
2961
}

0 commit comments

Comments
 (0)