Skip to content

Commit b47aaf7

Browse files
authored
feat: add dip to spiritnet runtime (#616)
## fixes [#3250](KILTprotocol/ticket#3250)
1 parent 4377d49 commit b47aaf7

File tree

11 files changed

+376
-5
lines changed

11 files changed

+376
-5
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

runtimes/peregrine/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ std = [
185185
"kilt-runtime-api-staking/std",
186186
"kilt-support/std",
187187
"log/std",
188-
"kilt-support/std",
189188
"pallet-aura/std",
190189
"pallet-authorship/std",
191190
"pallet-balances/std",

runtimes/peregrine/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ use frame_support::{
3030
traits::{AsEnsureOriginWithArg, ConstU32, EitherOfDiverse, Everything, InstanceFilter, PrivilegeCmp},
3131
weights::{ConstantMultiplier, Weight},
3232
};
33-
use frame_system::{EnsureRoot, EnsureSigned};
33+
use frame_system::{pallet_prelude::BlockNumberFor, EnsureRoot, EnsureSigned};
3434
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
3535

3636
#[cfg(feature = "try-runtime")]
3737
use frame_try_runtime::UpgradeCheckSelect;
3838

39-
use frame_system::pallet_prelude::BlockNumberFor;
4039
use sp_api::impl_runtime_apis;
4140
use sp_core::{ConstBool, OpaqueMetadata};
4241
use sp_runtime::{
@@ -830,6 +829,8 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
830829
| did::Call::submit_did_call { .. }
831830
| did::Call::update_deposit { .. }
832831
| did::Call::change_deposit_owner { .. }
832+
| did::Call::create_from_account { .. }
833+
| did::Call::dispatch_as { .. }
833834
)
834835
| RuntimeCall::DidLookup(
835836
// Excludes `reclaim_deposit`

runtimes/spiritnet/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ attestation.workspace = true
3636
ctype.workspace = true
3737
delegation.workspace = true
3838
did.workspace = true
39+
kilt-runtime-api-dip-provider.workspace = true
3940
kilt-support.workspace = true
41+
pallet-deposit-storage.workspace = true
4042
pallet-did-lookup.workspace = true
43+
pallet-dip-provider.workspace = true
4144
pallet-inflation.workspace = true
4245
pallet-migration.workspace = true
4346
pallet-web3-names.workspace = true
@@ -126,7 +129,9 @@ runtime-benchmarks = [
126129
"pallet-balances/runtime-benchmarks",
127130
"pallet-collective/runtime-benchmarks",
128131
"pallet-democracy/runtime-benchmarks",
132+
"pallet-deposit-storage/runtime-benchmarks",
129133
"pallet-did-lookup/runtime-benchmarks",
134+
"pallet-dip-provider/runtime-benchmarks",
130135
"pallet-indices/runtime-benchmarks",
131136
"pallet-inflation/runtime-benchmarks",
132137
"pallet-membership/runtime-benchmarks",
@@ -171,6 +176,7 @@ std = [
171176
"frame-system/std",
172177
"frame-try-runtime?/std",
173178
"kilt-runtime-api-did/std",
179+
"kilt-runtime-api-dip-provider/std",
174180
"kilt-runtime-api-public-credentials/std",
175181
"kilt-runtime-api-staking/std",
176182
"kilt-support/std",
@@ -180,7 +186,9 @@ std = [
180186
"pallet-balances/std",
181187
"pallet-collective/std",
182188
"pallet-democracy/std",
189+
"pallet-deposit-storage/std",
183190
"pallet-did-lookup/std",
191+
"pallet-dip-provider/std",
184192
"pallet-indices/std",
185193
"pallet-inflation/std",
186194
"pallet-membership/std",
@@ -241,7 +249,9 @@ try-runtime = [
241249
"pallet-balances/try-runtime",
242250
"pallet-collective/try-runtime",
243251
"pallet-democracy/try-runtime",
252+
"pallet-deposit-storage/try-runtime",
244253
"pallet-did-lookup/try-runtime",
254+
"pallet-dip-provider/try-runtime",
245255
"pallet-indices/try-runtime",
246256
"pallet-inflation/try-runtime",
247257
"pallet-membership/try-runtime",

runtimes/spiritnet/src/dip/mod.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// KILT Blockchain – https://botlabs.org
2+
// Copyright (C) 2019-2024 BOTLabs GmbH
3+
4+
// The KILT Blockchain is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// The KILT Blockchain is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
// If you feel like getting in touch with us, you can do so at [email protected]
18+
19+
use did::{DidRawOrigin, EnsureDidOrigin};
20+
use frame_system::EnsureSigned;
21+
use runtime_common::{
22+
constants::{deposit_storage::MAX_DEPOSIT_PALLET_KEY_LENGTH, dip_provider::MAX_LINKED_ACCOUNTS},
23+
dip::{
24+
deposit::{DepositCollectorHooks, DepositHooks, DepositNamespace},
25+
did::LinkedDidInfoProvider,
26+
merkle::DidMerkleRootGenerator,
27+
},
28+
AccountId, DidIdentifier,
29+
};
30+
use sp_core::ConstU32;
31+
32+
use crate::{weights, Balances, Runtime, RuntimeEvent, RuntimeHoldReason};
33+
34+
pub(crate) mod runtime_api;
35+
36+
impl pallet_dip_provider::Config for Runtime {
37+
// Only DID origins can submit the commitment identity tx, which will go through
38+
// only if the DID in the origin matches the identifier specified in the tx.
39+
type CommitOriginCheck = EnsureDidOrigin<DidIdentifier, AccountId>;
40+
type CommitOrigin = DidRawOrigin<DidIdentifier, AccountId>;
41+
type Identifier = DidIdentifier;
42+
// The identity commitment is defined as the Merkle root of the linked identity
43+
// info, as specified by the [`LinkedDidInfoProvider`].
44+
type IdentityCommitmentGenerator = DidMerkleRootGenerator<Runtime>;
45+
// Identity info is defined as the collection of DID keys, linked accounts, and
46+
// the optional web3name of a given DID subject.
47+
type IdentityProvider = LinkedDidInfoProvider<MAX_LINKED_ACCOUNTS>;
48+
type ProviderHooks = DepositCollectorHooks;
49+
type RuntimeEvent = RuntimeEvent;
50+
type WeightInfo = weights::pallet_dip_provider::WeightInfo<Runtime>;
51+
}
52+
53+
impl pallet_deposit_storage::Config for Runtime {
54+
#[cfg(feature = "runtime-benchmarks")]
55+
type BenchmarkHooks = runtime_common::dip::deposit::PalletDepositStorageBenchmarkHooks;
56+
// Any signed origin can submit the tx, which will go through only if the
57+
// deposit payer matches the signed origin.
58+
type CheckOrigin = EnsureSigned<AccountId>;
59+
// The balances pallet is used to reserve/unreserve tokens.
60+
type Currency = Balances;
61+
type DepositHooks = DepositHooks;
62+
type MaxKeyLength = ConstU32<MAX_DEPOSIT_PALLET_KEY_LENGTH>;
63+
type Namespace = DepositNamespace;
64+
type RuntimeEvent = RuntimeEvent;
65+
type RuntimeHoldReason = RuntimeHoldReason;
66+
type WeightInfo = weights::pallet_deposit_storage::WeightInfo<Runtime>;
67+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// KILT Blockchain – https://botlabs.org
2+
// Copyright (C) 2019-2024 BOTLabs GmbH
3+
4+
// The KILT Blockchain is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// The KILT Blockchain is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
// If you feel like getting in touch with us, you can do so at [email protected]
18+
19+
use did::KeyIdOf;
20+
use pallet_did_lookup::linkable_account::LinkableAccountId;
21+
use pallet_dip_provider::IdentityCommitmentVersion;
22+
use parity_scale_codec::{Decode, Encode};
23+
use runtime_common::{
24+
dip::{did::LinkedDidInfoProviderError, merkle::DidMerkleProofError},
25+
DidIdentifier,
26+
};
27+
use scale_info::TypeInfo;
28+
use sp_std::vec::Vec;
29+
30+
use crate::Runtime;
31+
32+
/// Parameters for a DIP proof request.
33+
#[derive(Encode, Decode, TypeInfo)]
34+
pub struct DipProofRequest {
35+
/// The subject identifier for which to generate the DIP proof.
36+
pub(crate) identifier: DidIdentifier,
37+
/// The DIP version.
38+
pub(crate) version: IdentityCommitmentVersion,
39+
/// The DID key IDs of the subject's DID Document to reveal in the DIP
40+
/// proof.
41+
pub(crate) keys: Vec<KeyIdOf<Runtime>>,
42+
/// The list of accounts linked to the subject's DID to reveal in the
43+
/// DIP proof.
44+
pub(crate) accounts: Vec<LinkableAccountId>,
45+
/// A flag indicating whether the web3name claimed by the DID subject
46+
/// should revealed in the DIP proof.
47+
pub(crate) should_include_web3_name: bool,
48+
}
49+
50+
#[derive(Encode, Decode, TypeInfo)]
51+
pub enum DipProofError {
52+
IdentityProvider(LinkedDidInfoProviderError),
53+
MerkleProof(DidMerkleProofError),
54+
}

runtimes/spiritnet/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use runtime_common::{
5858
assets::{AssetDid, PublicCredentialsFilter},
5959
authorization::{AuthorizationId, PalletAuthorize},
6060
constants::{self, UnvestedFundsAllowedWithdrawReasons, EXISTENTIAL_DEPOSIT, KILT},
61+
dip::merkle::{CompleteMerkleProof, DidMerkleProofOf, DidMerkleRootGenerator},
6162
errors::PublicCredentialsApiError,
6263
fees::{ToAuthor, WeightToFee},
6364
pallet_id, AccountId, AuthorityId, Balance, BlockHashCount, BlockLength, BlockNumber, BlockWeights, DidIdentifier,
@@ -75,6 +76,7 @@ pub use sp_runtime::BuildStorage;
7576
#[cfg(test)]
7677
mod tests;
7778

79+
mod dip;
7880
mod weights;
7981
pub mod xcm_config;
8082

@@ -743,8 +745,10 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
743745
| RuntimeCall::Ctype(..)
744746
| RuntimeCall::Delegation(..)
745747
| RuntimeCall::Democracy(..)
748+
| RuntimeCall::DepositStorage(..)
746749
| RuntimeCall::Did(..)
747750
| RuntimeCall::DidLookup(..)
751+
| RuntimeCall::DipProvider(..)
748752
| RuntimeCall::Indices(
749753
// Excludes `force_transfer`, and `transfer`
750754
pallet_indices::Call::claim { .. }
@@ -796,6 +800,7 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
796800
| delegation::Call::change_deposit_owner { .. }
797801
)
798802
| RuntimeCall::Democracy(..)
803+
// Excludes `DepositStorage`
799804
| RuntimeCall::Did(
800805
// Excludes `reclaim_deposit`
801806
did::Call::add_key_agreement_key { .. }
@@ -812,6 +817,8 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
812817
| did::Call::submit_did_call { .. }
813818
| did::Call::update_deposit { .. }
814819
| did::Call::change_deposit_owner { .. }
820+
| did::Call::create_from_account { .. }
821+
| did::Call::dispatch_as { .. }
815822
)
816823
| RuntimeCall::DidLookup(
817824
// Excludes `reclaim_deposit`
@@ -822,6 +829,7 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
822829
| pallet_did_lookup::Call::update_deposit { .. }
823830
| pallet_did_lookup::Call::change_deposit_owner { .. }
824831
)
832+
| RuntimeCall::DipProvider(..)
825833
| RuntimeCall::Indices(..)
826834
| RuntimeCall::Multisig(..)
827835
| RuntimeCall::ParachainStaking(..)
@@ -972,6 +980,8 @@ construct_runtime! {
972980
Web3Names: pallet_web3_names = 68,
973981
PublicCredentials: public_credentials = 69,
974982
Migration: pallet_migration = 70,
983+
DipProvider: pallet_dip_provider = 71,
984+
DepositStorage: pallet_deposit_storage = 72,
975985

976986
// Parachains pallets. Start indices at 80 to leave room.
977987

@@ -1015,6 +1025,7 @@ impl did::DeriveDidCallAuthorizationVerificationKeyRelationship for RuntimeCall
10151025
RuntimeCall::Attestation { .. } => Ok(did::DidVerificationKeyRelationship::AssertionMethod),
10161026
RuntimeCall::Ctype { .. } => Ok(did::DidVerificationKeyRelationship::AssertionMethod),
10171027
RuntimeCall::Delegation { .. } => Ok(did::DidVerificationKeyRelationship::CapabilityDelegation),
1028+
RuntimeCall::DipProvider { .. } => Ok(did::DidVerificationKeyRelationship::Authentication),
10181029
// DID creation is not allowed through the DID proxy.
10191030
RuntimeCall::Did(did::Call::create { .. }) => Err(did::RelationshipDeriveError::NotCallableByDid),
10201031
RuntimeCall::Did { .. } => Ok(did::DidVerificationKeyRelationship::Authentication),
@@ -1105,6 +1116,8 @@ mod benches {
11051116
[public_credentials, PublicCredentials]
11061117
[pallet_xcm, PolkadotXcm]
11071118
[pallet_migration, Migration]
1119+
[pallet_dip_provider, DipProvider]
1120+
[pallet_deposit_storage, DepositStorage]
11081121
[frame_benchmarking::baseline, Baseline::<Runtime>]
11091122
);
11101123
}
@@ -1371,6 +1384,16 @@ impl_runtime_apis! {
13711384
}
13721385
}
13731386

1387+
impl kilt_runtime_api_dip_provider::DipProvider<Block, dip::runtime_api::DipProofRequest, CompleteMerkleProof<Hash, DidMerkleProofOf<Runtime>>, dip::runtime_api::DipProofError> for Runtime {
1388+
fn generate_proof(request: dip::runtime_api::DipProofRequest) -> Result<CompleteMerkleProof<Hash, DidMerkleProofOf<Runtime>>, dip::runtime_api::DipProofError> {
1389+
use pallet_dip_provider::traits::IdentityProvider;
1390+
1391+
let identity_details = pallet_dip_provider::IdentityProviderOf::<Runtime>::retrieve(&request.identifier).map_err(dip::runtime_api::DipProofError::IdentityProvider)?;
1392+
1393+
DidMerkleRootGenerator::<Runtime>::generate_proof(&identity_details, request.version, request.keys.iter(), request.should_include_web3_name, request.accounts.iter()).map_err(dip::runtime_api::DipProofError::MerkleProof)
1394+
}
1395+
}
1396+
13741397
#[cfg(feature = "runtime-benchmarks")]
13751398
impl frame_benchmarking::Benchmark<Block> for Runtime {
13761399
fn benchmark_metadata(extra: bool) -> (

runtimes/spiritnet/src/weights/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ pub mod frame_system;
2424
pub mod pallet_balances;
2525
pub mod pallet_collective;
2626
pub mod pallet_democracy;
27+
pub mod pallet_deposit_storage;
2728
pub mod pallet_did_lookup;
29+
pub mod pallet_dip_provider;
2830
pub mod pallet_indices;
2931
pub mod pallet_inflation;
3032
pub mod pallet_membership;

0 commit comments

Comments
 (0)