Skip to content

Commit

Permalink
Improve identity provider flow
Browse files Browse the repository at this point in the history
  • Loading branch information
ntn-x2 committed Nov 13, 2023
1 parent 467d8df commit 1e338a4
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 269 deletions.
120 changes: 0 additions & 120 deletions crates/kilt-dip-support/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,124 +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<OutputA, OutputB, OutputC> {
pub a: OutputA,
pub b: OutputB,
pub c: OutputC,
}

impl<OutputA, OutputB, OutputC> From<(OutputA, OutputB, OutputC)>
for CombinedIdentityResult<OutputA, OutputB, OutputC>
{
fn from(value: (OutputA, OutputB, OutputC)) -> Self {
Self {
a: value.0,
b: value.1,
c: value.2,
}
}
}

impl<OutputA, OutputB, OutputC> CombinedIdentityResult<OutputA, OutputB, OutputC>
where
OutputB: Default,
OutputC: Default,
{
pub fn from_a(a: OutputA) -> Self {
Self {
a,
b: OutputB::default(),
c: OutputC::default(),
}
}
}

impl<OutputA, OutputB, OutputC> CombinedIdentityResult<OutputA, OutputB, OutputC>
where
OutputA: Default,
OutputC: Default,
{
pub fn from_b(b: OutputB) -> Self {
Self {
a: OutputA::default(),
b,
c: OutputC::default(),
}
}
}

impl<OutputA, OutputB, OutputC> CombinedIdentityResult<OutputA, OutputB, OutputC>
where
OutputA: Default,
OutputB: Default,
{
pub fn from_c(c: OutputC) -> Self {
Self {
a: OutputA::default(),
b: OutputB::default(),
c,
}
}
}

pub struct CombineIdentityFrom<A, B, C>(PhantomData<(A, B, C)>);

#[derive(Encode, Decode, TypeInfo)]
pub enum CombineError<ErrorA, ErrorB, ErrorC> {
A(ErrorA),
B(ErrorB),
C(ErrorC),
}

impl<ErrorA, ErrorB, ErrorC> From<CombineError<ErrorA, ErrorB, ErrorC>> for u16
where
ErrorA: Into<u16>,
ErrorB: Into<u16>,
ErrorC: Into<u16>,
{
fn from(value: CombineError<ErrorA, ErrorB, ErrorC>) -> Self {
match value {
CombineError::A(error) => error.into(),
CombineError::B(error) => error.into(),
CombineError::C(error) => error.into(),
}
}
}

