Skip to content

Commit

Permalink
Adding request_intent trusted call (#3143)
Browse files Browse the repository at this point in the history
  • Loading branch information
silva-fj authored Oct 25, 2024
1 parent 64136cb commit 3d496aa
Show file tree
Hide file tree
Showing 25 changed files with 564 additions and 175 deletions.
4 changes: 4 additions & 0 deletions common/primitives/core/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,10 @@ impl Identity {
pub fn hash(&self) -> H256 {
self.using_encoded(blake2_256).into()
}

pub fn from_email(email: &str) -> Self {
Identity::Email(IdentityString::new(email.as_bytes().to_vec()))
}
}

impl From<ed25519::Public> for Identity {
Expand Down
13 changes: 13 additions & 0 deletions common/primitives/core/src/intent.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
use crate::{AccountId, Balance};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::H160;
use sp_runtime::{traits::ConstU32, BoundedVec};

pub const CALL_ETHEREUM_INPUT_LEN: u32 = 10 * 1024;

pub const MAX_REMARK_LEN: u32 = u32::max_value();

#[derive(Encode, Decode, Debug, Clone, PartialEq, Eq, MaxEncodedLen, TypeInfo)]
pub enum Intent {
#[codec(index = 0)]
TransferEthereum(TransferEthereum),
#[codec(index = 1)]
CallEthereum(CallEthereum),
#[codec(index = 2)]
SystemRemark(BoundedVec<u8, ConstU32<MAX_REMARK_LEN>>),
#[codec(index = 3)]
TransferNative(TransferNative),
}

#[derive(Encode, Decode, Debug, Clone, PartialEq, Eq, MaxEncodedLen, TypeInfo)]
Expand All @@ -26,3 +33,9 @@ pub struct CallEthereum {
pub address: H160,
pub input: BoundedVec<u8, CallEthereumInputLen>,
}

#[derive(Encode, Decode, Debug, Clone, PartialEq, Eq, MaxEncodedLen, TypeInfo)]
pub struct TransferNative {
pub to: AccountId,
pub value: Balance,
}
3 changes: 2 additions & 1 deletion parachain/pallets/omni-account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod mock;
mod tests;

pub use core_primitives::{Identity, Intent, MemberAccount, OmniAccountConverter};
pub use frame_system::pallet_prelude::BlockNumberFor;
pub use frame_system::{self as system, pallet_prelude::BlockNumberFor};
pub use pallet::*;

use frame_support::pallet_prelude::*;
Expand Down Expand Up @@ -178,6 +178,7 @@ pub mod pallet {
let omni_account = MemberAccountHash::<T>::get(member_account_hash)
.ok_or(Error::<T>::AccountNotFound)?;
let result = call.dispatch(RawOrigin::OmniAccount(omni_account.clone()).into());
system::Pallet::<T>::inc_account_nonce(&omni_account);
Self::deposit_event(Event::DispatchedAsOmniAccount {
who: omni_account,
result: result.map(|_| ()).map_err(|e| e.error),
Expand Down
64 changes: 44 additions & 20 deletions parachain/pallets/omni-account/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn add_account_without_creating_store_fails() {
OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
),
Error::<TestRuntime>::AccountNotFound
);
Expand All @@ -104,7 +104,7 @@ fn add_account_works() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));

System::assert_has_event(
Expand All @@ -124,7 +124,7 @@ fn add_account_works() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));

System::assert_has_event(
Expand Down Expand Up @@ -180,13 +180,13 @@ fn add_account_with_already_linked_account_fails() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call.clone()
call.clone(),
));

assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));

System::assert_has_event(
Expand All @@ -211,7 +211,7 @@ fn add_account_with_already_linked_account_fails() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
charlie().identity.hash(),
call
call,
));

System::assert_has_event(
Expand Down Expand Up @@ -256,7 +256,7 @@ fn add_account_store_len_limit_reached_works() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));

System::assert_has_event(
Expand Down Expand Up @@ -288,7 +288,7 @@ fn remove_account_works() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));

// normal signed origin should give `BadOrigin`, no matter
Expand All @@ -313,7 +313,7 @@ fn remove_account_works() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));

System::assert_has_event(
Expand Down Expand Up @@ -345,7 +345,7 @@ fn remove_account_works() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));

assert!(!AccountStore::<TestRuntime>::contains_key(alice().omni_account));
Expand All @@ -367,15 +367,15 @@ fn remove_account_empty_account_check_works() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));

let call = remove_accounts_call(vec![]);
// execution itself is ok, but error is shown in the dispatch result
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));
System::assert_has_event(
Event::DispatchedAsOmniAccount {
Expand Down Expand Up @@ -407,7 +407,7 @@ fn publicize_account_works() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));

let expected_member_accounts: MemberAccounts<TestRuntime> =
Expand All @@ -421,7 +421,7 @@ fn publicize_account_works() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));

System::assert_has_event(
Expand Down Expand Up @@ -460,7 +460,7 @@ fn publicize_account_identity_not_found_works() {
OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
),
Error::<TestRuntime>::AccountNotFound
);
Expand All @@ -474,14 +474,14 @@ fn publicize_account_identity_not_found_works() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));

let call = publicize_account_call(charlie().identity);
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));
System::assert_has_event(
Event::DispatchedAsOmniAccount {
Expand Down Expand Up @@ -512,7 +512,7 @@ fn request_intent_works() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));

let intent =
Expand All @@ -522,7 +522,7 @@ fn request_intent_works() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));

System::assert_has_event(
Expand Down Expand Up @@ -559,7 +559,7 @@ fn dispatch_as_signed_works() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
));

let call = make_balance_transfer_call(bob().native_account, 5);
Expand All @@ -576,3 +576,27 @@ fn dispatch_as_signed_works() {
assert_eq!(Balances::free_balance(bob().native_account), 5);
});
}

#[test]
fn dispatch_as_omni_account_increments_omni_account_nonce() {
new_test_ext().execute_with(|| {
let tee_signer = get_tee_signer();

let bob = private_member_account(bob());

assert_ok!(OmniAccount::create_account_store(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity,
));

assert_eq!(System::account_nonce(alice().omni_account), 0);

let call = add_account_call(bob.clone());
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call,
));
assert_eq!(System::account_nonce(alice().omni_account), 1);
});
}
Loading

0 comments on commit 3d496aa

Please sign in to comment.