-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
45,058 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ALCHEMY_API_KEY= | ||
ROPSTEN_PRIVATE_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
node_modules | ||
.env | ||
|
||
#Hardhat files | ||
cache | ||
artifacts | ||
dist | ||
typechain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"overrides": [ | ||
{ | ||
"files": "*.sol", | ||
"options": { | ||
"printWidth": 120, | ||
"tabWidth": 4, | ||
"useTabs": false, | ||
"singleQuote": false, | ||
"bracketSpacing": false, | ||
"explicitTypes": "always", | ||
"semi": true | ||
} | ||
} | ||
] | ||
} |
122 changes: 122 additions & 0 deletions
122
bidirectional_payment_channel/contracts/PaymentChannel.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
pragma solidity ^0.8.7; | ||
|
||
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; | ||
|
||
contract PaymentChannel { | ||
using ECDSA for bytes32; | ||
|
||
event ChallengedExit(address indexed sender, uint256 nonce); | ||
event Withdrawn(address indexed to, uint256 amount); | ||
|
||
address payable[2] public users; | ||
mapping(address => bool) public isUser; | ||
|
||
mapping(address => uint256) public balances; | ||
|
||
uint256 public immutable challengeTime; | ||
uint256 public expiresAt; | ||
uint256 public nonce; | ||
|
||
modifier checkBalances(uint256[2] memory _balances) { | ||
require(address(this).balance >= _balances[0] + _balances[1], "contract balance not enough"); | ||
_; | ||
} | ||
|
||
constructor( | ||
address payable[2] memory _users, | ||
uint256[2] memory _balances, | ||
uint256 _expiresAt, | ||
uint256 _challengeTime | ||
) payable checkBalances(_balances) { | ||
require(_expiresAt > block.timestamp); | ||
require(_challengeTime > 0); | ||
|
||
expiresAt = _expiresAt; | ||
challengeTime = _challengeTime; | ||
|
||
for (uint256 i = 0; i < _users.length; i++) { | ||
address payable user = _users[i]; | ||
|
||
require(!isUser[user], "duplicate users"); | ||
|
||
users[i] = user; | ||
isUser[user] = true; | ||
balances[user] = _balances[i]; | ||
} | ||
} | ||
|
||
function verify( | ||
bytes[2] memory _signatures, | ||
address _contract, | ||
address[2] memory _signers, | ||
uint256[2] memory _balances, | ||
uint256 _nonce | ||
) public pure returns (bool) { | ||
for (uint256 i = 0; i < _signatures.length; i++) { | ||
bytes memory signature = _signatures[i]; | ||
address signer = _signers[i]; | ||
|
||
address recovered = keccak256(abi.encodePacked(_contract, _balances, _nonce)) | ||
.toEthSignedMessageHash() | ||
.recover(signature); | ||
|
||
if (recovered != signer) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
modifier checkSignatures( | ||
bytes[2] memory _signatures, | ||
uint256[2] memory _balances, | ||
uint256 _nonce | ||
) { | ||
address[2] memory signers; | ||
|
||
// Copy users from storage to memory | ||
for (uint256 i = 0; i < users.length; i++) { | ||
signers[i] = users[i]; | ||
} | ||
|
||
require(verify(_signatures, address(this), signers, _balances, _nonce), "Invalid signatures"); | ||
|
||
_; | ||
} | ||
|
||
modifier onlyUser() { | ||
require(isUser[msg.sender], "only users"); | ||
_; | ||
} | ||
|
||
function challengeExit( | ||
uint256[2] memory _balances, | ||
uint256 _nonce, | ||
bytes[2] memory _signatures | ||
) public onlyUser checkSignatures(_signatures, _balances, _nonce) checkBalances(_balances) { | ||
require(block.timestamp < expiresAt, "challenge period expired"); | ||
require(_nonce > nonce, "nonce must be greater than the current nonce"); | ||
|
||
for (uint256 i = 0; i < _balances.length; i++) { | ||
balances[users[i]] = _balances[i]; | ||
} | ||
|
||
nonce = _nonce; | ||
expiresAt = block.timestamp + challengeTime; | ||
|
||
emit ChallengedExit(msg.sender, nonce); | ||
} | ||
|
||
function withdraw() public onlyUser { | ||
require(block.timestamp >= expiresAt, "Challenge period not expired yet"); | ||
|
||
uint256 amount = balances[msg.sender]; | ||
balances[msg.sender] = 0; | ||
|
||
payable(msg.sender).transfer(amount); | ||
|
||
emit Withdrawn(msg.sender, amount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { config as dotenvConfig } from "dotenv"; | ||
import "@typechain/hardhat"; | ||
import "@nomiclabs/hardhat-ethers"; | ||
import "@nomiclabs/hardhat-waffle"; | ||
import "@nomiclabs/hardhat-etherscan"; | ||
import "solidity-coverage"; | ||
import "hardhat-gas-reporter"; | ||
import { HardhatUserConfig } from "hardhat/types/config"; | ||
|
||
dotenvConfig(); | ||
|
||
const gasPrice = parseInt(process.env.GAS_PRICE || "1000000000"); | ||
|
||
const config: HardhatUserConfig = { | ||
solidity: { | ||
compilers: [ | ||
{ | ||
version: "0.8.10", | ||
settings: { | ||
optimizer: { | ||
enabled: true, | ||
runs: 200, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
networks: { | ||
hardhat: { | ||
initialBaseFeePerGas: 0, | ||
}, | ||
rinkeby: { | ||
url: process.env.RINKEBY_URL || "", | ||
accounts: | ||
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], | ||
gasPrice, | ||
}, | ||
mainnet: { | ||
url: process.env.MAINNET_URL || "", | ||
accounts: | ||
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], | ||
gasPrice, | ||
}, | ||
}, | ||
gasReporter: { | ||
enabled: process.env.REPORT_GAS !== undefined, | ||
currency: "USD", | ||
gasPrice: 120, | ||
coinmarketcap: process.env.COINMARKETCAP_API_KEY, | ||
}, | ||
etherscan: { | ||
apiKey: process.env.ETHERSCAN_API_KEY, | ||
}, | ||
}; | ||
|
||
export default config; |
Oops, something went wrong.