Skip to content

Commit ea79f1c

Browse files
authored
Feat/update readme (#20)
* populate the readme * update code examples and improve structure * improve phrasing * add live links to docs
1 parent f4dfdfb commit ea79f1c

File tree

1 file changed

+168
-2
lines changed

1 file changed

+168
-2
lines changed

README.md

Lines changed: 168 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,169 @@
1-
# Credible-std
1+
# credible-std
22

3-
Credible-std is a standard library for the Assertions that are used in the Credible Security Layer.
3+
credible-std is a standard library for implementing assertions in the Phylax Credible Layer (PCL). It provides the core contracts and interfaces needed to create and manage assertions for smart contract security monitoring.
4+
5+
## Overview
6+
7+
The Phylax Credible Layer (PCL) is a security framework that enables real-time monitoring and validation of smart contract behavior through assertions. credible-std provides the foundational contracts and utilities needed to implement these assertions.
8+
9+
### Key Components
10+
11+
- `Credible.sol`: Base contract that provides access to the PhEvm precompile for assertion validation
12+
- `Assertion.sol`: Abstract contract for implementing assertions with trigger registration and validation logic
13+
- `StateChanges.sol`: Utilities for tracking and validating contract state changes with type-safe conversions
14+
- `TriggerRecorder.sol`: Manages assertion triggers for function calls, storage changes, and balance changes
15+
- `PhEvm.sol`: Interface for the PhEvm precompile that enables assertion validation
16+
- `CredibleTest.sol`: Testing utilities for assertion development and validation
17+
18+
## Features
19+
20+
- **Trigger System**: Register triggers for function calls, storage changes, and balance changes to monitor specific contract behaviors
21+
- **State Change Tracking**: Type-safe utilities for monitoring and validating contract state changes with built-in conversion helpers
22+
- **Testing Framework**: Comprehensive testing utilities for assertion development with built-in validation helpers
23+
- **PhEvm Integration**: Direct access to the PhEvm precompile for advanced assertion logic and validation
24+
25+
You can find detailed documentation on the Credible Layer and how to use the credible-std library in the [Credible Layer Documentation](https://docs.phylax.systems/credible/credible-introduction).
26+
27+
## Installation
28+
29+
### Using Foundry
30+
31+
Add the following to your `foundry.toml`:
32+
33+
```toml
34+
[dependencies]
35+
credible-std = { git = "https://github.com/phylaxsystems/credible-std.git" }
36+
```
37+
38+
Then run:
39+
40+
```bash
41+
forge install
42+
```
43+
44+
Alternatively you can install the package using forge:
45+
46+
```bash
47+
forge install phylax-systems/credible-std
48+
```
49+
50+
### Using Hardhat
51+
52+
Add the dependency to your `package.json`:
53+
54+
```json
55+
{
56+
"dependencies": {
57+
"credible-std": "github:phylaxsystems/credible-std"
58+
}
59+
}
60+
```
61+
62+
Then run:
63+
64+
```bash
65+
npm install
66+
```
67+
68+
## Usage
69+
70+
### Assertion Lifecycle
71+
72+
1. Create an assertion contract that inherits from `Assertion`
73+
2. Initialize the assertion in the constructor with the contract address you want to monitor
74+
3. Register triggers in the `triggers()` function for when the assertion should be checked
75+
4. Implement validation logic in your assertion function(s)
76+
5. Add the assertion to your test environment using `cl.addAssertion()`
77+
6. Test the assertion using `cl.validate()`
78+
79+
### Creating an Assertion
80+
81+
```solidity
82+
// SPDX-License-Identifier: MIT
83+
pragma solidity ^0.8.13;
84+
85+
import {Assertion} from "credible-std/src/Assertion.sol"; // Credible Layer precompiles
86+
import {Ownable} from "../../src/Ownable.sol"; // Contract to write assertions for
87+
88+
contract OwnableAssertion is Assertion {
89+
Ownable ownable;
90+
91+
constructor(address ownable_) {
92+
ownable = Ownable(ownable_); // Define address of Ownable contract
93+
}
94+
95+
// Define selectors for the assertions, several assertions can be defined here
96+
// This function is required by the Assertion interface
97+
function triggers() external view override {
98+
registerCallTrigger(this.assertionOwnershipChange.selector); // Register the selector for the assertionOwnershipChange function
99+
}
100+
101+
// This function is used to check if the ownership has changed
102+
// Get the owner of the contract before and after the transaction
103+
// Return false if the owner has changed, true if it has not
104+
function assertionOwnershipChange() external {
105+
ph.forkPreState(); // Fork the pre-state of the transaction
106+
address preOwner = ownable.owner(); // Get the owner of the contract before the transaction
107+
ph.forkPostState(); // Fork the post-state of the transaction
108+
address postOwner = ownable.owner(); // Get the owner of the contract after the transaction
109+
require(postOwner == preOwner, "Ownership has changed"); // revert if the owner has changed
110+
}
111+
}
112+
```
113+
114+
For a detailed guide on how to write assertions check out the [Writing Assertions](https://docs.phylax.systems/credible/pcl-assertion-guide) section of the documentation.
115+
116+
### Available Cheatcodes
117+
118+
The credible-std provides several cheatcodes for assertion validation:
119+
120+
- `forkPreState()`: Forks to the state prior to the assertion triggering transaction
121+
- `forkPostState()`: Forks to the state after the assertion triggering transaction
122+
- `load(address target, bytes32 slot)`: Loads a storage slot from an address
123+
- `getLogs()`: Retrieves logs from the assertion triggering transaction
124+
- `getCallInputs(address target, bytes4 selector)`: Gets call inputs for a given target and selector
125+
- `getStateChanges(address contractAddress, bytes32 slot)`: Gets state changes for a given contract and storage slot
126+
127+
These cheatcodes can be accessed through the `ph` instance in your assertion contracts, which is provided by the `Credible` base contract.
128+
129+
### Testing Assertions
130+
131+
```solidity
132+
// SPDX-License-Identifier: MIT
133+
pragma solidity ^0.8.13;
134+
135+
import {Credible} from "credible-std/src/Credible.sol";
136+
import {OwnableAssertion} from "../src/OwnableAssertion.sol";
137+
import {Ownable} from "../../src/Ownable.sol";
138+
import {CredibleTest} from "credible-std/src/CredibleTest.sol";
139+
import {Test} from "forge-std/Test.sol";
140+
141+
contract TestOwnableAssertion is CredibleTest, Test {
142+
// Contract state variables
143+
Ownable public assertionAdopter;
144+
address public initialOwner = address(0xdead);
145+
address public newOwner = address(0xdeadbeef);
146+
147+
function setUp() public {
148+
assertionAdopter = new Ownable();
149+
vm.deal(initialOwner, 1 ether);
150+
}
151+
152+
function test_assertionOwnershipChanged() public {
153+
address aaAddress = address(assertionAdopter);
154+
string memory label = "Ownership has changed";
155+
156+
// Associate the assertion with the protocol
157+
// cl will manage the correct assertion execution under the hood when the protocol is being called
158+
cl.addAssertion(label, aaAddress, type(OwnableAssertion).creationCode, abi.encode(assertionAdopter));
159+
160+
vm.prank(initialOwner);
161+
vm.expectRevert("Assertions Reverted"); // If the assertion fails, it will revert with this message
162+
cl.validate(
163+
label, aaAddress, 0, abi.encodePacked(assertionAdopter.transferOwnership.selector, abi.encode(newOwner))
164+
);
165+
}
166+
}
167+
```
168+
169+
For a detailed guide on how to test assertions check out the [Testing Assertions](https://docs.phylax.systems/credible/testing-assertions) section of the documentation.

0 commit comments

Comments
 (0)