Skip to content

Commit

Permalink
copy from parachain
Browse files Browse the repository at this point in the history
  • Loading branch information
0xverin committed Sep 4, 2024
1 parent c8b4943 commit 5b03d34
Show file tree
Hide file tree
Showing 77 changed files with 2,877 additions and 1,040 deletions.
11 changes: 6 additions & 5 deletions contracts/A20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ contract A20 is DynamicAssertion {
}

Logging.info("begin create assertion for A20");
AssertionLogic.Condition memory condition = AssertionLogic.Condition(
"$has_joined",
AssertionLogic.Op.Equal,
"true"
);
AssertionLogic.Condition memory condition = AssertionLogic
.newConditionWithoutSubCc(
"$has_joined",
AssertionLogic.Op.Equal,
"true"
);
string[] memory assertions = new string[](1);
assertions[0] = AssertionLogic.toString(condition);

Expand Down
61 changes: 44 additions & 17 deletions contracts/libraries/AssertionLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

pragma solidity ^0.8.8;

import "./StringCleaner.sol";

library AssertionLogic {
enum Op {
GreaterThan,
Expand All @@ -32,21 +34,40 @@ library AssertionLogic {
string src;
Op op;
string dst;
CompositeCondition cc;
}

struct CompositeCondition {
Condition[] conditions;
bool isAnd; // true for 'And', false for 'Or'
}

function newConditionWithoutSubCc(
string memory src,
Op op,
string memory dst
) internal pure returns (Condition memory) {
CompositeCondition memory subCc;
return Condition(src, op, dst, subCc);
}

function addCondition(
CompositeCondition memory cc,
uint256 i,
string memory src,
Op op,
string memory dst
) internal pure {
cc.conditions[i] = Condition(src, op, dst);
CompositeCondition memory subCc;
cc.conditions[i] = Condition(src, op, dst, subCc);
}

function addCompositeCondition(
CompositeCondition memory cc,
uint256 i,
CompositeCondition memory subCc
) internal pure {
cc.conditions[i] = Condition("", Op.Equal, "", subCc);
}

function andOp(
Expand Down Expand Up @@ -83,36 +104,42 @@ library AssertionLogic {
abi.encodePacked(result, cc.isAnd ? '"and":[' : '"or":[')
);
for (uint256 i = 0; i < cc.conditions.length; i++) {
Condition memory c = cc.conditions[i];
if (i > 0) {
result = string(abi.encodePacked(result, ","));
}
result = string(
abi.encodePacked(result, toString(cc.conditions[i]))
);
if (c.cc.conditions.length > 0) {
result = string(abi.encodePacked(result, toString(c.cc)));
} else {
result = string(abi.encodePacked(result, toString(c)));
}
}
result = string(abi.encodePacked(result, "]"));
}

result = string(abi.encodePacked(result, "}"));

return result;
// the assembled result may contain some invisible characters that cause the unit test failure, so we need to clear it here.
return StringCleaner.cleanString(result);
}

function toString(
Condition memory condition
) internal pure returns (string memory) {
return
string(
abi.encodePacked(
'{"src":"',
condition.src,
'","op":"',
operatorToString(condition.op),
'","dst":"',
condition.dst,
'"}'
)
);
string memory result = string(
abi.encodePacked(
'{"src":"',
condition.src,
'","op":"',
operatorToString(condition.op),
'","dst":"',
condition.dst,
'"}'
)
);

// the assembled result may contain some invisible characters that cause the unit test failure, so we need to clear it here.
return StringCleaner.cleanString(result);
}

function operatorToString(Op op) internal pure returns (string memory) {
Expand Down
88 changes: 88 additions & 0 deletions contracts/libraries/Identities.sol
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,92 @@ library Identities {
}
return (false);
}

function web3_network_to_chain(
uint32 network
) internal pure returns (string memory chain) {
if (network == Web3Networks.Polkadot) {
chain = "polkadot";
} else if (network == Web3Networks.Kusama) {
chain = "kusama";
} else if (network == Web3Networks.Litentry) {
chain = "litentry";
} else if (network == Web3Networks.Litmus) {
chain = "litmus";
} else if (network == Web3Networks.Khala) {
chain = "khala";
} else if (network == Web3Networks.Ethereum) {
chain = "ethereum";
} else if (network == Web3Networks.Bsc) {
chain = "bsc";
} else if (network == Web3Networks.Polygon) {
chain = "polygon";
} else if (network == Web3Networks.Arbitrum) {
chain = "arbitrum";
} else if (network == Web3Networks.Solana) {
chain = "solana";
} else if (network == Web3Networks.Combo) {
chain = "combo";
}
}

function get_network_name(
uint32 network
) internal pure returns (string memory) {
if (network == Web3Networks.Polkadot) {
return "Polkadot";
}
if (network == Web3Networks.Kusama) {
return "Kusama";
}
if (network == Web3Networks.Litentry) {
return "Litentry";
}
if (network == Web3Networks.Litmus) {
return "Litmus";
}
if (network == Web3Networks.LitentryRococo) {
return "LitentryRococo";
}
if (network == Web3Networks.Khala) {
return "Khala";
}
if (network == Web3Networks.SubstrateTestnet) {
return "SubstrateTestnet";
}
if (network == Web3Networks.Ethereum) {
return "Ethereum";
}
if (network == Web3Networks.Bsc) {
return "Bsc";
}
if (network == Web3Networks.Polygon) {
return "Polygon";
}
if (network == Web3Networks.Arbitrum) {
return "Arbitrum";
}
if (network == Web3Networks.Solana) {
return "Solana";
}
if (network == Web3Networks.Combo) {
return "Combo";
}
if (network == Web3Networks.BitcoinP2tr) {
return "BitcoinP2tr";
}
if (network == Web3Networks.BitcoinP2pkh) {
return "BitcoinP2pkh";
}
if (network == Web3Networks.BitcoinP2sh) {
return "BitcoinP2sh";
}
if (network == Web3Networks.BitcoinP2wpkh) {
return "BitcoinP2wpkh";
}
if (network == Web3Networks.BitcoinP2wsh) {
return "BitcoinP2wsh";
}
return "";
}
}
66 changes: 66 additions & 0 deletions contracts/libraries/StringCleaner.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 <https://www.gnu.org/licenses/>.

// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity ^0.8.0;

/**
* @title StringCleaner
* @dev A library for cleaning strings by removing non-visible ASCII characters.
*/
library StringCleaner {
/**
* @dev Cleans the input string by removing non-visible ASCII characters.
* It iterates through each character in the string and retains only visible characters
* (ASCII range 0x20 to 0x7E).
* @param str The input string to be cleaned.
* @return The cleaned string with only visible characters.
*/
function cleanString(
string memory str
) internal pure returns (string memory) {
bytes memory b = bytes(str);
bytes memory cleaned = new bytes(b.length);
uint256 cleanedIndex = 0;

for (uint256 i = 0; i < b.length; i++) {
bytes1 char = b[i];
if (isVisibleChar(char)) {
cleaned[cleanedIndex] = char;
cleanedIndex++;
}
}

// Create a new bytes array of the correct length and copy cleaned characters into it
bytes memory trimmedCleaned = new bytes(cleanedIndex);
for (uint256 j = 0; j < cleanedIndex; j++) {
trimmedCleaned[j] = cleaned[j];
}

return string(trimmedCleaned);
}

/**
* @dev Checks if a given character is a visible ASCII character.
* This includes characters in the ASCII range from 0x20 (space) to 0x7E (~).
* @param char The character to be checked.
* @return True if the character is visible, false otherwise.
*/
function isVisibleChar(bytes1 char) internal pure returns (bool) {
return (char >= 0x20 && char <= 0x7E);
}
}
64 changes: 64 additions & 0 deletions contracts/libraries/StringComparison.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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 <https://www.gnu.org/licenses/>.

// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity ^0.8.0;

library StringComparison {
/**
* @notice Converts a given string to lowercase.
* @dev This function iterates through each character of the input string,
* checks if it is an uppercase letter (A-Z), and converts it to the
* corresponding lowercase letter (a-z). Characters outside the range
* of uppercase letters remain unchanged.
* @param str The input string to be converted to lowercase.
* @return A new string with all uppercase letters converted to lowercase.
*/
function toLower(string memory str) internal pure returns (string memory) {
bytes memory bStr = bytes(str);
bytes memory bLower = new bytes(bStr.length);

for (uint i = 0; i < bStr.length; i++) {
// Uppercase character range in ASCII: A-Z (65-90)
if (bStr[i] >= 0x41 && bStr[i] <= 0x5A) {
// Convert to lowercase by adding 32 (A -> a, B -> b, ..., Z -> z)
bLower[i] = bytes1(uint8(bStr[i]) + 32);
} else {
bLower[i] = bStr[i];
}
}

return string(bLower);
}

/**
* @notice Compares two strings for equality, ignoring case.
* @dev Converts both input strings to lowercase using the `toLower` function
* and then compares their keccak256 hashes to determine if they are equal.
* @param str1 The first string to compare.
* @param str2 The second string to compare.
* @return A boolean value indicating whether the two strings are equal, ignoring case.
*/
function compareStringsIgnoreCase(
string memory str1,
string memory str2
) internal pure returns (bool) {
return
keccak256(abi.encodePacked(toLower(str1))) ==
keccak256(abi.encodePacked(toLower(str2)));
}
}
Loading

0 comments on commit 5b03d34

Please sign in to comment.