Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
54a23a5
Refactor pallet-dip-provider
ntn-x2 Jul 11, 2023
25b3cb6
Remove unused dependencies from pallet-dip-provider
ntn-x2 Jul 11, 2023
c7ef216
mIntroduce state proofs in pallet-dip-consumer
ntn-x2 Jul 11, 2023
06de832
Temporarly fix kilt-dip-support to compile
ntn-x2 Jul 11, 2023
dda48d6
Whole project compiling
ntn-x2 Jul 11, 2023
cfe1fb3
Add parachain root state proof verifier
ntn-x2 Jul 12, 2023
77b2a48
Proof of storage entry for parachain also working
ntn-x2 Jul 12, 2023
4fd3f06
Half-way
ntn-x2 Jul 12, 2023
a9ca939
Before replacing 'read_proof_check'
ntn-x2 Jul 13, 2023
81ea88c
Ported the proof verification for no_std
ntn-x2 Jul 13, 2023
e8919f1
Refactoring on the way
ntn-x2 Jul 13, 2023
8808f14
New overarching verifier
ntn-x2 Jul 13, 2023
16b1bc9
Whole workspace compiling (not run/tested yet)
ntn-x2 Jul 13, 2023
47481e0
Keep track of the last 2 relay state roots to allow for proof creation
ntn-x2 Jul 14, 2023
50edc7b
Compiles
ntn-x2 Jul 17, 2023
f542428
Use relay block height instead of block hash
ntn-x2 Jul 18, 2023
318d765
Cleaning before kilt-dip-support
ntn-x2 Jul 20, 2023
7b67a57
On the way there
ntn-x2 Jul 20, 2023
d7dda64
Step n.2
ntn-x2 Jul 20, 2023
6b94cbf
all good
ntn-x2 Jul 21, 2023
622c046
Everything compiling
ntn-x2 Jul 21, 2023
2718f95
Still working
ntn-x2 Jul 21, 2023
f00cf33
Refactor complete
ntn-x2 Jul 21, 2023
f91693d
Pallet updated
ntn-x2 Jul 21, 2023
d81ac18
Renaming
ntn-x2 Jul 21, 2023
22b577c
Change SignedExtra to not be Optional
ntn-x2 Jul 21, 2023
358c095
clippy
ntn-x2 Jul 21, 2023
6020f80
Remove unnecessary derive
ntn-x2 Jul 21, 2023
408f5b9
Remove XCM from templates
ntn-x2 Jul 21, 2023
4dfc7a3
Remove unwanted file
ntn-x2 Jul 21, 2023
ff68537
Merge branch 'aa/dip' into aa/state-proofs
ntn-x2 Aug 10, 2023
9cbbd54
feat: add support for relay chain (#553)
ntn-x2 Aug 23, 2023
9d36356
feat: add relaychain support (#563)
ntn-x2 Sep 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Refactor pallet-dip-provider
  • Loading branch information
ntn-x2 committed Jul 11, 2023
commit 54a23a534da9aafb99ab13c3e2c5594d6292625c
91 changes: 40 additions & 51 deletions pallets/pallet-dip-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,37 @@ pub use crate::pallet::*;
pub mod pallet {
use super::*;

use frame_support::{pallet_prelude::*, traits::EnsureOrigin, weights::Weight};
use frame_support::{pallet_prelude::*, traits::EnsureOrigin};
use frame_system::pallet_prelude::*;
use sp_std::{boxed::Box, fmt::Debug};
use xcm::{latest::prelude::*, VersionedMultiAsset, VersionedMultiLocation};
use parity_scale_codec::FullCodec;
use sp_std::fmt::Debug;

use dip_support::IdentityDetailsAction;

use crate::traits::{IdentityProofDispatcher, IdentityProofGenerator, IdentityProvider, SubmitterInfo, TxBuilder};
use crate::traits::{IdentityCommitmentGenerator, IdentityProvider, SubmitterInfo};

pub type IdentityOf<T> = <<T as Config>::IdentityProvider as IdentityProvider<<T as Config>::Identifier>>::Success;
pub type IdentityProofActionOf<T> = IdentityDetailsAction<<T as Config>::Identifier, <T as Config>::ProofOutput>;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);

#[pallet::config]
pub trait Config: frame_system::Config {
type CommitOriginCheck: EnsureOrigin<Self::RuntimeOrigin, Success = Self::CommitOrigin>;
type CommitOrigin: SubmitterInfo<Submitter = Self::AccountId>;
type Identifier: Parameter;
type IdentityProofGenerator: IdentityProofGenerator<
type Identifier: Parameter + MaxEncodedLen;
type IdentityCommitment: Clone + Eq + Debug + TypeInfo + FullCodec + MaxEncodedLen;
type IdentityCommitmentGenerator: IdentityCommitmentGenerator<
Self::Identifier,
IdentityOf<Self>,
Output = Self::ProofOutput,
Output = Self::IdentityCommitment,
>;
type IdentityProofDispatcher: IdentityProofDispatcher<Self::Identifier, Self::ProofOutput, Self::AccountId, ()>;
type IdentityProvider: IdentityProvider<Self::Identifier>;
type ProofOutput: Clone + Eq + Debug;
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type TxBuilder: TxBuilder<Self::Identifier, Self::ProofOutput, ()>;
}

#[pallet::storage]
#[pallet::getter(fn identity_commitments)]
pub(crate) type IdentityCommitments<T> =
StorageMap<_, Twox64Concat, <T as Config>::Identifier, <T as Config>::IdentityCommitment>;

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::storage_version(STORAGE_VERSION)]
Expand All @@ -67,60 +67,49 @@ pub mod pallet {
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
IdentityInfoDispatched(IdentityProofActionOf<T>, Box<MultiLocation>),
IdentityCommitted {
identifier: T::Identifier,
commitment: T::IdentityCommitment,
},
IdentityDeleted {
identifier: T::Identifier,
},
}

#[pallet::error]
pub enum Error<T> {
BadVersion,
Dispatch,
IdentityNotFound,
IdentityProofGeneration,
Predispatch,
IdentityCommitmentGeneration,
}

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
// TODO: Update weight
#[pallet::weight(0)]
pub fn commit_identity(
origin: OriginFor<T>,
identifier: T::Identifier,
destination: Box<VersionedMultiLocation>,
asset: Box<VersionedMultiAsset>,
weight: Weight,
) -> DispatchResult {
let dispatcher = T::CommitOriginCheck::ensure_origin(origin).map(|e| e.submitter())?;

let destination: MultiLocation = (*destination).try_into().map_err(|_| Error::<T>::BadVersion)?;
let action: IdentityProofActionOf<T> = match T::IdentityProvider::retrieve(&identifier) {
Ok(Some(identity)) => {
let identity_proof = T::IdentityProofGenerator::generate_commitment(&identifier, &identity)
.map_err(|_| Error::<T>::IdentityProofGeneration)?;
Ok(IdentityDetailsAction::Updated(identifier, identity_proof, ()))
}
Ok(None) => Ok(IdentityDetailsAction::Deleted(identifier)),
pub fn commit_identity(origin: OriginFor<T>, identifier: T::Identifier) -> DispatchResult {
// TODO: use dispatcher to get deposit
let _dispatcher =
T::CommitOriginCheck::ensure_origin(origin).map(|e: <T as Config>::CommitOrigin| e.submitter())?;

let identity_commitment: Option<T::IdentityCommitment> = match T::IdentityProvider::retrieve(&identifier) {
Ok(Some(identity)) => T::IdentityCommitmentGenerator::generate_commitment(&identifier, &identity)
.map(Some)
.map_err(|_| Error::<T>::IdentityCommitmentGeneration),
Ok(None) => Ok(None),
Err(_) => Err(Error::<T>::IdentityNotFound),
}?;
// TODO: Add correct version creation based on lookup (?)

let asset: MultiAsset = (*asset).try_into().map_err(|_| Error::<T>::BadVersion)?;

let (ticket, _) = T::IdentityProofDispatcher::pre_dispatch::<T::TxBuilder>(
action.clone(),
dispatcher,
asset,
weight,
destination,
)
.map_err(|_| Error::<T>::Predispatch)?;

// TODO: Use returned asset of `pre_dispatch` to charge the tx submitter for the
// fee.
T::IdentityProofDispatcher::dispatch(ticket).map_err(|_| Error::<T>::Dispatch)?;
if let Some(commitment) = identity_commitment {
// TODO: Take deposit (once 0.9.42 PR is merged into develop)
IdentityCommitments::<T>::insert(&identifier, commitment.clone());
Self::deposit_event(Event::<T>::IdentityCommitted { identifier, commitment });
} else {
// TODO: Release deposit (once 0.9.42 PR is merged into develop)
IdentityCommitments::<T>::remove(&identifier);
Self::deposit_event(Event::<T>::IdentityDeleted { identifier });
}

Self::deposit_event(Event::IdentityInfoDispatched(action, Box::new(destination)));
Ok(())
}
}
Expand Down
71 changes: 6 additions & 65 deletions pallets/pallet-dip-provider/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,24 @@
// If you feel like getting in touch with us, you can do so at [email protected]

use did::DidRawOrigin;
use dip_support::IdentityDetailsAction;
use xcm::{latest::prelude::*, DoubleEncoded};

pub use identity_generation::*;
pub mod identity_generation {
use sp_std::marker::PhantomData;

pub trait IdentityProofGenerator<Identifier, Identity> {
pub trait IdentityCommitmentGenerator<Identifier, Identity> {
type Error;
type Output;

fn generate_commitment(identifier: &Identifier, identity: &Identity) -> Result<Self::Output, Self::Error>;
}

// Implement the `IdentityProofGenerator` by returning the `Default` value for
// the `Output` type.
pub struct DefaultIdentityProofGenerator<Output>(PhantomData<Output>);
// Implement the `IdentityCommitmentGenerator` by returning the `Default` value
// for the `Output` type.
pub struct DefaultIdentityCommitmentGenerator<Output>(PhantomData<Output>);

impl<Identifier, Identity, Output> IdentityProofGenerator<Identifier, Identity>
for DefaultIdentityProofGenerator<Output>
impl<Identifier, Identity, Output> IdentityCommitmentGenerator<Identifier, Identity>
for DefaultIdentityCommitmentGenerator<Output>
where
Output: Default,
{
Expand All @@ -49,52 +47,6 @@ pub mod identity_generation {
}
}

pub use identity_dispatch::*;
pub mod identity_dispatch {
use super::*;

use frame_support::weights::Weight;

pub trait IdentityProofDispatcher<Identifier, IdentityRoot, AccountId, Details = ()> {
type PreDispatchOutput;
type Error;

fn pre_dispatch<B: TxBuilder<Identifier, IdentityRoot, Details>>(
action: IdentityDetailsAction<Identifier, IdentityRoot, Details>,
source: AccountId,
asset: MultiAsset,
weight: Weight,
destination: MultiLocation,
) -> Result<(Self::PreDispatchOutput, MultiAssets), Self::Error>;

fn dispatch(pre_output: Self::PreDispatchOutput) -> Result<(), Self::Error>;
}

// Returns `Ok` without doing anything.
pub struct NullIdentityProofDispatcher;

impl<Identifier, IdentityRoot, AccountId, Details>
IdentityProofDispatcher<Identifier, IdentityRoot, AccountId, Details> for NullIdentityProofDispatcher
{
type PreDispatchOutput = ();
type Error = ();

fn pre_dispatch<_B>(
_action: IdentityDetailsAction<Identifier, IdentityRoot, Details>,
_source: AccountId,
_asset: MultiAsset,
_weight: Weight,
_destination: MultiLocation,
) -> Result<((), MultiAssets), Self::Error> {
Ok(((), MultiAssets::default()))
}

fn dispatch(_pre_output: Self::PreDispatchOutput) -> Result<(), Self::Error> {
Ok(())
}
}
}

pub use identity_provision::*;
pub mod identity_provision {
use sp_std::marker::PhantomData;
Expand Down Expand Up @@ -134,17 +86,6 @@ pub mod identity_provision {
}
}

// Given a destination and an identity action, creates and encodes the proper
// `Transact` call.
pub trait TxBuilder<Identifier, Proof, Details = ()> {
type Error;

fn build(
dest: MultiLocation,
action: IdentityDetailsAction<Identifier, Proof, Details>,
) -> Result<DoubleEncoded<()>, Self::Error>;
}

pub trait SubmitterInfo {
type Submitter;

Expand Down