diff --git a/Cargo.lock b/Cargo.lock index b60282ca9..adf473b2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1509,6 +1509,8 @@ dependencies = [ name = "cord-identifier" version = "0.9.1" dependencies = [ + "base58", + "blake2-rfc", "cord-primitives", "cord-utilities", "frame-benchmarking", @@ -1685,11 +1687,6 @@ dependencies = [ name = "cord-primitives" version = "0.9.1" dependencies = [ - "base58", - "blake2-rfc", - "frame-support", - "parity-scale-codec", - "scale-info", "sp-core", "sp-runtime", "sp-std 8.0.0 (git+https://github.com/dhiway/substrate-sdk?branch=release-v1.5.0)", diff --git a/pallets/asset/src/lib.rs b/pallets/asset/src/lib.rs index 64f73a3ec..bf6dc89a7 100644 --- a/pallets/asset/src/lib.rs +++ b/pallets/asset/src/lib.rs @@ -32,9 +32,10 @@ use sp_runtime::traits::UniqueSaturatedInto; #[frame_support::pallet] pub mod pallet { use super::*; - pub use cord_primitives::{curi::Ss58Identifier, CountOf, RatingOf}; + pub use cord_primitives::{CountOf, RatingOf}; use frame_support::{pallet_prelude::*, Twox64Concat}; use frame_system::pallet_prelude::*; + pub use identifier::{IdentifierCreator, IdentifierTimeline, IdentifierType, Ss58Identifier}; use sp_runtime::{ traits::{Hash, IdentifyAccount, Verify, Zero}, BoundedVec, @@ -221,8 +222,9 @@ pub mod pallet { &[&digest.encode()[..], &issuer.encode()[..]].concat()[..], ); - let identifier = Ss58Identifier::to_asset_id(&(id_digest).encode()[..]) - .map_err(|_| Error::::InvalidIdentifierLength)?; + let identifier = + Ss58Identifier::create_identifier(&(id_digest).encode()[..], IdentifierType::Asset) + .map_err(|_| Error::::InvalidIdentifierLength)?; ensure!(!>::contains_key(&identifier), Error::::AssetIdAlreadyExists); @@ -288,8 +290,11 @@ pub mod pallet { .concat()[..], ); - let instance_id = Ss58Identifier::to_asset_instance_id(&(id_digest).encode()[..]) - .map_err(|_| Error::::InvalidIdentifierLength)?; + let instance_id = Ss58Identifier::create_identifier( + &(id_digest).encode()[..], + IdentifierType::AssetInstance, + ) + .map_err(|_| Error::::InvalidIdentifierLength)?; let block_number = frame_system::Pallet::::block_number(); @@ -391,7 +396,7 @@ impl Pallet { let tx_moment = Self::timepoint(); let tx_entry = EventEntryOf { action: tx_action, location: tx_moment }; - let _ = identifier::Pallet::::update_timeline(tx_id, IdentifierTypeOf::Asset, tx_entry); + let _ = IdentifierTimeline::update_timeline::(tx_id, IdentifierTypeOf::Asset, tx_entry); Ok(()) } diff --git a/pallets/chain-space/src/benchmarking.rs b/pallets/chain-space/src/benchmarking.rs index e3df94329..a16854a64 100644 --- a/pallets/chain-space/src/benchmarking.rs +++ b/pallets/chain-space/src/benchmarking.rs @@ -1,23 +1,24 @@ #![cfg(feature = "runtime-benchmarks")] -use crate::*; +use super::*; use codec::Encode; -use cord_primitives::curi::Ss58Identifier; use cord_utilities::traits::GenerateBenchmarkOrigin; use frame_benchmarking::{account, benchmarks}; -use frame_support::{pallet_prelude::EnsureOrigin, sp_runtime::traits::Hash}; +use frame_support::sp_runtime::traits::Hash; use frame_system::RawOrigin; +use identifier::{IdentifierType, Ss58Identifier}; fn assert_last_event(generic_event: ::RuntimeEvent) { frame_system::Pallet::::assert_last_event(generic_event.into()); } pub fn generate_space_id(digest: &SpaceCodeOf) -> SpaceIdOf { - Ss58Identifier::to_space_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Space).unwrap() } pub fn generate_authorization_id(digest: &SpaceCodeOf) -> AuthorizationIdOf { - Ss58Identifier::to_authorization_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Authorization) + .unwrap() } const SEED: u32 = 0; @@ -26,9 +27,8 @@ benchmarks! { where_clause { where ::EnsureOrigin: GenerateBenchmarkOrigin, - T::ChainSpaceOrigin: EnsureOrigin, + // T::ChainSpaceOrigin: EnsureOrigin, } - add_delegate { let caller: T::AccountId = account("caller", 0, SEED); let did: T::SpaceCreatorId = account("did", 0, SEED); diff --git a/pallets/chain-space/src/lib.rs b/pallets/chain-space/src/lib.rs index 07090238b..ed2804b3c 100644 --- a/pallets/chain-space/src/lib.rs +++ b/pallets/chain-space/src/lib.rs @@ -125,10 +125,11 @@ pub type SpaceAuthorizationOf = SpaceAuthorization::InvalidIdentifierLength)?; + let identifier = + Ss58Identifier::create_identifier(&id_digest.encode()[..], IdentifierType::Space) + .map_err(|_| Error::::InvalidIdentifierLength)?; ensure!(!>::contains_key(&identifier), Error::::SpaceAlreadyAnchored); @@ -526,8 +528,11 @@ pub mod pallet { let auth_id_digest = T::Hashing::hash(&[&identifier.encode()[..], &creator.encode()[..]].concat()[..]); - let authorization_id = Ss58Identifier::to_authorization_id(&auth_id_digest.encode()) - .map_err(|_| Error::::InvalidIdentifierLength)?; + let authorization_id = Ss58Identifier::create_identifier( + &auth_id_digest.encode(), + IdentifierType::Authorization, + ) + .map_err(|_| Error::::InvalidIdentifierLength)?; let mut delegates: BoundedVec, T::MaxSpaceDelegates> = BoundedVec::default(); @@ -933,8 +938,9 @@ impl Pallet { &[&space_id.encode()[..], &delegate.encode()[..], &creator.encode()[..]].concat()[..], ); - let delegate_authorization_id = Ss58Identifier::to_authorization_id(&id_digest.encode()) - .map_err(|_| Error::::InvalidIdentifierLength)?; + let delegate_authorization_id = + Ss58Identifier::create_identifier(&id_digest.encode(), IdentifierType::Authorization) + .map_err(|_| Error::::InvalidIdentifierLength)?; ensure!( !Authorizations::::contains_key(&delegate_authorization_id), @@ -1253,7 +1259,7 @@ impl Pallet { let tx_moment = Self::timepoint(); let tx_entry = EventEntryOf { action: tx_action, location: tx_moment }; - let _ = identifier::Pallet::::update_timeline(tx_id, tx_type, tx_entry); + let _ = IdentifierTimeline::update_timeline::(tx_id, tx_type, tx_entry); Ok(()) } diff --git a/pallets/chain-space/src/tests.rs b/pallets/chain-space/src/tests.rs index 94b1d2d79..a24a501f7 100644 --- a/pallets/chain-space/src/tests.rs +++ b/pallets/chain-space/src/tests.rs @@ -8,11 +8,12 @@ use sp_runtime::{traits::Hash, AccountId32}; use sp_std::prelude::*; pub fn generate_space_id(digest: &SpaceCodeOf) -> SpaceIdOf { - Ss58Identifier::to_space_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Space).unwrap() } pub fn generate_authorization_id(digest: &SpaceCodeOf) -> AuthorizationIdOf { - Ss58Identifier::to_authorization_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Authorization) + .unwrap() } pub(crate) const DID_00: SubjectId = SubjectId(AccountId32::new([1u8; 32])); diff --git a/pallets/network-score/src/benchmarking.rs b/pallets/network-score/src/benchmarking.rs index e92711124..a4fb36fd8 100644 --- a/pallets/network-score/src/benchmarking.rs +++ b/pallets/network-score/src/benchmarking.rs @@ -20,22 +20,27 @@ use super::*; use codec::Encode; -use cord_primitives::curi::Ss58Identifier; use cord_utilities::traits::GenerateBenchmarkOrigin; use frame_benchmarking::{account, benchmarks}; use frame_support::{sp_runtime::traits::Hash, BoundedVec}; use frame_system::RawOrigin; +use identifier::{IdentifierType, Ss58Identifier}; use pallet_chain_space::SpaceCodeOf; const SEED: u32 = 0; const MAX_PAYLOAD_BYTE_LENGTH: u32 = 15 * 1024; pub fn generate_space_id(other_digest: &SpaceCodeOf) -> SpaceIdOf { - Ss58Identifier::to_space_id(&(other_digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(other_digest).encode()[..], IdentifierType::Space).unwrap() } pub fn generate_rating_id(other_digest: &RatingEntryHashOf) -> RatingEntryIdOf { - Ss58Identifier::to_scoring_id(&(other_digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(other_digest).encode()[..], IdentifierType::Rating).unwrap() +} + +pub fn generate_authorization_id(digest: &SpaceCodeOf) -> AuthorizationIdOf { + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Authorization) + .unwrap() } fn assert_last_event(generic_event: ::RuntimeEvent) { @@ -75,8 +80,7 @@ benchmarks! { let auth_digest = ::Hashing::hash( &[&space_id.encode()[..], &did1.encode()[..]].concat()[..], ); - let authorization_id: AuthorizationIdOf = - Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); + let authorization_id: AuthorizationIdOf = generate_authorization_id::(&auth_digest); let origin = ::EnsureOrigin::generate_origin(caller.clone(), did1.clone()); @@ -85,7 +89,7 @@ benchmarks! { let id_digest = ::Hashing::hash( &[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..] ); - let identifier = Ss58Identifier::to_scoring_id(&id_digest.encode()[..]).unwrap(); + let identifier = generate_rating_id::(&id_digest); let chain_space_origin = RawOrigin::Root.into(); @@ -129,18 +133,18 @@ benchmarks! { let id_digest_add = ::Hashing::hash( &[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_add.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..] ); - let identifier_add = Ss58Identifier::to_scoring_id(&id_digest_add.encode()[..]).unwrap(); + let identifier_add = generate_rating_id::(&id_digest_add); let id_digest = ::Hashing::hash( &[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_revoke.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..] ); - let identifier_revoke = Ss58Identifier::to_scoring_id(&id_digest.encode()[..]).unwrap(); + let identifier_revoke = generate_rating_id::(&id_digest); let auth_digest = ::Hashing::hash( &[&space_id.encode()[..], &did1.encode()[..]].concat()[..], ); let authorization_id: AuthorizationIdOf = - Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); + generate_authorization_id::(&auth_digest); let origin = ::EnsureOrigin::generate_origin(caller.clone(), did1.clone()); let chain_space_origin = RawOrigin::Root.into(); @@ -202,23 +206,22 @@ benchmarks! { let id_digest_add = ::Hashing::hash( &[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_add.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..] ); - let identifier_add = Ss58Identifier::to_scoring_id(&id_digest_add.encode()[..]).unwrap(); + let identifier_add = generate_rating_id::(&id_digest_add); let id_digest = ::Hashing::hash( &[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_revoke.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..] ); - let identifier_revoke = Ss58Identifier::to_scoring_id(&id_digest.encode()[..]).unwrap(); + let identifier_revoke = generate_rating_id::(&id_digest); let id_digest_revise = ::Hashing::hash( &[&(entry_revise_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_revise.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..] ); - let identifier_revise = Ss58Identifier::to_scoring_id(&id_digest_revise.encode()[..]).unwrap(); + let identifier_revise = generate_rating_id::(&id_digest_revise); let auth_digest = ::Hashing::hash( &[&space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let authorization_id: AuthorizationIdOf = - Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); + let authorization_id: AuthorizationIdOf = generate_authorization_id::(&auth_digest); let origin = ::EnsureOrigin::generate_origin(caller.clone(), did.clone()); let chain_space_origin = RawOrigin::Root.into(); diff --git a/pallets/network-score/src/lib.rs b/pallets/network-score/src/lib.rs index 532b4c890..d10dbdec0 100644 --- a/pallets/network-score/src/lib.rs +++ b/pallets/network-score/src/lib.rs @@ -123,10 +123,11 @@ use sp_runtime::traits::UniqueSaturatedInto; #[frame_support::pallet] pub mod pallet { use super::*; - pub use cord_primitives::{curi::Ss58Identifier, CountOf, RatingOf}; + pub use cord_primitives::{CountOf, RatingOf}; use cord_utilities::traits::CallSources; use frame_support::{pallet_prelude::*, Twox64Concat}; use frame_system::pallet_prelude::*; + pub use identifier::{IdentifierCreator, IdentifierTimeline, IdentifierType, Ss58Identifier}; use sp_runtime::traits::Hash; use sp_std::{prelude::Clone, str}; @@ -404,8 +405,11 @@ pub mod pallet { .concat()[..], ); - let identifier = Ss58Identifier::to_scoring_id(&(id_digest).encode()[..]) - .map_err(|_| Error::::InvalidIdentifierLength)?; + let identifier = Ss58Identifier::create_identifier( + &(id_digest).encode()[..], + IdentifierType::Rating, + ) + .map_err(|_| Error::::InvalidIdentifierLength)?; ensure!( !>::contains_key(&identifier), @@ -528,8 +532,11 @@ pub mod pallet { .concat()[..], ); - let identifier = Ss58Identifier::to_scoring_id(&(id_digest).encode()[..]) - .map_err(|_| Error::::InvalidIdentifierLength)?; + let identifier = Ss58Identifier::create_identifier( + &(id_digest).encode()[..], + IdentifierType::Rating, + ) + .map_err(|_| Error::::InvalidIdentifierLength)?; ensure!( !>::contains_key(&identifier), @@ -683,8 +690,11 @@ pub mod pallet { .concat()[..], ); - let identifier = Ss58Identifier::to_scoring_id(&(id_digest).encode()[..]) - .map_err(|_| Error::::InvalidIdentifierLength)?; + let identifier = Ss58Identifier::create_identifier( + &(id_digest).encode()[..], + IdentifierType::Rating, + ) + .map_err(|_| Error::::InvalidIdentifierLength)?; ensure!( !>::contains_key(&identifier), @@ -817,7 +827,7 @@ impl Pallet { let tx_moment = Self::timepoint(); let tx_entry = EventEntryOf { action: tx_action, location: tx_moment }; - let _ = identifier::Pallet::::update_timeline(tx_id, IdentifierTypeOf::Rating, tx_entry); + let _ = IdentifierTimeline::update_timeline::(tx_id, IdentifierTypeOf::Rating, tx_entry); Ok(()) } diff --git a/pallets/network-score/src/tests.rs b/pallets/network-score/src/tests.rs index 85fe5ac0b..b27e42fd2 100644 --- a/pallets/network-score/src/tests.rs +++ b/pallets/network-score/src/tests.rs @@ -27,11 +27,11 @@ use sp_runtime::{traits::Hash, AccountId32}; use sp_std::prelude::*; pub fn generate_rating_id(digest: &RatingEntryHashOf) -> RatingEntryIdOf { - Ss58Identifier::to_scoring_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Rating).unwrap() } pub fn generate_space_id(digest: &SpaceCodeOf) -> SpaceIdOf { - Ss58Identifier::to_space_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Space).unwrap() } pub(crate) const DID_00: SubjectId = SubjectId(AccountId32::new([1u8; 32])); @@ -68,7 +68,8 @@ fn check_successful_rating_creation() { &[&space_id.encode()[..], &creator.encode()[..]].concat()[..], ); let authorization_id: AuthorizationIdOf = - Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); + Ss58Identifier::create_identifier(&auth_digest.encode()[..], IdentifierType::Authorization) + .unwrap(); new_test_ext().execute_with(|| { System::set_block_number(1); diff --git a/pallets/schema/src/benchmarking.rs b/pallets/schema/src/benchmarking.rs index 0736cd412..2efb214bd 100644 --- a/pallets/schema/src/benchmarking.rs +++ b/pallets/schema/src/benchmarking.rs @@ -36,17 +36,18 @@ fn assert_last_event(generic_event: ::RuntimeEvent) { } pub fn generate_schema_id(digest: &SchemaHashOf) -> SchemaIdOf { - Ss58Identifier::to_schema_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Schema).unwrap() } /// Generates a space ID from a digest. pub fn generate_space_id(digest: &SchemaHashOf) -> SpaceIdOf { - Ss58Identifier::to_space_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Space).unwrap() } /// Generates an authorization ID from a digest. pub fn generate_authorization_id(digest: &SchemaHashOf) -> AuthorizationIdOf { - Ss58Identifier::to_authorization_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Authorization) + .unwrap() } benchmarks! { diff --git a/pallets/schema/src/lib.rs b/pallets/schema/src/lib.rs index 6b777f65d..f5bf07851 100644 --- a/pallets/schema/src/lib.rs +++ b/pallets/schema/src/lib.rs @@ -67,10 +67,10 @@ use frame_support::ensure; #[frame_support::pallet] pub mod pallet { use super::*; - pub use cord_primitives::curi::Ss58Identifier; pub use cord_utilities::traits::CallSources; use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; + pub use identifier::{IdentifierCreator, IdentifierTimeline, IdentifierType, Ss58Identifier}; use sp_runtime::{traits::Hash, SaturatedConversion}; /// The current storage version. @@ -190,8 +190,11 @@ pub mod pallet { [..], ); - let identifier = Ss58Identifier::to_schema_id(&(id_digest).encode()[..]) - .map_err(|_| Error::::InvalidIdentifierLength)?; + let identifier = Ss58Identifier::create_identifier( + &(id_digest).encode()[..], + IdentifierType::Schema, + ) + .map_err(|_| Error::::InvalidIdentifierLength)?; ensure!(!>::contains_key(&identifier), Error::::SchemaAlreadyAnchored); @@ -263,7 +266,7 @@ impl Pallet { let tx_moment = Self::timepoint(); let tx_entry = EventEntryOf { action: tx_action, location: tx_moment }; - let _ = identifier::Pallet::::update_timeline(tx_id, IdentifierTypeOf::Schema, tx_entry); + let _ = IdentifierTimeline::update_timeline::(tx_id, IdentifierTypeOf::Schema, tx_entry); Ok(()) } diff --git a/pallets/schema/src/tests.rs b/pallets/schema/src/tests.rs index a756a04d0..24715895b 100644 --- a/pallets/schema/src/tests.rs +++ b/pallets/schema/src/tests.rs @@ -81,17 +81,18 @@ where /// /// This function will panic if the conversion from digest to schema ID fails. pub fn generate_schema_id(digest: &SchemaHashOf) -> SchemaIdOf { - Ss58Identifier::to_schema_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Schema).unwrap() } /// Generates a space ID from a digest. pub fn generate_space_id(digest: &SchemaHashOf) -> SpaceIdOf { - Ss58Identifier::to_space_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Space).unwrap() } /// Generates an authorization ID from a digest. pub fn generate_authorization_id(digest: &SchemaHashOf) -> AuthorizationIdOf { - Ss58Identifier::to_authorization_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Authorization) + .unwrap() } // submit_schema_creation_operation diff --git a/pallets/statement/src/benchmarking.rs b/pallets/statement/src/benchmarking.rs index 14e03bfc8..d49b4daa5 100644 --- a/pallets/statement/src/benchmarking.rs +++ b/pallets/statement/src/benchmarking.rs @@ -2,11 +2,11 @@ use super::*; use codec::Encode; -use cord_primitives::curi::Ss58Identifier; use cord_utilities::traits::GenerateBenchmarkOrigin; use frame_benchmarking::{account, benchmarks}; use frame_support::sp_runtime::traits::Hash; use frame_system::RawOrigin; +use identifier::{IdentifierType, Ss58Identifier}; use pallet_chain_space::SpaceCodeOf; const SEED: u32 = 0; @@ -14,17 +14,18 @@ const MAX_PAYLOAD_BYTE_LENGTH: u32 = 5 * 1024; /// Generates a statement ID from a statement digest. pub fn generate_statement_id(digest: &StatementDigestOf) -> StatementIdOf { - Ss58Identifier::to_statement_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Statement).unwrap() } /// Generates a space ID from a digest. pub fn generate_space_id(digest: &SpaceCodeOf) -> SpaceIdOf { - Ss58Identifier::to_space_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Space).unwrap() } /// Generates an authorization ID from a digest. pub fn generate_authorization_id(digest: &SpaceCodeOf) -> AuthorizationIdOf { - Ss58Identifier::to_authorization_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Authorization) + .unwrap() } fn assert_last_event(generic_event: ::RuntimeEvent) { @@ -59,14 +60,13 @@ benchmarks! { .concat()[..], ); - let identifier = Ss58Identifier::to_statement_id(&(id_digest).encode()[..]).unwrap(); + let identifier = generate_statement_id::(&id_digest); let auth_digest = ::Hashing::hash( &[&space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let authorization_id: Ss58Identifier = - Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); + let authorization_id: Ss58Identifier = generate_authorization_id::(&auth_digest); let origin = ::EnsureOrigin::generate_origin(caller, did.clone()); let chain_space_origin = RawOrigin::Root.into(); @@ -102,14 +102,13 @@ benchmarks! { &[&statement_digest.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let identifier = Ss58Identifier::to_statement_id(&(statement_id_digest).encode()[..]).unwrap(); + let identifier = generate_statement_id::(&statement_id_digest); let auth_digest = ::Hashing::hash( &[&space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let authorization_id: Ss58Identifier = - Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); + let authorization_id: Ss58Identifier = generate_authorization_id::(&auth_digest); let statement_update = [12u8; 32].to_vec(); let update_digest = ::Hashing::hash(&statement_update[..]); @@ -147,14 +146,13 @@ benchmarks! { let statement_id_digest = ::Hashing::hash( &[&statement_digest.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let identifier = Ss58Identifier::to_statement_id(&(statement_id_digest).encode()[..]).unwrap(); + let identifier = generate_statement_id::(&statement_id_digest); let auth_digest = ::Hashing::hash( &[&space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let authorization_id: Ss58Identifier = - Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); + let authorization_id: Ss58Identifier = generate_authorization_id::(&auth_digest); let origin = ::EnsureOrigin::generate_origin(caller, did.clone()); let chain_space_origin = RawOrigin::Root.into(); @@ -190,13 +188,13 @@ benchmarks! { &[&statement_digest.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let identifier = Ss58Identifier::to_statement_id(&(statement_id_digest).encode()[..]).unwrap(); + let identifier = generate_statement_id::(&statement_id_digest); let auth_digest = ::Hashing::hash( &[&space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let authorization_id: Ss58Identifier = Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); + let authorization_id: Ss58Identifier = generate_authorization_id::(&auth_digest); let origin = ::EnsureOrigin::generate_origin(caller, did.clone()); let chain_space_origin = RawOrigin::Root.into(); @@ -232,14 +230,13 @@ benchmarks! { &[&statement_digest.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let identifier = Ss58Identifier::to_statement_id(&(statement_id_digest).encode()[..]).unwrap(); + let identifier = generate_statement_id::(&statement_id_digest); let auth_digest = ::Hashing::hash( &[&space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let authorization_id: Ss58Identifier = - Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); + let authorization_id: Ss58Identifier = generate_authorization_id::(&auth_digest); let origin = ::EnsureOrigin::generate_origin(caller, did.clone()); let chain_space_origin = RawOrigin::Root.into(); @@ -281,8 +278,7 @@ benchmarks! { &[&space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let authorization_id: Ss58Identifier = - Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); + let authorization_id: Ss58Identifier = generate_authorization_id::(&auth_digest); let origin = ::EnsureOrigin::generate_origin(caller, did.clone()); let chain_space_origin = RawOrigin::Root.into(); @@ -314,14 +310,13 @@ benchmarks! { &[&statement_digest.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let identifier = Ss58Identifier::to_statement_id(&(statement_id_digest).encode()[..]).unwrap(); + let identifier = generate_statement_id::(&statement_id_digest); let auth_digest = ::Hashing::hash( &[&space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let authorization_id: Ss58Identifier = - Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); + let authorization_id: Ss58Identifier = generate_authorization_id::(&auth_digest); let origin = ::EnsureOrigin::generate_origin(caller, did.clone()); let chain_space_origin = RawOrigin::Root.into(); @@ -357,13 +352,13 @@ benchmarks! { &[&statement_digest.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let identifier = Ss58Identifier::to_statement_id(&(statement_id_digest).encode()[..]).unwrap(); + let identifier = generate_statement_id::(&statement_id_digest); let auth_digest = ::Hashing::hash( &[&space_id.encode()[..], &did.encode()[..]].concat()[..], ); - let authorization_id: Ss58Identifier = Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap(); + let authorization_id: Ss58Identifier = generate_authorization_id::(&auth_digest); let origin = ::EnsureOrigin::generate_origin(caller, did.clone()); let chain_space_origin = RawOrigin::Root.into(); diff --git a/pallets/statement/src/lib.rs b/pallets/statement/src/lib.rs index 226d3c1ee..ecf57df16 100644 --- a/pallets/statement/src/lib.rs +++ b/pallets/statement/src/lib.rs @@ -80,7 +80,7 @@ pub mod mock; #[cfg(test)] pub mod tests; -use cord_primitives::{curi::Ss58Identifier, StatusOf}; +use cord_primitives::StatusOf; use frame_support::{ensure, storage::types::StorageMap}; use sp_runtime::traits::UniqueSaturatedInto; use sp_std::{prelude::Clone, str}; @@ -102,6 +102,7 @@ pub mod pallet { use cord_utilities::traits::CallSources; use frame_support::pallet_prelude::{OptionQuery, *}; use frame_system::pallet_prelude::*; + pub use identifier::{IdentifierCreator, IdentifierTimeline, IdentifierType, Ss58Identifier}; use sp_runtime::traits::Hash; /// The current storage version. @@ -411,8 +412,11 @@ pub mod pallet { &[&digest.encode()[..], &space_id.encode()[..], &creator.encode()[..]].concat()[..], ); - let identifier = Ss58Identifier::to_statement_id(&(id_digest).encode()[..]) - .map_err(|_| Error::::InvalidIdentifierLength)?; + let identifier = Ss58Identifier::create_identifier( + &(id_digest).encode()[..], + IdentifierType::Statement, + ) + .map_err(|_| Error::::InvalidIdentifierLength)?; ensure!( !>::contains_key(&identifier), @@ -533,9 +537,6 @@ pub mod pallet { StatementDetailsOf:: { digest: new_statement_digest, ..statement_details }, ); - pallet_chain_space::Pallet::::increment_usage(&space_id) - .map_err(>::from)?; - Self::update_activity(&statement_id, CallTypeOf::Update).map_err(>::from)?; Self::deposit_event(Event::Update { @@ -894,6 +895,7 @@ pub mod pallet { /// of the outcome. #[pallet::call_index(5)] #[pallet::weight(::WeightInfo::register_batch(digests.len().saturated_into()))] + #[rustfmt::skip] pub fn register_batch( origin: OriginFor, digests: Vec>, @@ -926,10 +928,13 @@ pub mod pallet { .concat()[..], ); - let identifier_result = Ss58Identifier::to_statement_id(&id_digest.encode()); + let identifier_result = Ss58Identifier::create_identifier( + &(id_digest).encode()[..], + IdentifierType::Statement, + ); match identifier_result { - Ok(identifier) => + Ok(identifier) => { if >::contains_key(&identifier) { fail += 1; indices.push(index as u16); @@ -952,7 +957,8 @@ pub mod pallet { } else { success += 1; } - }, + } + }, Err(_) => { fail += 1; indices.push(index as u16); @@ -1158,7 +1164,7 @@ impl Pallet { let tx_entry = EventEntryOf { action: tx_action, location: tx_moment }; let _ = - identifier::Pallet::::update_timeline(tx_id, IdentifierTypeOf::Statement, tx_entry); + IdentifierTimeline::update_timeline::(tx_id, IdentifierTypeOf::Statement, tx_entry); Ok(()) } diff --git a/pallets/statement/src/tests.rs b/pallets/statement/src/tests.rs index bddb89565..cf3cd59fb 100644 --- a/pallets/statement/src/tests.rs +++ b/pallets/statement/src/tests.rs @@ -10,22 +10,23 @@ use sp_runtime::{traits::Hash, AccountId32}; /// Generates a statement ID from a statement digest. pub fn generate_statement_id(digest: &StatementDigestOf) -> StatementIdOf { - Ss58Identifier::to_statement_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Statement).unwrap() } /// Generates a schema ID from a schema digest. pub fn generate_schema_id(digest: &SchemaHashOf) -> SchemaIdOf { - Ss58Identifier::to_schema_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Schema).unwrap() } /// Generates a space ID from a digest. pub fn generate_space_id(digest: &SpaceCodeOf) -> SpaceIdOf { - Ss58Identifier::to_space_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Space).unwrap() } /// Generates an authorization ID from a digest. pub fn generate_authorization_id(digest: &SpaceCodeOf) -> AuthorizationIdOf { - Ss58Identifier::to_authorization_id(&(digest).encode()[..]).unwrap() + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Authorization) + .unwrap() } pub(crate) const DID_00: SubjectId = SubjectId(AccountId32::new([1u8; 32])); diff --git a/primitives/cord/Cargo.toml b/primitives/cord/Cargo.toml index ab4569929..9b6add7e2 100644 --- a/primitives/cord/Cargo.toml +++ b/primitives/cord/Cargo.toml @@ -11,27 +11,10 @@ repository.workspace = true workspace = true [dependencies] -codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = [ - "derive", - "max-encoded-len", -] } -scale-info = { version = "2.10.0", default-features = false, features = [ - "derive", -] } -base58 = "0.2.0" -blake2-rfc = { version = "0.2.18", default-features = false } sp-core = { git = "https://github.com/dhiway/substrate-sdk", default-features = false, branch = "release-v1.5.0" } -frame-support = { git = "https://github.com/dhiway/substrate-sdk", default-features = false, branch = "release-v1.5.0" } sp-runtime = { git = "https://github.com/dhiway/substrate-sdk", default-features = false, branch = "release-v1.5.0" } sp-std = { git = "https://github.com/dhiway/substrate-sdk", default-features = false, branch = "release-v1.5.0" } [features] default = ["std"] -std = [ - "codec/std", - "scale-info/std", - "frame-support/std", - "sp-core/std", - "sp-runtime/std", - "sp-std/std", -] +std = ["sp-core/std", "sp-runtime/std", "sp-std/std"] diff --git a/primitives/cord/src/lib.rs b/primitives/cord/src/lib.rs index 63956e5a6..5150dc7dd 100644 --- a/primitives/cord/src/lib.rs +++ b/primitives/cord/src/lib.rs @@ -25,7 +25,7 @@ use sp_runtime::{ MultiSignature, OpaqueExtrinsic, }; use sp_std::vec::Vec; -pub mod curi; +// pub mod curi; // pub mod nid; /// An index to a block. diff --git a/primitives/identifier/Cargo.toml b/primitives/identifier/Cargo.toml index afed740ce..0348257e0 100644 --- a/primitives/identifier/Cargo.toml +++ b/primitives/identifier/Cargo.toml @@ -36,6 +36,8 @@ codec = { package = "parity-scale-codec", version = "3.6.1", default-features = scale-info = { version = "2.10.0", default-features = false, features = [ "derive", ] } +base58 = "0.2.0" +blake2-rfc = { version = "0.2.18", default-features = false } cord-primitives = { package = "cord-primitives", path = "../cord", default-features = false } cord-utilities = { package = "cord-utilities", path = "../../utilities", default-features = false } diff --git a/primitives/cord/src/curi.rs b/primitives/identifier/src/curi.rs similarity index 58% rename from primitives/cord/src/curi.rs rename to primitives/identifier/src/curi.rs index d5a57f242..4689a23f9 100644 --- a/primitives/cord/src/curi.rs +++ b/primitives/identifier/src/curi.rs @@ -16,32 +16,78 @@ // You should have received a copy of the GNU General Public License // along with CORD. If not, see . -// #![cfg_attr(not(feature = "std"), no_std)] #![allow(clippy::unused_unit)] + use crate::*; use base58::{FromBase58, ToBase58}; use blake2_rfc::blake2b::{Blake2b, Blake2bResult}; use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::{ensure, sp_runtime::RuntimeDebug, traits::ConstU32, BoundedVec}; use scale_info::TypeInfo; -use sp_std::{fmt::Debug, prelude::Clone, str, vec}; +use sp_std::{ + fmt::Debug, + prelude::{Clone, Vec}, + str, vec, +}; /// CORD Identifier Prefix const PREFIX: &[u8] = b"CRDIDFR"; -/// CORD idents -const IDENT_SPACE: u16 = 3390; -const IDENT_AUTH: u16 = 2092; -const IDENT_SCHEMA: u16 = 7366; -const IDENT_STATEMENT: u16 = 8902; -const IDENT_ENTITY: u16 = 6480; -const IDENT_TEMPLATE: u16 = 8911; -const IDENT_ASSET: u16 = 2348; -const IDENT_RATING: u16 = 6077; -const IDENT_ASSET_INSTANCE: u16 = 11380; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum IdentifierType { + Authorization, + Space, + Schema, + Statement, + Entity, + Template, + Asset, + AssetInstance, + Rating, +} + +impl IdentifierType { + const IDENT_AUTH: u16 = 2092; + const IDENT_SPACE: u16 = 3390; + const IDENT_SCHEMA: u16 = 7366; + const IDENT_STATEMENT: u16 = 8902; + const IDENT_ENTITY: u16 = 6480; + const IDENT_TEMPLATE: u16 = 8911; + const IDENT_ASSET: u16 = 2348; + const IDENT_RATING: u16 = 6077; + const IDENT_ASSET_INSTANCE: u16 = 11380; + + fn ident_value(&self) -> u16 { + match self { + IdentifierType::Authorization => Self::IDENT_AUTH, + IdentifierType::Space => Self::IDENT_SPACE, + IdentifierType::Schema => Self::IDENT_SCHEMA, + IdentifierType::Statement => Self::IDENT_STATEMENT, + IdentifierType::Entity => Self::IDENT_ENTITY, + IdentifierType::Template => Self::IDENT_TEMPLATE, + IdentifierType::Asset => Self::IDENT_ASSET, + IdentifierType::AssetInstance => Self::IDENT_ASSET_INSTANCE, + IdentifierType::Rating => Self::IDENT_RATING, + } + } + fn from_u16(value: u16) -> Option { + match value { + 2092 => Some(IdentifierType::Authorization), + 3390 => Some(IdentifierType::Space), + 7366 => Some(IdentifierType::Schema), + 8902 => Some(IdentifierType::Statement), + 6480 => Some(IdentifierType::Entity), + 8911 => Some(IdentifierType::Template), + 2348 => Some(IdentifierType::Asset), + 6077 => Some(IdentifierType::AssetInstance), + 11380 => Some(IdentifierType::Rating), + _ => None, + } + } +} /// The minimum length of a valid identifier. pub const MINIMUM_IDENTIFIER_LENGTH: usize = 2; -// const MINIMUM_IDENTIFIER_LENGTH_U32: u32 = MINIMUM_IDENTIFIER_LENGTH as u32; /// The maximum length of a valid identifier. pub const MAXIMUM_IDENTIFIER_LENGTH: usize = 49; const MAXIMUM_IDENTIFIER_LENGTH_U32: u32 = MAXIMUM_IDENTIFIER_LENGTH as u32; @@ -68,34 +114,37 @@ pub enum IdentifierError { InvalidIdentifier, /// The identifier has an invalid length. InvalidIdentifierLength, + /// Identifier timeline update failed + UpdateFailed, + MaxEventsHistoryExceeded, } -/// An error with the interpretation of a secret. -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum IdentifierType { - Registry, - Authorization, - Schema, - Statement, +pub trait IdentifierCreator { + fn create_identifier( + data: &[u8], + id_type: IdentifierType, + ) -> Result; } -impl TryFrom> for Ss58Identifier { - type Error = &'static str; - - fn try_from(value: Vec) -> Result { - let identifier = Ss58Identifier::to_space_id(&value[..]) - .map_err(|_| "Cannot convert provided input to a valid identifier.")?; - - Ok(identifier) +impl IdentifierCreator for Ss58Identifier { + fn create_identifier( + data: &[u8], + id_type: IdentifierType, + ) -> Result { + let id_ident = id_type.ident_value(); + Ss58Identifier::from_encoded(data, id_ident) } } -#[cfg(feature = "std")] -impl TryFrom for Ss58Identifier { - type Error = &'static str; +pub trait CordIdentifierType { + fn get_type(&self) -> Result; +} + +impl CordIdentifierType for Ss58Identifier { + fn get_type(&self) -> Result { + let identifier_type_u16 = self.get_identifier_type()?; - fn try_from(value: String) -> Result { - Self::try_from(value.into_bytes()) + IdentifierType::from_u16(identifier_type_u16).ok_or(IdentifierError::InvalidIdentifier) } } @@ -145,59 +194,35 @@ impl Ss58Identifier { )) } - pub fn to_authorization_id(data: &[u8]) -> Result { - Self::from_encoded(data, IDENT_AUTH) - } - pub fn to_space_id(data: &[u8]) -> Result { - Self::from_encoded(data, IDENT_SPACE) - } - pub fn to_schema_id(data: &[u8]) -> Result { - Self::from_encoded(data, IDENT_SCHEMA) - } - pub fn to_statement_id(data: &[u8]) -> Result { - Self::from_encoded(data, IDENT_STATEMENT) - } - pub fn to_entity_id(data: &[u8]) -> Result { - Self::from_encoded(data, IDENT_ENTITY) - } - pub fn to_template_id(data: &[u8]) -> Result { - Self::from_encoded(data, IDENT_TEMPLATE) - } - pub fn to_asset_id(data: &[u8]) -> Result { - Self::from_encoded(data, IDENT_ASSET) - } - pub fn to_asset_instance_id(data: &[u8]) -> Result { - Self::from_encoded(data, IDENT_ASSET_INSTANCE) - } - pub fn to_scoring_id(data: &[u8]) -> Result { - Self::from_encoded(data, IDENT_RATING) - } pub fn inner(&self) -> &[u8] { &self.0 } - pub fn get_ident(id: Self, id_ident: u16) -> IdentVerificationResult { - let identifier = str::from_utf8(id.inner()).map_err(|_| IdentifierError::InvalidFormat)?; + pub fn get_identifier_type(&self) -> Result { + let identifier = + str::from_utf8(self.inner()).map_err(|_| IdentifierError::InvalidFormat)?; let data = identifier.from_base58().map_err(|_| IdentifierError::InvalidIdentifier)?; + if data.len() < 2 { return Err(IdentifierError::InvalidIdentifierLength); } + + // Ensure the identifier length is within the expected range ensure!( - (identifier.len() > 2 && identifier.len() < 50), + identifier.len() > 2 && identifier.len() < 50, IdentifierError::InvalidIdentifierLength ); - let (_prefix_len, ident) = match data[0] { - 0..=63 => (1, data[0] as u16), + + // Extract the identifier type + match data[0] { + 0..=63 => Ok(data[0] as u16), 64..=127 => { let lower = (data[0] << 2) | (data[1] >> 6); let upper = data[1] & 0b00111111; - (2, (lower as u16) | ((upper as u16) << 8)) + Ok((lower as u16) | ((upper as u16) << 8)) }, - _ => return Err(IdentifierError::InvalidPrefix), - }; - - ensure!(ident == id_ident, IdentifierError::InvalidPrefix); - Ok(()) + _ => Err(IdentifierError::InvalidPrefix), + } } pub fn default_error() -> Self { @@ -214,6 +239,27 @@ impl Ss58Identifier { } } +pub struct IdentifierTimeline; + +impl IdentifierTimeline { + pub fn update_timeline( + id: &IdentifierOf, + id_type: IdentifierTypeOf, + entry: EventEntryOf, + ) -> Result<(), IdentifierError> + where + Pallet: IdentifierUpdate, + { + as IdentifierUpdate< + IdentifierOf, + IdentifierTypeOf, + EventEntryOf, + IdentifierError, + >>::update_timeline(id, id_type, entry) + .map_err(|_| IdentifierError::MaxEventsHistoryExceeded) + } +} + impl AsRef<[u8]> for Ss58Identifier { fn as_ref(&self) -> &[u8] { &self.0[..] diff --git a/primitives/identifier/src/lib.rs b/primitives/identifier/src/lib.rs index 8bd9153e4..8eb7606c1 100644 --- a/primitives/identifier/src/lib.rs +++ b/primitives/identifier/src/lib.rs @@ -21,8 +21,12 @@ #![cfg_attr(not(feature = "std"), no_std)] #![allow(clippy::unused_unit)] -use cord_primitives::curi::Ss58Identifier; -use sp_runtime::{BoundedVec, DispatchResult}; +pub mod curi; +pub use crate::curi::{ + CordIdentifierType, IdentifierCreator, IdentifierError, IdentifierTimeline, IdentifierType, + Ss58Identifier, +}; +use sp_runtime::BoundedVec; use sp_std::{prelude::Clone, str}; pub mod types; pub use crate::types::*; @@ -30,6 +34,7 @@ use frame_support::traits::Get; use frame_system::pallet_prelude::BlockNumberFor; pub use crate::{pallet::*, types::*}; +pub use pallet::*; use sp_std::vec; #[frame_support::pallet] @@ -85,22 +90,27 @@ pub mod pallet { } } -impl Pallet { - pub fn update_timeline( +pub trait IdentifierUpdate { + fn update_timeline(id: &I, id_type: IT, entry: EE) -> Result<(), E>; +} + +impl IdentifierUpdate + for Pallet +{ + fn update_timeline( id: &IdentifierOf, id_type: IdentifierTypeOf, entry: EventEntryOf, - ) -> DispatchResult { - Identifiers::::try_mutate(id, id_type, |timeline| -> DispatchResult { + ) -> Result<(), IdentifierError> { + Identifiers::::try_mutate(id, id_type, |timeline| { let events = timeline.get_or_insert_with(BoundedVec::default); if events.len() == T::MaxEventsHistory::get() as usize { events.remove(1); } - events.try_push(entry).map_err(|_| Error::::MaxEventsHistoryExceeded)?; - - Ok(()) + events.try_push(entry).map_err(|_| IdentifierError::MaxEventsHistoryExceeded) }) + .map_err(|_| IdentifierError::MaxEventsHistoryExceeded) // Map DispatchError to your custom Error } } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 76c988287..7203ab5db 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -23,10 +23,11 @@ use codec::{Decode, Encode}; use scale_info::TypeInfo; -pub use cord_primitives::{curi::Ss58Identifier, AccountId, Signature}; use cord_primitives::{ prod_or_fast, AccountIndex, Balance, BlockNumber, DidIdentifier, Hash, Moment, Nonce, }; +pub use cord_primitives::{AccountId, Signature}; +pub use identifier::Ss58Identifier; use frame_support::{ construct_runtime, derive_impl, diff --git a/rust-toolchain.toml b/rust-toolchain.toml index cbab7963b..5607016d2 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,5 +1,5 @@ [toolchain] -channel = "stable" +channel = "1.74.1" components = [ "cargo", "clippy",