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

Doc updates #5

Merged
merged 3 commits into from
Oct 21, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release
run: cd contracts/ && npx semantic-release
66 changes: 56 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,59 @@
# Sample Hardhat Project
## Introduction

This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, and a script that deploys that contract.
Solve3 addresses the issues caused by bots in various Web3 activities, such as games, NFT sales, and ticket purchases. By implementing captchas and backend proofs, Solve3 ensures a secure and equitable user experience, reducing disruptions caused by bots.

Try running some of the following tasks:
- [Introduction](#introduction)
- [Solve3 Contracts](#solve3-contracts)
- [Solve3Master Contract](#solve3master-contract)
- [Solve3Verify Contract](#solve3verify-contract)
- [Implementation Guide](#implementation-guide)

```shell
npx hardhat help
npx hardhat test
REPORT_GAS=true npx hardhat test
npx hardhat node
npx hardhat run scripts/deploy.ts
```
## Solve3 Contracts

Solve3 consists of two essential contracts: `Solve3Master` and `Solve3Verify`. We'll provide an overview of these contracts, their functions, and their deployment addresses.

### Solve3Master Contract

The `Solve3Master` contract is the core of Solve3, managing signer status, nonces, and contract ownership. While developers are not required to implement it, understanding its inner workings can be valuable.

* **Functions**:

* `initialize(address _signer)`: Initializes the contract with a signer address.
* `getNonce(address _account)`: Retrieves the nonce of an account.
* `getTimestampAndNonce(address _account)`: Gets the timestamp and nonce of an account.
* `isSigner(address _account)`: Checks if an account is a signer.
* `setSigner(address _account, bool _flag)`: Sets the signer status of an account.
* `transferOwnership(address _newOwner)`: Transfers ownership of the contract.
* `recoverERC20(address _token)`: Recovers ERC20 tokens.
* **Events**:

* `OwnershipTransferred`: Triggered when contract ownership is transferred.
* `SignerChanged`: Fired when the signer status of an account changes.

### Solve3Verify Contract

The `Solve3Verify` abstract contract is used to protect your smart contracts with Solve3. You must implement this contract to validate Solve3 proofs. It includes functions to set timestamps and disable Solve3 if needed.

#### Implementation Guide

**Note:** Implementation guide can be found [here](contracts/README.md).

* **Functions**:

* `__init_Solve3Verify(address _solve3Master)`: Initializes the `Solve3Verify` contract with the Solve3 master contract.
* `disableSolve3(bool _flag)`: Allows you to disable or enable Solve3 as needed.
* `setValidPeriodSeconds(uint256 _validPeriodSeconds)`: Sets the period in seconds for which a signature is valid.
* **Error Handling**:

* `Solve3VerifyInitializedAlready`: Thrown if trying to initialize the contract when it's already initialized.
* `Solve3VerifyIsDisabled`: Thrown when a function requires Solve3 to be disabled.
* `Solve3VerifyIsNotDisabled`: Thrown when a function requires Solve3 not to be disabled.
* `Solve3VerifyUnableToVerify`: Thrown when a Solve3 proof cannot be verified.
* `Solve3VerifyAddressMismatch`: Thrown when the account in the proof does not match the sender's account.
* `Solve3VerifyMsgSignedTooEarly`: Thrown when a message is signed too early.
* `Solve3VerifySignatureInvalid`: Thrown when a signature is invalid.
* **Events**:

* `Solve3VerifyDisabled`: Fired when Solve3 is disabled.
* `Solve3ValidFromTimestampSet`: Triggered when the valid-from timestamp is set.
* `Solve3ValidPeriodSecondsSet`: Triggered when the valid period in seconds is set.
2 changes: 1 addition & 1 deletion contracts/ISolve3Master.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//SPDX-License-Identifier: MIT

pragma solidity 0.8.14;
pragma solidity >=0.8.14;

interface ISolve3Master {
function initialize(address _signer) external;
Expand Down
2 changes: 1 addition & 1 deletion contracts/MasterStorage.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//SPDX-License-Identifier: MIT

pragma solidity 0.8.14;
pragma solidity >=0.8.14;

import "./Structs.sol";

Expand Down
160 changes: 160 additions & 0 deletions contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
## Introduction

The `Solve3Verify.sol` contract is a critical component of the [Solve3](https://solve3.org) bot protection system. It enables you to verify Solve3 proofs and ensure secure interactions within your smart contracts. This documentation provides a detailed guide on integrating and using the `Solve3Verify` contract in your Ethereum-based projects.

- [Introduction](#introduction)
- [Quick Start](#quick-start)
- [Step 1: Import the Contract](#step-1-import-the-contract)
- [Step 2: Inherit from `Solve3Verify`](#step-2-inherit-from-solve3verify)
- [Step 3: Initialize the Contract](#step-3-initialize-the-contract)
- [Step 4: Abstract Function `disableSolve3`](#step-4-abstract-function-disablesolve3)
- [Optional Functions](#optional-functions)
- [Set Valid From Timestamp](#set-valid-from-timestamp)
- [Set Valid Period Seconds](#set-valid-period-seconds)
- [Custom `validFrom` Implementation](#custom-validfrom-implementation)
- [Custom `validPeriod` Implementation](#custom-validperiod-implementation)
- [Public Variables and View Functions](#public-variables-and-view-functions)
- [Events](#events)
- [Errors](#errors)


## Quick Start

Integrating the `Solve3Verify` contract into your project is a straightforward process. Here's a quick start guide:

### Step 1: Import the Contract

Begin by importing the `Solve3Verify` contract into your Solidity project.

```solidity
import "@solve3/contracts/Solve3Verify.sol";
```

### Step 2: Inherit from `Solve3Verify`

In your contract, inherit from the `Solve3Verify` contract.

```solidity
contract YourContract is Solve3Verify {
// Your contract code here
}
```

### Step 3: Initialize the Contract

In your contract's constructor, call the `__init_Solve3Verify` function with the address of the Solve3 Master contract as an argument.

```solidity
constructor(address _solve3Master) {
__init_Solve3Verify(_solve3Master);
// Your constructor code here
}
```

### Step 4: Abstract Function `disableSolve3`

Since Solve3 is in beta, you have to implement a function to disable Solve3 verification in your contract.

**Note:** Please also add access control like OpenZeppelin `Ownable`

```solidity
function disableSolve3(bool _flag) external {
_disableSolve3(_flag);
}
```

Your contract is now ready to verify Solve3 proofs for secure interactions.

## Optional Functions

The `Solve3Verify` contract provides optional functions to enhance flexibility and control in your project based on your personal needs or the chain you want to deploy to.

### Set Valid From Timestamp

To customize the timestamp from which the signature is valid, you can implement the following internal function:

```solidity
function _setValidFromTimestamp(uint256 _validFromTimestamp) internal {
validFromTimestamp = _validFromTimestamp;
}
```

This function allows you to adjust the timestamp based on your specific requirements.

**Note:** Per default the `validFromTimestamp` is set to timestamp of the contract deployment.

### Set Valid Period Seconds

You can modify the period in seconds for which the signature is valid using this internal function:

```solidity
function _setValidPeriodSeconds(uint256 _validPeriodSeconds) internal {
validPeriodSeconds = _validPeriodSeconds;
}
```

Use this function to change the period for which the signature remains valid.

**Note:** Per default the `validPeriodSeconds` is set to 300 seconds.

Developers can customize the `validFrom` and `validPeriod` functionality by overriding the following functions:

### Custom `validFrom` Implementation

`function validFrom() public view virtual returns (uint256)`

This overridable function allows developers to modify the timestamp from which the signature is considered valid.

```solidity
function validFrom() public view virtual returns (uint256) {
// Custom logic to determine the validFrom timestamp
return yourCustomValidFromTimestamp;
}
```

### Custom `validPeriod` Implementation

`function validPeriod() public view virtual returns (uint256)`

This overridable function enables developers to change the period in seconds for which the signature remains valid.

```solidity
function validPeriod() public view virtual returns (uint256) {
// Custom logic to determine the valid period in seconds
return yourCustomValidPeriodSeconds;
}
```

These functions provide flexibility for developers to adapt Solve3 verification to their specific project requirements.


## Public Variables and View Functions

The `Solve3Verify` contract includes various public variables and view functions for inspecting its state. These include:

* `public solve3Master`: The address of the Solve3 Master contract.
* `public solve3Disabled`: A flag indicating whether Solve3 verification is disabled.
* `public validFromTimestamp`: The timestamp from which the signature is valid.
* `public validPeriodSeconds`: The period in seconds for which the signature is valid.

## Events

The `Solve3Verify` contract emits various events to track significant contract actions and state changes:

* `Solve3VerifyDisabled(bool disabled)`: Triggered when Solve3 verification is enabled or disabled.
* `Solve3VerifyInitialized(address indexed solve3Master)`: Indicates the successful initialization of the Solve3 contract.
* `Solve3VerifySuccess(address indexed account, uint256 timestamp)`: Recorded when Solve3 verification succeeds.
* `Solve3ValidFromTimestampSet(uint256 validFromTimestamp)`: Signals changes to the valid-from timestamp.
* `Solve3ValidPeriodSecondsSet(uint256 validPeriodSeconds)`: Notifies changes to the valid period in seconds.

## Errors

The `Solve3Verify` contract defines several errors that can be raised during contract execution. These errors provide information about specific issues or constraints. Here are the defined errors:

* `Solve3VerifyInitializedAlready()`: Raised when the Solve3 contract is initialized more than once.
* `Solve3VerifyIsDisabled()`: Triggered when Solve3 verification is disabled.
* `Solve3VerifyIsNotDisabled()`: Raised when Solve3 verification is enabled.
* `Solve3VerifyUnableToVerify()`: Indicates a failure to verify a Solve3 proof.
* `Solve3VerifyAddressMismatch()`: Raised when the account address does not match the sender's address.
* `Solve3VerifyMsgSignedTooEarly()`: Occurs when the message is signed too early.
* `Solve3VerifySignatureInvalid()`: Raised when the Solve3 signature is invalid.
2 changes: 1 addition & 1 deletion contracts/Solve3Master.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//SPDX-License-Identifier: MIT

pragma solidity 0.8.14;
pragma solidity >=0.8.14;

import "./MasterStorage.sol";
import "./ISolve3Master.sol";
Expand Down
9 changes: 1 addition & 8 deletions contracts/Solve3Verify.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//SPDX-License-Identifier: MIT

pragma solidity 0.8.14;
pragma solidity >=0.8.14;

import "./ISolve3Master.sol";

Expand Down Expand Up @@ -101,13 +101,6 @@ abstract contract Solve3Verify {
emit Solve3ValidFromTimestampSet(_validFromTimestamp);
}

/// @notice Abstract function to set the Valid Period Seconds
/// @dev Can be used to change the period in seconds for which the signature is valid
/// @param _validPeriodSeconds the period in seconds for which the signature is valid
function setValidPeriodSeconds(uint256 _validPeriodSeconds) external virtual {
_setValidPeriodSeconds(_validPeriodSeconds);
}

/// @notice Internal function to set the Valid Period Seconds
/// @dev Can be used by the abstract function to change the period in seconds for which the signature is valid
/// @param _validPeriodSeconds the period in seconds for which the signature is valid
Expand Down
2 changes: 1 addition & 1 deletion contracts/Structs.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//SPDX-License-Identifier: MIT

pragma solidity 0.8.14;
pragma solidity >=0.8.14;

interface Structs {
struct EIP712Domain {
Expand Down
27 changes: 27 additions & 0 deletions contracts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@solve3/contracts",
"version": "1.0.5",
"homepage": "https://solve3.org",
"description": "Contracts for Solve3 smart contract bot protection",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/solve3-org/contracts.git"
},
"keywords": [
"solidity"
],
"author": "Solve3.org",
"license": "MIT",
"bugs": {
"url": "https://github.com/solve3-org/contracts/issues"
},
"files": [
"*.sol",
"!mocks/",
"!proxy"
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@solve3/contracts",
"name": "solve3-contracts",
"version": "0.0.0-semantic-release",
"description": "",
"private": false,
Expand Down
Loading