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 new DID runtime APIs to aid with DID deletion #852

Merged
merged 4 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 13 additions & 1 deletion dip-template/runtimes/dip-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,9 @@ impl_runtime_apis! {
LinkableAccountId,
Balance,
Hash,
BlockNumber
BlockNumber,
(),
()
> for Runtime {
fn query_by_web3_name(name: Vec<u8>) -> Option<kilt_runtime_api_did::RawDidLinkedInfo<
DidIdentifier,
Expand Down Expand Up @@ -755,6 +757,16 @@ impl_runtime_apis! {
>> {
dids.into_iter().map(Self::query).collect()
}

// We don't return anything here, since the runtime does not require the resources to be cleaned up.
fn linked_resources(_did: DidIdentifier) -> Vec<()> {
[].into()
}

// We don't return anything here, since the runtime does not require the resources to be cleaned up.
fn linked_resources_deletion_calls(_did: DidIdentifier) -> Vec<()> {
[].into()
}
}

impl kilt_runtime_api_dip_provider::DipProvider<Block, runtime_api::DipProofRequest, CompleteMerkleProof<Hash, DidMerkleProofOf<Runtime>>, runtime_api::DipProofError> for Runtime {
Expand Down
11 changes: 9 additions & 2 deletions runtime-api/did/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ pub type RawDidLinkedInfo<DidIdentifier, AccountId, LinkableAccountId, Balance,
>;

sp_api::decl_runtime_apis! {
#[api_version(3)]
pub trait Did<DidIdentifier, AccountId, LinkableAccountId, Balance, Key: Ord, BlockNumber: MaxEncodedLen> where
#[api_version(4)]
pub trait Did<DidIdentifier, AccountId, LinkableAccountId, Balance, Key: Ord, BlockNumber: MaxEncodedLen, LinkedResource, RuntimeCall> where
DidIdentifier: Codec,
AccountId: Codec,
LinkableAccountId: Codec,
BlockNumber: Codec,
Key: Codec,
Balance: Codec,
LinkedResource: Codec,
RuntimeCall: Codec,
{
/// Given a web3name this returns:
/// * the DID
Expand Down Expand Up @@ -111,5 +113,10 @@ sp_api::decl_runtime_apis! {
/// Allows for batching multiple `query` requests into one. For each requested name, the corresponding vector entry contains either `Some` or `None` depending on the result of each query.
#[allow(clippy::type_complexity)]
fn batch_query(dids: Vec<DidIdentifier>) -> Vec<Option<RawDidLinkedInfo<DidIdentifier, AccountId, LinkableAccountId, Balance, Key, BlockNumber>>>;

/// Returns the list of linked resources for a given DID that must be deleted before the DID itself can be deleted.
fn linked_resources(did: DidIdentifier) -> Vec<LinkedResource>;
/// Returns the list of calls that must be executed to delete the linked resources of a given DID, before deleting the DID itself.
fn linked_resources_deletion_calls(did: DidIdentifier) -> Vec<RuntimeCall>;
}
}
19 changes: 19 additions & 0 deletions runtimes/common/src/did/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// KILT Blockchain – https://botlabs.org
// Copyright (C) 2019-2024 BOTLabs GmbH

// The KILT Blockchain is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The KILT Blockchain is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// If you feel like getting in touch with us, you can do so at [email protected]

pub mod runtime_apis;
34 changes: 34 additions & 0 deletions runtimes/common/src/did/runtime_apis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// KILT Blockchain – https://botlabs.org
// Copyright (C) 2019-2024 BOTLabs GmbH

// The KILT Blockchain is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The KILT Blockchain is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// If you feel like getting in touch with us, you can do so at [email protected]

use pallet_did_lookup::linkable_account::LinkableAccountId;
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;

/// The kind of resources that can be linked to a DID, preventing its deletion.
#[derive(Debug, Encode, Decode, TypeInfo, PartialEq, Eq, PartialOrd, Ord)]
pub enum LinkedDidResource<Web3Name, DotName> {
/// A Web3name.
Web3Name(Web3Name),
/// A Dotname.
DotName(DotName),
/// An account linked to the DID and resolvable by or to a Web3name.
Web3NameAccount(LinkableAccountId),
/// An account linked to the DID and resolvable by or to a Dotname.
DotNameAccount(LinkableAccountId),
Comment on lines +31 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's because we have the two instances of the linking pallet?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly.

}
1 change: 1 addition & 0 deletions runtimes/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub mod authorization;
pub mod bonded_coins;
pub mod constants;
pub mod deposits;
pub mod did;
pub mod dip;
pub mod dot_names;
pub use dot_names::DotName;
Expand Down
14 changes: 13 additions & 1 deletion runtimes/kestrel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,9 @@ impl_runtime_apis! {
LinkableAccountId,
Balance,
Hash,
BlockNumber
BlockNumber,
(),
()
> for Runtime {
fn query_by_web3_name(name: Vec<u8>) -> Option<kilt_runtime_api_did::RawDidLinkedInfo<
DidIdentifier,
Expand Down Expand Up @@ -1133,6 +1135,16 @@ impl_runtime_apis! {
>> {
dids.into_iter().map(Self::query).collect()
}

// We don't return anything here, since the runtime does not require the resources to be cleaned up.
fn linked_resources(_did: DidIdentifier) -> Vec<()> {
[].into()
}

// We don't return anything here, since the runtime does not require the resources to be cleaned up.
fn linked_resources_deletion_calls(_did: DidIdentifier) -> Vec<()> {
[].into()
}
}

impl kilt_runtime_api_public_credentials::PublicCredentials<Block, Vec<u8>, Hash, public_credentials::CredentialEntry<Hash, DidIdentifier, BlockNumber, AccountId, Balance, AuthorizationId<<Runtime as delegation::Config>::DelegationNodeId>>, PublicCredentialsFilter<Hash, AccountId>, PublicCredentialsApiError> for Runtime {
Expand Down
54 changes: 50 additions & 4 deletions runtimes/peregrine/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use runtime_common::{
AssetId as BondedAssetId, FixedPoint, FixedPointUnderlyingType,
},
constants::SLOT_DURATION,
did::runtime_apis::LinkedDidResource,
dip::merkle::{CompleteMerkleProof, DidMerkleProofOf, DidMerkleRootGenerator},
errors::PublicCredentialsApiError,
AccountId, AuthorityId, Balance, BlockNumber, DidIdentifier, Hash, Nonce,
Expand All @@ -55,17 +56,20 @@ use sp_runtime::{
traits::{Block as BlockT, TryConvert},
ApplyExtrinsicResult, KeyTypeId, SaturatedConversion,
};
use sp_std::{prelude::*, str::FromStr, vec::Vec};
use sp_std::{iter, prelude::*, str::FromStr, vec::Vec};
use sp_version::{ApisVec, RuntimeVersion};
use unique_linking_runtime_api::{AddressResult, NameResult};

use crate::{
kilt::{DipProofError, DipProofRequest, DotName, NativeAndForeignAssets, UniqueLinkingDeployment},
kilt::{
did::DotNamesDeployment, DipProofError, DipProofRequest, DotName, NativeAndForeignAssets,
UniqueLinkingDeployment,
},
parachain::ConsensusHook,
xcm::UniversalLocation,
AssetSwitchPool1, Aura, Block, BondedCurrencies, BondedFungibles, DotNames, Executive, InherentDataExt,
ParachainStaking, ParachainSystem, Runtime, RuntimeCall, RuntimeGenesisConfig, SessionKeys, TransactionPayment,
UniqueLinking, VERSION,
UniqueLinking, Web3Name, VERSION,
};

// This is necessary since by default `RUNTIME_API_VERSIONS` generated by
Expand Down Expand Up @@ -258,7 +262,9 @@ impl_runtime_apis! {
LinkableAccountId,
Balance,
Hash,
BlockNumber
BlockNumber,
LinkedDidResource<Web3Name, DotName>,
RuntimeCall
> for Runtime {
fn query_by_web3_name(name: Vec<u8>) -> Option<RawDidLinkedInfo<
DidIdentifier,
Expand Down Expand Up @@ -380,6 +386,15 @@ impl_runtime_apis! {
>> {
dids.into_iter().map(Self::query).collect()
}

fn linked_resources(did: DidIdentifier) -> Vec<LinkedDidResource<Web3Name, DotName>> {
linked_resources_for_did(&did).collect()
}

fn linked_resources_deletion_calls(did: DidIdentifier) -> Vec<RuntimeCall> {
let linked_resources = linked_resources_for_did(&did);
linked_resources.map(map_linked_resource_to_call).collect()
}
}

impl kilt_runtime_api_public_credentials::PublicCredentials<Block, Vec<u8>, Hash, CredentialEntry<Hash, DidIdentifier, BlockNumber, AccountId, Balance, AuthorizationId<<Runtime as delegation::Config>::DelegationNodeId>>, PublicCredentialsFilter<Hash, AccountId>, PublicCredentialsApiError> for Runtime {
Expand Down Expand Up @@ -679,3 +694,34 @@ impl_runtime_apis! {
}
}
}

/// Returns an iterator over all linked resources for a given DID.
fn linked_resources_for_did(did: &DidIdentifier) -> impl Iterator<Item = LinkedDidResource<Web3Name, DotName>> {
let web3_name = pallet_web3_names::Names::<Runtime>::get(did);
let dot_name = pallet_web3_names::Names::<Runtime, DotNamesDeployment>::get(did);
let web3_name_accounts = pallet_did_lookup::ConnectedAccounts::<Runtime>::iter_key_prefix(did);
let dot_name_accounts =
pallet_did_lookup::ConnectedAccounts::<Runtime, UniqueLinkingDeployment>::iter_key_prefix(did);

iter::once(web3_name.map(LinkedDidResource::Web3Name))
.chain(iter::once(dot_name.map(LinkedDidResource::DotName)))
.chain(web3_name_accounts.map(|acc| Some(LinkedDidResource::Web3NameAccount(acc))))
.chain(dot_name_accounts.map(|acc| Some(LinkedDidResource::DotNameAccount(acc))))
.flatten()
}

/// Creates a `RuntimeCall` that, if submitted by the owner DID,
/// would remove the provided linked resource.
fn map_linked_resource_to_call(resource: LinkedDidResource<Web3Name, DotName>) -> RuntimeCall {
match resource {
LinkedDidResource::Web3Name(_) => RuntimeCall::Web3Names(pallet_web3_names::Call::release_by_owner {}),

LinkedDidResource::DotName(_) => RuntimeCall::DotNames(pallet_web3_names::Call::release_by_owner {}),
LinkedDidResource::Web3NameAccount(account) => {
RuntimeCall::DidLookup(pallet_did_lookup::Call::remove_account_association { account })
}
LinkedDidResource::DotNameAccount(account) => {
RuntimeCall::UniqueLinking(pallet_did_lookup::Call::remove_account_association { account })
}
}
}
51 changes: 47 additions & 4 deletions runtimes/spiritnet/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use sp_runtime::{
traits::{Block as BlockT, TryConvert},
ApplyExtrinsicResult, KeyTypeId,
};
use sp_std::vec::Vec;
use sp_std::{iter, vec::Vec};
use sp_version::{ApisVec, RuntimeVersion};

use kilt_runtime_api_did::RawDidLinkedInfo;
Expand All @@ -36,18 +36,19 @@ use runtime_common::{
assets::{AssetDid, PublicCredentialsFilter},
authorization::AuthorizationId,
constants::SLOT_DURATION,
did::runtime_apis::LinkedDidResource,
dip::merkle::{CompleteMerkleProof, DidMerkleProofOf, DidMerkleRootGenerator},
errors::PublicCredentialsApiError,
AccountId, AuthorityId, Balance, BlockNumber, DidIdentifier, Hash, Nonce,
};
use unique_linking_runtime_api::{AddressResult, NameResult};

use crate::{
kilt::{DipProofError, DipProofRequest, DotName, UniqueLinkingDeployment},
kilt::{did::DotNamesDeployment, DipProofError, DipProofRequest, DotName, UniqueLinkingDeployment},
parachain::ConsensusHook,
xcm::UniversalLocation,
AssetSwitchPool1, Aura, Block, DotNames, Executive, InherentDataExt, ParachainStaking, ParachainSystem, Runtime,
RuntimeCall, RuntimeGenesisConfig, SessionKeys, TransactionPayment, UniqueLinking, VERSION,
RuntimeCall, RuntimeGenesisConfig, SessionKeys, TransactionPayment, UniqueLinking, Web3Name, VERSION,
};

// This is necessary since by default `RUNTIME_API_VERSIONS` generated by
Expand Down Expand Up @@ -240,7 +241,9 @@ impl_runtime_apis! {
LinkableAccountId,
Balance,
Hash,
BlockNumber
BlockNumber,
LinkedDidResource<Web3Name, DotName>,
RuntimeCall
> for Runtime {
fn query_by_web3_name(name: Vec<u8>) -> Option<RawDidLinkedInfo<
DidIdentifier,
Expand Down Expand Up @@ -362,6 +365,15 @@ impl_runtime_apis! {
>> {
dids.into_iter().map(Self::query).collect()
}

fn linked_resources(did: DidIdentifier) -> Vec<LinkedDidResource<Web3Name, DotName>> {
linked_resources_for_did(&did).collect()
}

fn linked_resources_deletion_calls(did: DidIdentifier) -> Vec<RuntimeCall> {
let linked_resources = linked_resources_for_did(&did);
linked_resources.map(map_linked_resource_to_call).collect()
}
}

impl kilt_runtime_api_public_credentials::PublicCredentials<Block, Vec<u8>, Hash, CredentialEntry<Hash, DidIdentifier, BlockNumber, AccountId, Balance, AuthorizationId<<Runtime as delegation::Config>::DelegationNodeId>>, PublicCredentialsFilter<Hash, AccountId>, PublicCredentialsApiError> for Runtime {
Expand Down Expand Up @@ -512,3 +524,34 @@ impl_runtime_apis! {
}
}
}

/// Returns an iterator over all linked resources for a given DID.
fn linked_resources_for_did(did: &DidIdentifier) -> impl Iterator<Item = LinkedDidResource<Web3Name, DotName>> {
let web3_name = pallet_web3_names::Names::<Runtime>::get(did);
let dot_name = pallet_web3_names::Names::<Runtime, DotNamesDeployment>::get(did);
let web3_name_accounts = pallet_did_lookup::ConnectedAccounts::<Runtime>::iter_key_prefix(did);
let dot_name_accounts =
pallet_did_lookup::ConnectedAccounts::<Runtime, UniqueLinkingDeployment>::iter_key_prefix(did);

iter::once(web3_name.map(LinkedDidResource::Web3Name))
.chain(iter::once(dot_name.map(LinkedDidResource::DotName)))
.chain(web3_name_accounts.map(|acc| Some(LinkedDidResource::Web3NameAccount(acc))))
.chain(dot_name_accounts.map(|acc| Some(LinkedDidResource::DotNameAccount(acc))))
.flatten()
}

/// Creates a `RuntimeCall` that, if submitted by the owner DID,
/// would remove the provided linked resource.
fn map_linked_resource_to_call(resource: LinkedDidResource<Web3Name, DotName>) -> RuntimeCall {
match resource {
LinkedDidResource::Web3Name(_) => RuntimeCall::Web3Names(pallet_web3_names::Call::release_by_owner {}),

LinkedDidResource::DotName(_) => RuntimeCall::DotNames(pallet_web3_names::Call::release_by_owner {}),
LinkedDidResource::Web3NameAccount(account) => {
RuntimeCall::DidLookup(pallet_did_lookup::Call::remove_account_association { account })
}
LinkedDidResource::DotNameAccount(account) => {
RuntimeCall::UniqueLinking(pallet_did_lookup::Call::remove_account_association { account })
}
}
}
Loading