-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding native_submitPlainRequest RPC method (#3245)
- Loading branch information
Showing
10 changed files
with
198 additions
and
77 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
87 changes: 87 additions & 0 deletions
87
tee-worker/omni-executor/rpc-server/src/methods/submit_plain_request.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,87 @@ | ||
use crate::{ | ||
error_code::*, | ||
native_call_authenticated::{verify_native_call_authenticated, NativeCallAuthenticated}, | ||
request::PlainRequest, | ||
server::RpcContext, | ||
}; | ||
use executor_core::native_call::NativeCall; | ||
use executor_primitives::{ | ||
utils::hex::{FromHexPrefixed, ToHexPrefixed}, | ||
OmniAccountAuthType, | ||
}; | ||
use jsonrpsee::{ | ||
types::{ErrorCode, ErrorObject}, | ||
RpcModule, | ||
}; | ||
use native_task_handler::NativeTask; | ||
use parentchain_rpc_client::{SubstrateRpcClient, SubstrateRpcClientFactory}; | ||
use parity_scale_codec::Decode; | ||
use std::sync::Arc; | ||
use tokio::{runtime::Handle, sync::oneshot, task}; | ||
|
||
pub fn register_submit_plain_request< | ||
AccountId: Send + Sync + 'static, | ||
Header: Send + Sync + 'static, | ||
RpcClient: SubstrateRpcClient<AccountId, Header> + Send + Sync + 'static, | ||
RpcClientFactory: SubstrateRpcClientFactory<AccountId, Header, RpcClient> + Send + Sync + 'static, | ||
>( | ||
module: &mut RpcModule<RpcContext<AccountId, Header, RpcClient, RpcClientFactory>>, | ||
) { | ||
module | ||
.register_async_method("native_submitPlainRequest", |params, ctx, _| async move { | ||
let Ok(hex_request) = params.one::<String>() else { | ||
return Err(ErrorCode::ParseError.into()); | ||
}; | ||
let Ok(request) = PlainRequest::from_hex(&hex_request) else { | ||
return Err(ErrorCode::ServerError(INVALID_PLAIN_REQUEST_CODE).into()); | ||
}; | ||
let join_handle = task::spawn_blocking({ | ||
let ctx = ctx.clone(); | ||
let plain_request = request.clone(); | ||
|| handle_plain_request(plain_request, ctx, Handle::current()) | ||
}); | ||
let (native_call, auth_type) = join_handle.await.map_err(|e| { | ||
log::error!("Failed to handle Plain request: {:?}", e); | ||
ErrorCode::InternalError | ||
})??; | ||
let (response_sender, response_receiver) = oneshot::channel(); | ||
let native_task = NativeTask { call: native_call, auth_type, response_sender }; | ||
|
||
if ctx.native_task_sender.send(native_task).await.is_err() { | ||
log::error!("Failed to send request to native call executor"); | ||
return Err(ErrorCode::InternalError.into()); | ||
} | ||
match response_receiver.await { | ||
Ok(response) => Ok::<String, ErrorObject>(response.to_hex()), | ||
Err(e) => { | ||
log::error!("Failed to receive response from native call handler: {:?}", e); | ||
Err(ErrorCode::InternalError.into()) | ||
}, | ||
} | ||
}) | ||
.expect("Failed to register native_submitPlainRequest method"); | ||
} | ||
|
||
fn handle_plain_request< | ||
'a, | ||
AccountId, | ||
Header, | ||
RpcClient: SubstrateRpcClient<AccountId, Header>, | ||
RpcClientFactory: SubstrateRpcClientFactory<AccountId, Header, RpcClient>, | ||
>( | ||
request: PlainRequest, | ||
ctx: Arc<RpcContext<AccountId, Header, RpcClient, RpcClientFactory>>, | ||
handle: Handle, | ||
) -> Result<(NativeCall, OmniAccountAuthType), ErrorObject<'a>> { | ||
if request.mrenclave != ctx.mrenclave { | ||
return Err(ErrorCode::ServerError(INVALID_MRENCLAVE_CODE).into()); | ||
} | ||
let nca = NativeCallAuthenticated::decode(&mut request.payload.as_slice()) | ||
.map_err(|_| ErrorCode::ServerError(INVALID_NATIVE_CALL_AUTHENTICATED_CODE))?; | ||
|
||
if verify_native_call_authenticated(ctx, handle, &nca).is_err() { | ||
return Err(ErrorCode::ServerError(AUTHENTICATION_FAILED_CODE).into()); | ||
} | ||
|
||
Ok((nca.call, nca.authentication.into())) | ||
} |
59 changes: 59 additions & 0 deletions
59
tee-worker/omni-executor/rpc-server/src/native_call_authenticated.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,59 @@ | ||
use crate::{ | ||
authentication::{ | ||
verify_auth_token_authentication, verify_email_authentication, | ||
verify_oauth2_authentication, verify_web3_authentication, Authentication, | ||
AuthenticationError, | ||
}, | ||
server::RpcContext, | ||
}; | ||
use executor_core::native_call::NativeCall; | ||
use executor_primitives::Nonce; | ||
use parentchain_rpc_client::{SubstrateRpcClient, SubstrateRpcClientFactory}; | ||
use parity_scale_codec::{Decode, Encode}; | ||
use std::sync::Arc; | ||
use tokio::runtime::Handle; | ||
|
||
#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq)] | ||
pub struct NativeCallAuthenticated { | ||
pub call: NativeCall, | ||
pub nonce: Nonce, | ||
pub authentication: Authentication, | ||
} | ||
|
||
pub fn verify_native_call_authenticated< | ||
AccountId, | ||
Header, | ||
RpcClient: SubstrateRpcClient<AccountId, Header>, | ||
RpcClientFactory: SubstrateRpcClientFactory<AccountId, Header, RpcClient>, | ||
>( | ||
ctx: Arc<RpcContext<AccountId, Header, RpcClient, RpcClientFactory>>, | ||
handle: Handle, | ||
authenticated_call: &NativeCallAuthenticated, | ||
) -> Result<(), AuthenticationError> { | ||
let authentication_result = match authenticated_call.authentication { | ||
Authentication::Web3(ref signature) => verify_web3_authentication( | ||
signature, | ||
&authenticated_call.call, | ||
authenticated_call.nonce, | ||
ctx.mrenclave, | ||
), | ||
Authentication::Email(ref verification_code) => verify_email_authentication( | ||
ctx, | ||
authenticated_call.call.sender_identity(), | ||
verification_code, | ||
), | ||
Authentication::OAuth2(ref oauth2_data) => verify_oauth2_authentication( | ||
ctx, | ||
handle, | ||
authenticated_call.call.sender_identity(), | ||
oauth2_data, | ||
), | ||
Authentication::AuthToken(ref auth_token) => verify_auth_token_authentication( | ||
ctx, | ||
handle, | ||
authenticated_call.call.sender_identity(), | ||
auth_token, | ||
), | ||
}; | ||
authentication_result | ||
} |
Oops, something went wrong.