From 1fd1efc011b4d51dcb08b422b540d18b955f18a6 Mon Sep 17 00:00:00 2001 From: Kai <7630809+Kailai-Wang@users.noreply.github.com> Date: Wed, 9 Oct 2024 00:45:11 +0200 Subject: [PATCH] Use encoded Identity for hashing (#3120) * Use encoded hash directly * small update * remove test - it is a bit verbose --- common/primitives/core/src/identity.rs | 13 ++----- parachain/pallets/omni-account/src/lib.rs | 11 +++--- parachain/pallets/omni-account/src/tests.rs | 40 ++++++++++----------- 3 files changed, 27 insertions(+), 37 deletions(-) diff --git a/common/primitives/core/src/identity.rs b/common/primitives/core/src/identity.rs index 3e5980a826..ffc9940cde 100644 --- a/common/primitives/core/src/identity.rs +++ b/common/primitives/core/src/identity.rs @@ -469,9 +469,8 @@ impl Identity { )) } - pub fn hash(&self) -> Result { - let did = self.to_did()?; - Ok(H256::from(blake2_256(&did.encode()))) + pub fn hash(&self) -> H256 { + self.using_encoded(blake2_256).into() } } @@ -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()))); - } } diff --git a/parachain/pallets/omni-account/src/lib.rs b/parachain/pallets/omni-account/src/lib.rs index bc29bef7c1..f2676ec01d 100644 --- a/parachain/pallets/omni-account/src/lib.rs +++ b/parachain/pallets/omni-account/src/lib.rs @@ -56,7 +56,7 @@ pub trait GetAccountStoreHash { impl GetAccountStoreHash for BoundedVec { fn hash(&self) -> H256 { let hashes: Vec = self.iter().map(|member| member.hash).collect(); - H256::from(blake2_256(&hashes.encode())) + hashes.using_encoded(blake2_256).into() } } @@ -169,7 +169,7 @@ pub mod pallet { AccountIsPrivate, EmptyAccount, AccountStoreHashMismatch, - AccountStoreHashMaissing, + AccountStoreHashMissing, } #[pallet::call] @@ -333,12 +333,12 @@ pub mod pallet { maybe_account_store_hash: Option, ) -> Result<(), Error> { let current_account_store_hash = - AccountStoreHash::::get(who).ok_or(Error::::AccountStoreHashMaissing)?; + AccountStoreHash::::get(who).ok_or(Error::::AccountStoreHashMissing)?; match maybe_account_store_hash { Some(h) => { ensure!(current_account_store_hash == h, Error::::AccountStoreHashMismatch); }, - None => return Err(Error::::AccountStoreHashMaissing), + None => return Err(Error::::AccountStoreHashMissing), } Ok(()) @@ -348,8 +348,7 @@ pub mod pallet { owner_identity: Identity, owner_account_id: T::AccountId, ) -> Result, Error> { - let owner_identity_hash = - owner_identity.hash().map_err(|_| Error::::InvalidAccount)?; + let owner_identity_hash = owner_identity.hash(); if MemberAccountHash::::contains_key(owner_identity_hash) { return Err(Error::::AccountAlreadyAdded); } diff --git a/parachain/pallets/omni-account/src/tests.rs b/parachain/pallets/omni-account/src/tests.rs index 7712940b0b..0e3855195a 100644 --- a/parachain/pallets/omni-account/src/tests.rs +++ b/parachain/pallets/omni-account/src/tests.rs @@ -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 = @@ -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 @@ -164,7 +164,7 @@ fn add_account_hash_checking_works() { charlie_member_account, None ), - Error::::AccountStoreHashMaissing + Error::::AccountStoreHashMissing ); }); } @@ -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( @@ -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( @@ -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]), @@ -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]), @@ -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]), @@ -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()), @@ -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())); @@ -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 }, ]); @@ -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 }, ]); @@ -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()); @@ -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()), @@ -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()),