diff --git a/Cargo.lock b/Cargo.lock index abcc85ce8..ca9bc7a55 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4528,15 +4528,19 @@ name = "kilt-dip-support" version = "1.12.0-dev" dependencies = [ "cumulus-pallet-parachain-system", + "cumulus-primitives-core", "did", "frame-support", "frame-system", "hash-db", "hex-literal 0.3.4", "log", + "pallet-did-lookup", "pallet-dip-consumer", "pallet-dip-provider", "pallet-relay-store", + "pallet-web3-names", + "parachain-info", "parity-scale-codec", "rococo-runtime", "scale-info", diff --git a/crates/kilt-dip-support/Cargo.toml b/crates/kilt-dip-support/Cargo.toml index b9ba5ff71..d9d604884 100644 --- a/crates/kilt-dip-support/Cargo.toml +++ b/crates/kilt-dip-support/Cargo.toml @@ -17,9 +17,11 @@ log.workspace = true # Internal dependencies did.workspace = true +pallet-did-lookup.workspace = true pallet-dip-consumer.workspace = true pallet-dip-provider.workspace = true pallet-relay-store.workspace = true +pallet-web3-names.workspace = true # Parity dependencies parity-scale-codec = {workspace = true, features = ["derive"]} @@ -42,6 +44,8 @@ xcm-executor.workspace = true # Cumulus dependencies cumulus-pallet-parachain-system.workspace = true +cumulus-primitives-core.workspace = true +parachain-info.workspace = true [dev-dependencies] hex-literal.workspace = true @@ -53,9 +57,11 @@ std = [ "hash-db/std", "log/std", "did/std", + "pallet-did-lookup/std", "pallet-dip-consumer/std", "pallet-dip-provider/std", "pallet-relay-store/std", + "pallet-web3-names/std", "parity-scale-codec/std", "scale-info/std", "frame-system/std", @@ -70,6 +76,8 @@ std = [ "xcm/std", "xcm-executor/std", "cumulus-pallet-parachain-system/std", + "cumulus-primitives-core/std", + "parachain-info/std", ] runtime-benchmarks = [ "pallet-dip-consumer/runtime-benchmarks", diff --git a/crates/kilt-dip-support/src/export/child.rs b/crates/kilt-dip-support/src/export/child.rs index c23808009..6df44c2e4 100644 --- a/crates/kilt-dip-support/src/export/child.rs +++ b/crates/kilt-dip-support/src/export/child.rs @@ -16,12 +16,17 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org -use did::{did_details::DidVerificationKey, DidVerificationKeyRelationship}; -use pallet_dip_consumer::traits::IdentityProofVerifier; +use cumulus_primitives_core::ParaId; +use did::{did_details::DidVerificationKey, DidVerificationKeyRelationship, KeyIdOf}; +use frame_support::Parameter; +use frame_system::pallet_prelude::BlockNumberFor; +use pallet_did_lookup::linkable_account::LinkableAccountId; +use pallet_dip_consumer::{traits::IdentityProofVerifier, RuntimeCallOf}; +use pallet_dip_provider::IdentityCommitmentOf; use parity_scale_codec::{Codec, Decode, Encode, HasCompact}; use scale_info::TypeInfo; use sp_core::{RuntimeDebug, U256}; -use sp_runtime::traits::{AtLeast32BitUnsigned, CheckedSub, Get, Hash, MaybeDisplay, Member, SimpleBitOps}; +use sp_runtime::traits::{AtLeast32BitUnsigned, Get, Hash, MaybeDisplay, Member, SimpleBitOps}; use sp_std::{marker::PhantomData, vec::Vec}; use crate::{ @@ -33,6 +38,7 @@ use crate::{ RelayChainStorageInfo, }, utils::OutputOf, + FrameSystemDidSignatureContext, ProviderParachainStateInfoViaProviderPallet, }; #[derive(Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo, Clone)] @@ -112,11 +118,154 @@ where } } -pub struct VersionedDipChildProviderStateProofVerifier< +// Implements the same `IdentityProvider` trait, but it is internally configured +// by receiving the runtime definitions of both the provider and the receiver. +pub struct KiltVersionedChildProviderVerifier< + KiltRuntime, + KiltParachainId, + RelayChainInfo, + KiltDipMerkleHasher, + LocalDidCallVerifier, + const MAX_REVEALED_KEYS_COUNT: u32, + const MAX_REVEALED_ACCOUNTS_COUNT: u32, + const MAX_DID_SIGNATURE_DURATION: u16, +>( + PhantomData<( + KiltRuntime, + KiltParachainId, + RelayChainInfo, + KiltDipMerkleHasher, + LocalDidCallVerifier, + )>, +); + +impl< + ConsumerRuntime, + KiltRuntime, + KiltParachainId, + RelayChainInfo, + KiltDipMerkleHasher, + LocalDidCallVerifier, + const MAX_REVEALED_KEYS_COUNT: u32, + const MAX_REVEALED_ACCOUNTS_COUNT: u32, + const MAX_DID_SIGNATURE_DURATION: u16, + > IdentityProofVerifier + for KiltVersionedChildProviderVerifier< + KiltRuntime, + KiltParachainId, + RelayChainInfo, + KiltDipMerkleHasher, + LocalDidCallVerifier, + MAX_REVEALED_KEYS_COUNT, + MAX_REVEALED_ACCOUNTS_COUNT, + MAX_DID_SIGNATURE_DURATION, + > where + KiltRuntime: did::Config + + pallet_web3_names::Config + + pallet_did_lookup::Config + + parachain_info::Config + + pallet_dip_provider::Config, + KiltParachainId: Get, + OutputOf: Ord + From::Hasher>>, + KeyIdOf: Into, + KiltDipMerkleHasher: sp_core::Hasher>, + ConsumerRuntime: pallet_dip_consumer::Config, + ConsumerRuntime::LocalIdentityInfo: Bump + Default + Encode, + RelayChainInfo: RelayChainStorageInfo> + + HistoricalBlockRegistry< + BlockNumber = ::BlockNumber, + Hasher = ::Hasher, + >, + RelayChainInfo::ParaId: From, + OutputOf<::Hasher>: + Ord + Default + sp_std::hash::Hash + Copy + Member + MaybeDisplay + SimpleBitOps + Codec, + ::Hasher: Parameter + 'static, + ::BlockNumber: Copy + + Into + + TryFrom + + HasCompact + + Member + + sp_std::hash::Hash + + MaybeDisplay + + AtLeast32BitUnsigned + + Codec + + Parameter + + 'static, + RelayChainInfo::Key: AsRef<[u8]>, + LocalDidCallVerifier: DipCallOriginFilter< + RuntimeCallOf, + OriginInfo = ( + DidVerificationKey, + DidVerificationKeyRelationship, + ), + >, +{ + type Error = DipChildProviderStateProofVerifierError< + ParachainHeadProofVerifierError, + DipIdentityCommitmentProofVerifierError, + DidMerkleProofVerifierError, + RevealedDidKeysSignatureAndCallVerifierError, + >; + type Proof = VersionedChildParachainDipStateProof< + ::BlockNumber, + ::Hasher, + Vec>, + RevealedDidMerkleProofLeaf< + KeyIdOf, + KiltRuntime::AccountId, + BlockNumberFor, + KiltRuntime::Web3Name, + LinkableAccountId, + >, + >; + type VerificationResult = RevealedDidMerkleProofLeaves< + KeyIdOf, + KiltRuntime::AccountId, + BlockNumberFor, + KiltRuntime::Web3Name, + LinkableAccountId, + MAX_REVEALED_KEYS_COUNT, + MAX_REVEALED_ACCOUNTS_COUNT, + >; + + fn verify_proof_for_call_against_details( + call: &RuntimeCallOf, + subject: &ConsumerRuntime::Identifier, + submitter: &ConsumerRuntime::AccountId, + identity_details: &mut Option, + proof: Self::Proof, + ) -> Result { + , + KiltDipMerkleHasher, + KeyIdOf, + KiltRuntime::AccountId, + KiltRuntime::Web3Name, + LinkableAccountId, + MAX_REVEALED_KEYS_COUNT, + MAX_REVEALED_ACCOUNTS_COUNT, + FrameSystemDidSignatureContext, + LocalDidCallVerifier, + > as IdentityProofVerifier>::verify_proof_for_call_against_details( + call, + subject, + submitter, + identity_details, + proof, + ) + } +} + +// More generic version compared to `VersionedChildKiltProviderVerifier`, to be +// used in cases in which it is not possible or not desirable to depend on the +// whole provider runtime definition. Hence, required types must be filled in +// manually. +pub struct GenericVersionedDipChildProviderStateProofVerifier< RelayChainInfo, ChildProviderParachainId, ChildProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, @@ -124,7 +273,6 @@ pub struct VersionedDipChildProviderStateProofVerifier< ProviderLinkedAccountId, const MAX_REVEALED_KEYS_COUNT: u32, const MAX_REVEALED_ACCOUNTS_COUNT: u32, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, >( @@ -133,25 +281,21 @@ pub struct VersionedDipChildProviderStateProofVerifier< RelayChainInfo, ChildProviderParachainId, ChildProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, ProviderWeb3Name, ProviderLinkedAccountId, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, )>, ); impl< - Call, - Subject, + ConsumerRuntime, RelayChainInfo, ChildProviderParachainId, ChildProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, @@ -159,15 +303,13 @@ impl< ProviderLinkedAccountId, const MAX_REVEALED_KEYS_COUNT: u32, const MAX_REVEALED_ACCOUNTS_COUNT: u32, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, - > IdentityProofVerifier - for VersionedDipChildProviderStateProofVerifier< + > IdentityProofVerifier + for GenericVersionedDipChildProviderStateProofVerifier< RelayChainInfo, ChildProviderParachainId, ChildProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, @@ -175,20 +317,20 @@ impl< ProviderLinkedAccountId, MAX_REVEALED_KEYS_COUNT, MAX_REVEALED_ACCOUNTS_COUNT, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, > where - Call: Encode, - TxSubmitter: Encode, + ConsumerRuntime: pallet_dip_consumer::Config, + ConsumerRuntime::LocalIdentityInfo: Bump + Default, - RelayChainInfo: RelayChainStorageInfo + RelayChainInfo: RelayChainStorageInfo> + HistoricalBlockRegistry< BlockNumber = ::BlockNumber, Hasher = ::Hasher, >, OutputOf<::Hasher>: Ord + Default + sp_std::hash::Hash + Copy + Member + MaybeDisplay + SimpleBitOps + Codec, + ::Hasher: Parameter + 'static, ::BlockNumber: Copy + Into + TryFrom @@ -197,31 +339,33 @@ impl< + sp_std::hash::Hash + MaybeDisplay + AtLeast32BitUnsigned - + Codec, + + Codec + + Parameter + + 'static, RelayChainInfo::Key: AsRef<[u8]>, ChildProviderParachainId: Get, - ChildProviderStateInfo: ProviderParachainStateInfo, + ChildProviderStateInfo: + ProviderParachainStateInfo, OutputOf: Ord + From::Hasher>>, - ChildProviderStateInfo::BlockNumber: Encode + Clone, + ChildProviderStateInfo::BlockNumber: Parameter + 'static, ChildProviderStateInfo::Commitment: Decode, ChildProviderStateInfo::Key: AsRef<[u8]>, LocalContextProvider: - DidSignatureVerifierContext::BlockNumber>, - LocalContextProvider::BlockNumber: CheckedSub + From, - LocalContextProvider::Hash: Encode, + DidSignatureVerifierContext, Hash = ConsumerRuntime::Hash>, LocalContextProvider::SignedExtra: Encode, - LocalDidDetails: Bump + Default + Encode, - LocalDidCallVerifier: - DipCallOriginFilter, DidVerificationKeyRelationship)>, + LocalDidCallVerifier: DipCallOriginFilter< + RuntimeCallOf, + OriginInfo = (DidVerificationKey, DidVerificationKeyRelationship), + >, ProviderDipMerkleHasher: sp_core::Hasher, - ProviderDidKeyId: Encode + Clone + Into, - ProviderAccountId: Encode + Clone, - ProviderLinkedAccountId: Encode + Clone, - ProviderWeb3Name: Encode + Clone, + ProviderDidKeyId: Parameter + 'static + Into, + ProviderAccountId: Parameter + 'static, + ProviderLinkedAccountId: Parameter + 'static, + ProviderWeb3Name: Parameter + 'static, { type Error = DipChildProviderStateProofVerifierError< ParachainHeadProofVerifierError, @@ -229,7 +373,6 @@ impl< DidMerkleProofVerifierError, RevealedDidKeysSignatureAndCallVerifierError, >; - type IdentityDetails = LocalDidDetails; type Proof = VersionedChildParachainDipStateProof< ::BlockNumber, ::Hasher, @@ -242,7 +385,6 @@ impl< ProviderLinkedAccountId, >, >; - type Submitter = TxSubmitter; type VerificationResult = RevealedDidMerkleProofLeaves< ProviderDidKeyId, ProviderAccountId, @@ -254,31 +396,33 @@ impl< >; fn verify_proof_for_call_against_details( - call: &Call, - subject: &Subject, - submitter: &Self::Submitter, - identity_details: &mut Option, + call: &RuntimeCallOf, + subject: &ConsumerRuntime::Identifier, + submitter: &ConsumerRuntime::AccountId, + identity_details: &mut Option, proof: Self::Proof, ) -> Result { match proof { - VersionedChildParachainDipStateProof::V0(v0_proof) => { - v0::DipChildProviderStateProofVerifier::< - RelayChainInfo, - ChildProviderParachainId, - ChildProviderStateInfo, - TxSubmitter, - ProviderDipMerkleHasher, - ProviderDidKeyId, - ProviderAccountId, - ProviderWeb3Name, - ProviderLinkedAccountId, - MAX_REVEALED_KEYS_COUNT, - MAX_REVEALED_ACCOUNTS_COUNT, - LocalDidDetails, - LocalContextProvider, - LocalDidCallVerifier, - >::verify_proof_for_call_against_details(call, subject, submitter, identity_details, v0_proof) - } + VersionedChildParachainDipStateProof::V0(v0_proof) => as IdentityProofVerifier>::verify_proof_for_call_against_details( + call, + subject, + submitter, + identity_details, + v0_proof, + ), } } } @@ -335,7 +479,6 @@ mod v0 { RelayChainInfo, ChildProviderParachainId, ChildProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, @@ -343,7 +486,6 @@ mod v0 { ProviderLinkedAccountId, const MAX_REVEALED_KEYS_COUNT: u32, const MAX_REVEALED_ACCOUNTS_COUNT: u32, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, >( @@ -352,25 +494,21 @@ mod v0 { RelayChainInfo, ChildProviderParachainId, ChildProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, ProviderWeb3Name, ProviderLinkedAccountId, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, )>, ); impl< - Call, - Subject, + ConsumerRuntime, RelayChainInfo, ChildProviderParachainId, ChildProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, @@ -378,15 +516,13 @@ mod v0 { ProviderLinkedAccountId, const MAX_REVEALED_KEYS_COUNT: u32, const MAX_REVEALED_ACCOUNTS_COUNT: u32, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, - > IdentityProofVerifier + > IdentityProofVerifier for DipChildProviderStateProofVerifier< RelayChainInfo, ChildProviderParachainId, ChildProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, @@ -394,18 +530,18 @@ mod v0 { ProviderLinkedAccountId, MAX_REVEALED_KEYS_COUNT, MAX_REVEALED_ACCOUNTS_COUNT, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, > where - Call: Encode, - TxSubmitter: Encode, + ConsumerRuntime: pallet_dip_consumer::Config, + ConsumerRuntime::LocalIdentityInfo: Bump + Default, - RelayChainInfo: RelayChainStorageInfo + RelayChainInfo: RelayChainStorageInfo> + HistoricalBlockRegistry< BlockNumber = ::BlockNumber, Hasher = ::Hasher, >, + ::Hasher: Parameter + 'static, OutputOf<::Hasher>: Ord + Default + sp_std::hash::Hash + Copy + Member + MaybeDisplay + SimpleBitOps + Codec, ::BlockNumber: Copy @@ -416,35 +552,36 @@ mod v0 { + sp_std::hash::Hash + MaybeDisplay + AtLeast32BitUnsigned - + Codec, + + Codec + + Parameter + + 'static, RelayChainInfo::Key: AsRef<[u8]>, ChildProviderParachainId: Get, - ChildProviderStateInfo: - ProviderParachainStateInfo, + ChildProviderStateInfo: ProviderParachainStateInfo< + Identifier = ConsumerRuntime::Identifier, + Commitment = ProviderDipMerkleHasher::Out, + >, OutputOf: Ord + From::Hasher>>, - ChildProviderStateInfo::BlockNumber: Encode + Clone, + ChildProviderStateInfo::BlockNumber: Parameter + 'static, ChildProviderStateInfo::Commitment: Decode, ChildProviderStateInfo::Key: AsRef<[u8]>, LocalContextProvider: - DidSignatureVerifierContext::BlockNumber>, - LocalContextProvider::BlockNumber: CheckedSub + From, - LocalContextProvider::Hash: Encode, + DidSignatureVerifierContext, Hash = ConsumerRuntime::Hash>, LocalContextProvider::SignedExtra: Encode, - LocalDidDetails: Bump + Default + Encode, LocalDidCallVerifier: DipCallOriginFilter< - Call, + RuntimeCallOf, OriginInfo = (DidVerificationKey, DidVerificationKeyRelationship), >, ProviderDipMerkleHasher: sp_core::Hasher, - ProviderDidKeyId: Encode + Clone + Into, - ProviderAccountId: Encode + Clone, - ProviderLinkedAccountId: Encode + Clone, - ProviderWeb3Name: Encode + Clone, + ProviderDidKeyId: Parameter + 'static + Into, + ProviderAccountId: Parameter + 'static, + ProviderLinkedAccountId: Parameter + 'static, + ProviderWeb3Name: Parameter + 'static, { type Error = DipChildProviderStateProofVerifierError< ParachainHeadProofVerifierError, @@ -452,7 +589,6 @@ mod v0 { DidMerkleProofVerifierError, RevealedDidKeysSignatureAndCallVerifierError, >; - type IdentityDetails = LocalDidDetails; type Proof = ChildParachainDipStateProof< ::BlockNumber, ::Hasher, @@ -465,7 +601,6 @@ mod v0 { ProviderLinkedAccountId, >, >; - type Submitter = TxSubmitter; type VerificationResult = RevealedDidMerkleProofLeaves< ProviderDidKeyId, ProviderAccountId, @@ -477,10 +612,10 @@ mod v0 { >; fn verify_proof_for_call_against_details( - call: &Call, - subject: &Subject, - submitter: &Self::Submitter, - identity_details: &mut Option, + call: &RuntimeCallOf, + subject: &ConsumerRuntime::Identifier, + submitter: &ConsumerRuntime::AccountId, + identity_details: &mut Option, proof: Self::Proof, ) -> Result { // 1. Retrieve block hash from provider at the proof height diff --git a/crates/kilt-dip-support/src/export/mod.rs b/crates/kilt-dip-support/src/export/mod.rs index c6a92e7d5..8d2f28992 100644 --- a/crates/kilt-dip-support/src/export/mod.rs +++ b/crates/kilt-dip-support/src/export/mod.rs @@ -16,16 +16,16 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org -mod child; +pub mod child; +pub mod sibling; + mod common; -mod sibling; pub use child::{ - DipChildProviderStateProofVerifierError, VersionedChildParachainDipStateProof, - VersionedDipChildProviderStateProofVerifier, + DipChildProviderStateProofVerifierError, KiltVersionedChildProviderVerifier, VersionedChildParachainDipStateProof, }; pub use sibling::{ - DipSiblingProviderStateProofVerifierError, VersionedDipSiblingProviderStateProofVerifier, + DipSiblingProviderStateProofVerifierError, KiltVersionedSiblingProviderVerifier, VersionedSiblingParachainDipStateProof, }; diff --git a/crates/kilt-dip-support/src/export/sibling.rs b/crates/kilt-dip-support/src/export/sibling.rs index e0b3154c5..5f8256010 100644 --- a/crates/kilt-dip-support/src/export/sibling.rs +++ b/crates/kilt-dip-support/src/export/sibling.rs @@ -16,12 +16,17 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org -use did::{did_details::DidVerificationKey, DidVerificationKeyRelationship}; -use pallet_dip_consumer::traits::IdentityProofVerifier; +use cumulus_primitives_core::ParaId; +use did::{did_details::DidVerificationKey, DidVerificationKeyRelationship, KeyIdOf}; +use frame_support::Parameter; +use frame_system::pallet_prelude::BlockNumberFor; +use pallet_did_lookup::linkable_account::LinkableAccountId; +use pallet_dip_consumer::{traits::IdentityProofVerifier, RuntimeCallOf}; +use pallet_dip_provider::IdentityCommitmentOf; use parity_scale_codec::{Decode, Encode, HasCompact}; use scale_info::TypeInfo; use sp_core::{RuntimeDebug, U256}; -use sp_runtime::traits::{CheckedSub, Get}; +use sp_runtime::traits::Get; use sp_std::{marker::PhantomData, vec::Vec}; use crate::{ @@ -30,6 +35,7 @@ use crate::{ state_proofs::{parachain::DipIdentityCommitmentProofVerifierError, relay_chain::ParachainHeadProofVerifierError}, traits::{self, Bump, DidSignatureVerifierContext, DipCallOriginFilter}, utils::OutputOf, + FrameSystemDidSignatureContext, ProviderParachainStateInfoViaProviderPallet, }; #[derive(Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo, Clone)] @@ -104,12 +110,137 @@ where } } } +// Implements the same `IdentityProvider` trait, but it is internally configured +// by receiving the runtime definitions of both the provider and the receiver. +pub struct KiltVersionedSiblingProviderVerifier< + KiltRuntime, + KiltParachainId, + RelayChainStateInfo, + KiltDipMerkleHasher, + LocalDidCallVerifier, + const MAX_REVEALED_KEYS_COUNT: u32, + const MAX_REVEALED_ACCOUNTS_COUNT: u32, + const MAX_DID_SIGNATURE_DURATION: u16, +>( + PhantomData<( + KiltRuntime, + KiltParachainId, + RelayChainStateInfo, + KiltDipMerkleHasher, + LocalDidCallVerifier, + )>, +); -pub struct VersionedDipSiblingProviderStateProofVerifier< +impl< + ConsumerRuntime, + KiltRuntime, + KiltParachainId, + RelayChainStateInfo, + KiltDipMerkleHasher, + LocalDidCallVerifier, + const MAX_REVEALED_KEYS_COUNT: u32, + const MAX_REVEALED_ACCOUNTS_COUNT: u32, + const MAX_DID_SIGNATURE_DURATION: u16, + > IdentityProofVerifier + for KiltVersionedSiblingProviderVerifier< + KiltRuntime, + KiltParachainId, + RelayChainStateInfo, + KiltDipMerkleHasher, + LocalDidCallVerifier, + MAX_REVEALED_KEYS_COUNT, + MAX_REVEALED_ACCOUNTS_COUNT, + MAX_DID_SIGNATURE_DURATION, + > where + KiltRuntime: did::Config + + pallet_web3_names::Config + + pallet_did_lookup::Config + + parachain_info::Config + + pallet_dip_provider::Config, + KiltParachainId: Get, + OutputOf: Ord + From>, + KeyIdOf: Into, + KiltDipMerkleHasher: sp_core::Hasher>, + ConsumerRuntime: pallet_dip_consumer::Config, + ConsumerRuntime::LocalIdentityInfo: Bump + Default + Encode, + RelayChainStateInfo: traits::RelayChainStorageInfo + traits::RelayChainStateInfo, + RelayChainStateInfo::ParaId: From, + RelayChainStateInfo::BlockNumber: Parameter + 'static + Copy + Into + TryFrom + HasCompact, + RelayChainStateInfo::Key: AsRef<[u8]>, + LocalDidCallVerifier: DipCallOriginFilter< + RuntimeCallOf, + OriginInfo = ( + DidVerificationKey, + DidVerificationKeyRelationship, + ), + >, +{ + type Error = DipSiblingProviderStateProofVerifierError< + ParachainHeadProofVerifierError, + DipIdentityCommitmentProofVerifierError, + DidMerkleProofVerifierError, + RevealedDidKeysSignatureAndCallVerifierError, + >; + type Proof = VersionedSiblingParachainDipStateProof< + RelayChainStateInfo::BlockNumber, + Vec>, + RevealedDidMerkleProofLeaf< + KeyIdOf, + KiltRuntime::AccountId, + BlockNumberFor, + KiltRuntime::Web3Name, + LinkableAccountId, + >, + BlockNumberFor, + >; + type VerificationResult = RevealedDidMerkleProofLeaves< + KeyIdOf, + KiltRuntime::AccountId, + BlockNumberFor, + KiltRuntime::Web3Name, + LinkableAccountId, + MAX_REVEALED_KEYS_COUNT, + MAX_REVEALED_ACCOUNTS_COUNT, + >; + + fn verify_proof_for_call_against_details( + call: &RuntimeCallOf, + subject: &ConsumerRuntime::Identifier, + submitter: &ConsumerRuntime::AccountId, + identity_details: &mut Option, + proof: Self::Proof, + ) -> Result { + , + KiltDipMerkleHasher, + KeyIdOf, + KiltRuntime::AccountId, + KiltRuntime::Web3Name, + LinkableAccountId, + MAX_REVEALED_KEYS_COUNT, + MAX_REVEALED_ACCOUNTS_COUNT, + FrameSystemDidSignatureContext, + LocalDidCallVerifier, + > as IdentityProofVerifier>::verify_proof_for_call_against_details( + call, + subject, + submitter, + identity_details, + proof, + ) + } +} + +// More generic version compared to `VersionedSiblingKiltProviderVerifier`, to +// be used in cases in which it is not possible or not desirable to depend on +// the whole provider runtime definition. Hence, required types must be filled +// in manually. +pub struct GenericVersionedDipSiblingProviderStateProofVerifier< RelayChainStateInfo, SiblingProviderParachainId, SiblingProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, @@ -117,7 +248,6 @@ pub struct VersionedDipSiblingProviderStateProofVerifier< ProviderLinkedAccountId, const MAX_REVEALED_KEYS_COUNT: u32, const MAX_REVEALED_ACCOUNTS_COUNT: u32, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, >( @@ -126,25 +256,21 @@ pub struct VersionedDipSiblingProviderStateProofVerifier< RelayChainStateInfo, SiblingProviderParachainId, SiblingProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, ProviderWeb3Name, ProviderLinkedAccountId, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, )>, ); impl< - Call, - Subject, + ConsumerRuntime, RelayChainStateInfo, SiblingProviderParachainId, SiblingProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, @@ -152,15 +278,13 @@ impl< ProviderLinkedAccountId, const MAX_REVEALED_KEYS_COUNT: u32, const MAX_REVEALED_ACCOUNTS_COUNT: u32, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, - > IdentityProofVerifier - for VersionedDipSiblingProviderStateProofVerifier< + > IdentityProofVerifier + for GenericVersionedDipSiblingProviderStateProofVerifier< RelayChainStateInfo, SiblingProviderParachainId, SiblingProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, @@ -168,40 +292,41 @@ impl< ProviderLinkedAccountId, MAX_REVEALED_KEYS_COUNT, MAX_REVEALED_ACCOUNTS_COUNT, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, > where - Call: Encode, - TxSubmitter: Encode, + ConsumerRuntime: pallet_dip_consumer::Config, + ConsumerRuntime::LocalIdentityInfo: Bump + Default, RelayChainStateInfo: traits::RelayChainStorageInfo + traits::RelayChainStateInfo, OutputOf: Ord, - RelayChainStateInfo::BlockNumber: Copy + Into + TryFrom + HasCompact, + RelayChainStateInfo::BlockNumber: Parameter + 'static + Copy + Into + TryFrom + HasCompact, RelayChainStateInfo::Key: AsRef<[u8]>, SiblingProviderParachainId: Get, - SiblingProviderStateInfo: - traits::ProviderParachainStateInfo, + SiblingProviderStateInfo: traits::ProviderParachainStateInfo< + Identifier = ConsumerRuntime::Identifier, + Commitment = ProviderDipMerkleHasher::Out, + >, OutputOf: Ord + From>, - SiblingProviderStateInfo::BlockNumber: Encode + Clone, + SiblingProviderStateInfo::BlockNumber: Parameter + 'static, SiblingProviderStateInfo::Commitment: Decode, SiblingProviderStateInfo::Key: AsRef<[u8]>, - LocalContextProvider: DidSignatureVerifierContext, - LocalContextProvider::BlockNumber: Encode + CheckedSub + From + PartialOrd, - LocalContextProvider::Hash: Encode, + LocalContextProvider: + DidSignatureVerifierContext, Hash = ConsumerRuntime::Hash>, LocalContextProvider::SignedExtra: Encode, - LocalDidDetails: Bump + Default + Encode, - LocalDidCallVerifier: - DipCallOriginFilter, DidVerificationKeyRelationship)>, + LocalDidCallVerifier: DipCallOriginFilter< + RuntimeCallOf, + OriginInfo = (DidVerificationKey, DidVerificationKeyRelationship), + >, ProviderDipMerkleHasher: sp_core::Hasher, - ProviderDidKeyId: Encode + Clone + Into, - ProviderAccountId: Encode + Clone, - ProviderLinkedAccountId: Encode + Clone, - ProviderWeb3Name: Encode + Clone, + ProviderDidKeyId: Parameter + 'static + Into, + ProviderAccountId: Parameter + 'static, + ProviderLinkedAccountId: Parameter + 'static, + ProviderWeb3Name: Parameter + 'static, { type Error = DipSiblingProviderStateProofVerifierError< ParachainHeadProofVerifierError, @@ -209,7 +334,6 @@ impl< DidMerkleProofVerifierError, RevealedDidKeysSignatureAndCallVerifierError, >; - type IdentityDetails = LocalDidDetails; type Proof = VersionedSiblingParachainDipStateProof< RelayChainStateInfo::BlockNumber, Vec>, @@ -220,9 +344,8 @@ impl< ProviderWeb3Name, ProviderLinkedAccountId, >, - LocalContextProvider::BlockNumber, + BlockNumberFor, >; - type Submitter = TxSubmitter; type VerificationResult = RevealedDidMerkleProofLeaves< ProviderDidKeyId, ProviderAccountId, @@ -234,31 +357,33 @@ impl< >; fn verify_proof_for_call_against_details( - call: &Call, - subject: &Subject, - submitter: &Self::Submitter, - identity_details: &mut Option, + call: &RuntimeCallOf, + subject: &ConsumerRuntime::Identifier, + submitter: &ConsumerRuntime::AccountId, + identity_details: &mut Option, proof: Self::Proof, ) -> Result { match proof { - VersionedSiblingParachainDipStateProof::V0(v0_proof) => { - v0::DipSiblingProviderStateProofVerifier::< - RelayChainStateInfo, - SiblingProviderParachainId, - SiblingProviderStateInfo, - TxSubmitter, - ProviderDipMerkleHasher, - ProviderDidKeyId, - ProviderAccountId, - ProviderWeb3Name, - ProviderLinkedAccountId, - MAX_REVEALED_KEYS_COUNT, - MAX_REVEALED_ACCOUNTS_COUNT, - LocalDidDetails, - LocalContextProvider, - LocalDidCallVerifier, - >::verify_proof_for_call_against_details(call, subject, submitter, identity_details, v0_proof) - } + VersionedSiblingParachainDipStateProof::V0(v0_proof) => as IdentityProofVerifier>::verify_proof_for_call_against_details( + call, + subject, + submitter, + identity_details, + v0_proof, + ), } } } @@ -270,6 +395,7 @@ pub mod latest { mod v0 { use super::*; + use frame_support::Parameter; use sp_std::borrow::Borrow; use crate::{ @@ -297,7 +423,6 @@ mod v0 { RelayChainStateInfo, SiblingProviderParachainId, SiblingProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, @@ -305,7 +430,6 @@ mod v0 { ProviderLinkedAccountId, const MAX_REVEALED_KEYS_COUNT: u32, const MAX_REVEALED_ACCOUNTS_COUNT: u32, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, >( @@ -314,25 +438,21 @@ mod v0 { RelayChainStateInfo, SiblingProviderParachainId, SiblingProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, ProviderWeb3Name, ProviderLinkedAccountId, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, )>, ); impl< - Call, - Subject, + ConsumerRuntime, RelayChainStateInfo, SiblingProviderParachainId, SiblingProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, @@ -340,15 +460,13 @@ mod v0 { ProviderLinkedAccountId, const MAX_REVEALED_KEYS_COUNT: u32, const MAX_REVEALED_ACCOUNTS_COUNT: u32, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, - > IdentityProofVerifier + > IdentityProofVerifier for DipSiblingProviderStateProofVerifier< RelayChainStateInfo, SiblingProviderParachainId, SiblingProviderStateInfo, - TxSubmitter, ProviderDipMerkleHasher, ProviderDidKeyId, ProviderAccountId, @@ -356,42 +474,41 @@ mod v0 { ProviderLinkedAccountId, MAX_REVEALED_KEYS_COUNT, MAX_REVEALED_ACCOUNTS_COUNT, - LocalDidDetails, LocalContextProvider, LocalDidCallVerifier, > where - Call: Encode, - TxSubmitter: Encode, + ConsumerRuntime: pallet_dip_consumer::Config, + ConsumerRuntime::LocalIdentityInfo: Bump + Default, RelayChainStateInfo: traits::RelayChainStorageInfo + traits::RelayChainStateInfo, OutputOf: Ord, - RelayChainStateInfo::BlockNumber: Copy + Into + TryFrom + HasCompact, + RelayChainStateInfo::BlockNumber: Parameter + 'static + Copy + Into + TryFrom + HasCompact, RelayChainStateInfo::Key: AsRef<[u8]>, SiblingProviderParachainId: Get, - SiblingProviderStateInfo: - traits::ProviderParachainStateInfo, + SiblingProviderStateInfo: traits::ProviderParachainStateInfo< + Identifier = ConsumerRuntime::Identifier, + Commitment = ProviderDipMerkleHasher::Out, + >, OutputOf: Ord + From>, - SiblingProviderStateInfo::BlockNumber: Encode + Clone, + SiblingProviderStateInfo::BlockNumber: Parameter + 'static, SiblingProviderStateInfo::Commitment: Decode, SiblingProviderStateInfo::Key: AsRef<[u8]>, - LocalContextProvider: DidSignatureVerifierContext, - LocalContextProvider::BlockNumber: Encode + CheckedSub + From + PartialOrd, - LocalContextProvider::Hash: Encode, + LocalContextProvider: + DidSignatureVerifierContext, Hash = ConsumerRuntime::Hash>, LocalContextProvider::SignedExtra: Encode, - LocalDidDetails: Bump + Default + Encode, LocalDidCallVerifier: DipCallOriginFilter< - Call, + RuntimeCallOf, OriginInfo = (DidVerificationKey, DidVerificationKeyRelationship), >, ProviderDipMerkleHasher: sp_core::Hasher, - ProviderDidKeyId: Encode + Clone + Into, - ProviderAccountId: Encode + Clone, - ProviderLinkedAccountId: Encode + Clone, - ProviderWeb3Name: Encode + Clone, + ProviderDidKeyId: Parameter + 'static + Into, + ProviderAccountId: Parameter + 'static, + ProviderLinkedAccountId: Parameter + 'static, + ProviderWeb3Name: Parameter + 'static, { type Error = DipSiblingProviderStateProofVerifierError< ParachainHeadProofVerifierError, @@ -399,7 +516,6 @@ mod v0 { DidMerkleProofVerifierError, RevealedDidKeysSignatureAndCallVerifierError, >; - type IdentityDetails = LocalDidDetails; type Proof = SiblingParachainDipStateProof< RelayChainStateInfo::BlockNumber, Vec>, @@ -410,9 +526,8 @@ mod v0 { ProviderWeb3Name, ProviderLinkedAccountId, >, - LocalContextProvider::BlockNumber, + BlockNumberFor, >; - type Submitter = TxSubmitter; type VerificationResult = RevealedDidMerkleProofLeaves< ProviderDidKeyId, ProviderAccountId, @@ -424,10 +539,10 @@ mod v0 { >; fn verify_proof_for_call_against_details( - call: &Call, - subject: &Subject, - submitter: &Self::Submitter, - identity_details: &mut Option, + call: &RuntimeCallOf, + subject: &ConsumerRuntime::Identifier, + submitter: &ConsumerRuntime::AccountId, + identity_details: &mut Option, proof: Self::Proof, ) -> Result { // 1. Verify relay chain proof. diff --git a/crates/kilt-dip-support/src/lib.rs b/crates/kilt-dip-support/src/lib.rs index 2daacfc64..032280168 100644 --- a/crates/kilt-dip-support/src/lib.rs +++ b/crates/kilt-dip-support/src/lib.rs @@ -29,4 +29,8 @@ pub mod utils; mod export; pub use export::*; -pub use state_proofs::relay_chain::RococoStateRootsViaRelayStorePallet; +pub use state_proofs::{ + parachain::{DipIdentityCommitmentProofVerifier, DipIdentityCommitmentProofVerifierError}, + relay_chain::{ParachainHeadProofVerifier, ParachainHeadProofVerifierError, RelayStateRootsViaRelayStorePallet}, +}; +pub use traits::{FrameSystemDidSignatureContext, ProviderParachainStateInfoViaProviderPallet}; diff --git a/crates/kilt-dip-support/src/state_proofs.rs b/crates/kilt-dip-support/src/state_proofs.rs index 1f4866ab8..0156143ce 100644 --- a/crates/kilt-dip-support/src/state_proofs.rs +++ b/crates/kilt-dip-support/src/state_proofs.rs @@ -166,9 +166,9 @@ pub(super) mod relay_chain { } } - pub struct RococoStateRootsViaRelayStorePallet(PhantomData); + pub struct RelayStateRootsViaRelayStorePallet(PhantomData); - impl RelayChainStorageInfo for RococoStateRootsViaRelayStorePallet + impl RelayChainStorageInfo for RelayStateRootsViaRelayStorePallet where Runtime: pallet_relay_store::Config, { @@ -190,7 +190,7 @@ pub(super) mod relay_chain { } } - impl RelayChainStateInfo for RococoStateRootsViaRelayStorePallet + impl RelayChainStateInfo for RelayStateRootsViaRelayStorePallet where Runtime: pallet_relay_store::Config, { diff --git a/crates/kilt-dip-support/src/traits.rs b/crates/kilt-dip-support/src/traits.rs index d3c013b1b..aa426aae1 100644 --- a/crates/kilt-dip-support/src/traits.rs +++ b/crates/kilt-dip-support/src/traits.rs @@ -17,7 +17,7 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org use frame_system::pallet_prelude::BlockNumberFor; -use pallet_dip_provider::IdentityCommitmentVersion; +use pallet_dip_provider::{IdentityCommitmentOf, IdentityCommitmentVersion}; use sp_core::storage::StorageKey; use sp_runtime::traits::{CheckedAdd, One, Zero}; use sp_std::marker::PhantomData; @@ -87,7 +87,7 @@ where T: pallet_dip_provider::Config, { type BlockNumber = BlockNumberFor; - type Commitment = T::IdentityCommitment; + type Commitment = IdentityCommitmentOf; type Hasher = T::Hashing; type Identifier = T::Identifier; type Key = StorageKey; diff --git a/crates/kilt-dip-support/src/utils.rs b/crates/kilt-dip-support/src/utils.rs index 30d2d01d5..e382093b9 100644 --- a/crates/kilt-dip-support/src/utils.rs +++ b/crates/kilt-dip-support/src/utils.rs @@ -16,123 +16,4 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org -use pallet_dip_provider::traits::IdentityProvider; -use parity_scale_codec::{Decode, Encode}; -use scale_info::TypeInfo; -use sp_std::marker::PhantomData; - -pub struct CombinedIdentityResult { - pub a: OutputA, - pub b: OutputB, - pub c: OutputC, -} - -impl From<(OutputA, OutputB, OutputC)> - for CombinedIdentityResult -{ - fn from(value: (OutputA, OutputB, OutputC)) -> Self { - Self { - a: value.0, - b: value.1, - c: value.2, - } - } -} - -impl CombinedIdentityResult -where - OutputB: Default, - OutputC: Default, -{ - pub fn from_a(a: OutputA) -> Self { - Self { - a, - b: OutputB::default(), - c: OutputC::default(), - } - } -} - -impl CombinedIdentityResult -where - OutputA: Default, - OutputC: Default, -{ - pub fn from_b(b: OutputB) -> Self { - Self { - a: OutputA::default(), - b, - c: OutputC::default(), - } - } -} - -impl CombinedIdentityResult -where - OutputA: Default, - OutputB: Default, -{ - pub fn from_c(c: OutputC) -> Self { - Self { - a: OutputA::default(), - b: OutputB::default(), - c, - } - } -} - -pub struct CombineIdentityFrom(PhantomData<(A, B, C)>); - -#[derive(Encode, Decode, TypeInfo)] -pub enum CombineError { - A(ErrorA), - B(ErrorB), - C(ErrorC), -} - -impl From> for u16 -where - ErrorA: Into, - ErrorB: Into, - ErrorC: Into, -{ - fn from(value: CombineError) -> Self { - match value { - CombineError::A(error) => error.into(), - CombineError::B(error) => error.into(), - CombineError::C(error) => error.into(), - } - } -} - -impl IdentityProvider for CombineIdentityFrom -where - A: IdentityProvider, - B: IdentityProvider, - C: IdentityProvider, -{ - type Error = CombineError; - type Success = CombinedIdentityResult, Option, Option>; - - fn retrieve(identifier: &Identifier) -> Result, Self::Error> { - match ( - A::retrieve(identifier), - B::retrieve(identifier), - C::retrieve(identifier), - ) { - // If no details is returned, return None for the whole result - (Ok(None), Ok(None), Ok(None)) => Ok(None), - // Otherwise, return `Some` or `None` depending on each result - (Ok(ok_a), Ok(ok_b), Ok(ok_c)) => Ok(Some(CombinedIdentityResult { - a: ok_a, - b: ok_b, - c: ok_c, - })), - (Err(e), _, _) => Err(CombineError::A(e)), - (_, Err(e), _) => Err(CombineError::B(e)), - (_, _, Err(e)) => Err(CombineError::C(e)), - } - } -} - pub type OutputOf = ::Output; diff --git a/dip-template/runtimes/dip-consumer/src/dip.rs b/dip-template/runtimes/dip-consumer/src/dip.rs index 91ca198c5..db25e1933 100644 --- a/dip-template/runtimes/dip-consumer/src/dip.rs +++ b/dip-template/runtimes/dip-consumer/src/dip.rs @@ -16,46 +16,36 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org -use did::{did_details::DidVerificationKey, DidVerificationKeyRelationship, KeyIdOf}; -use dip_provider_runtime_template::{AccountId as ProviderAccountId, Runtime as ProviderRuntime, Web3Name}; +use did::{did_details::DidVerificationKey, DidVerificationKeyRelationship}; +use dip_provider_runtime_template::{AccountId as ProviderAccountId, Runtime as ProviderRuntime}; use frame_support::traits::Contains; +use frame_system::EnsureSigned; use kilt_dip_support::{ - traits::{DipCallOriginFilter, FrameSystemDidSignatureContext, ProviderParachainStateInfoViaProviderPallet}, - RococoStateRootsViaRelayStorePallet, VersionedDipSiblingProviderStateProofVerifier, + traits::DipCallOriginFilter, KiltVersionedSiblingProviderVerifier, RelayStateRootsViaRelayStorePallet, }; -use pallet_did_lookup::linkable_account::LinkableAccountId; use pallet_dip_consumer::traits::IdentityProofVerifier; use sp_core::ConstU32; use sp_runtime::traits::BlakeTwo256; use crate::{AccountId, DidIdentifier, Runtime, RuntimeCall, RuntimeOrigin}; -pub type MerkleProofVerifierOutputOf = - >::VerificationResult; -pub type ProofVerifier = VersionedDipSiblingProviderStateProofVerifier< - RococoStateRootsViaRelayStorePallet, +pub type MerkleProofVerifierOutput = >::VerificationResult; +pub type ProofVerifier = KiltVersionedSiblingProviderVerifier< + ProviderRuntime, ConstU32<2_000>, - ProviderParachainStateInfoViaProviderPallet, - AccountId, + RelayStateRootsViaRelayStorePallet, BlakeTwo256, - KeyIdOf, - ProviderAccountId, - Web3Name, - LinkableAccountId, + DipCallFilter, 10, 10, - u128, - // Signatures are valid for 50 blocks - FrameSystemDidSignatureContext, - DipCallFilter, + 50, >; impl pallet_dip_consumer::Config for Runtime { type DipCallOriginFilter = PreliminaryDipOriginFilter; + type DispatchOriginCheck = EnsureSigned; type Identifier = DidIdentifier; - type IdentityProof = >::Proof; type LocalIdentityInfo = u128; - type ProofVerificationError = >::Error; type ProofVerifier = ProofVerifier; type RuntimeCall = RuntimeCall; type RuntimeOrigin = RuntimeOrigin; diff --git a/dip-template/runtimes/dip-consumer/src/origin_adapter.rs b/dip-template/runtimes/dip-consumer/src/origin_adapter.rs index b9d008e5c..d973367b3 100644 --- a/dip-template/runtimes/dip-consumer/src/origin_adapter.rs +++ b/dip-template/runtimes/dip-consumer/src/origin_adapter.rs @@ -16,7 +16,7 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org -use crate::{AccountId, DidIdentifier, MerkleProofVerifierOutputOf, RuntimeCall, RuntimeOrigin, Web3Name}; +use crate::{AccountId, DidIdentifier, MerkleProofVerifierOutput, RuntimeOrigin, Web3Name}; use frame_support::traits::EnsureOrigin; use pallet_dip_consumer::{DipOrigin, EnsureDipOrigin}; use pallet_postit::traits::Usernamable; @@ -41,9 +41,7 @@ impl EnsureOrigin for EnsureDipOriginAdapter { } #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] -pub struct DipOriginAdapter( - DipOrigin>, -); +pub struct DipOriginAdapter(DipOrigin); impl Usernamable for DipOriginAdapter { type Username = Web3Name; diff --git a/dip-template/runtimes/dip-provider/src/dip.rs b/dip-template/runtimes/dip-provider/src/dip.rs index 12ac28a33..91c52a8c1 100644 --- a/dip-template/runtimes/dip-provider/src/dip.rs +++ b/dip-template/runtimes/dip-provider/src/dip.rs @@ -19,10 +19,10 @@ use did::{DidRawOrigin, EnsureDidOrigin, KeyIdOf}; use frame_system::EnsureSigned; use pallet_did_lookup::linkable_account::LinkableAccountId; -use pallet_dip_provider::{traits::IdentityProvider, IdentityCommitmentVersion}; +use pallet_dip_provider::IdentityCommitmentVersion; use parity_scale_codec::{Decode, Encode}; use runtime_common::dip::{ - did::LinkedDidInfoProviderOf, + did::{LinkedDidInfoProvider, LinkedDidInfoProviderError}, merkle::{DidMerkleProofError, DidMerkleRootGenerator}, }; use scale_info::TypeInfo; @@ -31,7 +31,7 @@ use sp_std::vec::Vec; use crate::{ deposit::{DepositHooks, DepositNamespaces}, - AccountId, Balances, DidIdentifier, Hash, Runtime, RuntimeEvent, RuntimeHoldReason, + AccountId, Balances, DidIdentifier, Runtime, RuntimeEvent, RuntimeHoldReason, }; pub mod runtime_api { @@ -48,9 +48,8 @@ pub mod runtime_api { #[derive(Encode, Decode, TypeInfo)] pub enum DipProofError { - IdentityNotFound, - IdentityProviderError( as IdentityProvider>::Error), - MerkleProofError(DidMerkleProofError), + IdentityProvider(LinkedDidInfoProviderError), + MerkleProof(DidMerkleProofError), } } @@ -140,11 +139,8 @@ impl pallet_dip_provider::Config for Runtime { type CommitOriginCheck = EnsureDidOrigin; type CommitOrigin = DidRawOrigin; type Identifier = DidIdentifier; - type IdentityCommitment = Hash; type IdentityCommitmentGenerator = DidMerkleRootGenerator; - type IdentityCommitmentGeneratorError = DidMerkleProofError; - type IdentityProvider = LinkedDidInfoProviderOf; - type IdentityProviderError = as IdentityProvider>::Error; + type IdentityProvider = LinkedDidInfoProvider; type ProviderHooks = deposit::DepositCollectorHooks; type RuntimeEvent = RuntimeEvent; } diff --git a/dip-template/runtimes/dip-provider/src/lib.rs b/dip-template/runtimes/dip-provider/src/lib.rs index 94e58c36c..cab15778c 100644 --- a/dip-template/runtimes/dip-provider/src/lib.rs +++ b/dip-template/runtimes/dip-provider/src/lib.rs @@ -571,10 +571,9 @@ impl_runtime_apis! { impl kilt_runtime_api_dip_provider::DipProvider>, runtime_api::DipProofError> for Runtime { fn generate_proof(request: runtime_api::DipProofRequest) -> Result>, runtime_api::DipProofError> { - let maybe_identity_details = IdentityProviderOf::::retrieve(&request.identifier).map_err(runtime_api::DipProofError::IdentityProviderError)?; - let identity_details = maybe_identity_details.ok_or(runtime_api::DipProofError::IdentityNotFound)?; + let identity_details = IdentityProviderOf::::retrieve(&request.identifier).map_err(runtime_api::DipProofError::IdentityProvider)?; - DidMerkleRootGenerator::::generate_proof(&identity_details, request.version, request.keys.iter(), request.should_include_web3_name, request.accounts.iter()).map_err(runtime_api::DipProofError::MerkleProofError) + DidMerkleRootGenerator::::generate_proof(&identity_details, request.version, request.keys.iter(), request.should_include_web3_name, request.accounts.iter()).map_err(runtime_api::DipProofError::MerkleProof) } } } diff --git a/pallets/pallet-deposit-storage/src/deposit.rs b/pallets/pallet-deposit-storage/src/deposit.rs index a76acce61..c01296e23 100644 --- a/pallets/pallet-deposit-storage/src/deposit.rs +++ b/pallets/pallet-deposit-storage/src/deposit.rs @@ -24,7 +24,7 @@ use frame_support::{ }, }; use kilt_support::Deposit; -use pallet_dip_provider::{traits::ProviderHooks as DipProviderHooks, IdentityCommitmentVersion}; +use pallet_dip_provider::{traits::ProviderHooks as DipProviderHooks, IdentityCommitmentOf, IdentityCommitmentVersion}; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; use sp_runtime::traits::Get; @@ -74,7 +74,7 @@ where fn on_identity_committed( identifier: &Runtime::Identifier, submitter: &Runtime::AccountId, - _commitment: &Runtime::IdentityCommitment, + _commitment: &IdentityCommitmentOf, version: IdentityCommitmentVersion, ) -> Result<(), Self::Error> { let namespace = DepositsNamespace::get(); @@ -111,7 +111,7 @@ where fn on_commitment_removed( identifier: &Runtime::Identifier, _submitter: &Runtime::AccountId, - _commitment: &Runtime::IdentityCommitment, + _commitment: &IdentityCommitmentOf, version: pallet_dip_provider::IdentityCommitmentVersion, ) -> Result<(), Self::Error> { let namespace = DepositsNamespace::get(); diff --git a/pallets/pallet-deposit-storage/src/lib.rs b/pallets/pallet-deposit-storage/src/lib.rs index 267492344..e683b27ea 100644 --- a/pallets/pallet-deposit-storage/src/lib.rs +++ b/pallets/pallet-deposit-storage/src/lib.rs @@ -24,6 +24,7 @@ pub mod traits; pub use deposit::FixedDepositCollectorViaDepositsPallet; pub use pallet::*; +pub use traits::NoopDepositStorageHooks; #[frame_support::pallet(dev_mode)] pub mod pallet { diff --git a/pallets/pallet-dip-consumer/src/lib.rs b/pallets/pallet-dip-consumer/src/lib.rs index c513bbc13..e3a0a2133 100644 --- a/pallets/pallet-dip-consumer/src/lib.rs +++ b/pallets/pallet-dip-consumer/src/lib.rs @@ -25,7 +25,7 @@ pub mod traits; mod origin; -pub use crate::{origin::*, pallet::*}; +pub use crate::{origin::*, pallet::*, traits::SuccessfulProofVerifier}; #[frame_support::pallet(dev_mode)] pub mod pallet { @@ -39,10 +39,9 @@ pub mod pallet { use crate::traits::IdentityProofVerifier; - pub type VerificationResultOf = <::ProofVerifier as IdentityProofVerifier< - ::RuntimeCall, - ::Identifier, - >>::VerificationResult; + pub type IdentityProofOf = <::ProofVerifier as IdentityProofVerifier>::Proof; + pub type RuntimeCallOf = ::RuntimeCall; + pub type VerificationResultOf = <::ProofVerifier as IdentityProofVerifier>::VerificationResult; const STORAGE_VERSION: StorageVersion = StorageVersion::new(0); @@ -55,31 +54,17 @@ pub mod pallet { pub trait Config: frame_system::Config { /// Preliminary filter to filter out calls before doing any heavier /// computations. - type DipCallOriginFilter: Contains<::RuntimeCall>; + type DipCallOriginFilter: Contains>; + /// The origin check for the `dispatch_as` call. + type DispatchOriginCheck: EnsureOrigin<::RuntimeOrigin, Success = Self::AccountId>; /// The identifier of a subject, e.g., a DID. type Identifier: Parameter + MaxEncodedLen; - /// The proof users must provide to operate with their higher-level - /// identity. Depending on the use cases, this proof can contain - /// heterogeneous bits of information that the proof verifier will - /// utilize. For instance, a proof could contain both a Merkle proof and - /// a DID signature. - type IdentityProof: Parameter; /// The details stored in this pallet associated with any given subject. type LocalIdentityInfo: FullCodec + TypeInfo + MaxEncodedLen; - type ProofVerificationError: Into; /// The logic of the proof verifier, called upon each execution of the /// `dispatch_as` extrinsic. - type ProofVerifier: IdentityProofVerifier< - ::RuntimeCall, - Self::Identifier, - Error = Self::ProofVerificationError, - Proof = Self::IdentityProof, - IdentityDetails = Self::LocalIdentityInfo, - Submitter = ::AccountId, - >; - /// The overarching runtime call type. + type ProofVerifier: IdentityProofVerifier; type RuntimeCall: Parameter + Dispatchable::RuntimeOrigin>; - /// The overarching runtime origin type. type RuntimeOrigin: From> + From<::RuntimeOrigin>; } @@ -89,8 +74,6 @@ pub mod pallet { #[pallet::error] pub enum Error { - /// An identity with the provided identifier could not be found. - IdentityNotFound, /// The identity proof provided could not be successfully verified. InvalidProof(u16), /// The specified call is filtered by the DIP call origin filter. @@ -112,12 +95,10 @@ pub mod pallet { pub fn dispatch_as( origin: OriginFor, identifier: T::Identifier, - proof: T::IdentityProof, - call: Box<::RuntimeCall>, + proof: IdentityProofOf, + call: Box>, ) -> DispatchResult { - // TODO: Make origin check configurable, and require that it at least returns - // the submitter's account. - let submitter = ensure_signed(origin)?; + let submitter = T::DispatchOriginCheck::ensure_origin(origin)?; ensure!(T::DipCallOriginFilter::contains(&*call), Error::::Filtered); let mut identity_entry = IdentityEntries::::get(&identifier); let proof_verification_result = T::ProofVerifier::verify_proof_for_call_against_details( diff --git a/pallets/pallet-dip-consumer/src/traits.rs b/pallets/pallet-dip-consumer/src/traits.rs index 0b7fa2a85..921316628 100644 --- a/pallets/pallet-dip-consumer/src/traits.rs +++ b/pallets/pallet-dip-consumer/src/traits.rs @@ -16,40 +16,42 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org -use sp_std::marker::PhantomData; +use frame_support::Parameter; -pub trait IdentityProofVerifier { - type Error; - type IdentityDetails; - type Proof; - type Submitter; +use crate::{Config, RuntimeCallOf}; + +pub trait IdentityProofVerifier +where + Runtime: Config, +{ + type Error: Into; + type Proof: Parameter; type VerificationResult; fn verify_proof_for_call_against_details( - call: &Call, - subject: &Subject, - submitter: &Self::Submitter, - identity_details: &mut Option, + call: &RuntimeCallOf, + subject: &Runtime::Identifier, + submitter: &Runtime::AccountId, + identity_details: &mut Option, proof: Self::Proof, ) -> Result; } // Always returns success. -pub struct SuccessfulProofVerifier(PhantomData<(IdentityDetails, Proof, Submitter)>); -impl IdentityProofVerifier - for SuccessfulProofVerifier +pub struct SuccessfulProofVerifier; +impl IdentityProofVerifier for SuccessfulProofVerifier +where + Runtime: Config, { - type Error = (); - type IdentityDetails = IdentityDetails; - type Proof = Proof; - type Submitter = Submitter; + type Error = u16; + type Proof = (); type VerificationResult = (); fn verify_proof_for_call_against_details( - _call: &Call, - _subject: &Subject, - _submitter: &Self::Submitter, - _identity_details: &mut Option, + _call: &RuntimeCallOf, + _subject: &Runtime::Identifier, + _submitter: &Runtime::AccountId, + _identity_details: &mut Option, _proof: Self::Proof, ) -> Result { Ok(()) diff --git a/pallets/pallet-dip-provider/src/lib.rs b/pallets/pallet-dip-provider/src/lib.rs index f01b78c52..45ac1d34d 100644 --- a/pallets/pallet-dip-provider/src/lib.rs +++ b/pallets/pallet-dip-provider/src/lib.rs @@ -22,7 +22,10 @@ pub mod traits; -pub use crate::pallet::*; +pub use crate::{ + pallet::*, + traits::{DefaultIdentityCommitmentGenerator, DefaultIdentityProvider, NoopHooks}, +}; #[frame_support::pallet(dev_mode)] pub mod pallet { @@ -30,13 +33,13 @@ pub mod pallet { use frame_support::{pallet_prelude::*, traits::EnsureOrigin}; use frame_system::pallet_prelude::*; - use parity_scale_codec::FullCodec; - use sp_std::fmt::Debug; use crate::traits::{IdentityCommitmentGenerator, IdentityProvider, ProviderHooks, SubmitterInfo}; + pub type IdentityCommitmentOf = + <::IdentityCommitmentGenerator as IdentityCommitmentGenerator>::Output; pub type IdentityProviderOf = ::IdentityProvider; - pub type IdentityOf = <::IdentityProvider as IdentityProvider<::Identifier>>::Success; + pub type IdentityOf = <::IdentityProvider as IdentityProvider>::Success; pub type IdentityCommitmentVersion = u16; pub const LATEST_COMMITMENT_VERSION: IdentityCommitmentVersion = 0; @@ -47,16 +50,8 @@ pub mod pallet { type CommitOriginCheck: EnsureOrigin; type CommitOrigin: SubmitterInfo; type Identifier: Parameter + MaxEncodedLen; - type IdentityCommitment: Clone + Eq + Debug + TypeInfo + FullCodec + MaxEncodedLen; - type IdentityCommitmentGenerator: IdentityCommitmentGenerator< - Self::Identifier, - IdentityOf, - Error = Self::IdentityCommitmentGeneratorError, - Output = Self::IdentityCommitment, - >; - type IdentityCommitmentGeneratorError: Into; - type IdentityProvider: IdentityProvider; - type IdentityProviderError: Into; + type IdentityCommitmentGenerator: IdentityCommitmentGenerator; + type IdentityProvider: IdentityProvider; type ProviderHooks: ProviderHooks; type RuntimeEvent: From> + IsType<::RuntimeEvent>; } @@ -69,7 +64,7 @@ pub mod pallet { ::Identifier, Twox64Concat, IdentityCommitmentVersion, - ::IdentityCommitment, + IdentityCommitmentOf, >; #[pallet::pallet] @@ -79,24 +74,20 @@ pub mod pallet { #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { - IdentityCommitted { + VersionedIdentityCommitted { identifier: T::Identifier, - commitment: T::IdentityCommitment, + commitment: IdentityCommitmentOf, version: IdentityCommitmentVersion, }, VersionedIdentityDeleted { identifier: T::Identifier, version: IdentityCommitmentVersion, }, - IdentityDeleted { - identifier: T::Identifier, - }, } #[pallet::error] pub enum Error { - IdentityNotFound, - LimitTooLow, + CommitmentNotFound, IdentityProvider(u16), IdentityCommitmentGenerator(u16), Hook(u16), @@ -116,14 +107,11 @@ pub mod pallet { T::CommitOriginCheck::ensure_origin(origin).map(|e: ::CommitOrigin| e.submitter())?; let commitment_version = version.unwrap_or(LATEST_COMMITMENT_VERSION); - let commitment = match T::IdentityProvider::retrieve(&identifier) { - Ok(None) => Err(Error::::IdentityNotFound), - Err(error) => Err(Error::::IdentityProvider(error.into())), - Ok(Some(identity)) => { - T::IdentityCommitmentGenerator::generate_commitment(&identifier, &identity, commitment_version) - .map_err(|error| Error::::IdentityCommitmentGenerator(error.into())) - } - }?; + let identity = T::IdentityProvider::retrieve(&identifier) + .map_err(|error| Error::::IdentityProvider(error.into()))?; + let commitment = + T::IdentityCommitmentGenerator::generate_commitment(&identifier, &identity, commitment_version) + .map_err(|error| Error::::IdentityCommitmentGenerator(error.into()))?; IdentityCommitments::::try_mutate(&identifier, commitment_version, |commitment_entry| { if let Some(old_commitment) = commitment_entry { @@ -142,7 +130,7 @@ pub mod pallet { T::ProviderHooks::on_identity_committed(&identifier, &dispatcher, &commitment, commitment_version) .map_err(|e| Error::::Hook(e.into()))?; *commitment_entry = Some(commitment.clone()); - Self::deposit_event(Event::::IdentityCommitted { + Self::deposit_event(Event::::VersionedIdentityCommitted { identifier: identifier.clone(), commitment, version: commitment_version, @@ -175,8 +163,9 @@ pub mod pallet { pub fn delete_identity_commitment_storage_entry( identifier: &T::Identifier, version: IdentityCommitmentVersion, - ) -> Result { - let commitment = IdentityCommitments::::take(identifier, version).ok_or(Error::::IdentityNotFound)?; + ) -> Result, DispatchError> { + let commitment = + IdentityCommitments::::take(identifier, version).ok_or(Error::::CommitmentNotFound)?; Self::deposit_event(Event::::VersionedIdentityDeleted { identifier: identifier.clone(), version, diff --git a/pallets/pallet-dip-provider/src/traits.rs b/pallets/pallet-dip-provider/src/traits.rs index 52785c8ad..8cf5c0914 100644 --- a/pallets/pallet-dip-provider/src/traits.rs +++ b/pallets/pallet-dip-provider/src/traits.rs @@ -19,82 +19,84 @@ use did::DidRawOrigin; use frame_support::sp_runtime::AccountId32; -use crate::{Config, IdentityCommitmentVersion}; +use crate::{Config, IdentityCommitmentOf, IdentityCommitmentVersion}; -pub use identity_generation::*; -pub mod identity_generation { +pub use identity_provision::*; +pub mod identity_provision { use super::*; use sp_std::marker::PhantomData; - pub trait IdentityCommitmentGenerator { - type Error; - type Output; + pub trait IdentityProvider + where + Runtime: Config, + { + type Error: Into; + type Success; - fn generate_commitment( - identifier: &Identifier, - identity: &Identity, - version: IdentityCommitmentVersion, - ) -> Result; + fn retrieve(identifier: &Runtime::Identifier) -> Result; } - // Implement the `IdentityCommitmentGenerator` by returning the `Default` value - // for the `Output` type. - pub struct DefaultIdentityCommitmentGenerator(PhantomData); + // Return the `Default` value if `Identity` adn `Details` both implement it. + pub struct DefaultIdentityProvider(PhantomData); - impl IdentityCommitmentGenerator - for DefaultIdentityCommitmentGenerator + impl IdentityProvider for DefaultIdentityProvider where - Output: Default, + Runtime: Config, + Identity: Default, { - type Error = (); - type Output = Output; + type Error = u16; + type Success = Identity; - fn generate_commitment( - _identifier: &Identifier, - _identity: &Identity, - _version: IdentityCommitmentVersion, - ) -> Result { - Ok(Output::default()) + fn retrieve(_identifier: &Runtime::Identifier) -> Result { + Ok(Identity::default()) } } } -pub use identity_provision::*; -pub mod identity_provision { - use sp_std::marker::PhantomData; - - pub trait IdentityProvider { - type Error; - type Success; +pub use identity_generation::*; +pub mod identity_generation { + use super::*; - fn retrieve(identifier: &Identifier) -> Result, Self::Error>; - } + use crate::IdentityOf; - // Return the `Default` value if `Identity` adn `Details` both implement it. - pub struct DefaultIdentityProvider(PhantomData); + use parity_scale_codec::{FullCodec, MaxEncodedLen}; + use scale_info::TypeInfo; + use sp_std::{fmt::Debug, marker::PhantomData}; - impl IdentityProvider for DefaultIdentityProvider + pub trait IdentityCommitmentGenerator where - Identity: Default, + Runtime: Config, + Runtime::IdentityProvider: IdentityProvider, { - type Error = (); - type Success = Identity; + type Error: Into; + type Output: Clone + Eq + Debug + TypeInfo + FullCodec + MaxEncodedLen; - fn retrieve(_identifier: &Identifier) -> Result, Self::Error> { - Ok(Some(Identity::default())) - } + fn generate_commitment( + identifier: &Runtime::Identifier, + identity: &IdentityOf, + version: IdentityCommitmentVersion, + ) -> Result; } - // Always return `None`. Might be useful for tests. - pub struct NoneIdentityProvider; + // Implement the `IdentityCommitmentGenerator` by returning the `Default` value + // for the `Output` type. + pub struct DefaultIdentityCommitmentGenerator(PhantomData); - impl IdentityProvider for NoneIdentityProvider { - type Error = (); - type Success = (); + impl IdentityCommitmentGenerator for DefaultIdentityCommitmentGenerator + where + Runtime: Config, + Output: Default + Clone + Eq + Debug + TypeInfo + FullCodec + MaxEncodedLen, + { + type Error = u16; + type Output = Output; - fn retrieve(_identifier: &Identifier) -> Result, Self::Error> { - Ok(None) + fn generate_commitment( + _identifier: &Runtime::Identifier, + _identity: &IdentityOf, + _version: IdentityCommitmentVersion, + ) -> Result { + Ok(Output::default()) } } } @@ -133,14 +135,14 @@ where fn on_identity_committed( identifier: &Runtime::Identifier, submitter: &Runtime::AccountId, - commitment: &Runtime::IdentityCommitment, + commitment: &IdentityCommitmentOf, version: IdentityCommitmentVersion, ) -> Result<(), Self::Error>; fn on_commitment_removed( identifier: &Runtime::Identifier, submitter: &Runtime::AccountId, - commitment: &Runtime::IdentityCommitment, + commitment: &IdentityCommitmentOf, version: IdentityCommitmentVersion, ) -> Result<(), Self::Error>; } @@ -156,7 +158,7 @@ where fn on_commitment_removed( _identifier: &Runtime::Identifier, _submitter: &Runtime::AccountId, - _commitment: &Runtime::IdentityCommitment, + _commitment: &IdentityCommitmentOf, _version: IdentityCommitmentVersion, ) -> Result<(), Self::Error> { Ok(()) @@ -165,7 +167,7 @@ where fn on_identity_committed( _identifier: &Runtime::Identifier, _submitter: &Runtime::AccountId, - _commitment: &Runtime::IdentityCommitment, + _commitment: &IdentityCommitmentOf, _version: IdentityCommitmentVersion, ) -> Result<(), Self::Error> { Ok(()) diff --git a/runtimes/common/src/dip/did.rs b/runtimes/common/src/dip/did.rs index f4255c438..134c65de9 100644 --- a/runtimes/common/src/dip/did.rs +++ b/runtimes/common/src/dip/did.rs @@ -18,98 +18,83 @@ use did::did_details::DidDetails; use frame_system::pallet_prelude::BlockNumberFor; -use kilt_dip_support::{ - merkle::RevealedWeb3Name, - utils::{CombineIdentityFrom, CombinedIdentityResult}, -}; +use kilt_dip_support::merkle::RevealedWeb3Name; use pallet_did_lookup::linkable_account::LinkableAccountId; use pallet_dip_provider::traits::IdentityProvider; use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; -use sp_std::{marker::PhantomData, vec::Vec}; +use sp_std::vec::Vec; #[derive(Encode, Decode, TypeInfo)] -pub enum DidIdentityProviderError { +pub enum LinkedDidInfoProviderError { DidNotFound, + DidDeleted, Internal, } -impl From for u16 { - fn from(value: DidIdentityProviderError) -> Self { +impl From for u16 { + fn from(value: LinkedDidInfoProviderError) -> Self { match value { - DidIdentityProviderError::DidNotFound => 0, - DidIdentityProviderError::Internal => u16::MAX, + LinkedDidInfoProviderError::DidNotFound => 0, + LinkedDidInfoProviderError::DidDeleted => 1, + LinkedDidInfoProviderError::Internal => u16::MAX, } } } -pub struct DidIdentityProvider(PhantomData); +pub type Web3OwnershipOf = + RevealedWeb3Name<::Web3Name, BlockNumberFor>; -impl IdentityProvider for DidIdentityProvider +pub struct LinkedDidInfoOf where - T: did::Config, + Runtime: did::Config + pallet_web3_names::Config, { - type Error = DidIdentityProviderError; - type Success = DidDetails; - - fn retrieve(identifier: &T::DidIdentifier) -> Result, Self::Error> { - match ( - did::Pallet::::get_did(identifier), - did::Pallet::::get_deleted_did(identifier), - ) { - (Some(details), _) => Ok(Some(details)), - (_, Some(_)) => Ok(None), - _ => Err(DidIdentityProviderError::DidNotFound), - } - } -} - -pub type Web3OwnershipOf = RevealedWeb3Name<::Web3Name, BlockNumberFor>; - -pub struct DidWeb3NameProvider(PhantomData); - -impl IdentityProvider for DidWeb3NameProvider -where - T: pallet_web3_names::Config, -{ - type Error = DidIdentityProviderError; - type Success = Web3OwnershipOf; - - fn retrieve(identifier: &T::Web3NameOwner) -> Result, Self::Error> { - let Some(web3_name) = pallet_web3_names::Pallet::::names(identifier) else { - return Ok(None); - }; - let Some(details) = pallet_web3_names::Pallet::::owner(&web3_name) else { - log::error!( - "Inconsistent reverse map pallet_web3_names::owner(web3_name). Cannot find owner for web3name {:#?}", - web3_name - ); - return Err(DidIdentityProviderError::Internal); - }; - Ok(Some(Web3OwnershipOf:: { - web3_name, - claimed_at: details.claimed_at, - })) - } + pub did_details: DidDetails, + pub web3_name_details: Option>, + pub linked_accounts: Vec, } -pub struct DidLinkedAccountsProvider(PhantomData); +pub struct LinkedDidInfoProvider; -impl IdentityProvider for DidLinkedAccountsProvider +impl IdentityProvider for LinkedDidInfoProvider where - T: pallet_did_lookup::Config, + Runtime: did::Config::Identifier> + + pallet_web3_names::Config::Identifier> + + pallet_did_lookup::Config::Identifier> + + pallet_dip_provider::Config, { - type Error = DidIdentityProviderError; - type Success = Vec; + type Error = LinkedDidInfoProviderError; + type Success = LinkedDidInfoOf; - fn retrieve(identifier: &T::DidIdentifier) -> Result, Self::Error> { - Ok(Some( - pallet_did_lookup::ConnectedAccounts::::iter_key_prefix(identifier).collect(), - )) + fn retrieve(identifier: &Runtime::Identifier) -> Result { + let did_details = match ( + did::Pallet::::get_did(identifier), + did::Pallet::::get_deleted_did(identifier), + ) { + (Some(details), _) => Ok(details), + (_, Some(_)) => Err(LinkedDidInfoProviderError::DidDeleted), + _ => Err(LinkedDidInfoProviderError::DidNotFound), + }?; + let web3_name_details = if let Some(web3_name) = pallet_web3_names::Pallet::::names(identifier) { + let Some(ownership) = pallet_web3_names::Pallet::::owner(&web3_name) else { + log::error!( + "Inconsistent reverse map pallet_web3_names::owner(web3_name). Cannot find owner for web3name {:#?}", + web3_name + ); + return Err(LinkedDidInfoProviderError::Internal); + }; + Ok(Some(Web3OwnershipOf:: { + web3_name, + claimed_at: ownership.claimed_at, + })) + } else { + Ok(None) + }?; + let linked_accounts = pallet_did_lookup::ConnectedAccounts::::iter_key_prefix(identifier).collect(); + Ok(LinkedDidInfoOf { + did_details, + web3_name_details, + linked_accounts, + }) } } - -pub type LinkedDidInfoProviderOf = - CombineIdentityFrom, DidWeb3NameProvider, DidLinkedAccountsProvider>; -pub type LinkedDidInfoOf = - CombinedIdentityResult>, Option>, Option>>; diff --git a/runtimes/common/src/dip/merkle.rs b/runtimes/common/src/dip/merkle.rs index d7af9c87b..ec62b3d6a 100644 --- a/runtimes/common/src/dip/merkle.rs +++ b/runtimes/common/src/dip/merkle.rs @@ -20,7 +20,10 @@ use frame_support::RuntimeDebug; use frame_system::pallet_prelude::BlockNumberFor; use kilt_dip_support::merkle::{DidKeyMerkleKey, DidKeyMerkleValue, DidMerkleProof}; use pallet_did_lookup::linkable_account::LinkableAccountId; -use pallet_dip_provider::{traits::IdentityCommitmentGenerator, IdentityCommitmentVersion}; +use pallet_dip_provider::{ + traits::{IdentityCommitmentGenerator, IdentityProvider}, + IdentityCommitmentVersion, IdentityOf, +}; use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; use sp_std::{borrow::ToOwned, marker::PhantomData, vec::Vec}; @@ -28,7 +31,7 @@ use sp_trie::{generate_trie_proof, LayoutV1, MemoryDB, TrieDBMutBuilder, TrieHas use kilt_dip_support::merkle::{DidKeyRelationship, RevealedDidMerkleProofLeaf}; -use crate::{dip::did::LinkedDidInfoOf, DidIdentifier}; +use crate::dip::did::LinkedDidInfoOf; pub type BlindedValue = Vec; pub type DidMerkleProofOf = DidMerkleProof< @@ -51,7 +54,6 @@ pub struct CompleteMerkleProof { #[derive(Clone, RuntimeDebug, Encode, Decode, TypeInfo, PartialEq)] pub enum DidMerkleProofError { UnsupportedVersion, - DidNotFound, KeyNotFound, LinkedAccountNotFound, Web3NameNotFound, @@ -62,10 +64,9 @@ impl From for u16 { fn from(value: DidMerkleProofError) -> Self { match value { DidMerkleProofError::UnsupportedVersion => 0, - DidMerkleProofError::DidNotFound => 1, - DidMerkleProofError::KeyNotFound => 2, - DidMerkleProofError::LinkedAccountNotFound => 3, - DidMerkleProofError::Web3NameNotFound => 4, + DidMerkleProofError::KeyNotFound => 1, + DidMerkleProofError::LinkedAccountNotFound => 2, + DidMerkleProofError::Web3NameNotFound => 3, DidMerkleProofError::Internal => u16::MAX, } } @@ -89,10 +90,11 @@ pub mod v0 { where Runtime: did::Config + pallet_did_lookup::Config + pallet_web3_names::Config, { - // Fails if the DID details do not exist. - let (Some(did_details), web3_name, linked_accounts) = (&identity.a, &identity.b, &identity.c) else { - return Err(DidMerkleProofError::DidNotFound); - }; + let LinkedDidInfoOf { + did_details, + web3_name_details, + linked_accounts, + } = identity; let mut trie = TrieHash::>::default(); let mut trie_builder = TrieDBMutBuilder::>::new(db, &mut trie).build(); @@ -186,30 +188,28 @@ pub mod v0 { })?; // Linked accounts - if let Some(linked_accounts) = linked_accounts { - linked_accounts - .iter() - .try_for_each(|linked_account| -> Result<(), DidMerkleProofError> { - let linked_account_leaf = - ProofLeafOf::::LinkedAccount(linked_account.clone().into(), ().into()); - trie_builder - .insert( - linked_account_leaf.encoded_key().as_slice(), - linked_account_leaf.encoded_value().as_slice(), - ) - .map_err(|_| { - log::error!( - "Failed to insert linked account in the trie builder. Linked account leaf: {:#?}", - linked_account_leaf - ); - DidMerkleProofError::Internal - })?; - Ok(()) - })?; - } + linked_accounts + .iter() + .try_for_each(|linked_account| -> Result<(), DidMerkleProofError> { + let linked_account_leaf = + ProofLeafOf::::LinkedAccount(linked_account.clone().into(), ().into()); + trie_builder + .insert( + linked_account_leaf.encoded_key().as_slice(), + linked_account_leaf.encoded_value().as_slice(), + ) + .map_err(|_| { + log::error!( + "Failed to insert linked account in the trie builder. Linked account leaf: {:#?}", + linked_account_leaf + ); + DidMerkleProofError::Internal + })?; + Ok(()) + })?; // Web3name, if present - if let Some(web3name_details) = web3_name { + if let Some(web3name_details) = web3_name_details { let web3_name_leaf = ProofLeafOf::::Web3Name( web3name_details.web3_name.clone().into(), web3name_details.claimed_at.into(), @@ -243,10 +243,11 @@ pub mod v0 { K: Iterator>, A: Iterator, { - // Fails if the DID details do not exist. - let (Some(did_details), linked_web3_name, linked_accounts) = (&identity.a, &identity.b, &identity.c) else { - return Err(DidMerkleProofError::DidNotFound); - }; + let LinkedDidInfoOf { + did_details, + web3_name_details, + linked_accounts, + } = identity; let mut db = MemoryDB::default(); let root = calculate_root_with_db(identity, &mut db)?; @@ -278,11 +279,6 @@ pub mod v0 { )) }) .chain(account_ids.map(|account_id| -> Result<_, DidMerkleProofError> { - let Some(linked_accounts) = linked_accounts else { - // Directly LinkedAccountNotFound since there's no linked accounts to check - // against. - return Err(DidMerkleProofError::LinkedAccountNotFound); - }; if linked_accounts.contains(account_id) { Ok(RevealedDidMerkleProofLeaf::LinkedAccount( account_id.clone().into(), @@ -294,7 +290,7 @@ pub mod v0 { })) .collect::, _>>()?; - match (should_include_web3_name, linked_web3_name) { + match (should_include_web3_name, web3_name_details) { // If web3name should be included and it exists... (true, Some(web3name_details)) => { leaves.push(RevealedDidMerkleProofLeaf::Web3Name( @@ -328,10 +324,11 @@ pub mod v0 { } pub(super) fn generate_commitment( - identity: &LinkedDidInfoOf, + identity: &IdentityOf, ) -> Result where - Runtime: did::Config + pallet_did_lookup::Config + pallet_web3_names::Config, + Runtime: did::Config + pallet_did_lookup::Config + pallet_web3_names::Config + pallet_dip_provider::Config, + Runtime::IdentityProvider: IdentityProvider>, { let mut db = MemoryDB::default(); calculate_root_with_db(identity, &mut db) @@ -340,18 +337,19 @@ pub mod v0 { pub struct DidMerkleRootGenerator(PhantomData); -impl IdentityCommitmentGenerator> for DidMerkleRootGenerator +impl IdentityCommitmentGenerator for DidMerkleRootGenerator where - Runtime: did::Config + pallet_did_lookup::Config + pallet_web3_names::Config, + Runtime: did::Config + pallet_did_lookup::Config + pallet_web3_names::Config + pallet_dip_provider::Config, + Runtime::IdentityProvider: IdentityProvider>, { type Error = DidMerkleProofError; type Output = Runtime::Hash; fn generate_commitment( - _identifier: &DidIdentifier, - identity: &LinkedDidInfoOf, + _identifier: &Runtime::Identifier, + identity: &IdentityOf, version: IdentityCommitmentVersion, - ) -> Result { + ) -> Result { match version { 0 => v0::generate_commitment::(identity), _ => Err(DidMerkleProofError::UnsupportedVersion),