Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding request_intent trusted call #3143

Merged
merged 35 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
dd7c83f
adding add_account trusted call
silva-fj Oct 21, 2024
e3371d1
refactoring email verification code and helpers to make them generic for
silva-fj Oct 21, 2024
5fe2e24
refactoring in_memory_store
silva-fj Oct 22, 2024
2ef4c1d
cleaning up omni-account repository
silva-fj Oct 22, 2024
97ce656
refactoring verification_code_store
silva-fj Oct 22, 2024
fd60367
adding omni_account_requestVerificationCode rpc method
silva-fj Oct 22, 2024
fd15914
using more descriptive names
silva-fj Oct 22, 2024
08b8ff6
adding dependencies
silva-fj Oct 22, 2024
2554852
adding trusted_call_authenticated
silva-fj Oct 22, 2024
d3c4839
removing unused imports
silva-fj Oct 22, 2024
1e5c5b8
Revert "adding add_account trusted call"
silva-fj Oct 22, 2024
3da4354
refactoring TrustedCallAuthenticated and verification methods
silva-fj Oct 22, 2024
624ce88
refactoring request parsing and adding authentication verification
silva-fj Oct 22, 2024
f2f0ce8
adding request_intent trusted call
silva-fj Oct 22, 2024
d85bdde
handling omni-account nonce in dispatch_as_omni_account extrinsic
silva-fj Oct 23, 2024
c3f0333
adding new intents
silva-fj Oct 23, 2024
8794ee1
adding license comments
silva-fj Oct 23, 2024
aac3e96
exposing substrate-api-client's ac_compose_macros
silva-fj Oct 24, 2024
a35bf6c
adding dependencies to native-task receiver
silva-fj Oct 24, 2024
4aa638a
updating types
silva-fj Oct 24, 2024
0825205
fixing typo
silva-fj Oct 24, 2024
d371343
handling request intent trusted call
silva-fj Oct 24, 2024
b6635c1
Merge remote-tracking branch 'origin/dev' into adding-omni-account-tr…
silva-fj Oct 24, 2024
6cf04d1
fixing clippy issues
silva-fj Oct 24, 2024
4b37dcc
fixing typo
silva-fj Oct 24, 2024
0d537a3
refatoring email code verification
silva-fj Oct 24, 2024
d56c701
fixing fmt issue
silva-fj Oct 24, 2024
20bb704
removing unused import
silva-fj Oct 24, 2024
ca18774
refactoring requestEmailVerificationCode
silva-fj Oct 25, 2024
da06ac4
fixing fmt issue
silva-fj Oct 25, 2024
57ecad1
refactoring in_memory_store
silva-fj Oct 25, 2024
1a6ebad
fixing build
silva-fj Oct 25, 2024
a1cfa82
removing nonce check
silva-fj Oct 25, 2024
855ac7b
cleaning up trusted call handlers
silva-fj Oct 25, 2024
e18386a
fixing clippy issues
silva-fj Oct 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Comment on lines +17 to +20
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is fine for the demo, how about having a more generic CallNative(Box<RuntimeCall>)?

}

#[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,
}
7 changes: 6 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 @@ -159,6 +159,7 @@ pub mod pallet {
AccountStoreLenLimitReached,
AccountNotFound,
InvalidAccount,
InvalidNonce,
UnknownAccountStore,
EmptyAccount,
RequireOmniExecutor,
Expand All @@ -173,11 +174,15 @@ pub mod pallet {
origin: OriginFor<T>,
member_account_hash: H256,
call: Box<<T as Config>::RuntimeCall>,
nonce: <T as frame_system::Config>::Nonce,
) -> DispatchResultWithPostInfo {
let _ = T::TEECallOrigin::ensure_origin(origin)?;
let omni_account = MemberAccountHash::<T>::get(member_account_hash)
.ok_or(Error::<T>::AccountNotFound)?;
let account_nonce = system::Pallet::<T>::account_nonce(&omni_account);
ensure!(nonce == account_nonce, Error::<T>::InvalidNonce);
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
116 changes: 95 additions & 21 deletions parachain/pallets/omni-account/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ fn add_account_without_creating_store_fails() {
new_test_ext().execute_with(|| {
let tee_signer = get_tee_signer();
let call = add_account_call(private_member_account(bob()));
let nonce = 0;

assert_noop!(
OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
nonce,
),
Error::<TestRuntime>::AccountNotFound
);
Expand All @@ -101,10 +103,12 @@ fn add_account_works() {
));

let call = add_account_call(bob.clone());
let mut nonce = 0;
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
nonce
));

System::assert_has_event(
Expand All @@ -121,10 +125,12 @@ fn add_account_works() {
);

let call = add_account_call(charlie.clone());
nonce += 1;
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
nonce,
));

