Skip to content

Commit

Permalink
Add pallet-utility to provider and DID runtime API
Browse files Browse the repository at this point in the history
  • Loading branch information
ntn-x2 committed Nov 27, 2023
1 parent d66cfb7 commit 55e45f8
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
2 changes: 2 additions & 0 deletions 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 dip-template/runtimes/dip-consumer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,5 @@ runtime-benchmarks = [
"runtime-common/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
]
7 changes: 6 additions & 1 deletion dip-template/runtimes/dip-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ scale-info = {workspace = true, features = ["derive"]}
did.workspace = true
kilt-support.workspace = true
kilt-dip-support.workspace = true
kilt-runtime-api-did.workspace = true
kilt-runtime-api-dip-provider.workspace = true
pallet-deposit-storage.workspace = true
pallet-did-lookup.workspace = true
Expand All @@ -42,6 +43,7 @@ pallet-sudo.workspace = true
pallet-timestamp.workspace = true
pallet-transaction-payment.workspace = true
pallet-transaction-payment-rpc-runtime-api.workspace = true
pallet-utility.workspace = true
sp-api.workspace = true
sp-block-builder.workspace = true
sp-consensus-aura.workspace = true
Expand Down Expand Up @@ -75,6 +77,7 @@ std = [
"did/std",
"kilt-support/std",
"kilt-dip-support/std",
"kilt-runtime-api-did/std",
"kilt-runtime-api-dip-provider/std",
"pallet-deposit-storage/std",
"pallet-did-lookup/std",
Expand All @@ -93,6 +96,7 @@ std = [
"pallet-timestamp/std",
"pallet-transaction-payment/std",
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-utility/std",
"sp-api/std",
"sp-block-builder/std",
"sp-consensus-aura/std",
Expand Down Expand Up @@ -121,5 +125,6 @@ runtime-benchmarks = [
"pallet-web3-names/runtime-benchmarks",
"runtime-common/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"frame-support/runtime-benchmarks"
"frame-support/runtime-benchmarks",
"pallet-utility/runtime-benchmarks"
]
102 changes: 102 additions & 0 deletions dip-template/runtimes/dip-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

use pallet_did_lookup::linkable_account::LinkableAccountId;
use pallet_web3_names::web3_name::AsciiWeb3Name;
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
Expand Down Expand Up @@ -119,6 +120,7 @@ construct_runtime!(
Timestamp: pallet_timestamp = 2,
ParachainInfo: parachain_info = 3,
Sudo: pallet_sudo = 4,
Utility: pallet_utility = 5,

// Money
Balances: pallet_balances = 10,
Expand Down Expand Up @@ -269,6 +271,13 @@ impl pallet_sudo::Config for Runtime {
type WeightInfo = ();
}

impl pallet_utility::Config for Runtime {
type PalletsOrigin = OriginCaller;
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

pub const EXISTENTIAL_DEPOSIT: Balance = MILLIUNIT;

impl pallet_balances::Config for Runtime {
Expand Down Expand Up @@ -569,6 +578,99 @@ impl_runtime_apis! {
}
}

impl kilt_runtime_api_did::Did<
Block,
DidIdentifier,
AccountId,
LinkableAccountId,
Balance,
Hash,
BlockNumber
> for Runtime {
fn query_by_web3_name(name: Vec<u8>) -> Option<kilt_runtime_api_did::RawDidLinkedInfo<
DidIdentifier,
AccountId,
LinkableAccountId,
Balance,
Hash,
BlockNumber
>
> {
let name: pallet_web3_names::web3_name::AsciiWeb3Name<Runtime> = name.try_into().ok()?;
pallet_web3_names::Owner::<Runtime>::get(&name)
.and_then(|owner_info| {
did::Did::<Runtime>::get(&owner_info.owner).map(|details| (owner_info, details))
})
.map(|(owner_info, details)| {
let accounts = pallet_did_lookup::ConnectedAccounts::<Runtime>::iter_key_prefix(
&owner_info.owner,
).collect();
let service_endpoints = did::ServiceEndpoints::<Runtime>::iter_prefix(&owner_info.owner).map(|e| From::from(e.1)).collect();

kilt_runtime_api_did::RawDidLinkedInfo{
identifier: owner_info.owner,
w3n: Some(name.into()),
accounts,
service_endpoints,
details: details.into(),
}
})
}

fn query_by_account(account: LinkableAccountId) -> Option<
kilt_runtime_api_did::RawDidLinkedInfo<
DidIdentifier,
AccountId,
LinkableAccountId,
Balance,
Hash,
BlockNumber
>
> {
pallet_did_lookup::ConnectedDids::<Runtime>::get(account)
.and_then(|owner_info| {
did::Did::<Runtime>::get(&owner_info.did).map(|details| (owner_info, details))
})
.map(|(connection_record, details)| {
let w3n = pallet_web3_names::Names::<Runtime>::get(&connection_record.did).map(Into::into);
let accounts = pallet_did_lookup::ConnectedAccounts::<Runtime>::iter_key_prefix(&connection_record.did).collect();
let service_endpoints = did::ServiceEndpoints::<Runtime>::iter_prefix(&connection_record.did).map(|e| From::from(e.1)).collect();

kilt_runtime_api_did::RawDidLinkedInfo {
identifier: connection_record.did,
w3n,
accounts,
service_endpoints,
details: details.into(),
}
})
}

fn query(did: DidIdentifier) -> Option<
kilt_runtime_api_did::RawDidLinkedInfo<
DidIdentifier,
AccountId,
LinkableAccountId,
Balance,
Hash,
BlockNumber
>
> {
let details = did::Did::<Runtime>::get(&did)?;
let w3n = pallet_web3_names::Names::<Runtime>::get(&did).map(Into::into);
let accounts = pallet_did_lookup::ConnectedAccounts::<Runtime>::iter_key_prefix(&did).collect();
let service_endpoints = did::ServiceEndpoints::<Runtime>::iter_prefix(&did).map(|e| From::from(e.1)).collect();

Some(kilt_runtime_api_did::RawDidLinkedInfo {
identifier: did,
w3n,
accounts,
service_endpoints,
details: details.into(),
})
}
}

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 identity_details = IdentityProviderOf::<Runtime>::retrieve(&request.identifier).map_err(runtime_api::DipProofError::IdentityProvider)?;
Expand Down

0 comments on commit 55e45f8

Please sign in to comment.