Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update contracts #4

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 71 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,69 +1,96 @@
1. ## Description
## Description

Dynamic VC assertion contract written by solidity, using [Hardhat](https://hardhat.org) for compile and test.
Dynamic VC assertion contract is written by solidity, using [Hardhat](https://hardhat.org) for compilation and testing.

## Environment setup
## Environment setup

- Install [nvm](https://github.com/nvm-sh/nvm)
- Inside the repository, run `nvm use` to set the correct Node version.
- If the version is not installed, run `nvm install`.
- Install [nvm](https://github.com/nvm-sh/nvm)
- Inside the repository, run `nvm use` to set the correct Node version.
- If the version is not installed, run `nvm install`.

## Installation
## Installation

```shell
nvm use
corepack enable pnpm
pnpm install
```
```shell
nvm use
corepack enable pnpm
pnpm install
```

## Usage
## Usage

### Compile
### Compile

1. Using hardhat.
1. Using hardhat.

```shell
pnpm compile
```
```shell
pnpm compile
```

After compiled, the contract bytecode will generate in file `artifacts/contracts/**/{contractName}.sol/{contractName}.json`, e.g. the bytecode of A1 is in the file `artifacts/contracts/A1.sol/A1.json`.
After compiling, the contract bytecode will generate in file `artifacts/contracts/**/{contractName}.sol/{contractName}.json`, e.g. the bytecode of A1 is in the file `artifacts/contracts/A1.sol/A1.json`.

2. Using [Remix IDE](https://remix.ethereum.org).
2. Using [Remix IDE](https://remix.ethereum.org).

Should use the `dynamic` as your project root path in Remix IDE as below:
Should use the `dynamic` as your project root path in Remix IDE as below:

```shell
remixd -s your_repo_path/tee-worker/litentry/core/assertion-build/src/dynamic --remix-ide https://remix.ethereum.org
```
```shell
remixd -s your_repo_path/tee-worker/litentry/core/assertion-build/src/dynamic --remix-ide https://remix.ethereum.org
```

If you have not install remixd before, rub below script to install it.
If you have not installed Remixd before, run the below script to install it.

```shell
npm install -g @remix-project/remixd
```
```shell
npm install -g @remix-project/remixd
```

### Testing
### Deploy

- Test all: `pnpm test`.
The deployment script can be used to deploy a specific contract to the specified chain. Below is the usage of the deployment script.

```shell
pnpm test
```
#### Command Syntax

- Test single file: `pnpm test {testFilePath}`.
```shell
pnpm run deploy-contract --contract <ContractName> --chain <ChainName> [--mnemonic <MnemonicValue>] [--secrets <Secret1> <Secret2> ...]
```

Example:
#### Parameters

```shell
pnpm test tests/token-holding-amount.ts
```
- --contract: Specify the name of the contract you wish to deploy.
- --chain: Specify the target chain environment. Supported values are:
- local
- dev
- staging
- prod
- --mnemonic: Optional, the mnemonic string required to generate the wallet for contract deployment on the staging and production chains.
- --secrets: Optional, provide the required secret values for the contract. These may include API keys, private keys, or other sensitive information needed for the deployment contract, multiple secrets must be separated by blank, and the secret item does not support line breaks.

#### Vc DI tests(integration tests)
#### Example

1. Start parachain&&worker
2. `pnpm install`
3. `pnpm --filter integration-tests run test assertion_contracts.test.ts`
To deploy the `TokenMapping` contract to the `dev` chain with specific secrets, you would run the following command:

```shell
pnpm run deploy-contract --contract TokenMapping --chain dev --mnemonic "angle total unfold" --secrets "abc" "vna" "poi xyz"
```

#### Troubleshooting

- If you meet below error, run `chmod +x ./scripts/run_deploy.sh` can fix it.

```plain
sh: 1: ./scripts/run_deploy.sh: Permission denied
```

### Testing

- Test all: `pnpm test`.

```shell
pnpm test
```

- Test single file: `pnpm test {testFilePath}`.

Example:

```shell
pnpm test tests/token-holding-amount.ts
```
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 "";
}
}
Loading
Loading