Skip to content

Commit fc576a0

Browse files
authored
Standardize authentication for omni_transferWithdraw and omni_exportWallet (#3535)
1 parent b1dd9f7 commit fc576a0

File tree

16 files changed

+250
-283
lines changed

16 files changed

+250
-283
lines changed

tee-worker/omni-executor/executor-core/src/native_task.rs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use executor_primitives::{
2-
Identity, Intent, IntentId, Nonce, OmniAccountPermission, OmniAuth, ValidationData,
2+
AccountId, Identity, Intent, IntentId, Nonce, OmniAccountPermission, OmniAuth, ValidationData,
33
};
44
use parity_scale_codec::{Codec, Decode, Encode};
55
use std::fmt::Debug;
66
use std::vec::Vec;
77
use uuid::Uuid;
88

99
pub trait NativeTaskTrait: Codec {
10-
fn sender(&self) -> &Identity;
11-
1210
fn signature_message_prefix(&self) -> String {
1311
"Token: ".to_string()
1412
}
@@ -41,7 +39,7 @@ pub type PumpxChainId = u32;
4139
#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq)]
4240
pub enum NativeTask {
4341
RequestAuthToken(Identity),
44-
RequestIntent(Identity, IntentId, Intent),
42+
RequestIntent(AccountId, IntentId, Intent),
4543
CreateAccountStore(Identity),
4644
AddAccount(Identity, Identity, ValidationData, bool, Option<Vec<OmniAccountPermission>>),
4745
RemoveAccounts(Identity, Vec<Identity>),
@@ -52,14 +50,14 @@ pub enum NativeTask {
5250
#[codec(index = 20)]
5351
PumpxRequestJwt(Identity, String, Option<String>, GoogleCode, Option<String>),
5452
#[codec(index = 21)]
55-
PumpxExportWallet(Identity, GoogleCode, PumpxChainId, PumxWalletIndex, String),
53+
PumpxExportWallet(AccountId, GoogleCode, PumpxChainId, PumxWalletIndex, String),
5654
#[codec(index = 22)]
57-
PumpxAddWallet(Identity),
55+
PumpxAddWallet(AccountId),
5856
#[codec(index = 23)]
59-
PumpxSignLimitOrder(Identity, PumpxChainId, PumxWalletIndex, Vec<Vec<u8>>),
57+
PumpxSignLimitOrder(AccountId, PumpxChainId, PumxWalletIndex, Vec<Vec<u8>>),
6058
#[codec(index = 24)]
6159
PumpxTransferWidthdraw(
62-
Identity,
60+
AccountId,
6361
Option<u32>, // request_id
6462
u32, // chain_id
6563
u32, // wallet_index
@@ -70,28 +68,10 @@ pub enum NativeTask {
7068
Option<String>, // language
7169
),
7270
#[codec(index = 25)]
73-
PumpxNotifyLimitOrderResult(Identity, u32, String, Option<String>),
71+
PumpxNotifyLimitOrderResult(AccountId, u32, String, Option<String>),
7472
}
7573

7674
impl NativeTaskTrait for NativeTask {
77-
fn sender(&self) -> &Identity {
78-
match self {
79-
Self::RequestAuthToken(sender, ..) => sender,
80-
Self::RequestIntent(sender, ..) => sender,
81-
Self::CreateAccountStore(sender) => sender,
82-
Self::AddAccount(sender, ..) => sender,
83-
Self::RemoveAccounts(sender, ..) => sender,
84-
Self::PublicizeAccount(sender, ..) => sender,
85-
Self::SetPermissions(sender, ..) => sender,
86-
Self::PumpxRequestJwt(sender, ..) => sender,
87-
Self::PumpxExportWallet(sender, ..) => sender,
88-
Self::PumpxAddWallet(sender, ..) => sender,
89-
Self::PumpxSignLimitOrder(sender, ..) => sender,
90-
Self::PumpxTransferWidthdraw(sender, ..) => sender,
91-
Self::PumpxNotifyLimitOrderResult(sender, ..) => sender,
92-
}
93-
}
94-
9575
fn require_auth(&self) -> bool {
9676
// currently all tasks require auth
9777
true

tee-worker/omni-executor/native-task-handler/src/lib.rs

Lines changed: 84 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use heima_identity_verification::{get_verification_message, web2, web3};
2626
use parentchain_api_interface::{
2727
omni_account::calls::types::create_account_store::ClientId,
2828
runtime_types::{
29-
frame_system::pallet::Call as SystemCall,
30-
pallet_balances::pallet::Call as BalancesCall,
29+
// frame_system::pallet::Call as SystemCall,
30+
// pallet_balances::pallet::Call as BalancesCall,
3131
pallet_omni_account::pallet::{Call as OmniAccountCall, IntentCompletedDetail},
3232
paseo_runtime::RuntimeCall,
3333
},
@@ -275,9 +275,7 @@ async fn handle_native_task<
275275
send_ok(response_sender, NativeTaskOk::AuthToken(token));
276276
return;
277277
},
278-
NativeTask::RequestIntent(sender, intent_id, intent) => {
279-
let omni_account = sender.to_omni_account(client_id);
280-
278+
NativeTask::RequestIntent(omni_account, intent_id, intent) => {
281279
debug!("Intent requested");
282280

283281
let intent_id_storage = IntentIdStorage::new(ctx.storage_db.clone());
@@ -325,40 +323,54 @@ async fn handle_native_task<
325323
.await;
326324

327325
let (execution_result, should_notify_parentchain) = match intent {
328-
Intent::SystemRemark(remark) => {
329-
let remark_call = SystemCall::remark { remark: remark.to_vec() };
330-
let _ = dispatch_as_signed(
331-
&mut rpc_client,
332-
ctx.transaction_signer.clone(),
333-
sender,
334-
RuntimeCall::System(remark_call),
335-
auth_type,
336-
)
337-
.await;
338-
send_ok(
326+
Intent::SystemRemark(_remark) => {
327+
// let remark_call = SystemCall::remark { remark: remark.to_vec() };
328+
// let _ = dispatch_as_signed(
329+
// &mut rpc_client,
330+
// ctx.transaction_signer.clone(),
331+
// sender,
332+
// RuntimeCall::System(remark_call),
333+
// auth_type,
334+
// )
335+
// .await;
336+
// send_ok(
337+
// response_sender,
338+
// NativeTaskOk::RequestIntentResult { intent_id, success: true },
339+
// );
340+
// (IntentCompletedDetail::Success, true)
341+
info!("Intent rejected");
342+
send_error(
343+
"Intent not accepted".to_string(),
339344
response_sender,
340-
NativeTaskOk::RequestIntentResult { intent_id, success: true },
345+
NativeTaskError::InternalError,
341346
);
342-
(IntentCompletedDetail::Success, true)
347+
(IntentCompletedDetail::Failure, true)
343348
},
344-
Intent::TransferNative(transfer) => {
345-
let transfer_call = BalancesCall::transfer_allow_death {
346-
dest: transfer.to.to_subxt_type().into(),
347-
value: transfer.value,
348-
};
349-
let _ = dispatch_as_signed(
350-
&mut rpc_client,
351-
ctx.transaction_signer.clone(),
352-
sender,
353-
RuntimeCall::Balances(transfer_call),
354-
auth_type,
355-
)
356-
.await;
357-
send_ok(
349+
Intent::TransferNative(_transfer) => {
350+
// let transfer_call = BalancesCall::transfer_allow_death {
351+
// dest: transfer.to.to_subxt_type().into(),
352+
// value: transfer.value,
353+
// };
354+
// let _ = dispatch_as_signed(
355+
// &mut rpc_client,
356+
// ctx.transaction_signer.clone(),
357+
// sender,
358+
// RuntimeCall::Balances(transfer_call),
359+
// auth_type,
360+
// )
361+
// .await;
362+
// send_ok(
363+
// response_sender,
364+
// NativeTaskOk::RequestIntentResult { intent_id, success: true },
365+
// );
366+
// (IntentCompletedDetail::Success, true)
367+
info!("Intent rejected");
368+
send_error(
369+
"Intent not accepted".to_string(),
358370
response_sender,
359-
NativeTaskOk::RequestIntentResult { intent_id, success: true },
371+
NativeTaskError::InternalError,
360372
);
361-
(IntentCompletedDetail::Success, true)
373+
(IntentCompletedDetail::Failure, true)
362374
},
363375
Intent::CallEthereum(_) | Intent::TransferEthereum(_) => {
364376
// if let Err(e) = ctx
@@ -708,15 +720,15 @@ async fn handle_native_task<
708720
return;
709721
},
710722
NativeTask::PumpxExportWallet(
711-
sender,
723+
omni_account,
712724
google_code,
713725
pumpx_chain_id,
714726
pumpx_wallet_index,
715727
expected_wallet_address,
716728
) => {
717729
let storage = HeimaJwtStorage::new(ctx.storage_db.clone());
718730
let Ok(Some(access_token)) =
719-
storage.get(&(sender.to_omni_account(client_id), AUTH_TOKEN_ACCESS_TYPE))
731+
storage.get(&(omni_account.clone(), AUTH_TOKEN_ACCESS_TYPE))
720732
else {
721733
send_error(
722734
format!("Failed to get pumpx_{}_jwt_token", AUTH_TOKEN_ACCESS_TYPE),
@@ -756,7 +768,7 @@ async fn handle_native_task<
756768
.export_wallet(
757769
chain,
758770
pumpx_wallet_index,
759-
sender.to_omni_account(client_id).into(),
771+
omni_account.clone().into(),
760772
// TODO: theoretically we could pass the aes_key from initial RPC to signer, so that
761773
// we don't have to do double encryption/decryption
762774
ctx.aes256_key.to_vec(),
@@ -781,18 +793,14 @@ async fn handle_native_task<
781793
};
782794

783795
let omni_account_profile_storage = PumpxProfileStorage::new(ctx.storage_db.clone());
784-
if let Ok(maybe_profile) =
785-
omni_account_profile_storage.get(&sender.to_omni_account(client_id))
786-
{
796+
if let Ok(maybe_profile) = omni_account_profile_storage.get(&omni_account) {
787797
let profile = maybe_profile
788798
.map(|mut p| {
789799
p.wallet_exported = true;
790800
p
791801
})
792802
.unwrap_or_else(|| PumpxAccountProfile { wallet_exported: true });
793-
if let Err(e) =
794-
omni_account_profile_storage.insert(&sender.to_omni_account(client_id), profile)
795-
{
803+
if let Err(e) = omni_account_profile_storage.insert(&omni_account, profile) {
796804
error!("Failed to update pumpx account profile: {:?}", e);
797805
send_error(
798806
"Failed to update omni account profile".to_string(),
@@ -812,10 +820,9 @@ async fn handle_native_task<
812820
send_ok(response_sender, NativeTaskOk::PumpxExportWallet(decrypted_wallet));
813821
return;
814822
},
815-
NativeTask::PumpxAddWallet(sender) => {
823+
NativeTask::PumpxAddWallet(omni_account) => {
816824
let storage = HeimaJwtStorage::new(ctx.storage_db.clone());
817-
let Ok(Some(access_token)) =
818-
storage.get(&(sender.to_omni_account(client_id), AUTH_TOKEN_ACCESS_TYPE))
825+
let Ok(Some(access_token)) = storage.get(&(omni_account, AUTH_TOKEN_ACCESS_TYPE))
819826
else {
820827
send_error(
821828
format!("Failed to get pumpx_{}_jwt_token", AUTH_TOKEN_ACCESS_TYPE),
@@ -839,19 +846,7 @@ async fn handle_native_task<
839846
send_ok(response_sender, NativeTaskOk::PumpxAddWallet(backend_response));
840847
return;
841848
},
842-
NativeTask::PumpxSignLimitOrder(sender, chain_id, wallet_index, unsigned_tx) => {
843-
// This is a workaround, in this case the Substrate identity is the OmniAccount address itself
844-
let omni_account: AccountId = match sender {
845-
Identity::Substrate(ref account) => account.into(),
846-
_ => {
847-
send_error(
848-
"Invalid sender type for PumpxSignLimitOrder".to_string(),
849-
response_sender,
850-
NativeTaskError::InternalError,
851-
);
852-
return;
853-
},
854-
};
849+
NativeTask::PumpxSignLimitOrder(omni_account, chain_id, wallet_index, unsigned_tx) => {
855850
let Some(chain) = ChainType::from_pumpx_chain_id(chain_id) else {
856851
error!("Failed to map pumpx chain_id {}", chain_id);
857852
let response = NativeTaskResponse::Err(NativeTaskError::InternalError);
@@ -879,7 +874,7 @@ async fn handle_native_task<
879874
return;
880875
},
881876
NativeTask::PumpxTransferWidthdraw(
882-
sender,
877+
omni_account,
883878
request_id,
884879
chain_id,
885880
wallet_index,
@@ -892,7 +887,7 @@ async fn handle_native_task<
892887
// 1. Verify we have a valid Pumpx "access" token for the user
893888
let storage = HeimaJwtStorage::new(ctx.storage_db.clone());
894889
let Ok(Some(access_token)) =
895-
storage.get(&(sender.to_omni_account(client_id), AUTH_TOKEN_ACCESS_TYPE))
890+
storage.get(&(omni_account.clone(), AUTH_TOKEN_ACCESS_TYPE))
896891
else {
897892
send_error(
898893
"Failed to get access_token within NativeTask::PumpxTransferWidthdraw"
@@ -946,7 +941,7 @@ async fn handle_native_task<
946941
};
947942
return;
948943
},
949-
NativeTask::PumpxNotifyLimitOrderResult(sender, intent_id, result, message) => {
944+
NativeTask::PumpxNotifyLimitOrderResult(omni_account, intent_id, result, message) => {
950945
if result != "ok" && result != "nok" {
951946
send_error(
952947
format!("Invalid result value: {}. Must be 'ok' or 'nok'", result),
@@ -966,19 +961,6 @@ async fn handle_native_task<
966961
info!("Limit order result message for intent_id {}: {}", intent_id, msg);
967962
}
968963

969-
// This is a workaround, in this case the Substrate identity is the OmniAccount address itself
970-
let omni_account: AccountId = match sender {
971-
Identity::Substrate(ref account) => account.into(),
972-
_ => {
973-
send_error(
974-
"Invalid sender type for PumpxSignLimitOrder".to_string(),
975-
response_sender,
976-
NativeTaskError::InternalError,
977-
);
978-
return;
979-
},
980-
};
981-
982964
send_ok(response_sender, NativeTaskOk::PumpxNotifyLimitOrderResult);
983965

984966
notify_intent_completed(
@@ -1030,33 +1012,33 @@ fn send_ok(sender: ResponseSender, ok_res: NativeTaskOk) {
10301012
send_response(sender, NativeTaskResponse::Ok(ok_res));
10311013
}
10321014

1033-
async fn dispatch_as_signed<
1034-
Header: Send + Sync + 'static,
1035-
RpcClient: SubstrateRpcClient<Header> + Send + Sync + 'static,
1036-
>(
1037-
client: &mut RpcClient,
1038-
signer: Arc<ParentchainTxSigner>,
1039-
sender: Identity,
1040-
call: RuntimeCall,
1041-
auth_type: Option<OmniAccountAuthType>,
1042-
) {
1043-
let call = parentchain_api_interface::tx().omni_account().dispatch_as_signed(
1044-
sender.hash().to_subxt_type(),
1045-
call,
1046-
auth_type.map(|t| t.to_subxt_type()),
1047-
);
1048-
let tx = signer.sign(call).await;
1049-
// notify parentchain - for now we continue even with error
1050-
match client.submit_tx(&tx).await {
1051-
Ok(_) => {
1052-
debug!("Submitted dispatch_as_signed parentchain call")
1053-
},
1054-
Err(_) => {
1055-
error!("Failed to submit dispatch_as_signed parentchain call",);
1056-
signer.update_nonce().await
1057-
},
1058-
};
1059-
}
1015+
// async fn dispatch_as_signed<
1016+
// Header: Send + Sync + 'static,
1017+
// RpcClient: SubstrateRpcClient<Header> + Send + Sync + 'static,
1018+
// >(
1019+
// client: &mut RpcClient,
1020+
// signer: Arc<ParentchainTxSigner>,
1021+
// sender: Identity,
1022+
// call: RuntimeCall,
1023+
// auth_type: Option<OmniAccountAuthType>,
1024+
// ) {
1025+
// let call = parentchain_api_interface::tx().omni_account().dispatch_as_signed(
1026+
// sender.hash().to_subxt_type(),
1027+
// call,
1028+
// auth_type.map(|t| t.to_subxt_type()),
1029+
// );
1030+
// let tx = signer.sign(call).await;
1031+
// // notify parentchain - for now we continue even with error
1032+
// match client.submit_tx(&tx).await {
1033+
// Ok(_) => {
1034+
// debug!("Submitted dispatch_as_signed parentchain call")
1035+
// },
1036+
// Err(_) => {
1037+
// error!("Failed to submit dispatch_as_signed parentchain call",);
1038+
// signer.update_nonce().await
1039+
// },
1040+
// };
1041+
// }
10601042

10611043
async fn notify_intent_accepted<
10621044
Header: Send + Sync + 'static,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ use pumpx::*;
77
mod omni;
88
use omni::*;
99

10-
pub const PROTECTED_METHODS: [&str; 5] = [
10+
pub const PROTECTED_METHODS: [&str; 7] = [
1111
"omni_testProtectedMethod",
1212
"omni_addWallet",
1313
"omni_notifyLimitOrderResult",
1414
"omni_signLimitOrder",
1515
"omni_submitSwapOrder",
16+
"omni_transferWithdraw",
17+
"omni_exportWallet",
1618
];
1719

1820
pub fn register_methods(module: &mut RpcModule<RpcContext>) {

0 commit comments

Comments
 (0)