Skip to content

Commit

Permalink
feat: P-701 solidity VC: token holding amount for BRC20 tokens
Browse files Browse the repository at this point in the history
1) add the precompile function: test_hex_to_number, integration_test, parse_decimal, parse_int
2) extract new common functions for Web3Network: get_code, from_code, get_name
3) move integration test of precompile function to its own file
4) move all util functions to new library files
5) add BRC20 contracts, one contract only support one token
6) add new library AssertionLogic instead of assemble assertion JSON manually
  • Loading branch information
higherordertech committed Jun 10, 2024
1 parent c717119 commit 34ad2da
Show file tree
Hide file tree
Showing 45 changed files with 3,097 additions and 869 deletions.
71 changes: 71 additions & 0 deletions primitives/core/src/assertion/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.

use crate::alloc::string::String;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::{traits::ConstU32, BoundedVec};
Expand Down Expand Up @@ -143,6 +144,76 @@ impl Web3Network {
pub fn is_solana(&self) -> bool {
matches!(self, Self::Solana)
}

pub fn get_code(&self) -> u8 {
match self {
Web3Network::Polkadot => 0,
Web3Network::Kusama => 1,
Web3Network::Litentry => 2,
Web3Network::Litmus => 3,
Web3Network::LitentryRococo => 4,
Web3Network::Khala => 5,
Web3Network::SubstrateTestnet => 6,
Web3Network::Ethereum => 7,
Web3Network::Bsc => 8,
Web3Network::BitcoinP2tr => 9,
Web3Network::BitcoinP2pkh => 10,
Web3Network::BitcoinP2sh => 11,
Web3Network::BitcoinP2wpkh => 12,
Web3Network::BitcoinP2wsh => 13,
Web3Network::Polygon => 14,
Web3Network::Arbitrum => 15,
Web3Network::Solana => 16,
Web3Network::Combo => 17,
}
}

pub fn from_code(code: u8) -> Option<Web3Network> {
match code {
0 => Some(Web3Network::Polkadot),
1 => Some(Web3Network::Kusama),
2 => Some(Web3Network::Litentry),
3 => Some(Web3Network::Litmus),
4 => Some(Web3Network::LitentryRococo),
5 => Some(Web3Network::Khala),
6 => Some(Web3Network::SubstrateTestnet),
7 => Some(Web3Network::Ethereum),
8 => Some(Web3Network::Bsc),
9 => Some(Web3Network::BitcoinP2tr),
10 => Some(Web3Network::BitcoinP2pkh),
11 => Some(Web3Network::BitcoinP2sh),
12 => Some(Web3Network::BitcoinP2wpkh),
13 => Some(Web3Network::BitcoinP2wsh),
14 => Some(Web3Network::Polygon),
15 => Some(Web3Network::Arbitrum),
16 => Some(Web3Network::Solana),
17 => Some(Web3Network::Combo),
_ => None,
}
}

pub fn get_name(&self) -> String {
match self {
Web3Network::Polkadot => "polkadot".into(),
Web3Network::Kusama => "kusama".into(),
Web3Network::Litentry => "litentry".into(),
Web3Network::Litmus => "litmus".into(),
Web3Network::LitentryRococo => "litentry_rococo".into(),
Web3Network::Khala => "khala".into(),
Web3Network::SubstrateTestnet => "substrate_testnet".into(),
Web3Network::Ethereum => "ethereum".into(),
Web3Network::Bsc => "bsc".into(),
Web3Network::BitcoinP2tr => "bitcoin_p2tr".into(),
Web3Network::BitcoinP2pkh => "bitcoin_p2pkh".into(),
Web3Network::BitcoinP2sh => "bitcoin_p2sh".into(),
Web3Network::BitcoinP2wpkh => "bitcoin_p2wpkh".into(),
Web3Network::BitcoinP2wsh => "bitcoin_p2wsh".into(),
Web3Network::Polygon => "polygon".into(),
Web3Network::Arbitrum => "arbitrum".into(),
Web3Network::Solana => "solana".into(),
Web3Network::Combo => "combo".into(),
}
}
}

