Skip to content

Commit

Permalink
fix: VC range clause fix (#2961)
Browse files Browse the repository at this point in the history
* fix: P-717 use > instead of >= in amount VC first assertion clause

* fix: only use > 0 in clause when amount = 0 for amount assertion

* fix: use > instead of >= in solidity amount VC first assertion clause, only use > 0 in clause when amount = 0 for solidity amount assertion

* fix: update the schema url of solidity contract: TokenHoldingAmount

* fix: wrong op in first clause of bnb domain amount vc

---------

Co-authored-by: higherordertech <higherordertech>
  • Loading branch information
higherordertech authored Aug 7, 2024
1 parent 8af6dba commit 6fb6936
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -447,13 +447,8 @@ mod tests {
create_network_address_assertion_logics(Web3TokenType::Nfp),
Box::new(AssertionLogic::Item {
src: "$holding_amount".into(),
op: Op::GreaterEq,
op: Op::GreaterThan,
dst: "0".into()
}),
Box::new(AssertionLogic::Item {
src: "$holding_amount".into(),
op: Op::LessThan,
dst: "1".into()
})
]
}
Expand Down Expand Up @@ -483,7 +478,7 @@ mod tests {
create_network_address_assertion_logics(Web3TokenType::Nfp),
Box::new(AssertionLogic::Item {
src: "$holding_amount".into(),
op: Op::GreaterEq,
op: Op::GreaterThan,
dst: "0".into()
}),
Box::new(AssertionLogic::Item {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
string
memory description = "The amount of a particular token you are holding";
string memory assertion_type = "Token Holding Amount";
schema_url = "https://raw.githubusercontent.com/litentry/vc-jsonschema/main/dist/schemas/25-token-holding-amount/1-1-3.json";
schema_url = "https://raw.githubusercontent.com/litentry/vc-jsonschema/main/dist/schemas/25-token-holding-amount/1-1-4.json";

string memory tokenLowercaseName = abi.decode(params, (string));

Expand All @@ -68,6 +68,7 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
string[] memory assertions = assembleAssertions(
min,
max,
balance,
tokenLowercaseName
);

Expand Down Expand Up @@ -134,12 +135,13 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
function assembleAssertions(
uint256 min,
int256 max,
uint256 balance,
string memory tokenName
) private pure returns (string[] memory) {
string memory variable = "$holding_amount";
AssertionLogic.CompositeCondition memory cc = AssertionLogic
.CompositeCondition(
new AssertionLogic.Condition[](max > 0 ? 3 : 2),
new AssertionLogic.Condition[](max > 0 && balance > 0 ? 3 : 2),
true
);
AssertionLogic.andOp(
Expand All @@ -153,10 +155,12 @@ abstract contract TokenHoldingAmount is DynamicAssertion {
cc,
1,
variable,
AssertionLogic.Op.GreaterEq,
min == 0
? AssertionLogic.Op.GreaterThan
: AssertionLogic.Op.GreaterEq,
StringShift.toShiftedString(min, Constants.decimals_factor)
);
if (max > 0) {
if (max > 0 && balance > 0) {
AssertionLogic.andOp(
cc,
2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('TokenHoldingAmount', () => {
await expectAssertionResult(contract, val, {
tokenType: 'Token Holding Amount',
tokenDesc: 'The amount of a particular token you are holding',
schemaUrl: assembleSchemaUrl('25-token-holding-amount/1-1-3.json'),
schemaUrl: assembleSchemaUrl('25-token-holding-amount/1-1-4.json'),
assertions: [assertion],
result,
})
Expand All @@ -54,14 +54,9 @@ describe('TokenHoldingAmount', () => {
},
{
src: '$holding_amount',
op: Op.GTE,
op: Op.GT,
dst: '0',
},
{
src: '$holding_amount',
op: Op.LT,
dst: '1',
},
],
},
false
Expand Down Expand Up @@ -118,7 +113,7 @@ describe('TokenHoldingAmount', () => {
},
{
src: '$holding_amount',
op: Op.GTE,
op: Op.GT,
dst: '0',
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ mod tests {
create_ton_network_assertion_logic(),
Box::new(AssertionLogic::Item {
src: "$holding_amount".into(),
op: Op::GreaterEq,
op: Op::GreaterThan,
dst: "0".into()
}),
Box::new(AssertionLogic::Item {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,18 @@ fn update_assertion(token_type: Web3TokenType, balance: f64, credential: &mut Cr
Some(index) => {
let min = format!("{}", token_holding_amount_range[index]);
let max = format!("{}", token_holding_amount_range[index + 1]);
let min_item =
AssertionLogic::new_item(ASSERTION_KEYS.holding_amount, Op::GreaterEq, &min);
let max_item =
AssertionLogic::new_item(ASSERTION_KEYS.holding_amount, Op::LessThan, &max);
let min_item = AssertionLogic::new_item(
ASSERTION_KEYS.holding_amount,
if index == 0 { Op::GreaterThan } else { Op::GreaterEq },
&min,
);

assertion = assertion.add_item(min_item);
assertion = assertion.add_item(max_item);
if balance > 0_f64 {
let max_item =
AssertionLogic::new_item(ASSERTION_KEYS.holding_amount, Op::LessThan, &max);
assertion = assertion.add_item(max_item);
}

credential.credential_subject.values.push(index != 0 || balance > 0_f64);
},
Expand Down
15 changes: 10 additions & 5 deletions tee-worker/litentry/core/credentials/src/brc20/amount_holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,18 @@ fn update_assertion(token: &BRC20Token, balance: f64, credential: &mut Credentia
Some(index) => {
let min = format!("{}", range[index]);
let max = format!("{}", range[index + 1]);
let min_item =
AssertionLogic::new_item(ASSERTION_KEYS.holding_amount, Op::GreaterEq, &min);
let max_item =
AssertionLogic::new_item(ASSERTION_KEYS.holding_amount, Op::LessThan, &max);
let min_item = AssertionLogic::new_item(
ASSERTION_KEYS.holding_amount,
if index == 0 { Op::GreaterThan } else { Op::GreaterEq },
&min,
);

assertion = assertion.add_item(min_item);
assertion = assertion.add_item(max_item);
if balance > 0_f64 {
let max_item =
AssertionLogic::new_item(ASSERTION_KEYS.holding_amount, Op::LessThan, &max);
assertion = assertion.add_item(max_item);
}

credential.credential_subject.values.push(index != 0 || balance > 0_f64);
},
Expand Down
16 changes: 8 additions & 8 deletions tee-worker/litentry/core/credentials/src/credential_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ pub fn get_schema_url(assertion: &Assertion) -> Option<String> {

Assertion::Achainable(params) => match params {
AchainableParams::AmountHolding(_) =>
Some(format!("{BASE_URL}/17-token-holding-amount/1-1-0.json")),
Some(format!("{BASE_URL}/17-token-holding-amount/1-1-2.json")),

AchainableParams::AmountToken(_) =>
Some(format!("{BASE_URL}/17-token-holding-amount/1-1-0.json")),
Some(format!("{BASE_URL}/17-token-holding-amount/1-1-2.json")),

AchainableParams::Amount(_) => Some(format!("{BASE_URL}/11-token-holder/1-1-0.json")),

Expand Down Expand Up @@ -86,24 +86,24 @@ pub fn get_schema_url(assertion: &Assertion) -> Option<String> {
Some(format!("{BASE_URL}/14-generic-discord-role/1-1-0.json")),

Assertion::BnbDomainHolding =>
Some(format!("{BASE_URL}/15-bnb-domain-holding-amount/1-1-0.json")),
Some(format!("{BASE_URL}/15-bnb-domain-holding-amount/1-1-1.json")),

Assertion::BnbDigitDomainClub(_) =>
Some(format!("{BASE_URL}/16-bnb-3d-4d-club-domain-holding-amount/1-1-0.json")),
Some(format!("{BASE_URL}/16-bnb-3d-4d-club-domain-holding-amount/1-1-1.json")),

Assertion::VIP3MembershipCard(_) =>
Some(format!("{BASE_URL}/19-vip3-card-holder/1-1-0.json")),

Assertion::WeirdoGhostGangHolder =>
Some(format!("{BASE_URL}/18-weirdoghostgang-holder/1-1-0.json")),

Assertion::LITStaking => Some(format!("{BASE_URL}/17-token-holding-amount/1-1-0.json")),
Assertion::LITStaking => Some(format!("{BASE_URL}/17-token-holding-amount/1-1-2.json")),

Assertion::EVMAmountHolding(_) =>
Some(format!("{BASE_URL}/21-evm-holding-amount/1-1-0.json")),
Some(format!("{BASE_URL}/21-evm-holding-amount/1-1-2.json")),

Assertion::BRC20AmountHolder =>
Some(format!("{BASE_URL}/20-token-holding-amount-list/1-1-0.json")),
Some(format!("{BASE_URL}/20-token-holding-amount-list/1-1-1.json")),

Assertion::CryptoSummary => Some(format!("{BASE_URL}/23-crypto-summary/1-1-0.json")),

Expand All @@ -112,7 +112,7 @@ pub fn get_schema_url(assertion: &Assertion) -> Option<String> {
Assertion::NftHolder(_) => Some(format!("{BASE_URL}/26-nft-holder/1-1-2.json")),

Assertion::TokenHoldingAmount(_) =>
Some(format!("{BASE_URL}/25-token-holding-amount/1-1-3.json")),
Some(format!("{BASE_URL}/25-token-holding-amount/1-1-4.json")),

Assertion::Dynamic(..) => None,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,17 @@ fn update_assertion(token: ETokenAddress, balance: f64, credential: &mut Credent
Some(index) => {
let min = format!("{}", range[index]);
let max = format!("{}", range[index + 1]);
let min_item = AssertionLogic::new_item(content, Op::GreaterEq, &min);
let max_item = AssertionLogic::new_item(content, Op::LessThan, &max);
let min_item = AssertionLogic::new_item(
content,
if index == 0 { Op::GreaterThan } else { Op::GreaterEq },
&min,
);

assertion = assertion.add_item(min_item);
assertion = assertion.add_item(max_item);
if balance > 0_f64 {
let max_item = AssertionLogic::new_item(content, Op::LessThan, &max);
assertion = assertion.add_item(max_item);
}

credential.credential_subject.values.push(index != 0 || balance > 0_f64);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,18 @@ fn update_assertion(token_type: EVMTokenType, balance: f64, credential: &mut Cre
Some(index) => {
let min = format!("{}", &EVM_HOLDING_AMOUNT_RANGE[index]);
let max = format!("{}", &EVM_HOLDING_AMOUNT_RANGE[index + 1]);
let min_item =
AssertionLogic::new_item(ASSERTION_KEYS.holding_amount, Op::GreaterEq, &min);
let max_item =
AssertionLogic::new_item(ASSERTION_KEYS.holding_amount, Op::LessThan, &max);
let min_item = AssertionLogic::new_item(
ASSERTION_KEYS.holding_amount,
if index == 0 { Op::GreaterThan } else { Op::GreaterEq },
&min,
);

assertion = assertion.add_item(min_item);
assertion = assertion.add_item(max_item);
if balance > 0_f64 {
let max_item =
AssertionLogic::new_item(ASSERTION_KEYS.holding_amount, Op::LessThan, &max);
assertion = assertion.add_item(max_item);
}

credential.credential_subject.values.push(index != 0 || balance > 0_f64);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,19 @@ pub trait RangeCredentialDetail {
Some(index) => {
let min = range[index - 1];
let max = range[index];
let min_item =
AssertionLogic::new_item(breakdown, Op::GreaterEq, &format!("{}", min));
let max_item =
AssertionLogic::new_item(breakdown, Op::LessThan, &format!("{}", max));
let min_item = AssertionLogic::new_item(
breakdown,
if index == 1 { Op::GreaterThan } else { Op::GreaterEq },
&format!("{}", min),
);

vec![min_item, max_item]
if amount > 0 {
let max_item =
AssertionLogic::new_item(breakdown, Op::LessThan, &format!("{}", max));
vec![min_item, max_item]
} else {
vec![min_item]
}
},
None => {
// >= last value
Expand Down

0 comments on commit 6fb6936

Please sign in to comment.