From 17a8a948942f7238d0d4155ae5898d3eaa827916 Mon Sep 17 00:00:00 2001 From: Kai <7630809+Kailai-Wang@users.noreply.github.com> Date: Tue, 29 Oct 2024 10:34:42 +0100 Subject: [PATCH] Add runtime-api call to get OmniAccount (#3148) * add skeleton * add runtime api integration * clippy * Update parachain/pallets/omni-account/runtime-api/src/lib.rs Co-authored-by: Francisco Silva Signed-off-by: Kai <7630809+Kailai-Wang@users.noreply.github.com> * fix compile --------- Signed-off-by: Kai <7630809+Kailai-Wang@users.noreply.github.com> Co-authored-by: Jonathan Alvarez Co-authored-by: Francisco Silva --- parachain/Cargo.lock | 13 +++++++++ parachain/Cargo.toml | 2 ++ .../omni-account/runtime-api/Cargo.toml | 23 +++++++++++++++ .../omni-account/runtime-api/src/lib.rs | 29 +++++++++++++++++++ parachain/pallets/omni-account/src/lib.rs | 12 ++++++++ parachain/runtime/litentry/Cargo.toml | 2 ++ parachain/runtime/litentry/src/lib.rs | 6 ++++ parachain/runtime/paseo/Cargo.toml | 2 ++ parachain/runtime/paseo/src/lib.rs | 6 ++++ parachain/runtime/rococo/Cargo.toml | 2 ++ parachain/runtime/rococo/src/lib.rs | 6 ++++ .../bitacross/enclave-runtime/Cargo.lock | 1 - 12 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 parachain/pallets/omni-account/runtime-api/Cargo.toml create mode 100644 parachain/pallets/omni-account/runtime-api/src/lib.rs diff --git a/parachain/Cargo.lock b/parachain/Cargo.lock index 6a4230119a..b77a8838a6 100644 --- a/parachain/Cargo.lock +++ b/parachain/Cargo.lock @@ -5673,6 +5673,7 @@ dependencies = [ "pallet-message-queue", "pallet-multisig", "pallet-omni-account", + "pallet-omni-account-runtime-api", "pallet-parachain-staking", "pallet-preimage", "pallet-proxy", @@ -7837,6 +7838,16 @@ dependencies = [ "sp-std", ] +[[package]] +name = "pallet-omni-account-runtime-api" +version = "0.1.0" +dependencies = [ + "core-primitives", + "pallet-omni-account", + "parity-scale-codec", + "sp-api", +] + [[package]] name = "pallet-parachain-staking" version = "0.1.0" @@ -8565,6 +8576,7 @@ dependencies = [ "pallet-message-queue", "pallet-multisig", "pallet-omni-account", + "pallet-omni-account-runtime-api", "pallet-parachain-staking", "pallet-preimage", "pallet-proxy", @@ -10865,6 +10877,7 @@ dependencies = [ "pallet-message-queue", "pallet-multisig", "pallet-omni-account", + "pallet-omni-account-runtime-api", "pallet-parachain-staking", "pallet-preimage", "pallet-proxy", diff --git a/parachain/Cargo.toml b/parachain/Cargo.toml index d5bf8816b5..25a942e0ae 100644 --- a/parachain/Cargo.toml +++ b/parachain/Cargo.toml @@ -19,6 +19,7 @@ members = [ 'pallets/vc-management', 'pallets/xcm-asset-manager', 'pallets/omni-account', + 'pallets/omni-account/runtime-api', 'precompiles/*', 'runtime/litentry', 'runtime/rococo', @@ -259,6 +260,7 @@ pallet-extrinsic-filter = { path = "pallets/extrinsic-filter", default-features pallet-group = { path = "pallets/group", default-features = false } pallet-identity-management = { path = "pallets/identity-management", default-features = false } pallet-omni-account = { path = "pallets/omni-account", default-features = false } +pallet-omni-account-runtime-api = { path = "pallets/omni-account/runtime-api", default-features = false } pallet-parachain-staking = { path = "pallets/parachain-staking", default-features = false } pallet-score-staking = { path = "pallets/score-staking", default-features = false } pallet-teebag = { path = "pallets/teebag", default-features = false } diff --git a/parachain/pallets/omni-account/runtime-api/Cargo.toml b/parachain/pallets/omni-account/runtime-api/Cargo.toml new file mode 100644 index 0000000000..2b3a8f43a6 --- /dev/null +++ b/parachain/pallets/omni-account/runtime-api/Cargo.toml @@ -0,0 +1,23 @@ +[package] +authors = ['Trust Computing GmbH '] +version = "0.1.0" +edition = "2021" +homepage = 'https://litentry.com' +name = 'pallet-omni-account-runtime-api' +repository = 'https://github.com/litentry/litentry-parachain' + +[dependencies] +parity-scale-codec = { workspace = true } +sp-api = { workspace = true } + +core-primitives = { workspace = true } +pallet-omni-account = { workspace = true } + +[features] +default = ["std"] +std = [ + "parity-scale-codec/std", + "sp-api/std", + "core-primitives/std", + "pallet-omni-account/std", +] diff --git a/parachain/pallets/omni-account/runtime-api/src/lib.rs b/parachain/pallets/omni-account/runtime-api/src/lib.rs new file mode 100644 index 0000000000..fc44fc8e77 --- /dev/null +++ b/parachain/pallets/omni-account/runtime-api/src/lib.rs @@ -0,0 +1,29 @@ +// Copyright 2020-2024 Trust Computing GmbH. +// This file is part of Litentry. +// +// Litentry 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. +// +// Litentry 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 Litentry. If not, see . + +#![cfg_attr(not(feature = "std"), no_std)] + +use core_primitives::Identity; +use parity_scale_codec::Codec; + +sp_api::decl_runtime_apis! { + pub trait OmniAccountApi + where + AccountId: Codec, + { + fn omni_account(id: Identity) -> AccountId; + } +} diff --git a/parachain/pallets/omni-account/src/lib.rs b/parachain/pallets/omni-account/src/lib.rs index a83d5a3204..b0ed1dcdd7 100644 --- a/parachain/pallets/omni-account/src/lib.rs +++ b/parachain/pallets/omni-account/src/lib.rs @@ -384,6 +384,18 @@ pub mod pallet { } impl Pallet { + /// Given an `Identity`, get its derived OmniAccount: + /// - if the given Identity is a member Identity of some AccountStore, get its belonged OmniAccount + /// - directly derive it otherwise + pub fn omni_account(identity: Identity) -> T::AccountId { + let hash = identity.hash(); + if let Some(account) = MemberAccountHash::::get(hash) { + account + } else { + T::OmniAccountConverter::convert(&identity) + } + } + fn do_create_account_store(identity: Identity) -> Result, Error> { let hash = identity.hash(); let omni_account = T::OmniAccountConverter::convert(&identity); diff --git a/parachain/runtime/litentry/Cargo.toml b/parachain/runtime/litentry/Cargo.toml index 86aab76cea..50f801b63f 100644 --- a/parachain/runtime/litentry/Cargo.toml +++ b/parachain/runtime/litentry/Cargo.toml @@ -88,6 +88,7 @@ pallet-extrinsic-filter = { workspace = true } pallet-group = { workspace = true } pallet-identity-management = { workspace = true } pallet-omni-account = { workspace = true } +pallet-omni-account-runtime-api = { workspace = true } pallet-parachain-staking = { workspace = true } pallet-score-staking = { workspace = true } pallet-teebag = { workspace = true } @@ -257,6 +258,7 @@ std = [ "pallet-extrinsic-filter/std", "pallet-bitacross/std", "pallet-omni-account/std", + "pallet-omni-account-runtime-api/std", "pallet-identity-management/std", "pallet-score-staking/std", "pallet-teebag/std", diff --git a/parachain/runtime/litentry/src/lib.rs b/parachain/runtime/litentry/src/lib.rs index 8280bb8252..1b1ccebb34 100644 --- a/parachain/runtime/litentry/src/lib.rs +++ b/parachain/runtime/litentry/src/lib.rs @@ -1804,6 +1804,12 @@ impl_runtime_apis! { } } + impl pallet_omni_account_runtime_api::OmniAccountApi for Runtime { + fn omni_account(identity: Identity) -> AccountId { + OmniAccount::omni_account(identity) + } + } + #[cfg(feature = "try-runtime")] impl frame_try_runtime::TryRuntime for Runtime { fn on_runtime_upgrade(checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) { diff --git a/parachain/runtime/paseo/Cargo.toml b/parachain/runtime/paseo/Cargo.toml index 73f1ac8494..774ade6e48 100644 --- a/parachain/runtime/paseo/Cargo.toml +++ b/parachain/runtime/paseo/Cargo.toml @@ -88,6 +88,7 @@ pallet-extrinsic-filter = { workspace = true } pallet-group = { workspace = true } pallet-identity-management = { workspace = true } pallet-omni-account = { workspace = true } +pallet-omni-account-runtime-api = { workspace = true } pallet-parachain-staking = { workspace = true } pallet-score-staking = { workspace = true } pallet-teebag = { workspace = true } @@ -268,6 +269,7 @@ std = [ "pallet-group/std", "pallet-identity-management/std", "pallet-omni-account/std", + "pallet-omni-account-runtime-api/std", "pallet-score-staking/std", "pallet-teebag/std", "pallet-vc-management/std", diff --git a/parachain/runtime/paseo/src/lib.rs b/parachain/runtime/paseo/src/lib.rs index 32440c7506..b2b7e740e1 100644 --- a/parachain/runtime/paseo/src/lib.rs +++ b/parachain/runtime/paseo/src/lib.rs @@ -1865,6 +1865,12 @@ impl_runtime_apis! { } } + impl pallet_omni_account_runtime_api::OmniAccountApi for Runtime { + fn omni_account(identity: Identity) -> AccountId { + OmniAccount::omni_account(identity) + } + } + #[cfg(feature = "try-runtime")] impl frame_try_runtime::TryRuntime for Runtime { fn on_runtime_upgrade(checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) { diff --git a/parachain/runtime/rococo/Cargo.toml b/parachain/runtime/rococo/Cargo.toml index fdaae91906..5b4affe037 100644 --- a/parachain/runtime/rococo/Cargo.toml +++ b/parachain/runtime/rococo/Cargo.toml @@ -88,6 +88,7 @@ pallet-extrinsic-filter = { workspace = true } pallet-group = { workspace = true } pallet-identity-management = { workspace = true } pallet-omni-account = { workspace = true } +pallet-omni-account-runtime-api = { workspace = true } pallet-parachain-staking = { workspace = true } pallet-score-staking = { workspace = true } pallet-teebag = { workspace = true } @@ -268,6 +269,7 @@ std = [ "pallet-group/std", "pallet-identity-management/std", "pallet-omni-account/std", + "pallet-omni-account-runtime-api/std", "pallet-score-staking/std", "pallet-teebag/std", "pallet-vc-management/std", diff --git a/parachain/runtime/rococo/src/lib.rs b/parachain/runtime/rococo/src/lib.rs index 5809502b60..d1aa06d891 100644 --- a/parachain/runtime/rococo/src/lib.rs +++ b/parachain/runtime/rococo/src/lib.rs @@ -1864,6 +1864,12 @@ impl_runtime_apis! { } } + impl pallet_omni_account_runtime_api::OmniAccountApi for Runtime { + fn omni_account(identity: Identity) -> AccountId { + OmniAccount::omni_account(identity) + } + } + #[cfg(feature = "try-runtime")] impl frame_try_runtime::TryRuntime for Runtime { fn on_runtime_upgrade(checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) { diff --git a/tee-worker/bitacross/enclave-runtime/Cargo.lock b/tee-worker/bitacross/enclave-runtime/Cargo.lock index f8926bab5d..562624e4e9 100644 --- a/tee-worker/bitacross/enclave-runtime/Cargo.lock +++ b/tee-worker/bitacross/enclave-runtime/Cargo.lock @@ -2537,7 +2537,6 @@ dependencies = [ "frame-support", "hash-db 0.15.2", "itp-types", - "litentry-hex-utils", "parity-scale-codec", "sgx_tstd", "sp-core",