Skip to content

Commit

Permalink
P-179 Add credentials-priority-1-and-2 (#2218)
Browse files Browse the repository at this point in the history
* Merge dev

* remove debug

* refine codes

* update lock file

* Add AchainableMirr index
  • Loading branch information
outofxxx authored Nov 21, 2023
1 parent 858bf42 commit 99027b8
Show file tree
Hide file tree
Showing 18 changed files with 792 additions and 47 deletions.
11 changes: 11 additions & 0 deletions primitives/core/src/assertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ pub struct AchainableToken {
pub token: ParameterString,
}

#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq, MaxEncodedLen, TypeInfo)]
pub struct AchainableMirror {
pub name: ParameterString,
pub chain: Web3Network,
pub post_quantity: Option<ParameterString>,
}

#[rustfmt::skip]
#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq, MaxEncodedLen, TypeInfo)]
pub enum AchainableParams {
Expand All @@ -134,6 +141,8 @@ pub enum AchainableParams {
Date(AchainableDate),
#[codec(index = 10)]
Token(AchainableToken),
#[codec(index = 11)]
Mirror(AchainableMirror),
}

impl AchainableParams {
Expand All @@ -150,6 +159,7 @@ impl AchainableParams {
AchainableParams::DatePercent(p) => p.name.clone(),
AchainableParams::Date(p) => p.name.clone(),
AchainableParams::Token(p) => p.name.clone(),
AchainableParams::Mirror(p) => p.name.clone(),
}
}

Expand All @@ -166,6 +176,7 @@ impl AchainableParams {
AchainableParams::DatePercent(p) => p.chain,
AchainableParams::Date(p) => p.chain,
AchainableParams::Token(p) => p.chain,
AchainableParams::Mirror(p) => p.chain,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tee-worker/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 tee-worker/enclave-runtime/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 35 additions & 7 deletions tee-worker/litentry/core/assertion-build/src/achainable/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ compile_error!("feature \"std\" and feature \"sgx\" cannot be enabled at the sam
#[cfg(all(not(feature = "std"), feature = "sgx"))]
extern crate sgx_tstd as std;

use crate::{achainable::request_achainable, *};
use lc_data_providers::ConvertParameterString;
use crate::{
achainable::{request_achainable, request_achainable_balance},
*,
};
use lc_credentials::litentry_profile::holding_amount::LitentryProfileHoldingAmount;
use lc_data_providers::{achainable_names::AchainableNameBalance, ConvertParameterString};

const CREATED_OVER_AMOUNT_CONTRACTS: &str = "Created over {amount} contracts";
const BALANCE_OVER_AMOUNT: &str = "Balance over {amount}";
Expand Down Expand Up @@ -103,13 +107,37 @@ pub fn build_amount(req: &AssertionBuildRequest, param: AchainableAmount) -> Res
.collect::<Vec<String>>();

let achainable_param = AchainableParams::Amount(param.clone());
let flag = request_achainable(addresses, achainable_param.clone())?;
let bname = AchainableNameBalance::from(achainable_param.name()).map_err(|e| {
Error::RequestVCFailed(
Assertion::Achainable(achainable_param.clone()),
e.into_error_detail(),
)
})?;
let mut balance = 0.0;
let mut flag = false;
if bname == AchainableNameBalance::BalanceUnderAmount {
balance = request_achainable_balance(addresses, achainable_param.clone())?
.parse::<f64>()
.map_err(|_| {
Error::RequestVCFailed(
Assertion::Achainable(achainable_param.clone()),
ErrorDetail::ParseError,
)
})?;
} else {
flag = request_achainable(addresses, achainable_param.clone())?;
}

match Credential::new(&req.who, &req.shard) {
Ok(mut credential_unsigned) => {
let (desc, subtype, content) =
get_assertion_content(&achainable_param.to_string(&param.name)?, &param.chain);
credential_unsigned.add_subject_info(desc, subtype);
credential_unsigned.update_content(flag, content);
if bname == AchainableNameBalance::BalanceUnderAmount {
credential_unsigned.update_eth_holding_amount(balance);
} else {
let (desc, subtype, content) =
get_assertion_content(&achainable_param.to_string(&param.name)?, &param.chain);
credential_unsigned.add_subject_info(desc, subtype);
credential_unsigned.update_content(flag, content);
}

Ok(credential_unsigned)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,34 @@ compile_error!("feature \"std\" and feature \"sgx\" cannot be enabled at the sam
#[cfg(all(not(feature = "std"), feature = "sgx"))]
extern crate sgx_tstd as std;

use crate::{achainable::request_achainable, *};
use lc_data_providers::ConvertParameterString;

const VC_SUBJECT_DESCRIPTION: &str = "Achainable amount holding";
const VC_SUBJECT_TYPE: &str = "Amount holding";
use crate::{achainable::request_achainable_balance, *};
use lc_credentials::litentry_profile::token_balance::TokenBalanceInfo;
use lc_data_providers::{ETokenAddress, TokenFromString};

pub fn build_amount_holding(
req: &AssertionBuildRequest,
param: AchainableAmountHolding,
) -> Result<Credential> {
debug!("Assertion Achainable build_amount_holding, who: {:?}", account_id_to_string(&req.who));

let identities = transpose_identity(&req.identities);
let addresses = identities
.into_iter()
.flat_map(|(_, addresses)| addresses)
.collect::<Vec<String>>();

let achainable_param = AchainableParams::AmountHolding(param.clone());
let flag = request_achainable(addresses, achainable_param.clone())?;
let token = ETokenAddress::from_vec(param.clone().token.unwrap_or_default());
let achainable_param = AchainableParams::AmountHolding(param);
let balance = request_achainable_balance(addresses, achainable_param.clone())?
.parse::<f64>()
.map_err(|_| {
Error::RequestVCFailed(
Assertion::Achainable(achainable_param.clone()),
ErrorDetail::ParseError,
)
})?;

match Credential::new(&req.who, &req.shard) {
Ok(mut credential_unsigned) => {
credential_unsigned.add_subject_info(VC_SUBJECT_DESCRIPTION, VC_SUBJECT_TYPE);
credential_unsigned.update_holder(
flag,
&achainable_param.to_string(&param.amount)?,
&achainable_param.to_string(&param.date)?,
);

credential_unsigned.update_token_balance(token, balance);
Ok(credential_unsigned)
},
Err(e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,39 @@ compile_error!("feature \"std\" and feature \"sgx\" cannot be enabled at the sam
#[cfg(all(not(feature = "std"), feature = "sgx"))]
extern crate sgx_tstd as std;

use crate::{achainable::request_achainable, *};
use crate::{achainable::request_achainable_balance, *};
use lc_credentials::litentry_profile::token_balance::TokenBalanceInfo;
use lc_data_providers::{ETokenAddress, TokenFromString};

// Input params:
// {
// "name": "ERC20 balance over {amount}",
// "address": "0xb59490ab09a0f526cc7305822ac65f2ab12f9723",
// "params": {
// "chain": "ethereum",
// "amount": "0",
// "token": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
// }
// }

/// LIT / USDC / USDT Holder
/// assertions:[
/// {
/// and:[
/// {
/// src:$lit_holding_amount,
/// op: >=,
/// dst:100
/// },
/// {
/// src:$lit_holding_amount,
/// op: <,
/// dst:200
/// },
/// ]
/// }
///
///
pub fn build_amount_token(
req: &AssertionBuildRequest,
param: AchainableAmountToken,
Expand All @@ -34,10 +65,21 @@ pub fn build_amount_token(
.flat_map(|(_, addresses)| addresses)
.collect::<Vec<String>>();

let token = ETokenAddress::from_vec(param.clone().token.unwrap_or_default());
let achainable_param = AchainableParams::AmountToken(param);
let _flag = request_achainable(addresses, achainable_param.clone())?;
let balance = request_achainable_balance(addresses, achainable_param.clone())?
.parse::<f64>()
.map_err(|_| {
Error::RequestVCFailed(
Assertion::Achainable(achainable_param.clone()),
ErrorDetail::ParseError,
)
})?;
match Credential::new(&req.who, &req.shard) {
Ok(mut _credential_unsigned) => Ok(_credential_unsigned),
Ok(mut credential_unsigned) => {
credential_unsigned.update_token_balance(token, balance);
Ok(credential_unsigned)
},
Err(e) => {
error!("Generate unsigned credential failed {:?}", e);
Err(Error::RequestVCFailed(
Expand Down
77 changes: 77 additions & 0 deletions tee-worker/litentry/core/assertion-build/src/achainable/mirror.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2020-2023 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 <https://www.gnu.org/licenses/>.

#[cfg(all(feature = "std", feature = "sgx"))]
compile_error!("feature \"std\" and feature \"sgx\" cannot be enabled at the same time");

#[cfg(all(not(feature = "std"), feature = "sgx"))]
extern crate sgx_tstd as std;

use super::request_achainable;
use crate::*;
use lc_credentials::{litentry_profile::mirror::MirrorInfo, Credential};
use lc_data_providers::achainable_names::AchainableNameMirror;
use litentry_primitives::AchainableMirror;

// Request Inputs
// {
// "name": "Has written over quantity posts on Mirror",
// "address": "0xCdd39B6D1cC4D0a7243b389Ed9356E23Df6240eb",
// "params": {
// "chain": "ethereum",
// "postQuantity": "0"
// },
// "includeMetadata": true
// }

// {
// "name": "Is a publication on Mirror",
// "address": "0xCdd39B6D1cC4D0a7243b389Ed9356E23Df6240eb",
// "params": {
// "chain": "ethereum"
// },
// "includeMetadata": true
// }

pub fn build_on_mirror(req: &AssertionBuildRequest, param: AchainableMirror) -> Result<Credential> {
let identities = transpose_identity(&req.identities);
let addresses = identities
.into_iter()
.flat_map(|(_, addresses)| addresses)
.collect::<Vec<String>>();

let achainable_param = AchainableParams::Mirror(param);
let mtype = AchainableNameMirror::from(achainable_param.name()).map_err(|e| {
Error::RequestVCFailed(
Assertion::Achainable(achainable_param.clone()),
e.into_error_detail(),
)
})?;
let value = request_achainable(addresses, achainable_param.clone())?;
match Credential::new(&req.who, &req.shard) {
Ok(mut credential_unsigned) => {
credential_unsigned.update_mirror(mtype, value);
Ok(credential_unsigned)
},
Err(e) => {
error!("Generate unsigned credential failed {:?}", e);
Err(Error::RequestVCFailed(
Assertion::Achainable(achainable_param),
e.into_error_detail(),
))
},
}
}
22 changes: 20 additions & 2 deletions tee-worker/litentry/core/assertion-build/src/achainable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ use self::{
amount::build_amount, amount_holding::build_amount_holding, amount_token::build_amount_token,
amounts::build_amounts, basic::build_basic, between_percents::build_between_percents,
class_of_year::build_class_of_year, date::build_date, date_interval::build_date_interval,
date_percent::build_date_percent, token::build_token,
date_percent::build_date_percent, mirror::build_on_mirror, token::build_token,
};
use crate::*;
use lc_data_providers::{
achainable::{AchainableClient, AchainableTagDeFi, Params},
achainable::{AchainableClient, AchainableTagDeFi, HoldingAmount, Params},
DataProviderConfigReader, ReadDataProviderConfig,
};
use lc_stf_task_sender::AssertionBuildRequest;
Expand All @@ -44,6 +44,7 @@ pub mod class_of_year;
pub mod date;
pub mod date_interval;
pub mod date_percent;
pub mod mirror;
pub mod token;

pub fn build(req: &AssertionBuildRequest, param: AchainableParams) -> Result<Credential> {
Expand All @@ -59,6 +60,7 @@ pub fn build(req: &AssertionBuildRequest, param: AchainableParams) -> Result<Cre
AchainableParams::DatePercent(param) => build_date_percent(req, param),
AchainableParams::Date(param) => build_date(req, param),
AchainableParams::Token(param) => build_token(req, param),
AchainableParams::Mirror(param) => build_on_mirror(req, param),
}
}

Expand Down Expand Up @@ -138,3 +140,19 @@ pub fn request_achainable_classofyear(

Ok((longest_created_year.parse::<u32>().is_ok(), longest_created_year))
}

pub fn request_achainable_balance(
addresses: Vec<String>,
param: AchainableParams,
) -> Result<String> {
let request_param = Params::try_from(param.clone())?;

let data_provider_config = DataProviderConfigReader::read()
.map_err(|e| Error::RequestVCFailed(Assertion::Achainable(param.clone()), e))?;
let mut client: AchainableClient = AchainableClient::new(&data_provider_config);
let balance = client.holding_amount(addresses, request_param).map_err(|e| {
Error::RequestVCFailed(Assertion::Achainable(param.clone()), e.into_error_detail())
})?;

Ok(balance)
}
3 changes: 3 additions & 0 deletions tee-worker/litentry/core/credentials/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ itp-utils = { path = "../../../core-primitives/utils", default-features = false
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42", default-features = false }

# litentry
lc-data-providers = { path = "../data-providers", default-features = false }
litentry-primitives = { path = "../../primitives", default-features = false }

[features]
Expand All @@ -50,6 +51,7 @@ sgx = [
"litentry-primitives/sgx",
"rand-sgx",
"itp-time-utils/sgx",
"lc-data-providers/sgx",
]
std = [
"rust-base58",
Expand All @@ -63,4 +65,5 @@ std = [
"litentry-primitives/std",
"rand",
"itp-time-utils/std",
"lc-data-providers/std",
]
1 change: 1 addition & 0 deletions tee-worker/litentry/core/credentials/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub mod error;
pub use error::Error;
pub mod achainable;
pub mod assertion_logic;
pub mod litentry_profile;
pub mod oneblock;
pub mod schema;
use assertion_logic::{AssertionLogic, Op};
Expand Down
Loading

0 comments on commit 99027b8

Please sign in to comment.