Skip to content

Commit

Permalink
Use encoded Identity for hashing (#3120)
Browse files Browse the repository at this point in the history
* Use encoded hash directly

* small update

* remove test - it is a bit verbose
  • Loading branch information
Kailai-Wang authored Oct 8, 2024
1 parent b36e50c commit 1fd1efc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 37 deletions.
13 changes: 2 additions & 11 deletions common/primitives/core/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,8 @@ impl Identity {
))
}

pub fn hash(&self) -> Result<H256, &'static str> {
let did = self.to_did()?;
Ok(H256::from(blake2_256(&did.encode())))
pub fn hash(&self) -> H256 {
self.using_encoded(blake2_256).into()
}
}

Expand Down Expand Up @@ -796,12 +795,4 @@ mod tests {
assert_eq!(identity.to_did().unwrap(), did.as_str());
assert_eq!(Identity::from_did(did.as_str()).unwrap(), identity);
}

#[test]
fn test_identity_hash() {
let identity = Identity::Substrate([0; 32].into());
let did_str = "did:litentry:substrate:0x0000000000000000000000000000000000000000000000000000000000000000";
let hash = identity.hash().unwrap();
assert_eq!(hash, H256::from(blake2_256(&did_str.encode())));
}
}
11 changes: 5 additions & 6 deletions parachain/pallets/omni-account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub trait GetAccountStoreHash {
impl<T> GetAccountStoreHash for BoundedVec<MemberAccount, T> {
fn hash(&self) -> H256 {
let hashes: Vec<H256> = self.iter().map(|member| member.hash).collect();
H256::from(blake2_256(&hashes.encode()))
hashes.using_encoded(blake2_256).into()
}
}

Expand Down Expand Up @@ -169,7 +169,7 @@ pub mod pallet {
AccountIsPrivate,
EmptyAccount,
AccountStoreHashMismatch,
AccountStoreHashMaissing,
AccountStoreHashMissing,
}

#[pallet::call]
Expand Down Expand Up @@ -333,12 +333,12 @@ pub mod pallet {
maybe_account_store_hash: Option<H256>,
) -> Result<(), Error<T>> {
let current_account_store_hash =
AccountStoreHash::<T>::get(who).ok_or(Error::<T>::AccountStoreHashMaissing)?;
AccountStoreHash::<T>::get(who).ok_or(Error::<T>::AccountStoreHashMissing)?;
match maybe_account_store_hash {
Some(h) => {
ensure!(current_account_store_hash == h, Error::<T>::AccountStoreHashMismatch);
},
None => return Err(Error::<T>::AccountStoreHashMaissing),
None => return Err(Error::<T>::AccountStoreHashMissing),
}

Ok(())
Expand All @@ -348,8 +348,7 @@ pub mod pallet {
owner_identity: Identity,
owner_account_id: T::AccountId,
) -> Result<MemberAccounts<T>, Error<T>> {
let owner_identity_hash =
owner_identity.hash().map_err(|_| Error::<T>::InvalidAccount)?;
let owner_identity_hash = owner_identity.hash();
if MemberAccountHash::<T>::contains_key(owner_identity_hash) {
return Err(Error::<T>::AccountAlreadyAdded);
}
Expand Down
40 changes: 20 additions & 20 deletions parachain/pallets/omni-account/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ fn add_account_works() {

let who = alice();
let who_identity = Identity::from(who.clone());
let who_identity_hash = who_identity.hash().unwrap();
let who_identity_hash = who_identity.hash();

let bob_member_account = MemberAccount {
id: MemberIdentity::Private(bob().encode()),
hash: Identity::from(bob()).hash().unwrap(),
hash: Identity::from(bob()).hash(),
};
let charlie_member_account = MemberAccount {
id: MemberIdentity::Public(Identity::from(charlie())),
hash: Identity::from(charlie()).hash().unwrap(),
hash: Identity::from(charlie()).hash(),
};

let expected_member_accounts: MemberAccounts<TestRuntime> =
Expand Down Expand Up @@ -141,11 +141,11 @@ fn add_account_hash_checking_works() {

let bob_member_account = MemberAccount {
id: MemberIdentity::Private(bob().encode()),
hash: Identity::from(bob()).hash().unwrap(),
hash: Identity::from(bob()).hash(),
};
let charlie_member_account = MemberAccount {
id: MemberIdentity::Public(Identity::from(charlie())),
hash: Identity::from(charlie()).hash().unwrap(),
hash: Identity::from(charlie()).hash(),
};

// AccountStore gets created with the first account
Expand All @@ -164,7 +164,7 @@ fn add_account_hash_checking_works() {
charlie_member_account,
None
),
Error::<TestRuntime>::AccountStoreHashMaissing
Error::<TestRuntime>::AccountStoreHashMissing
);
});
}
Expand Down Expand Up @@ -194,7 +194,7 @@ fn add_account_already_linked_works() {

let member_account = MemberAccount {
id: MemberIdentity::Public(Identity::from(bob())),
hash: Identity::from(bob()).hash().unwrap(),
hash: Identity::from(bob()).hash(),
};

assert_ok!(OmniAccount::add_account(
Expand All @@ -217,7 +217,7 @@ fn add_account_already_linked_works() {
let who = Identity::from(bob());
let alice_member_account = MemberAccount {
id: MemberIdentity::Public(Identity::from(alice())),
hash: Identity::from(alice()).hash().unwrap(),
hash: Identity::from(alice()).hash(),
};
assert_noop!(
OmniAccount::add_account(
Expand All @@ -238,7 +238,7 @@ fn add_account_store_len_limit_reached_works() {

let who = alice();
let who_identity = Identity::from(who.clone());
let who_identity_hash = who_identity.hash().unwrap();
let who_identity_hash = who_identity.hash();

let member_account_2 = MemberAccount {
id: MemberIdentity::Private(vec![1, 2, 3]),
Expand Down Expand Up @@ -284,7 +284,7 @@ fn add_account_store_hash_mismatch_works() {

let who = alice();
let who_identity = Identity::from(who.clone());
let who_identity_hash = who_identity.hash().unwrap();
let who_identity_hash = who_identity.hash();

let member_account = MemberAccount {
id: MemberIdentity::Private(vec![1, 2, 3]),
Expand Down Expand Up @@ -358,7 +358,7 @@ fn remove_account_works() {
let tee_signer = get_tee_signer();
let who = alice();
let who_identity = Identity::from(who.clone());
let who_identity_hash = who_identity.hash().unwrap();
let who_identity_hash = who_identity.hash();

let member_account = MemberAccount {
id: MemberIdentity::Private(vec![1, 2, 3]),
Expand Down Expand Up @@ -428,7 +428,7 @@ fn remove_account_empty_account_check_works() {
let tee_signer = get_tee_signer();
let who = alice();
let who_identity = Identity::from(who.clone());
let who_identity_hash = who_identity.hash().unwrap();
let who_identity_hash = who_identity.hash();

assert_ok!(OmniAccount::add_account(
RuntimeOrigin::signed(tee_signer.clone()),
Expand Down Expand Up @@ -467,7 +467,7 @@ fn publicize_account_works() {
let tee_signer = get_tee_signer();
let who = alice();
let who_identity = Identity::from(who.clone());
let who_identity_hash = who_identity.hash().unwrap();
let who_identity_hash = who_identity.hash();

let private_identity = MemberIdentity::Private(vec![1, 2, 3]);
let public_identity = MemberIdentity::Public(Identity::from(bob()));
Expand All @@ -485,7 +485,7 @@ fn publicize_account_works() {
BoundedVec::truncate_from(vec![
MemberAccount {
id: MemberIdentity::Public(who_identity.clone()),
hash: who_identity.hash().unwrap(),
hash: who_identity.hash(),
},
MemberAccount { id: private_identity.clone(), hash: identity_hash },
]);
Expand All @@ -512,7 +512,7 @@ fn publicize_account_works() {
BoundedVec::truncate_from(vec![
MemberAccount {
id: MemberIdentity::Public(who_identity.clone()),
hash: who_identity.hash().unwrap(),
hash: who_identity.hash(),
},
MemberAccount { id: public_identity.clone(), hash: identity_hash },
]);
Expand All @@ -526,7 +526,7 @@ fn publicize_account_identity_not_found_works() {
let tee_signer = get_tee_signer();
let who = alice();
let who_identity = Identity::from(who.clone());
let who_identity_hash = who_identity.hash().unwrap();
let who_identity_hash = who_identity.hash();

let private_identity = MemberIdentity::Private(vec![1, 2, 3]);
let identity = Identity::from(bob());
Expand Down Expand Up @@ -582,10 +582,10 @@ fn publicize_account_identity_is_private_check_works() {
let tee_signer = get_tee_signer();
let who = alice();
let who_identity = Identity::from(who.clone());
let who_identity_hash = who_identity.hash().unwrap();
let who_identity_hash = who_identity.hash();

let private_identity = MemberIdentity::Private(vec![1, 2, 3]);
let identity_hash = Identity::from(bob()).hash().unwrap();
let identity_hash = Identity::from(bob()).hash();

assert_ok!(OmniAccount::add_account(
RuntimeOrigin::signed(tee_signer.clone()),
Expand Down Expand Up @@ -620,10 +620,10 @@ fn dispatch_as_signed_works() {
let tee_signer = get_tee_signer();
let who = alice();
let who_identity = Identity::from(who.clone());
let who_identity_hash = who_identity.hash().unwrap();
let who_identity_hash = who_identity.hash();

let private_identity = MemberIdentity::Private(vec![1, 2, 3]);
let identity_hash = Identity::from(bob()).hash().unwrap();
let identity_hash = Identity::from(bob()).hash();

assert_ok!(OmniAccount::add_account(
RuntimeOrigin::signed(tee_signer.clone()),
Expand Down

0 comments on commit 1fd1efc

Please sign in to comment.