Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add deposits for DIP provider pallet #574

Merged
merged 22 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
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
19 changes: 18 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ ctype = {path = "pallets/ctype", default-features = false}
delegation = {path = "pallets/delegation", default-features = false}
did = {path = "pallets/did", default-features = false}
pallet-configuration = {path = "pallets/pallet-configuration", default-features = false}
pallet-deposit-storage = {path = "pallets/pallet-deposit-storage", default-features = false}
pallet-dip-consumer = {path = "pallets/pallet-dip-consumer", default-features = false}
pallet-dip-provider = {path = "pallets/pallet-dip-provider", default-features = false}
pallet-did-lookup = {path = "pallets/pallet-did-lookup", default-features = false}
Expand Down
5 changes: 3 additions & 2 deletions crates/kilt-dip-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ scale-info = {workspace = true, features = ["derive"]}
# Substrate dependencies
frame-system.workspace = true
frame-support.workspace = true
sp-runtime.workspace = true
sp-core.workspace = true
sp-io.workspace = true
sp-runtime.workspace = true
sp-state-machine.workspace = true
sp-std.workspace = true
sp-trie.workspace = true
Expand All @@ -51,6 +51,7 @@ sp-io = { workspace = true, features = ["std"] }
default = ["std"]
std = [
"hash-db/std",
"log/std",
"did/std",
"pallet-dip-consumer/std",
"pallet-dip-provider/std",
Expand All @@ -59,9 +60,9 @@ std = [
"scale-info/std",
"frame-system/std",
"frame-support/std",
"sp-runtime/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-state-machine/std",
"sp-std/std",
"sp-trie/std",
Expand Down
6 changes: 6 additions & 0 deletions dip-template/runtimes/dip-provider/Cargo.toml
Ad96el marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ version.workspace = true
substrate-wasm-builder.workspace = true

[dependencies]
log.workspace = true
parity-scale-codec = {workspace = true, features = ["derive"]}
scale-info = {workspace = true, features = ["derive"]}

# DIP
did.workspace = true
kilt-support.workspace = true
kilt-dip-support.workspace = true
kilt-runtime-api-dip-provider.workspace = true
pallet-deposit-storage.workspace = true
pallet-did-lookup.workspace = true
pallet-dip-provider.workspace = true
pallet-web3-names.workspace = true
Expand Down Expand Up @@ -66,11 +69,14 @@ default = [
"std",
]
std = [
"log/std",
"parity-scale-codec/std",
"scale-info/std",
"did/std",
"kilt-support/std",
"kilt-dip-support/std",
"kilt-runtime-api-dip-provider/std",
"pallet-deposit-storage/std",
"pallet-did-lookup/std",
"pallet-dip-provider/std",
"pallet-web3-names/std",
Expand Down
118 changes: 105 additions & 13 deletions dip-template/runtimes/dip-provider/src/dip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// If you feel like getting in touch with us, you can do so at info@botlabs.org

use did::{DidRawOrigin, EnsureDidOrigin, KeyIdOf};
use frame_system::EnsureSigned;
use pallet_did_lookup::linkable_account::LinkableAccountId;
use pallet_dip_provider::{traits::IdentityProvider, IdentityCommitmentVersion};
use parity_scale_codec::{Decode, Encode};
Expand All @@ -25,24 +26,114 @@ use runtime_common::dip::{
merkle::{DidMerkleProofError, DidMerkleRootGenerator},
};
use scale_info::TypeInfo;
use sp_core::ConstU32;
use sp_std::vec::Vec;

use crate::{AccountId, DidIdentifier, Hash, Runtime, RuntimeEvent};
use crate::{
deposit::{DepositHooks, DepositNamespaces},
AccountId, Balances, DidIdentifier, Hash, Runtime, RuntimeEvent, RuntimeHoldReason,
};

pub mod runtime_api {
ntn-x2 marked this conversation as resolved.
Show resolved Hide resolved
use super::*;

#[derive(Encode, Decode, TypeInfo)]
pub struct DipProofRequest {
pub(crate) identifier: DidIdentifier,
pub(crate) version: IdentityCommitmentVersion,
pub(crate) keys: Vec<KeyIdOf<Runtime>>,
pub(crate) accounts: Vec<LinkableAccountId>,
pub(crate) should_include_web3_name: bool,
}

#[derive(Encode, Decode, TypeInfo)]
pub enum DipProofError {
IdentityNotFound,
IdentityProviderError(<LinkedDidInfoProviderOf<Runtime> as IdentityProvider<DidIdentifier>>::Error),
MerkleProofError(DidMerkleProofError),
}
}

pub mod deposit {
use super::*;

use crate::{Balance, UNIT};

use frame_support::traits::Get;
use pallet_deposit_storage::{
traits::DepositStorageHooks, DepositEntryOf, DepositKeyOf, FixedDepositCollectorViaDepositsPallet,
};
use parity_scale_codec::MaxEncodedLen;
use sp_core::{ConstU128, RuntimeDebug};

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Eq, RuntimeDebug)]
pub enum DepositNamespaces {
DipProvider,
}

#[derive(Encode, Decode, TypeInfo)]
pub struct RuntimeApiDipProofRequest {
pub(crate) identifier: DidIdentifier,
pub(crate) version: IdentityCommitmentVersion,
pub(crate) keys: Vec<KeyIdOf<Runtime>>,
pub(crate) accounts: Vec<LinkableAccountId>,
pub(crate) should_include_web3_name: bool,
pub struct DipProviderDepositNamespace;

impl Get<DepositNamespaces> for DipProviderDepositNamespace {
fn get() -> DepositNamespaces {
DepositNamespaces::DipProvider
}
}

pub const DEPOSIT_AMOUNT: Balance = 2 * UNIT;

pub type DepositCollectorHooks =
FixedDepositCollectorViaDepositsPallet<DipProviderDepositNamespace, ConstU128<DEPOSIT_AMOUNT>>;

pub enum CommitmentDepositRemovalHookError {
DecodeKey,
Internal,
}

impl From<CommitmentDepositRemovalHookError> for u16 {
fn from(value: CommitmentDepositRemovalHookError) -> Self {
match value {
CommitmentDepositRemovalHookError::DecodeKey => 0,
CommitmentDepositRemovalHookError::Internal => u16::MAX,
}
}
}

pub struct DepositHooks;

impl DepositStorageHooks<Runtime> for DepositHooks {
type Error = CommitmentDepositRemovalHookError;

fn on_deposit_reclaimed(
_namespace: &<Runtime as pallet_deposit_storage::Config>::Namespace,
key: &DepositKeyOf<Runtime>,
_deposit: DepositEntryOf<Runtime>,
) -> Result<(), Self::Error> {
let (identifier, commitment_version) = <(DidIdentifier, IdentityCommitmentVersion)>::decode(&mut &key[..])
.map_err(|_| CommitmentDepositRemovalHookError::DecodeKey)?;
pallet_dip_provider::Pallet::<Runtime>::delete_identity_commitment_storage_entry(
&identifier,
commitment_version,
)
.map_err(|_| {
log::error!(
"Should not fail to remove commitment for identifier {:#?} and version {commitment_version}",
identifier
);
CommitmentDepositRemovalHookError::Internal
})?;
Ok(())
}
}
}

#[derive(Encode, Decode, TypeInfo)]
pub enum RuntimeApiDipProofError {
IdentityNotFound,
IdentityProviderError(<LinkedDidInfoProviderOf<Runtime> as IdentityProvider<DidIdentifier>>::Error),
MerkleProofError(DidMerkleProofError),
impl pallet_deposit_storage::Config for Runtime {
type CheckOrigin = EnsureSigned<AccountId>;
type Currency = Balances;
type DepositHooks = DepositHooks;
type MaxKeyLength = ConstU32<256>;
type Namespace = DepositNamespaces;
type RuntimeEvent = RuntimeEvent;
type RuntimeHoldReason = RuntimeHoldReason;
}

impl pallet_dip_provider::Config for Runtime {
Expand All @@ -54,5 +145,6 @@ impl pallet_dip_provider::Config for Runtime {
type IdentityCommitmentGeneratorError = DidMerkleProofError;
type IdentityProvider = LinkedDidInfoProviderOf<Runtime>;
type IdentityProviderError = <LinkedDidInfoProviderOf<Runtime> as IdentityProvider<DidIdentifier>>::Error;
type ProviderHooks = deposit::DepositCollectorHooks;
type RuntimeEvent = RuntimeEvent;
}
13 changes: 7 additions & 6 deletions dip-template/runtimes/dip-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ construct_runtime!(
Web3Names: pallet_web3_names = 32,

// DIP
DipProvider: pallet_dip_provider = 40,
DepositStorage: pallet_deposit_storage = 40,
DipProvider: pallet_dip_provider = 41,
}
);

Expand Down Expand Up @@ -568,14 +569,14 @@ impl_runtime_apis! {
}
}

impl kilt_runtime_api_dip_provider::DipProvider<Block, RuntimeApiDipProofRequest, CompleteMerkleProof<Hash, DidMerkleProofOf<Runtime>>, RuntimeApiDipProofError> for Runtime {
fn generate_proof(request: RuntimeApiDipProofRequest) -> Result<CompleteMerkleProof<Hash, DidMerkleProofOf<Runtime>>, RuntimeApiDipProofError> {
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 linked_did_info = match <Runtime as pallet_dip_provider::Config>::IdentityProvider::retrieve(&request.identifier) {
Ok(Some(linked_did_info)) => Ok(linked_did_info),
Ok(None) => Err(RuntimeApiDipProofError::IdentityNotFound),
Err(e) => Err(RuntimeApiDipProofError::IdentityProviderError(e))
Ok(None) => Err(runtime_api::DipProofError::IdentityNotFound),
Err(e) => Err(runtime_api::DipProofError::IdentityProviderError(e))
}?;
DidMerkleRootGenerator::<Runtime>::generate_proof(&linked_did_info, request.version, request.keys.iter(), request.should_include_web3_name, request.accounts.iter()).map_err(RuntimeApiDipProofError::MerkleProofError)
DidMerkleRootGenerator::<Runtime>::generate_proof(&linked_did_info, request.version, request.keys.iter(), request.should_include_web3_name, request.accounts.iter()).map_err(runtime_api::DipProofError::MerkleProofError)
Ad96el marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
1 change: 0 additions & 1 deletion out.txt

This file was deleted.

41 changes: 41 additions & 0 deletions pallets/pallet-deposit-storage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[package]
authors.workspace = true
documentation.workspace = true
edition.workspace = true
homepage.workspace = true
license-file.workspace = true
readme.workspace = true
repository.workspace = true
version.workspace = true
name = "pallet-deposit-storage"
description = "Stores all deposits under a single pallet, with suport for namespacing different deposit contexts."

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
# Substrate dependencies
frame-support.workspace = true
frame-system.workspace = true
kilt-support.workspace = true
pallet-dip-provider.workspace = true
parity-scale-codec = {workspace = true, features = ["derive"]}
scale-info = {workspace = true, features = ["derive"]}
sp-runtime.workspace = true
sp-std.workspace = true

log.workspace = true

[features]
default = ["std"]
std = [
"frame-support/std",
"frame-system/std",
"kilt-support/std",
"pallet-dip-provider/std",
"parity-scale-codec/std",
"scale-info/std",
"sp-runtime/std",
"sp-std/std",
"log/std",
]
Loading