From b19bb9792848cf89cadfa67732650dd090f1bb7b Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 19 Oct 2022 08:30:15 +0200 Subject: [PATCH 1/9] Rename public credentials runtime API --- rpc/public-credentials/runtime-api/src/lib.rs | 2 +- rpc/public-credentials/src/lib.rs | 2 +- runtimes/clone/src/lib.rs | 2 +- runtimes/peregrine/src/lib.rs | 2 +- runtimes/spiritnet/src/lib.rs | 2 +- runtimes/standalone/src/lib.rs | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rpc/public-credentials/runtime-api/src/lib.rs b/rpc/public-credentials/runtime-api/src/lib.rs index 6b0b0ba5a8..da522ade20 100644 --- a/rpc/public-credentials/runtime-api/src/lib.rs +++ b/rpc/public-credentials/runtime-api/src/lib.rs @@ -24,7 +24,7 @@ use sp_std::vec::Vec; sp_api::decl_runtime_apis! { /// The API to query public credentials for a subject. - pub trait PublicCredentialsApi where + pub trait PublicCredentials where SubjectId: Codec, CredentialId: Codec, CredentialEntry: Codec diff --git a/rpc/public-credentials/src/lib.rs b/rpc/public-credentials/src/lib.rs index a5fede10c0..ec992622e2 100644 --- a/rpc/public-credentials/src/lib.rs +++ b/rpc/public-credentials/src/lib.rs @@ -16,7 +16,7 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org -pub use public_credentials_runtime_api::PublicCredentialsApi as PublicCredentialsRuntimeApi; +pub use public_credentials_runtime_api::PublicCredentials as PublicCredentialsRuntimeApi; use std::sync::Arc; diff --git a/runtimes/clone/src/lib.rs b/runtimes/clone/src/lib.rs index 9fbf43faae..2ed8663ad9 100644 --- a/runtimes/clone/src/lib.rs +++ b/runtimes/clone/src/lib.rs @@ -578,7 +578,7 @@ impl_runtime_apis! { } } - impl public_credentials_runtime_api::PublicCredentialsApi>> for Runtime { + impl public_credentials_runtime_api::PublicCredentials>> for Runtime { fn get_credential(_credential_id: Hash) -> Option>> { None } diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 1e5050a3d0..9bd6ff1621 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -1289,7 +1289,7 @@ impl_runtime_apis! { } } - impl public_credentials_runtime_api::PublicCredentialsApi::DelegationNodeId>>> for Runtime { + impl public_credentials_runtime_api::PublicCredentials::DelegationNodeId>>> for Runtime { fn get_credential(credential_id: Hash) -> Option::DelegationNodeId>>> { let subject = public_credentials::CredentialSubjects::::get(&credential_id)?; public_credentials::Credentials::::get(&subject, &credential_id) diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index ab4eda9b51..c248f54be3 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -1283,7 +1283,7 @@ impl_runtime_apis! { } } - impl public_credentials_runtime_api::PublicCredentialsApi::DelegationNodeId>>> for Runtime { + impl public_credentials_runtime_api::PublicCredentials::DelegationNodeId>>> for Runtime { fn get_credential(credential_id: Hash) -> Option::DelegationNodeId>>> { let subject = public_credentials::CredentialSubjects::::get(&credential_id)?; public_credentials::Credentials::::get(&subject, &credential_id) diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index 49b2836619..d1922b6fdc 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -1041,7 +1041,7 @@ impl_runtime_apis! { } } - impl public_credentials_runtime_api::PublicCredentialsApi::DelegationNodeId>>> for Runtime { + impl public_credentials_runtime_api::PublicCredentials::DelegationNodeId>>> for Runtime { fn get_credential(credential_id: Hash) -> Option::DelegationNodeId>>> { let subject = public_credentials::CredentialSubjects::::get(&credential_id)?; public_credentials::Credentials::::get(&subject, &credential_id) From db9580734cf93494bd11d1a8e946b69d1512a3fc Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 19 Oct 2022 08:49:03 +0200 Subject: [PATCH 2/9] Rename public credentials RPC interface --- rpc/public-credentials/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rpc/public-credentials/src/lib.rs b/rpc/public-credentials/src/lib.rs index ec992622e2..6ae6e7915c 100644 --- a/rpc/public-credentials/src/lib.rs +++ b/rpc/public-credentials/src/lib.rs @@ -37,10 +37,10 @@ pub trait PublicCredentialsFilter { fn should_include(&self, credential: &Credential) -> bool; } -#[rpc(client, server)] +#[rpc(client, server, namespace = "publicCredentials")] pub trait PublicCredentialsApi { /// Return a credential that matches the provided credential ID, if found. - #[method(name = "credentials_getCredential")] + #[method(name = "getCredential")] fn get_credential( &self, credential_id: OuterCredentialId, @@ -50,7 +50,7 @@ pub trait PublicCredentialsApi Date: Wed, 19 Oct 2022 09:41:43 +0200 Subject: [PATCH 3/9] Rename credential filter --- Cargo.lock | 1 + nodes/common/src/public_credentials.rs | 7 +++---- rpc/public-credentials/Cargo.toml | 1 + rpc/public-credentials/src/lib.rs | 8 ++------ support/src/traits.rs | 5 +++++ 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d5dedb345c..fc79a934fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8019,6 +8019,7 @@ name = "public-credentials-rpc" version = "1.8.0" dependencies = [ "jsonrpsee", + "kilt-support", "parity-scale-codec", "public-credentials-runtime-api", "sp-api", diff --git a/nodes/common/src/public_credentials.rs b/nodes/common/src/public_credentials.rs index 13ad2d5c7b..89173312fd 100644 --- a/nodes/common/src/public_credentials.rs +++ b/nodes/common/src/public_credentials.rs @@ -18,12 +18,11 @@ use serde::{Deserialize, Serialize}; -use kilt_support::deposit::Deposit; +use kilt_support::{deposit::Deposit, traits::ItemFilter}; use public_credentials::CredentialEntry; -use public_credentials_rpc::PublicCredentialsFilter; - #[derive(Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] /// Thin wrapper around a runtime credential entry as specified in the /// `public-credentials` pallet. This wrapper implements all the /// (de-)serialization logic. @@ -68,7 +67,7 @@ pub enum PublicCredentialFilter { } impl - PublicCredentialsFilter> + ItemFilter> for PublicCredentialFilter where CTypeHash: Eq, diff --git a/rpc/public-credentials/Cargo.toml b/rpc/public-credentials/Cargo.toml index 5a4e7f8128..e30eb95025 100644 --- a/rpc/public-credentials/Cargo.toml +++ b/rpc/public-credentials/Cargo.toml @@ -18,6 +18,7 @@ sp-blockchain = {git = "https://github.com/paritytech/substrate", default-featur sp-runtime = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.29"} # Internal runtime dependencies +kilt-support = {path = "../../support"} public-credentials-runtime-api = {version = "1.8.0", path = "./runtime-api"} [features] diff --git a/rpc/public-credentials/src/lib.rs b/rpc/public-credentials/src/lib.rs index 6ae6e7915c..49b54d4f01 100644 --- a/rpc/public-credentials/src/lib.rs +++ b/rpc/public-credentials/src/lib.rs @@ -31,11 +31,7 @@ use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_runtime::{generic::BlockId, traits::Block as BlockT}; -/// Filter that can be used after the credentials for a given subject have been -/// retrieved from the blockchain state. -pub trait PublicCredentialsFilter { - fn should_include(&self, credential: &Credential) -> bool; -} +use kilt_support::traits::ItemFilter; #[rpc(client, server, namespace = "publicCredentials")] pub trait PublicCredentialsApi { @@ -161,7 +157,7 @@ impl< CredentialId: Codec + Send + Sync + 'static + TryFrom + Into, CredentialEntry: Codec + Send + Sync + 'static, OuterCredentialEntry: Send + Sync + 'static + From, - CredentialFilter: Send + Sync + 'static + PublicCredentialsFilter, + CredentialFilter: Send + Sync + 'static + ItemFilter, { fn get_credential( &self, diff --git a/support/src/traits.rs b/support/src/traits.rs index 4879b2a12d..269608578d 100644 --- a/support/src/traits.rs +++ b/support/src/traits.rs @@ -75,3 +75,8 @@ pub trait GenerateBenchmarkOrigin { pub trait GetWorstCase { fn worst_case() -> Self; } + +/// Generic filter. +pub trait ItemFilter { + fn should_include(&self, credential: &Item) -> bool; +} From 52aeb5c97351525d05cb5b57ed8cf52eaa502c28 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 19 Oct 2022 10:01:55 +0200 Subject: [PATCH 4/9] Accept raw asset DID for runtime API --- rpc/public-credentials/runtime-api/src/lib.rs | 7 ++++--- runtimes/common/src/lib.rs | 1 + runtimes/standalone/src/lib.rs | 8 +++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/rpc/public-credentials/runtime-api/src/lib.rs b/rpc/public-credentials/runtime-api/src/lib.rs index da522ade20..63a8e0f6b2 100644 --- a/rpc/public-credentials/runtime-api/src/lib.rs +++ b/rpc/public-credentials/runtime-api/src/lib.rs @@ -24,12 +24,13 @@ use sp_std::vec::Vec; sp_api::decl_runtime_apis! { /// The API to query public credentials for a subject. - pub trait PublicCredentials where + pub trait PublicCredentials where SubjectId: Codec, CredentialId: Codec, - CredentialEntry: Codec + CredentialEntry: Codec, + Error: Codec, { fn get_credential(credential_id: CredentialId) -> Option; - fn get_credentials(subject: SubjectId) -> Vec<(CredentialId, CredentialEntry)>; + fn get_credentials(subject: SubjectId) -> Result, Error>; } } diff --git a/runtimes/common/src/lib.rs b/runtimes/common/src/lib.rs index 3e9c999644..c30bad31d4 100644 --- a/runtimes/common/src/lib.rs +++ b/runtimes/common/src/lib.rs @@ -45,6 +45,7 @@ use sp_std::marker::PhantomData; pub mod assets; pub mod authorization; pub mod constants; +pub mod errors; pub mod fees; pub mod migrations; pub mod pallet_id; diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index d1922b6fdc..4f83cef86c 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -56,6 +56,7 @@ use runtime_common::{ assets::AssetDid, authorization::{AuthorizationId, PalletAuthorize}, constants::{self, EXISTENTIAL_DEPOSIT, KILT}, + errors::PublicCredentialsApiError, fees::ToAuthor, AccountId, Balance, BlockNumber, DidIdentifier, Hash, Index, Signature, SlowAdjustingFeeUpdate, }; @@ -1041,14 +1042,15 @@ impl_runtime_apis! { } } - impl public_credentials_runtime_api::PublicCredentials::DelegationNodeId>>> for Runtime { + impl public_credentials_runtime_api::PublicCredentials, Hash, public_credentials::CredentialEntry::DelegationNodeId>>, PublicCredentialsApiError> for Runtime { fn get_credential(credential_id: Hash) -> Option::DelegationNodeId>>> { let subject = public_credentials::CredentialSubjects::::get(&credential_id)?; public_credentials::Credentials::::get(&subject, &credential_id) } - fn get_credentials(subject: ::SubjectId) -> Vec<(Hash, public_credentials::CredentialEntry::DelegationNodeId>>)> { - public_credentials::Credentials::::iter_prefix(&subject).collect() + fn get_credentials(subject: Vec) -> Result::DelegationNodeId>>)>, PublicCredentialsApiError> { + let asset_did = AssetDid::try_from(subject).map_err(|_| PublicCredentialsApiError::InvalidSubjectId)?; + Ok(public_credentials::Credentials::::iter_prefix(&asset_did).collect()) } } From e06f866692cd4c3d4a6782dbd1ef0c962e84d7ce Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 19 Oct 2022 10:30:01 +0200 Subject: [PATCH 5/9] Mashnet node compiling --- Cargo.lock | 28 +-- Cargo.toml | 1 - nodes/common/Cargo.toml | 18 -- nodes/common/src/lib.rs | 21 -- nodes/common/src/public_credentials.rs | 85 ------- nodes/parachain/Cargo.toml | 2 - nodes/parachain/src/rpc.rs | 34 --- nodes/parachain/src/service.rs | 6 - nodes/standalone/Cargo.toml | 2 - nodes/standalone/src/rpc.rs | 41 +--- rpc/public-credentials/Cargo.toml | 31 --- rpc/public-credentials/runtime-api/Cargo.toml | 3 + rpc/public-credentials/runtime-api/src/lib.rs | 7 +- rpc/public-credentials/src/lib.rs | 227 ------------------ runtimes/common/Cargo.toml | 2 +- runtimes/common/src/assets.rs | 29 +++ runtimes/standalone/Cargo.toml | 4 +- runtimes/standalone/src/lib.rs | 14 +- 18 files changed, 52 insertions(+), 503 deletions(-) delete mode 100644 nodes/common/Cargo.toml delete mode 100644 nodes/common/src/lib.rs delete mode 100644 nodes/common/src/public_credentials.rs delete mode 100644 rpc/public-credentials/Cargo.toml delete mode 100644 rpc/public-credentials/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index fc79a934fc..24f569a91a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3650,7 +3650,6 @@ dependencies = [ "hex-literal", "jsonrpsee", "log", - "node-common", "pallet-transaction-payment-rpc", "parity-scale-codec", "parking_lot 0.12.1", @@ -3660,7 +3659,6 @@ dependencies = [ "polkadot-primitives", "polkadot-service", "public-credentials", - "public-credentials-rpc", "runtime-common", "sc-basic-authorship", "sc-chain-spec", @@ -4630,11 +4628,9 @@ dependencies = [ "jsonrpsee", "log", "mashnet-node-runtime", - "node-common", "pallet-transaction-payment", "pallet-transaction-payment-rpc", "public-credentials", - "public-credentials-rpc", "runtime-common", "sc-basic-authorship", "sc-cli", @@ -5081,16 +5077,6 @@ dependencies = [ "libc", ] -[[package]] -name = "node-common" -version = "1.8.0" -dependencies = [ - "kilt-support", - "public-credentials", - "public-credentials-rpc", - "serde", -] - [[package]] name = "nodrop" version = "0.1.14" @@ -8014,23 +8000,11 @@ dependencies = [ "sp-std", ] -[[package]] -name = "public-credentials-rpc" -version = "1.8.0" -dependencies = [ - "jsonrpsee", - "kilt-support", - "parity-scale-codec", - "public-credentials-runtime-api", - "sp-api", - "sp-blockchain", - "sp-runtime", -] - [[package]] name = "public-credentials-runtime-api" version = "1.8.0" dependencies = [ + "kilt-support", "parity-scale-codec", "sp-api", "sp-std", diff --git a/Cargo.toml b/Cargo.toml index 16e6b28fb1..1a0328249c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,6 @@ members = [ "nodes/*", "pallets/*", "rpc/did/runtime-api", - "rpc/public-credentials", "rpc/public-credentials/runtime-api", "runtimes/*", "support", diff --git a/nodes/common/Cargo.toml b/nodes/common/Cargo.toml deleted file mode 100644 index c61e4dce5c..0000000000 --- a/nodes/common/Cargo.toml +++ /dev/null @@ -1,18 +0,0 @@ -[package] -authors = ["KILT "] -edition = "2021" -description = "Set of common utility types, functions, and traits that all node executables can tap into." -name = "node-common" -version = "1.8.0" - -[dependencies] -# Client dependencies -serde = {version = "1.0.137", features = ["derive"]} - -# Runtime dependencies -kilt-support = {path = "../../support"} -public-credentials = {path = "../../pallets/public-credentials"} -public-credentials-rpc = {path = "../../rpc/public-credentials"} - -[features] -default = [] diff --git a/nodes/common/src/lib.rs b/nodes/common/src/lib.rs deleted file mode 100644 index 5ace9a26fa..0000000000 --- a/nodes/common/src/lib.rs +++ /dev/null @@ -1,21 +0,0 @@ -// KILT Blockchain – https://botlabs.org -// Copyright (C) 2019-2022 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 . - -// If you feel like getting in touch with us, you can do so at info@botlabs.org - -pub mod public_credentials; - -pub use crate::public_credentials::*; diff --git a/nodes/common/src/public_credentials.rs b/nodes/common/src/public_credentials.rs deleted file mode 100644 index 89173312fd..0000000000 --- a/nodes/common/src/public_credentials.rs +++ /dev/null @@ -1,85 +0,0 @@ -// KILT Blockchain – https://botlabs.org -// Copyright (C) 2019-2022 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 . - -// If you feel like getting in touch with us, you can do so at info@botlabs.org - -use serde::{Deserialize, Serialize}; - -use kilt_support::{deposit::Deposit, traits::ItemFilter}; -use public_credentials::CredentialEntry; - -#[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -/// Thin wrapper around a runtime credential entry as specified in the -/// `public-credentials` pallet. This wrapper implements all the -/// (de-)serialization logic. -pub struct OuterCredentialEntry { - pub ctype_hash: CTypeHash, - pub attester: Attester, - pub revoked: bool, - pub block_number: BlockNumber, - #[serde(bound( - serialize = "AccountId: Serialize, Balance: std::fmt::Display", - deserialize = "AccountId: Deserialize<'de>, Balance: std::str::FromStr" - ))] - pub deposit: Deposit, - pub authorization_id: Option, -} - -impl - From> - for OuterCredentialEntry -{ - fn from(value: CredentialEntry) -> Self { - Self { - ctype_hash: value.ctype_hash, - attester: value.attester, - revoked: value.revoked, - block_number: value.block_number, - deposit: value.deposit, - authorization_id: value.authorization_id, - } - } -} - -/// Filter for public credentials retrieved for a provided subject as specified -/// in the RPC interface. -#[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub enum PublicCredentialFilter { - /// Filter credentials that match a specified Ctype. - CtypeHash(CTypeHash), - /// Filter credentials that have been issued by the specified attester. - Attester(Attester), -} - -impl - ItemFilter> - for PublicCredentialFilter -where - CTypeHash: Eq, - Attester: Eq, -{ - fn should_include( - &self, - credential: &CredentialEntry, - ) -> bool { - match self { - Self::CtypeHash(ctype_hash) => ctype_hash == &credential.ctype_hash, - Self::Attester(attester) => attester == &credential.attester, - } - } -} diff --git a/nodes/parachain/Cargo.toml b/nodes/parachain/Cargo.toml index 260944ecaa..102899a126 100644 --- a/nodes/parachain/Cargo.toml +++ b/nodes/parachain/Cargo.toml @@ -17,10 +17,8 @@ substrate-build-script-utils = {git = "https://github.com/paritytech/substrate", # Internal dependencies clone-runtime = {path = "../../runtimes/clone"} -node-common = {path = "../common"} peregrine-runtime = {path = "../../runtimes/peregrine"} public-credentials = {path = "../../pallets/public-credentials"} -public-credentials-rpc = {path = "../../rpc/public-credentials"} runtime-common = {path = "../../runtimes/common"} spiritnet-runtime = {path = "../../runtimes/spiritnet"} diff --git a/nodes/parachain/src/rpc.rs b/nodes/parachain/src/rpc.rs index 8cc5ff4ca4..104efcfc27 100644 --- a/nodes/parachain/src/rpc.rs +++ b/nodes/parachain/src/rpc.rs @@ -58,12 +58,6 @@ where C::Api: frame_rpc_system::AccountNonceApi, C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi, C::Api: BlockBuilder, - C::Api: public_credentials_rpc::PublicCredentialsRuntimeApi< - Block, - AssetDid, - Hash, - CredentialEntry>, - >, P: TransactionPool + 'static, { use frame_rpc_system::{System, SystemApiServer}; @@ -84,34 +78,6 @@ where // to call into the runtime. // // `module.merge(YourRpcStruct::new(ReferenceToClient).into_rpc())?;` - module.merge( - PublicCredentialsQuery::< - C, - Block, - // Input subject ID - String, - // Runtime subject ID - AssetDid, - // Input/output credential ID - Hash, - // Runtime credential ID - Hash, - // Input/output credential entry - node_common::OuterCredentialEntry< - Hash, - DidIdentifier, - BlockNumber, - AccountId, - Balance, - AuthorizationId, - >, - // Runtime credential entry - CredentialEntry>, - // Credential filter - node_common::PublicCredentialFilter, - >::new(client) - .into_rpc(), - )?; Ok(module) } diff --git a/nodes/parachain/src/service.rs b/nodes/parachain/src/service.rs index a103b7a40a..8d9604fd12 100644 --- a/nodes/parachain/src/service.rs +++ b/nodes/parachain/src/service.rs @@ -256,12 +256,6 @@ where + cumulus_primitives_core::CollectCollationInfo + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi + frame_rpc_system::AccountNonceApi - + public_credentials_rpc::PublicCredentialsRuntimeApi< - Block, - AssetDid, - Hash, - CredentialEntry>, - >, sc_client_api::StateBackendFor, Block>: sp_api::StateBackend, Executor: sc_executor::NativeExecutionDispatch + 'static, RB: FnOnce( diff --git a/nodes/standalone/Cargo.toml b/nodes/standalone/Cargo.toml index 709af490d8..6b3880b903 100644 --- a/nodes/standalone/Cargo.toml +++ b/nodes/standalone/Cargo.toml @@ -16,9 +16,7 @@ substrate-build-script-utils = {git = "https://github.com/paritytech/substrate", # Internal dependencies mashnet-node-runtime = {path = "../../runtimes/standalone"} -node-common = {path = "../common"} public-credentials = {path = "../../pallets/public-credentials"} -public-credentials-rpc = {path = "../../rpc/public-credentials"} runtime-common = {path = "../../runtimes/common"} # External dependencies diff --git a/nodes/standalone/src/rpc.rs b/nodes/standalone/src/rpc.rs index a4174c8bc7..28c78c4702 100644 --- a/nodes/standalone/src/rpc.rs +++ b/nodes/standalone/src/rpc.rs @@ -33,11 +33,7 @@ use sp_api::ProvideRuntimeApi; use sp_block_builder::BlockBuilder; use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata}; -use public_credentials::CredentialEntry; -use runtime_common::{ - assets::AssetDid, authorization::AuthorizationId, AccountId, Balance, Block, BlockNumber, DidIdentifier, Hash, - Index, -}; +use runtime_common::{AccountId, Balance, Block, Index}; /// Full client dependencies. pub struct FullDeps { @@ -58,17 +54,10 @@ where C::Api: frame_rpc_system::AccountNonceApi, C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi, C::Api: BlockBuilder, - C::Api: public_credentials_rpc::PublicCredentialsRuntimeApi< - Block, - AssetDid, - Hash, - CredentialEntry>, - >, P: TransactionPool + 'static, { use frame_rpc_system::{System, SystemApiServer}; use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer}; - use public_credentials_rpc::{PublicCredentialsApiServer, PublicCredentialsQuery}; let mut module = RpcModule::new(()); let FullDeps { @@ -85,34 +74,6 @@ where // to call into the runtime. // // `module.merge(YourRpcStruct::new(ReferenceToClient).into_rpc())?;` - module.merge( - PublicCredentialsQuery::< - C, - Block, - // Input subject ID - String, - // Runtime subject ID - AssetDid, - // Input/output credential ID - Hash, - // Runtime credential ID - Hash, - // Input/output credential entry - node_common::OuterCredentialEntry< - Hash, - DidIdentifier, - BlockNumber, - AccountId, - Balance, - AuthorizationId, - >, - // Runtime credential entry - CredentialEntry>, - // Credential filter - node_common::PublicCredentialFilter, - >::new(client) - .into_rpc(), - )?; Ok(module) } diff --git a/rpc/public-credentials/Cargo.toml b/rpc/public-credentials/Cargo.toml deleted file mode 100644 index e30eb95025..0000000000 --- a/rpc/public-credentials/Cargo.toml +++ /dev/null @@ -1,31 +0,0 @@ -[package] -name = "public-credentials-rpc" -version = "1.8.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -# Client dependencies -jsonrpsee = { version = "0.15.1", features = ["server", "macros"] } - -# External runtime dependencies -codec = { package = "parity-scale-codec", version = "3.1.5" } - -# Substrate runtime dependencies -sp-api = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.29"} -sp-blockchain = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.29"} -sp-runtime = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.29"} - -# Internal runtime dependencies -kilt-support = {path = "../../support"} -public-credentials-runtime-api = {version = "1.8.0", path = "./runtime-api"} - -[features] -default = ["std"] -std = [ - "codec/std", - "sp-api/std", - "sp-runtime/std", - "public-credentials-runtime-api/std", -] diff --git a/rpc/public-credentials/runtime-api/Cargo.toml b/rpc/public-credentials/runtime-api/Cargo.toml index 6e5042b7ff..a232a12979 100644 --- a/rpc/public-credentials/runtime-api/Cargo.toml +++ b/rpc/public-credentials/runtime-api/Cargo.toml @@ -14,10 +14,13 @@ codec = {package = "parity-scale-codec", version = "3.1.5", default-features = f sp-api = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false} sp-std = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false} +kilt-support = {path = "../../../support/", default-features = false} + [features] default = ["std"] std = [ "codec/std", + "kilt-support/std", "sp-api/std", "sp-std/std", ] diff --git a/rpc/public-credentials/runtime-api/src/lib.rs b/rpc/public-credentials/runtime-api/src/lib.rs index 63a8e0f6b2..2a017585c9 100644 --- a/rpc/public-credentials/runtime-api/src/lib.rs +++ b/rpc/public-credentials/runtime-api/src/lib.rs @@ -22,15 +22,18 @@ use codec::Codec; use sp_std::vec::Vec; +use kilt_support::traits::ItemFilter; + sp_api::decl_runtime_apis! { /// The API to query public credentials for a subject. - pub trait PublicCredentials where + pub trait PublicCredentials where SubjectId: Codec, CredentialId: Codec, CredentialEntry: Codec, + Filter: Codec + ItemFilter, Error: Codec, { fn get_credential(credential_id: CredentialId) -> Option; - fn get_credentials(subject: SubjectId) -> Result, Error>; + fn get_credentials(subject: SubjectId, filter: Option) -> Result, Error>; } } diff --git a/rpc/public-credentials/src/lib.rs b/rpc/public-credentials/src/lib.rs deleted file mode 100644 index 49b54d4f01..0000000000 --- a/rpc/public-credentials/src/lib.rs +++ /dev/null @@ -1,227 +0,0 @@ -// KILT Blockchain – https://botlabs.org -// Copyright (C) 2019-2022 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 . - -// If you feel like getting in touch with us, you can do so at info@botlabs.org - -pub use public_credentials_runtime_api::PublicCredentials as PublicCredentialsRuntimeApi; - -use std::sync::Arc; - -use codec::Codec; -use jsonrpsee::{ - core::{async_trait, RpcResult}, - proc_macros::rpc, - types::error::{CallError, ErrorObject}, -}; - -use sp_api::ProvideRuntimeApi; -use sp_blockchain::HeaderBackend; -use sp_runtime::{generic::BlockId, traits::Block as BlockT}; - -use kilt_support::traits::ItemFilter; - -#[rpc(client, server, namespace = "publicCredentials")] -pub trait PublicCredentialsApi { - /// Return a credential that matches the provided credential ID, if found. - #[method(name = "getCredential")] - fn get_credential( - &self, - credential_id: OuterCredentialId, - at: Option, - ) -> RpcResult>; - - /// Return all the credentials issued to the provided subject, optionally - /// filtering with the provided logic. The result is a vector of (credential - /// identifier, credential entry). - #[method(name = "getCredentials")] - fn get_credentials( - &self, - subject: OuterSubjectId, - filter: Option, - at: Option, - ) -> RpcResult>; -} - -pub struct PublicCredentialsQuery< - Client, - Block, - OuterSubjectId, - SubjectId, - OuterCredentialId, - CredentialId, - OuterCredentialEntry, - CredentialEntry, - CredentialFilter, -> { - client: Arc, - #[allow(clippy::type_complexity)] - _marker: std::marker::PhantomData<( - Block, - OuterSubjectId, - SubjectId, - OuterCredentialId, - CredentialId, - OuterCredentialEntry, - CredentialEntry, - CredentialFilter, - )>, -} - -impl< - Client, - Block, - OuterSubjectId, - SubjectId, - OuterCredentialId, - CredentialId, - OuterCredentialEntry, - CredentialEntry, - CredentialFilter, - > - PublicCredentialsQuery< - Client, - Block, - OuterSubjectId, - SubjectId, - OuterCredentialId, - CredentialId, - OuterCredentialEntry, - CredentialEntry, - CredentialFilter, - > -{ - pub fn new(client: Arc) -> Self { - Self { - client, - _marker: Default::default(), - } - } -} - -#[repr(i32)] -pub enum Error { - Runtime = 1, - Conversion, - Internal = i32::MAX, -} - -#[async_trait] -impl< - Client, - Block, - OuterSubjectId, - SubjectId, - OuterCredentialId, - CredentialId, - OuterCredentialEntry, - CredentialEntry, - CredentialFilter, - > - PublicCredentialsApiServer< - ::Hash, - OuterSubjectId, - OuterCredentialId, - OuterCredentialEntry, - CredentialFilter, - > - for PublicCredentialsQuery< - Client, - Block, - OuterSubjectId, - SubjectId, - OuterCredentialId, - CredentialId, - OuterCredentialEntry, - CredentialEntry, - CredentialFilter, - > where - Block: BlockT, - Client: Send + Sync + 'static + ProvideRuntimeApi + HeaderBackend, - Client::Api: PublicCredentialsRuntimeApi, - OuterSubjectId: Send + Sync + 'static, - SubjectId: Codec + Send + Sync + 'static + TryFrom, - OuterCredentialId: Send + Sync + 'static, - CredentialId: Codec + Send + Sync + 'static + TryFrom + Into, - CredentialEntry: Codec + Send + Sync + 'static, - OuterCredentialEntry: Send + Sync + 'static + From, - CredentialFilter: Send + Sync + 'static + ItemFilter, -{ - fn get_credential( - &self, - credential_id: OuterCredentialId, - at: Option<::Hash>, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); - - let into_credential_id: CredentialId = credential_id.try_into().map_err(|_| { - CallError::Custom(ErrorObject::owned( - Error::Conversion as i32, - "Unable to convert input to a valid credential ID.", - Option::::None, - )) - })?; - - let credential = api.get_credential(&at, into_credential_id).map_err(|_| { - CallError::Custom(ErrorObject::owned( - Error::Runtime as i32, - "Unable to get credential.", - Option::::None, - )) - })?; - Ok(credential.map(OuterCredentialEntry::from)) - } - - fn get_credentials( - &self, - subject: OuterSubjectId, - filter: Option, - at: Option<::Hash>, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); - - let into_subject: SubjectId = subject.try_into().map_err(|_| { - CallError::Custom(ErrorObject::owned( - Error::Conversion as i32, - "Unable to convert input to a valid subject ID", - Option::::None, - )) - })?; - - let credentials = api.get_credentials(&at, into_subject).map_err(|_| { - CallError::Custom(ErrorObject::owned( - Error::Runtime as i32, - "Unable to get credentials", - Option::::None, - )) - })?; - - let filtered_credentials = if let Some(filter) = filter { - credentials - .into_iter() - .filter(|(_, credential_entry)| filter.should_include(credential_entry)) - .collect() - } else { - credentials - }; - - Ok(filtered_credentials - .into_iter() - .map(|(id, entry)| (id.into(), entry.into())) - .collect()) - } -} diff --git a/runtimes/common/Cargo.toml b/runtimes/common/Cargo.toml index 84286d8cc4..beca2abff3 100644 --- a/runtimes/common/Cargo.toml +++ b/runtimes/common/Cargo.toml @@ -16,7 +16,7 @@ serde = {version = "1.0.144", optional = true, features = ["derive"]} smallvec = "1.8.0" attestation = {default-features = false, path = "../../pallets/attestation"} -kilt-support = { default-features = false, optional = true, path = "../../support" } +kilt-support = {default-features = false, path = "../../support"} parachain-staking = {default-features = false, path = "../../pallets/parachain-staking"} public-credentials = {default-features = false, path = "../../pallets/public-credentials"} diff --git a/runtimes/common/src/assets.rs b/runtimes/common/src/assets.rs index 4c2be05a69..3dd2c6b6fe 100644 --- a/runtimes/common/src/assets.rs +++ b/runtimes/common/src/assets.rs @@ -23,6 +23,11 @@ use sp_std::vec::Vec; use kilt_asset_dids::AssetDid as AssetIdentifier; +use kilt_support::traits::ItemFilter; +use public_credentials::CredentialEntry; + +use crate::{authorization::AuthorizationId, AccountId, Balance, BlockNumber, Hash}; + #[cfg(feature = "runtime-benchmarks")] pub use benchmarks::*; @@ -58,6 +63,30 @@ impl TryFrom for AssetDid { } } +/// Filter for public credentials retrieved for a provided subject as specified +/// in the runtime API interface. +#[derive(Encode, Decode, TypeInfo)] +pub enum PublicCredentialsFilter { + /// Filter credentials that match a specified Ctype. + CtypeHash(CTypeHash), + /// Filter credentials that have been issued by the specified attester. + Attester(Attester), +} + +impl ItemFilter>> + for PublicCredentialsFilter +{ + fn should_include( + &self, + credential: &CredentialEntry>, + ) -> bool { + match self { + Self::CtypeHash(ctype_hash) => ctype_hash == &credential.ctype_hash, + Self::Attester(attester) => attester == &credential.attester, + } + } +} + #[cfg(feature = "runtime-benchmarks")] mod benchmarks { use super::*; diff --git a/runtimes/standalone/Cargo.toml b/runtimes/standalone/Cargo.toml index eb64ae5ad2..b734467749 100644 --- a/runtimes/standalone/Cargo.toml +++ b/runtimes/standalone/Cargo.toml @@ -21,7 +21,7 @@ attestation = {default-features = false, path = "../../pallets/attestation"} ctype = {default-features = false, path = "../../pallets/ctype"} delegation = {default-features = false, path = "../../pallets/delegation"} did = {default-features = false, path = "../../pallets/did"} -kilt-support = {path = "../../support", default-features = false, optional = true} +kilt-support = {path = "../../support", default-features = false} pallet-did-lookup = {default-features = false, path = "../../pallets/pallet-did-lookup"} pallet-web3-names = {default-features = false, path = "../../pallets/pallet-web3-names"} public-credentials = {default-features = false, path = "../../pallets/public-credentials"} @@ -87,7 +87,6 @@ runtime-benchmarks = [ "frame-system-benchmarking", "hex-literal", "runtime-common/runtime-benchmarks", - "kilt-support", "kilt-support/runtime-benchmarks", "pallet-balances/runtime-benchmarks", "pallet-did-lookup/runtime-benchmarks", @@ -111,6 +110,7 @@ std = [ "frame-system-rpc-runtime-api/std", "did-rpc-runtime-api/std", "frame-try-runtime/std", + "kilt-support/std", "log/std", "pallet-aura/std", "pallet-authorship/std", diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index 4f83cef86c..f73f5d0497 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -51,9 +51,10 @@ use sp_version::RuntimeVersion; use delegation::DelegationAc; use did_rpc_runtime_api::runtime_decl_for_DidApi::DidApiV2; +use kilt_support::traits::ItemFilter; use pallet_did_lookup::{linkable_account::LinkableAccountId, migrations::EthereumMigration}; use runtime_common::{ - assets::AssetDid, + assets::{AssetDid, PublicCredentialsFilter}, authorization::{AuthorizationId, PalletAuthorize}, constants::{self, EXISTENTIAL_DEPOSIT, KILT}, errors::PublicCredentialsApiError, @@ -1042,15 +1043,20 @@ impl_runtime_apis! { } } - impl public_credentials_runtime_api::PublicCredentials, Hash, public_credentials::CredentialEntry::DelegationNodeId>>, PublicCredentialsApiError> for Runtime { + impl public_credentials_runtime_api::PublicCredentials, Hash, public_credentials::CredentialEntry::DelegationNodeId>>, PublicCredentialsFilter, PublicCredentialsApiError> for Runtime { fn get_credential(credential_id: Hash) -> Option::DelegationNodeId>>> { let subject = public_credentials::CredentialSubjects::::get(&credential_id)?; public_credentials::Credentials::::get(&subject, &credential_id) } - fn get_credentials(subject: Vec) -> Result::DelegationNodeId>>)>, PublicCredentialsApiError> { + fn get_credentials(subject: Vec, filter: Option>) -> Result::DelegationNodeId>>)>, PublicCredentialsApiError> { let asset_did = AssetDid::try_from(subject).map_err(|_| PublicCredentialsApiError::InvalidSubjectId)?; - Ok(public_credentials::Credentials::::iter_prefix(&asset_did).collect()) + let credentials_prefix = public_credentials::Credentials::::iter_prefix(&asset_did); + if let Some(filter) = filter { + Ok(credentials_prefix.filter(|(_, entry)| filter.should_include(&entry)).collect()) + } else { + Ok(credentials_prefix.collect()) + } } } From 901c0337d1a4a1124d0fa98bd2c5104d558e27fd Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 19 Oct 2022 11:56:04 +0200 Subject: [PATCH 6/9] Whole project compiling --- Cargo.lock | 1 + nodes/parachain/src/rpc.rs | 1 - nodes/parachain/src/service.rs | 10 ++-------- runtimes/clone/Cargo.toml | 3 +++ runtimes/clone/src/lib.rs | 12 +++++++----- runtimes/common/src/assets.rs | 7 ++++++- runtimes/common/src/authorization.rs | 1 - runtimes/peregrine/src/lib.rs | 16 ++++++++++++---- runtimes/spiritnet/src/lib.rs | 16 ++++++++++++---- 9 files changed, 43 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 24f569a91a..95ebb46bd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -984,6 +984,7 @@ dependencies = [ "frame-system-rpc-runtime-api", "frame-try-runtime", "hex-literal", + "kilt-support", "log", "pallet-aura", "pallet-authorship", diff --git a/nodes/parachain/src/rpc.rs b/nodes/parachain/src/rpc.rs index 104efcfc27..79166ad019 100644 --- a/nodes/parachain/src/rpc.rs +++ b/nodes/parachain/src/rpc.rs @@ -62,7 +62,6 @@ where { use frame_rpc_system::{System, SystemApiServer}; use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer}; - use public_credentials_rpc::{PublicCredentialsApiServer, PublicCredentialsQuery}; let mut module = RpcModule::new(()); let FullDeps { diff --git a/nodes/parachain/src/service.rs b/nodes/parachain/src/service.rs index 8d9604fd12..49be9efcd6 100644 --- a/nodes/parachain/src/service.rs +++ b/nodes/parachain/src/service.rs @@ -255,7 +255,7 @@ where + sp_block_builder::BlockBuilder + cumulus_primitives_core::CollectCollationInfo + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi - + frame_rpc_system::AccountNonceApi + + frame_rpc_system::AccountNonceApi, sc_client_api::StateBackendFor, Block>: sp_api::StateBackend, Executor: sc_executor::NativeExecutionDispatch + 'static, RB: FnOnce( @@ -493,13 +493,7 @@ where + frame_rpc_system::AccountNonceApi + pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi + sp_consensus_aura::AuraApi - + cumulus_primitives_core::CollectCollationInfo - + public_credentials_rpc::PublicCredentialsRuntimeApi< - Block, - AssetDid, - Hash, - CredentialEntry>, - >, + + cumulus_primitives_core::CollectCollationInfo, sc_client_api::StateBackendFor, Block>: sp_api::StateBackend, { start_node_impl::( diff --git a/runtimes/clone/Cargo.toml b/runtimes/clone/Cargo.toml index 55ea14f3ff..15f1903813 100644 --- a/runtimes/clone/Cargo.toml +++ b/runtimes/clone/Cargo.toml @@ -26,6 +26,7 @@ public-credentials-runtime-api = {path = "../../rpc/public-credentials/runtime-a # KILT pallets & primitives pallet-did-lookup = {path = "../../pallets/pallet-did-lookup", default-features = false} public-credentials = {path = "../../pallets/public-credentials", default-features = false} +kilt-support = {default-features = false, path = "../../support"} runtime-common = {path = "../../runtimes/common", default-features = false} # Substrate dependencies @@ -96,6 +97,7 @@ runtime-benchmarks = [ "frame-support/runtime-benchmarks", "frame-system-benchmarking", "frame-system/runtime-benchmarks", + "kilt-support/runtime-benchmarks", "hex-literal", "pallet-balances/runtime-benchmarks", "pallet-did-lookup/runtime-benchmarks", @@ -123,6 +125,7 @@ std = [ "frame-system-rpc-runtime-api/std", "frame-system/std", "frame-try-runtime/std", + "kilt-support/std", "log/std", "pallet-aura/std", "pallet-authorship/std", diff --git a/runtimes/clone/src/lib.rs b/runtimes/clone/src/lib.rs index 2ed8663ad9..33293bc85a 100644 --- a/runtimes/clone/src/lib.rs +++ b/runtimes/clone/src/lib.rs @@ -46,10 +46,12 @@ use sp_version::RuntimeVersion; use xcm::opaque::latest::BodyId; use xcm_executor::XcmExecutor; +use kilt_support::traits::ItemFilter; use runtime_common::{ - assets::AssetDid, + assets::{AssetDid, PublicCredentialsFilter}, authorization::AuthorizationId, constants::{self, HOURS, MILLI_KILT}, + errors::PublicCredentialsApiError, fees::{ToAuthor, WeightToFee}, AccountId, AuthorityId, Balance, BlockHashCount, BlockLength, BlockNumber, BlockWeights, DidIdentifier, FeeSplit, Hash, Header, Index, Signature, SlowAdjustingFeeUpdate, @@ -578,13 +580,13 @@ impl_runtime_apis! { } } - impl public_credentials_runtime_api::PublicCredentials>> for Runtime { - fn get_credential(_credential_id: Hash) -> Option>> { + impl public_credentials_runtime_api::PublicCredentials, Hash, public_credentials::CredentialEntry>, PublicCredentialsFilter, PublicCredentialsApiError> for Runtime { + fn get_credential(credential_id: Hash) -> Option>> { None } - fn get_credentials(_subject: AssetDid) -> Vec<(Hash, public_credentials::CredentialEntry>)> { - vec![] + fn get_credentials(subject: Vec, filter: Option>) -> Result>)>, PublicCredentialsApiError> { + Ok(vec![]) } } diff --git a/runtimes/common/src/assets.rs b/runtimes/common/src/assets.rs index 3dd2c6b6fe..724ee938fa 100644 --- a/runtimes/common/src/assets.rs +++ b/runtimes/common/src/assets.rs @@ -65,7 +65,7 @@ impl TryFrom for AssetDid { /// Filter for public credentials retrieved for a provided subject as specified /// in the runtime API interface. -#[derive(Encode, Decode, TypeInfo)] +#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)] pub enum PublicCredentialsFilter { /// Filter credentials that match a specified Ctype. CtypeHash(CTypeHash), @@ -73,6 +73,11 @@ pub enum PublicCredentialsFilter { Attester(Attester), } +#[test] +fn test_filter() { + print!("{:?}", PublicCredentialsFilter::::type_info()); +} + impl ItemFilter>> for PublicCredentialsFilter { diff --git a/runtimes/common/src/authorization.rs b/runtimes/common/src/authorization.rs index 1e7da8bc1a..f7d7144aba 100644 --- a/runtimes/common/src/authorization.rs +++ b/runtimes/common/src/authorization.rs @@ -24,7 +24,6 @@ use attestation::AttestationAccessControl; use public_credentials::PublicCredentialsAccessControl; #[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo, MaxEncodedLen)] -#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] pub enum AuthorizationId { Delegation(DelegationId), } diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 9bd6ff1621..2cad518fd1 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -47,14 +47,16 @@ use xcm_executor::XcmExecutor; use delegation::DelegationAc; use did_rpc_runtime_api::runtime_decl_for_DidApi::DidApiV2; +use kilt_support::traits::ItemFilter; use pallet_did_lookup::{linkable_account::LinkableAccountId, migrations::EthereumMigration}; pub use parachain_staking::InflationInfo; pub use public_credentials; use runtime_common::{ - assets::AssetDid, + assets::{AssetDid, PublicCredentialsFilter}, authorization::{AuthorizationId, PalletAuthorize}, constants::{self, EXISTENTIAL_DEPOSIT, KILT}, + errors::PublicCredentialsApiError, fees::{ToAuthor, WeightToFee}, migrations, pallet_id, AccountId, AuthorityId, Balance, BlockHashCount, BlockLength, BlockNumber, BlockWeights, DidIdentifier, FeeSplit, Hash, Header, Index, Signature, SlowAdjustingFeeUpdate, @@ -1289,14 +1291,20 @@ impl_runtime_apis! { } } - impl public_credentials_runtime_api::PublicCredentials::DelegationNodeId>>> for Runtime { + impl public_credentials_runtime_api::PublicCredentials, Hash, public_credentials::CredentialEntry::DelegationNodeId>>, PublicCredentialsFilter, PublicCredentialsApiError> for Runtime { fn get_credential(credential_id: Hash) -> Option::DelegationNodeId>>> { let subject = public_credentials::CredentialSubjects::::get(&credential_id)?; public_credentials::Credentials::::get(&subject, &credential_id) } - fn get_credentials(subject: ::SubjectId) -> Vec<(Hash, public_credentials::CredentialEntry::DelegationNodeId>>)> { - public_credentials::Credentials::::iter_prefix(&subject).collect() + fn get_credentials(subject: Vec, filter: Option>) -> Result::DelegationNodeId>>)>, PublicCredentialsApiError> { + let asset_did = AssetDid::try_from(subject).map_err(|_| PublicCredentialsApiError::InvalidSubjectId)?; + let credentials_prefix = public_credentials::Credentials::::iter_prefix(&asset_did); + if let Some(filter) = filter { + Ok(credentials_prefix.filter(|(_, entry)| filter.should_include(&entry)).collect()) + } else { + Ok(credentials_prefix.collect()) + } } } diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index c248f54be3..9577f90f64 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -47,14 +47,16 @@ use xcm_executor::XcmExecutor; use delegation::DelegationAc; use did_rpc_runtime_api::runtime_decl_for_DidApi::DidApiV2; +use kilt_support::traits::ItemFilter; use pallet_did_lookup::{linkable_account::LinkableAccountId, migrations::EthereumMigration}; pub use parachain_staking::InflationInfo; pub use public_credentials; use runtime_common::{ - assets::AssetDid, + assets::{AssetDid, PublicCredentialsFilter}, authorization::{AuthorizationId, PalletAuthorize}, constants::{self, EXISTENTIAL_DEPOSIT, KILT}, + errors::PublicCredentialsApiError, fees::{ToAuthor, WeightToFee}, migrations, pallet_id, AccountId, AuthorityId, Balance, BlockHashCount, BlockLength, BlockNumber, BlockWeights, DidIdentifier, FeeSplit, Hash, Header, Index, Signature, SlowAdjustingFeeUpdate, @@ -1283,14 +1285,20 @@ impl_runtime_apis! { } } - impl public_credentials_runtime_api::PublicCredentials::DelegationNodeId>>> for Runtime { + impl public_credentials_runtime_api::PublicCredentials, Hash, public_credentials::CredentialEntry::DelegationNodeId>>, PublicCredentialsFilter, PublicCredentialsApiError> for Runtime { fn get_credential(credential_id: Hash) -> Option::DelegationNodeId>>> { let subject = public_credentials::CredentialSubjects::::get(&credential_id)?; public_credentials::Credentials::::get(&subject, &credential_id) } - fn get_credentials(subject: ::SubjectId) -> Vec<(Hash, public_credentials::CredentialEntry::DelegationNodeId>>)> { - public_credentials::Credentials::::iter_prefix(&subject).collect() + fn get_credentials(subject: Vec, filter: Option>) -> Result::DelegationNodeId>>)>, PublicCredentialsApiError> { + let asset_did = AssetDid::try_from(subject).map_err(|_| PublicCredentialsApiError::InvalidSubjectId)?; + let credentials_prefix = public_credentials::Credentials::::iter_prefix(&asset_did); + if let Some(filter) = filter { + Ok(credentials_prefix.filter(|(_, entry)| filter.should_include(&entry)).collect()) + } else { + Ok(credentials_prefix.collect()) + } } } From a1053b04bb4cdea433097bd544d6db859cc7a3fe Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 19 Oct 2022 12:18:31 +0200 Subject: [PATCH 7/9] Clippy fix --- nodes/parachain/src/rpc.rs | 8 ++------ nodes/parachain/src/service.rs | 6 +----- nodes/standalone/src/rpc.rs | 2 +- runtimes/clone/src/lib.rs | 7 +++---- runtimes/common/src/errors.rs | 26 ++++++++++++++++++++++++++ runtimes/peregrine/src/lib.rs | 2 +- runtimes/spiritnet/src/lib.rs | 2 +- runtimes/standalone/src/lib.rs | 2 +- 8 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 runtimes/common/src/errors.rs diff --git a/nodes/parachain/src/rpc.rs b/nodes/parachain/src/rpc.rs index 79166ad019..716e39469a 100644 --- a/nodes/parachain/src/rpc.rs +++ b/nodes/parachain/src/rpc.rs @@ -33,11 +33,7 @@ use sp_api::ProvideRuntimeApi; use sp_block_builder::BlockBuilder; use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata}; -use public_credentials::CredentialEntry; -use runtime_common::{ - assets::AssetDid, authorization::AuthorizationId, AccountId, Balance, Block, BlockNumber, DidIdentifier, Hash, - Index, -}; +use runtime_common::{AccountId, Balance, Block, Index}; /// Full client dependencies. pub struct FullDeps { @@ -71,7 +67,7 @@ where } = deps; module.merge(System::new(client.clone(), pool, deny_unsafe).into_rpc())?; - module.merge(TransactionPayment::new(client.clone()).into_rpc())?; + module.merge(TransactionPayment::new(client).into_rpc())?; // Extend this RPC with a custom API by using the following syntax. // `YourRpcStruct` should have a reference to a client, which is needed // to call into the runtime. diff --git a/nodes/parachain/src/service.rs b/nodes/parachain/src/service.rs index 49be9efcd6..6ff06b1e17 100644 --- a/nodes/parachain/src/service.rs +++ b/nodes/parachain/src/service.rs @@ -42,11 +42,7 @@ use sp_runtime::traits::BlakeTwo256; use std::{sync::Arc, time::Duration}; use substrate_prometheus_endpoint::Registry; -use public_credentials::CredentialEntry; -use runtime_common::{ - assets::AssetDid, authorization::AuthorizationId, AccountId, AuthorityId, Balance, BlockNumber, DidIdentifier, - Index, -}; +use runtime_common::{AccountId, AuthorityId, Balance, BlockNumber, Index}; type Header = sp_runtime::generic::Header; pub type Block = sp_runtime::generic::Block; diff --git a/nodes/standalone/src/rpc.rs b/nodes/standalone/src/rpc.rs index 28c78c4702..d01cc66db6 100644 --- a/nodes/standalone/src/rpc.rs +++ b/nodes/standalone/src/rpc.rs @@ -67,7 +67,7 @@ where } = deps; module.merge(System::new(client.clone(), pool, deny_unsafe).into_rpc())?; - module.merge(TransactionPayment::new(client.clone()).into_rpc())?; + module.merge(TransactionPayment::new(client).into_rpc())?; // Extend this RPC with a custom API by using the following syntax. // `YourRpcStruct` should have a reference to a client, which is needed diff --git a/runtimes/clone/src/lib.rs b/runtimes/clone/src/lib.rs index 33293bc85a..8b11d33c87 100644 --- a/runtimes/clone/src/lib.rs +++ b/runtimes/clone/src/lib.rs @@ -46,9 +46,8 @@ use sp_version::RuntimeVersion; use xcm::opaque::latest::BodyId; use xcm_executor::XcmExecutor; -use kilt_support::traits::ItemFilter; use runtime_common::{ - assets::{AssetDid, PublicCredentialsFilter}, + assets::PublicCredentialsFilter, authorization::AuthorizationId, constants::{self, HOURS, MILLI_KILT}, errors::PublicCredentialsApiError, @@ -581,11 +580,11 @@ impl_runtime_apis! { } impl public_credentials_runtime_api::PublicCredentials, Hash, public_credentials::CredentialEntry>, PublicCredentialsFilter, PublicCredentialsApiError> for Runtime { - fn get_credential(credential_id: Hash) -> Option>> { + fn get_credential(_credential_id: Hash) -> Option>> { None } - fn get_credentials(subject: Vec, filter: Option>) -> Result>)>, PublicCredentialsApiError> { + fn get_credentials(_subject: Vec, _filter: Option>) -> Result>)>, PublicCredentialsApiError> { Ok(vec![]) } } diff --git a/runtimes/common/src/errors.rs b/runtimes/common/src/errors.rs new file mode 100644 index 0000000000..80e7ec9230 --- /dev/null +++ b/runtimes/common/src/errors.rs @@ -0,0 +1,26 @@ +// KILT Blockchain – https://botlabs.org +// Copyright (C) 2019-2022 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 . + +// If you feel like getting in touch with us, you can do so at info@botlabs.org + +use codec::{Decode, Encode, MaxEncodedLen}; +use scale_info::TypeInfo; +use sp_runtime::RuntimeDebug; + +#[derive(RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)] +pub enum PublicCredentialsApiError { + InvalidSubjectId, +} diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 2cad518fd1..0804e42808 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -1301,7 +1301,7 @@ impl_runtime_apis! { let asset_did = AssetDid::try_from(subject).map_err(|_| PublicCredentialsApiError::InvalidSubjectId)?; let credentials_prefix = public_credentials::Credentials::::iter_prefix(&asset_did); if let Some(filter) = filter { - Ok(credentials_prefix.filter(|(_, entry)| filter.should_include(&entry)).collect()) + Ok(credentials_prefix.filter(|(_, entry)| filter.should_include(entry)).collect()) } else { Ok(credentials_prefix.collect()) } diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index 9577f90f64..94359c99ef 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -1295,7 +1295,7 @@ impl_runtime_apis! { let asset_did = AssetDid::try_from(subject).map_err(|_| PublicCredentialsApiError::InvalidSubjectId)?; let credentials_prefix = public_credentials::Credentials::::iter_prefix(&asset_did); if let Some(filter) = filter { - Ok(credentials_prefix.filter(|(_, entry)| filter.should_include(&entry)).collect()) + Ok(credentials_prefix.filter(|(_, entry)| filter.should_include(entry)).collect()) } else { Ok(credentials_prefix.collect()) } diff --git a/runtimes/standalone/src/lib.rs b/runtimes/standalone/src/lib.rs index f73f5d0497..0772469feb 100644 --- a/runtimes/standalone/src/lib.rs +++ b/runtimes/standalone/src/lib.rs @@ -1053,7 +1053,7 @@ impl_runtime_apis! { let asset_did = AssetDid::try_from(subject).map_err(|_| PublicCredentialsApiError::InvalidSubjectId)?; let credentials_prefix = public_credentials::Credentials::::iter_prefix(&asset_did); if let Some(filter) = filter { - Ok(credentials_prefix.filter(|(_, entry)| filter.should_include(&entry)).collect()) + Ok(credentials_prefix.filter(|(_, entry)| filter.should_include(entry)).collect()) } else { Ok(credentials_prefix.collect()) } From 44f8e05188fc67f64cabd676b6645a66855dbc93 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Wed, 19 Oct 2022 12:32:19 +0200 Subject: [PATCH 8/9] Minor cleanups --- rpc/public-credentials/runtime-api/Cargo.toml | 1 + runtimes/common/src/assets.rs | 7 +------ runtimes/common/src/errors.rs | 5 ++--- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/rpc/public-credentials/runtime-api/Cargo.toml b/rpc/public-credentials/runtime-api/Cargo.toml index a232a12979..0a3c995f31 100644 --- a/rpc/public-credentials/runtime-api/Cargo.toml +++ b/rpc/public-credentials/runtime-api/Cargo.toml @@ -14,6 +14,7 @@ codec = {package = "parity-scale-codec", version = "3.1.5", default-features = f sp-api = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false} sp-std = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29", default-features = false} +# Internal dependencies kilt-support = {path = "../../../support/", default-features = false} [features] diff --git a/runtimes/common/src/assets.rs b/runtimes/common/src/assets.rs index 724ee938fa..3dd2c6b6fe 100644 --- a/runtimes/common/src/assets.rs +++ b/runtimes/common/src/assets.rs @@ -65,7 +65,7 @@ impl TryFrom for AssetDid { /// Filter for public credentials retrieved for a provided subject as specified /// in the runtime API interface. -#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)] +#[derive(Encode, Decode, TypeInfo)] pub enum PublicCredentialsFilter { /// Filter credentials that match a specified Ctype. CtypeHash(CTypeHash), @@ -73,11 +73,6 @@ pub enum PublicCredentialsFilter { Attester(Attester), } -#[test] -fn test_filter() { - print!("{:?}", PublicCredentialsFilter::::type_info()); -} - impl ItemFilter>> for PublicCredentialsFilter { diff --git a/runtimes/common/src/errors.rs b/runtimes/common/src/errors.rs index 80e7ec9230..bcde83aacf 100644 --- a/runtimes/common/src/errors.rs +++ b/runtimes/common/src/errors.rs @@ -16,11 +16,10 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org -use codec::{Decode, Encode, MaxEncodedLen}; +use codec::{Decode, Encode}; use scale_info::TypeInfo; -use sp_runtime::RuntimeDebug; -#[derive(RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)] +#[derive(Encode, Decode, TypeInfo)] pub enum PublicCredentialsApiError { InvalidSubjectId, } From 9d2c21dcc63d1d6cc098122e0baae458d65eec9d Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 20 Oct 2022 08:19:52 +0200 Subject: [PATCH 9/9] Add comment to runtime API functions for public credentials --- rpc/public-credentials/runtime-api/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rpc/public-credentials/runtime-api/src/lib.rs b/rpc/public-credentials/runtime-api/src/lib.rs index 2a017585c9..6e4f8d6a08 100644 --- a/rpc/public-credentials/runtime-api/src/lib.rs +++ b/rpc/public-credentials/runtime-api/src/lib.rs @@ -33,7 +33,11 @@ sp_api::decl_runtime_apis! { Filter: Codec + ItemFilter, Error: Codec, { + /// Return the public credential with the specified ID, if found. fn get_credential(credential_id: CredentialId) -> Option; + /// Return all the public credentials linked to the specified subject. + /// An optional filter can be passed to be applied to the result before being returned to the client. + /// It returns an error if the provided specified subject ID is not valid. fn get_credentials(subject: SubjectId, filter: Option) -> Result, Error>; } }