pub fn all_web3networks() -> Vec<Web3Network> {
Expand Down
17 changes: 17 additions & 0 deletions tee-worker/Cargo.lock

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

16 changes: 16 additions & 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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.deps
artifacts
.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"overrides": [
{
"files": "*.sol",
"options": {
"printWidth": 80,
"tabWidth": 4,
"useTabs": false,
"singleQuote": false,
"bracketSpacing": false
}
},
{
"files": "*.yml",
"options": {}
},
{
"files": "*.yaml",
"options": {}
},
{
"files": "*.toml",
"options": {}
},
{
"files": "*.json",
"options": {}
},
{
"files": "*.js",
"options": {}
},
{
"files": "*.ts",
"options": {}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@

pragma solidity ^0.8.8;

import {DynamicAssertion, Identity} from "./DynamicAssertion.sol";
import "./libraries/AssertionLogic.sol";
import "./libraries/Identities.sol";
import "./DynamicAssertion.sol";

contract A1 is DynamicAssertion {
function execute(Identity[] memory identities, string[] memory secrets)
function execute(
Identity[] memory identities,
string[] memory /*secrets*/
)
public
override
returns (
Expand All @@ -35,9 +40,6 @@ contract A1 is DynamicAssertion {
string
memory description = "You've identified at least one account/address in both Web2 and Web3.";
string memory assertion_type = "Basic Identity Verification";
assertions.push(
'{"and": [{ "src": "$has_web2_account", "op": "==", "dst": "true" }, { "src": "$has_web3_account", "op": "==", "dst": "true" } ] }'
);
schema_url = "https://raw.githubusercontent.com/litentry/vc-jsonschema/main/dist/schemas/1-basic-identity-verification/1-0-0.json";

bool result;
Expand All @@ -46,14 +48,34 @@ contract A1 is DynamicAssertion {
bool has_web2_identity = false;

for (uint256 i = 0; i < identities.length; i++) {
if (is_web2(identities[i])) {
if (Identities.is_web2(identities[i])) {
has_web2_identity = true;
} else if (is_web3(identities[i])) {
} else if (Identities.is_web3(identities[i])) {
has_web3_identity = true;
}
}
result = has_web2_identity && has_web3_identity;

AssertionLogic.CompositeCondition memory cc = AssertionLogic
.CompositeCondition(new AssertionLogic.Condition[](2), true);
AssertionLogic.andOp(
cc,
0,
"$has_web2_account",
AssertionLogic.Op.Equal,
"true"
);
AssertionLogic.andOp(
cc,
1,
"$has_web3_account",
AssertionLogic.Op.Equal,
"true"
);

string[] memory assertions = new string[](1);
assertions[0] = AssertionLogic.toString(cc);

return (description, assertion_type, assertions, schema_url, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@

pragma solidity ^0.8.8;

import {DynamicAssertion, Identity, HttpHeader} from "./DynamicAssertion.sol";
import "./libraries/AssertionLogic.sol";
import "./libraries/Http.sol";
import "./libraries/Identities.sol";
import "./libraries/Utils.sol";
import "./DynamicAssertion.sol";

contract A20 is DynamicAssertion {
function execute(Identity[] memory identities, string[] memory secrets)
function execute(
Identity[] memory identities,
string[] memory /*secrets*/
)
public
override
returns (
Expand All @@ -35,25 +42,30 @@ contract A20 is DynamicAssertion {
string
memory description = "The user is an early bird user of the IdentityHub EVM version and has generated at least 1 credential during 2023 Aug 14th ~ Aug 21st.";
string memory assertion_type = "IDHub EVM Version Early Bird";
assertions.push('{ "src": "$has_joined", "op": "==", "dst": "true" }');
schema_url = "https://raw.githubusercontent.com/litentry/vc-jsonschema/main/dist/schemas/12-idhub-evm-version-early-bird/1-0-0.json";
bool result = false;

bool result = false;
for (uint256 i = 0; i < identities.length; i++) {
if (is_web3(identities[i])) {
(bool success, string memory res) = toHex(identities[i].value);
if (Identities.is_web3(identities[i])) {
(bool success, string memory res) = Utils.toHex(
identities[i].value
);
if (success) {
if (!success) {
continue;
}
string memory url = concatenateStrings(
"http://localhost:19527/events/does-user-joined-evm-campaign?account=",
res
string memory url = string(
abi.encodePacked(
"https://archive-test.litentry.io/events/does-user-joined-evm-campaign?account=",
// below url is used for test against mock server
// "http://localhost:19527/events/does-user-joined-evm-campaign?account=",
res
)
);
string memory jsonPointer = "/hasJoined";
HttpHeader[] memory headers = new HttpHeader[](0);

(bool get_success, bool get_result) = GetBool(
(bool get_success, bool get_result) = Http.GetBool(
url,
jsonPointer,
headers
Expand All @@ -68,6 +80,15 @@ contract A20 is DynamicAssertion {
}
}
}

AssertionLogic.Condition memory condition = AssertionLogic.Condition(
"$has_joined",
AssertionLogic.Op.Equal,
"true"
);
string[] memory assertions = new string[](1);
assertions[0] = AssertionLogic.toString(condition);

return (description, assertion_type, assertions, schema_url, result);
}
}
Loading

0 comments on commit 34ad2da

Please sign in to comment.