System::assert_has_event(
Expand Down Expand Up @@ -177,16 +183,20 @@ fn add_account_with_already_linked_account_fails() {
));

let call = add_account_call(bob.clone());
let mut alice_nonce = 0;
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call.clone()
call.clone(),
alice_nonce,
));

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

System::assert_has_event(
Expand All @@ -208,10 +218,12 @@ fn add_account_with_already_linked_account_fails() {
));

let call = add_account_call(public_member_account(alice()));
let charlie_nonce = 0;
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
charlie().identity.hash(),
call
call,
charlie_nonce
));

System::assert_has_event(
Expand Down Expand Up @@ -252,11 +264,13 @@ fn add_account_store_len_limit_reached_works() {
vec![7, 8, 9],
H256::from(blake2_256(&[7, 8, 9])),
));
let nonce = 0;

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

System::assert_has_event(
Expand Down Expand Up @@ -285,10 +299,12 @@ fn remove_account_works() {
));

let call = add_account_call(bob.clone());
let mut nonce = 0;
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
nonce
));

// normal signed origin should give `BadOrigin`, no matter
Expand All @@ -310,10 +326,12 @@ fn remove_account_works() {
);

let call = remove_accounts_call(vec![bob.hash()]);
nonce += 1;
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
nonce
));

System::assert_has_event(
Expand Down Expand Up @@ -342,10 +360,12 @@ fn remove_account_works() {
assert!(!MemberAccountHash::<TestRuntime>::contains_key(bob.hash()));

let call = remove_accounts_call(vec![alice().identity.hash()]);
nonce += 1;
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
nonce
));

assert!(!AccountStore::<TestRuntime>::contains_key(alice().omni_account));
Expand All @@ -364,25 +384,29 @@ fn remove_account_empty_account_check_works() {

let bob = private_member_account(bob());
let call = add_account_call(bob);
let mut nonce = 0;
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
nonce
));

let call = remove_accounts_call(vec![]);
nonce += 1;
// 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,
nonce
));
System::assert_has_event(
Event::DispatchedAsOmniAccount {
who: alice().omni_account,
result: Err(DispatchError::Module(ModuleError {
index: 5,
error: [5, 0, 0, 0],
error: [6, 0, 0, 0],
message: Some("EmptyAccount"),
})),
}
Expand All @@ -404,10 +428,12 @@ fn publicize_account_works() {
));

let call = add_account_call(private_bob.clone());
let mut nonce = 0;
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
nonce
));

let expected_member_accounts: MemberAccounts<TestRuntime> =
Expand All @@ -418,10 +444,12 @@ fn publicize_account_works() {
);

let call = publicize_account_call(bob().identity);
nonce += 1;
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
nonce
));

System::assert_has_event(
Expand Down Expand Up @@ -456,11 +484,13 @@ fn publicize_account_identity_not_found_works() {
let bob = private_member_account(bob());

let call = publicize_account_call(charlie().identity);
let mut nonce = 0;
assert_noop!(
OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
nonce
),
Error::<TestRuntime>::AccountNotFound
);
Expand All @@ -474,14 +504,17 @@ fn publicize_account_identity_not_found_works() {
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
nonce
));

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

let call = add_account_call(bob);
let mut nonce = 0;
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
nonce
));

let intent =
Intent::CallEthereum(CallEthereum { address: H160::zero(), input: BoundedVec::new() });

let call = request_intent_call(intent.clone());
nonce += 1;
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
nonce
));

System::assert_has_event(
Expand Down Expand Up @@ -556,10 +593,12 @@ fn dispatch_as_signed_works() {
));

let call = add_account_call(private_member_account(bob()));
let nonce = 0;
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call
call,
nonce
));

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

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

let bob = private_member_account(bob());
let charlie = public_member_account(charlie());

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

let call = add_account_call(bob.clone());
let nonce = 0;
assert_ok!(OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call,
nonce
));

let call = add_account_call(charlie.clone());
assert_noop!(
OmniAccount::dispatch_as_omni_account(
RuntimeOrigin::signed(tee_signer.clone()),
alice().identity.hash(),
call,
nonce,
),
Error::<TestRuntime>::InvalidNonce
);
});
}
Loading
Loading