Skip to content

Commit a16626c

Browse files
authored
Make OmniAccount derivation on chain verifiable (#3511)
1 parent 7d24e2f commit a16626c

File tree

26 files changed

+215
-300
lines changed

26 files changed

+215
-300
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ rococo-fork.json
2222
**/.env
2323

2424
rust-analyzer.json
25+
26+
*.local.md

parachain/pallets/omni-account/runtime-api/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616

1717
#![cfg_attr(not(feature = "std"), no_std)]
1818

19+
#[cfg(not(feature = "std"))]
20+
extern crate alloc;
21+
22+
#[cfg(not(feature = "std"))]
23+
use alloc::string::String;
24+
25+
#[cfg(feature = "std")]
26+
use std::string::String;
27+
1928
use heima_primitives::Identity;
2029
use parity_scale_codec::Codec;
2130

@@ -24,6 +33,6 @@ sp_api::decl_runtime_apis! {
2433
where
2534
AccountId: Codec,
2635
{
27-
fn omni_account(id: Identity) -> AccountId;
36+
fn omni_account(client_id: String, id: Identity) -> AccountId;
2837
}
2938
}

parachain/pallets/omni-account/src/lib.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616

1717
#![cfg_attr(not(feature = "std"), no_std)]
1818

19+
#[cfg(not(feature = "std"))]
20+
extern crate alloc;
21+
22+
#[cfg(not(feature = "std"))]
23+
use alloc::string::String;
24+
25+
#[cfg(feature = "std")]
26+
use std::string::String;
27+
1928
#[cfg(test)]
2029
mod mock;
2130
#[cfg(test)]
@@ -323,11 +332,12 @@ pub mod pallet {
323332
#[pallet::weight((195_000_000, DispatchClass::Normal))]
324333
pub fn create_account_store(
325334
origin: OriginFor<T>,
335+
client_id: String,
326336
identity: Identity,
327337
) -> DispatchResultWithPostInfo {
328338
// initial creation request has to come from `TEECallOrigin`
329339
let _ = T::TEECallOrigin::ensure_origin(origin)?;
330-
let _ = Self::do_create_account_store(identity)?;
340+
let _ = Self::do_create_account_store(identity, &client_id)?;
331341
Ok(Pays::No.into())
332342
}
333343

@@ -449,16 +459,17 @@ pub mod pallet {
449459
#[pallet::weight((195_000_000, DispatchClass::Normal))]
450460
pub fn update_account_store_by_one(
451461
origin: OriginFor<T>,
462+
client_id: String,
452463
who: Identity,
453464
member_account: MemberAccount,
454465
) -> DispatchResultWithPostInfo {
455466
let _ = T::TEECallOrigin::ensure_origin(origin.clone())?;
456467

457-
let who_account = T::OmniAccountConverter::convert(&who);
468+
let who_account = T::OmniAccountConverter::convert(&who, &client_id);
458469

459470
let mut member_accounts = match AccountStore::<T>::get(&who_account) {
460471
Some(s) => s,
461-
None => Self::do_create_account_store(who)?,
472+
None => Self::do_create_account_store(who, &client_id)?,
462473
};
463474

464475
if !member_accounts.contains(&member_account) {
@@ -556,18 +567,21 @@ pub mod pallet {
556567
/// Given an `Identity`, get its derived OmniAccount:
557568
/// - if the given Identity is a member Identity of some AccountStore, get its belonged OmniAccount
558569
/// - directly derive it otherwise
559-
pub fn omni_account(identity: Identity) -> T::AccountId {
570+
pub fn omni_account(client_id: String, identity: Identity) -> T::AccountId {
560571
let hash = identity.hash();
561572
if let Some(account) = MemberAccountHash::<T>::get(hash) {
562573
account
563574
} else {
564-
T::OmniAccountConverter::convert(&identity)
575+
T::OmniAccountConverter::convert(&identity, &client_id)
565576
}
566577
}
567578

568-
fn do_create_account_store(identity: Identity) -> Result<MemberAccounts<T>, Error<T>> {
579+
fn do_create_account_store(
580+
identity: Identity,
581+
client_id: &str,
582+
) -> Result<MemberAccounts<T>, Error<T>> {
569583
let hash = identity.hash();
570-
let omni_account = T::OmniAccountConverter::convert(&identity);
584+
let omni_account = T::OmniAccountConverter::convert(&identity, client_id);
571585

572586
ensure!(!MemberAccountHash::<T>::contains_key(hash), Error::<T>::AccountAlreadyAdded);
573587
ensure!(

parachain/pallets/omni-account/src/mock.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ use sp_runtime::{
3333
};
3434
use sp_std::marker::PhantomData;
3535

36+
// Test client_id constant
37+
pub const TEST_CLIENT_ID: &str = "test_client";
38+
3639
pub type Signature = sp_runtime::MultiSignature;
3740
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
3841
pub type Balance = u64;
@@ -82,7 +85,7 @@ pub struct Accounts {
8285
fn create_accounts(keyring: AccountKeyring) -> Accounts {
8386
let native_account = keyring.to_account_id();
8487
let identity = Identity::from(native_account.clone());
85-
Accounts { native_account, omni_account: identity.to_omni_account(), identity }
88+
Accounts { native_account, omni_account: identity.to_omni_account("test_client"), identity }
8689
}
8790

8891
pub fn alice() -> Accounts {

parachain/pallets/omni-account/src/tests.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ fn create_account_store_works() {
6666

6767
assert_ok!(OmniAccount::create_account_store(
6868
RuntimeOrigin::signed(tee_signer.clone()),
69+
TEST_CLIENT_ID.to_string(),
6970
alice().identity,
7071
));
7172

@@ -83,7 +84,11 @@ fn create_account_store_works() {
8384

8485
// create it the second time will fail
8586
assert_noop!(
86-
OmniAccount::create_account_store(RuntimeOrigin::signed(tee_signer), alice().identity),
87+
OmniAccount::create_account_store(
88+
RuntimeOrigin::signed(tee_signer),
89+
TEST_CLIENT_ID.to_string(),
90+
alice().identity
91+
),
8792
Error::<Test>::AccountAlreadyAdded
8893
);
8994
});
@@ -120,6 +125,7 @@ fn add_account_works() {
120125

121126
assert_ok!(OmniAccount::create_account_store(
122127
RuntimeOrigin::signed(tee_signer.clone()),
128+
TEST_CLIENT_ID.to_string(),
123129
alice().identity,
124130
));
125131

@@ -217,6 +223,7 @@ fn add_account_with_already_linked_account_fails() {
217223

218224
assert_ok!(OmniAccount::create_account_store(
219225
RuntimeOrigin::signed(tee_signer.clone()),
226+
TEST_CLIENT_ID.to_string(),
220227
alice().identity.clone(),
221228
));
222229

@@ -251,6 +258,7 @@ fn add_account_with_already_linked_account_fails() {
251258
// intent to create a new AccountStore with an account that is already added
252259
assert_ok!(OmniAccount::create_account_store(
253260
RuntimeOrigin::signed(tee_signer.clone()),
261+
TEST_CLIENT_ID.to_string(),
254262
charlie().identity,
255263
));
256264

@@ -284,6 +292,7 @@ fn add_account_store_len_limit_reached_works() {
284292

285293
assert_ok!(OmniAccount::create_account_store(
286294
RuntimeOrigin::signed(tee_signer.clone()),
295+
TEST_CLIENT_ID.to_string(),
287296
alice().identity,
288297
));
289298

@@ -332,6 +341,7 @@ fn remove_account_works() {
332341

333342
assert_ok!(OmniAccount::create_account_store(
334343
RuntimeOrigin::signed(tee_signer.clone()),
344+
TEST_CLIENT_ID.to_string(),
335345
alice().identity,
336346
));
337347

@@ -422,6 +432,7 @@ fn remove_account_empty_account_check_works() {
422432

423433
assert_ok!(OmniAccount::create_account_store(
424434
RuntimeOrigin::signed(tee_signer.clone()),
435+
TEST_CLIENT_ID.to_string(),
425436
alice().identity,
426437
));
427438

@@ -466,6 +477,7 @@ fn publicize_account_works() {
466477

467478
assert_ok!(OmniAccount::create_account_store(
468479
RuntimeOrigin::signed(tee_signer.clone()),
480+
TEST_CLIENT_ID.to_string(),
469481
alice().identity.clone(),
470482
));
471483

@@ -545,6 +557,7 @@ fn publicize_account_identity_not_found_works() {
545557

546558
assert_ok!(OmniAccount::create_account_store(
547559
RuntimeOrigin::signed(tee_signer.clone()),
560+
TEST_CLIENT_ID.to_string(),
548561
alice().identity,
549562
));
550563

@@ -586,6 +599,7 @@ fn request_intent_works() {
586599

587600
assert_ok!(OmniAccount::create_account_store(
588601
RuntimeOrigin::signed(tee_signer.clone()),
602+
TEST_CLIENT_ID.to_string(),
589603
alice().identity
590604
));
591605

@@ -636,6 +650,7 @@ fn dispatch_as_signed_works() {
636650

637651
assert_ok!(OmniAccount::create_account_store(
638652
RuntimeOrigin::signed(tee_signer.clone()),
653+
TEST_CLIENT_ID.to_string(),
639654
alice().identity,
640655
));
641656

@@ -676,6 +691,7 @@ fn dispatch_as_omni_account_increments_omni_account_nonce() {
676691

677692
assert_ok!(OmniAccount::create_account_store(
678693
RuntimeOrigin::signed(tee_signer.clone()),
694+
TEST_CLIENT_ID.to_string(),
679695
alice().identity,
680696
));
681697

@@ -705,6 +721,7 @@ fn dispatch_as_signed_account_increments_omni_account_nonce() {
705721

706722
assert_ok!(OmniAccount::create_account_store(
707723
RuntimeOrigin::signed(tee_signer.clone()),
724+
TEST_CLIENT_ID.to_string(),
708725
alice().identity,
709726
));
710727

@@ -730,6 +747,7 @@ fn ensure_permission_works() {
730747

731748
assert_ok!(OmniAccount::create_account_store(
732749
RuntimeOrigin::signed(tee_signer.clone()),
750+
TEST_CLIENT_ID.to_string(),
733751
alice().identity,
734752
));
735753

@@ -844,6 +862,7 @@ fn set_permissions_works() {
844862

845863
assert_ok!(OmniAccount::create_account_store(
846864
RuntimeOrigin::signed(tee_signer.clone()),
865+
TEST_CLIENT_ID.to_string(),
847866
alice().identity,
848867
));
849868

@@ -944,6 +963,7 @@ fn set_permissions_with_only_one_member_fails() {
944963

945964
assert_ok!(OmniAccount::create_account_store(
946965
RuntimeOrigin::signed(tee_signer.clone()),
966+
TEST_CLIENT_ID.to_string(),
947967
alice().identity,
948968
));
949969

@@ -972,6 +992,7 @@ fn set_permissions_with_no_member_with_default_permissions_fails() {
972992

973993
assert_ok!(OmniAccount::create_account_store(
974994
RuntimeOrigin::signed(tee_signer.clone()),
995+
TEST_CLIENT_ID.to_string(),
975996
alice().identity,
976997
));
977998

@@ -1015,12 +1036,12 @@ fn auth_token_requested_works() {
10151036

10161037
assert_ok!(OmniAccount::auth_token_requested(
10171038
RuntimeOrigin::signed(tee_signer.clone()),
1018-
alice().identity.to_omni_account(),
1039+
alice().identity.to_omni_account(""),
10191040
10
10201041
));
10211042

10221043
System::assert_last_event(
1023-
Event::AuthTokenRequested { who: alice().identity.to_omni_account(), expires_at: 10 }
1044+
Event::AuthTokenRequested { who: alice().identity.to_omni_account(""), expires_at: 10 }
10241045
.into(),
10251046
);
10261047
});
@@ -1034,6 +1055,7 @@ fn create_account_store_already_exists_fails() {
10341055
// Create account store with alice
10351056
assert_ok!(OmniAccount::create_account_store(
10361057
RuntimeOrigin::signed(tee_signer.clone()),
1058+
TEST_CLIENT_ID.to_string(),
10371059
alice().identity,
10381060
));
10391061

@@ -1058,7 +1080,11 @@ fn create_account_store_already_exists_fails() {
10581080
));
10591081

10601082
assert_noop!(
1061-
OmniAccount::create_account_store(RuntimeOrigin::signed(tee_signer), alice().identity),
1083+
OmniAccount::create_account_store(
1084+
RuntimeOrigin::signed(tee_signer),
1085+
TEST_CLIENT_ID.to_string(),
1086+
alice().identity
1087+
),
10621088
Error::<Test>::AccountStoreAlreadyExists
10631089
);
10641090
});

parachain/pallets/vc-management/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ fn on_vc_issued_works() {
355355
new_test_ext().execute_with(|| {
356356
let signer: SystemAccountId = get_signer(TEST8_SIGNER_PUB);
357357
let alice: Identity = get_signer(ALICE_PUBKEY);
358-
let omni_account = alice.to_omni_account();
358+
let omni_account = alice.to_omni_account("");
359359
assert_ok!(VCManagement::on_vc_issued(
360360
RuntimeOrigin::signed(signer),
361361
alice.clone(),

0 commit comments

Comments
 (0)