impl<Runtime, A, B, C> IdentityProvider<Runtime> for CombineIdentityFrom<A, B, C>
where
Runtime: pallet_dip_provider::Config,
A: IdentityProvider<Runtime>,
B: IdentityProvider<Runtime>,
C: IdentityProvider<Runtime>,
{
type Error = CombineError<A::Error, B::Error, C::Error>;
type Success = CombinedIdentityResult<Option<A::Success>, Option<B::Success>, Option<C::Success>>;

fn retrieve(identifier: &Runtime::Identifier) -> Result<Option<Self::Success>, 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<Hasher> = <Hasher as sp_runtime::traits::Hash>::Output;
9 changes: 4 additions & 5 deletions dip-template/runtimes/dip-provider/src/dip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,8 +48,7 @@ pub mod runtime_api {

#[derive(Encode, Decode, TypeInfo)]
pub enum DipProofError {
IdentityNotFound,
IdentityProviderError(<LinkedDidInfoProviderOf<Runtime> as IdentityProvider<Runtime>>::Error),
IdentityProviderError(LinkedDidInfoProviderError),
MerkleProofError(DidMerkleProofError),
}
}
Expand Down Expand Up @@ -141,7 +140,7 @@ impl pallet_dip_provider::Config for Runtime {
type CommitOrigin = DidRawOrigin<DidIdentifier, AccountId>;
type Identifier = DidIdentifier;
type IdentityCommitmentGenerator = DidMerkleRootGenerator<Runtime>;
type IdentityProvider = LinkedDidInfoProviderOf<Runtime>;
type IdentityProvider = LinkedDidInfoProvider;
type ProviderHooks = deposit::DepositCollectorHooks;
type RuntimeEvent = RuntimeEvent;
}
3 changes: 1 addition & 2 deletions dip-template/runtimes/dip-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,7 @@ impl_runtime_apis! {

impl kilt_runtime_api_dip_provider::DipProvider<Block, runtime_api::DipProofRequest, CompleteMerkleProof<Hash, DidMerkleProofOf<Runtime>>, runtime_api::DipProofError> for Runtime {
fn generate_proof(request: runtime_api::DipProofRequest) -> Result<CompleteMerkleProof<Hash, DidMerkleProofOf<Runtime>>, runtime_api::DipProofError> {
let maybe_identity_details = IdentityProviderOf::<Runtime>::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::<Runtime>::retrieve(&request.identifier).map_err(runtime_api::DipProofError::IdentityProviderError)?;

DidMerkleRootGenerator::<Runtime>::generate_proof(&identity_details, request.version, request.keys.iter(), request.should_include_web3_name, request.accounts.iter()).map_err(runtime_api::DipProofError::MerkleProofError)
}
Expand Down
24 changes: 12 additions & 12 deletions pallets/pallet-dip-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

pub mod traits;

pub use crate::pallet::*;
pub use traits::{DefaultIdentityCommitmentGenerator, DefaultIdentityProvider, NoneIdentityProvider, NoopHooks};
pub use crate::{
pallet::*,
traits::{DefaultIdentityCommitmentGenerator, DefaultIdentityProvider, NoopHooks},
};

#[frame_support::pallet(dev_mode)]
pub mod pallet {
Expand Down Expand Up @@ -85,7 +87,7 @@ pub mod pallet {

#[pallet::error]
pub enum Error<T> {
IdentityNotFound,
CommitmentNotFound,
IdentityProvider(u16),
IdentityCommitmentGenerator(u16),
Hook(u16),
Expand All @@ -105,14 +107,11 @@ pub mod pallet {
T::CommitOriginCheck::ensure_origin(origin).map(|e: <T as Config>::CommitOrigin| e.submitter())?;

let commitment_version = version.unwrap_or(LATEST_COMMITMENT_VERSION);
let commitment = match T::IdentityProvider::retrieve(&identifier) {
Ok(None) => Err(Error::<T>::IdentityNotFound),
Err(error) => Err(Error::<T>::IdentityProvider(error.into())),
Ok(Some(identity)) => {
T::IdentityCommitmentGenerator::generate_commitment(&identifier, &identity, commitment_version)
.map_err(|error| Error::<T>::IdentityCommitmentGenerator(error.into()))
}
}?;
let identity = T::IdentityProvider::retrieve(&identifier)
.map_err(|error| Error::<T>::IdentityProvider(error.into()))?;
let commitment =
T::IdentityCommitmentGenerator::generate_commitment(&identifier, &identity, commitment_version)
.map_err(|error| Error::<T>::IdentityCommitmentGenerator(error.into()))?;

IdentityCommitments::<T>::try_mutate(&identifier, commitment_version, |commitment_entry| {
if let Some(old_commitment) = commitment_entry {
Expand Down Expand Up @@ -165,7 +164,8 @@ pub mod pallet {
identifier: &T::Identifier,
version: IdentityCommitmentVersion,
) -> Result<IdentityCommitmentOf<T>, DispatchError> {
let commitment = IdentityCommitments::<T>::take(identifier, version).ok_or(Error::<T>::IdentityNotFound)?;
let commitment =
IdentityCommitments::<T>::take(identifier, version).ok_or(Error::<T>::CommitmentNotFound)?;
Self::deposit_event(Event::<T>::VersionedIdentityDeleted {
identifier: identifier.clone(),
version,
Expand Down
21 changes: 3 additions & 18 deletions pallets/pallet-dip-provider/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub mod identity_provision {
type Error: Into<u16>;
type Success;

fn retrieve(identifier: &Runtime::Identifier) -> Result<Option<Self::Success>, Self::Error>;
fn retrieve(identifier: &Runtime::Identifier) -> Result<Self::Success, Self::Error>;
}

// Return the `Default` value if `Identity` adn `Details` both implement it.
Expand All @@ -48,23 +48,8 @@ pub mod identity_provision {
type Error = u16;
type Success = Identity;

fn retrieve(_identifier: &Runtime::Identifier) -> Result<Option<Self::Success>, Self::Error> {
Ok(Some(Identity::default()))
}
}

// Always return `None`. Might be useful for tests.
pub struct NoneIdentityProvider;

impl<Runtime> IdentityProvider<Runtime> for NoneIdentityProvider
where
Runtime: Config,
{
type Error = u16;
type Success = ();

fn retrieve(_identifier: &Runtime::Identifier) -> Result<Option<Self::Success>, Self::Error> {
Ok(None)
fn retrieve(_identifier: &Runtime::Identifier) -> Result<Self::Success, Self::Error> {
Ok(Identity::default())
}
}
}
Expand Down
Loading

0 comments on commit 1e338a4

Please sign in to comment.