Skip to content

Commit

Permalink
simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
netbonus committed May 6, 2024
1 parent 8fcf76a commit 71368b7
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 254 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ node_modules
coverage
.env
*.temp
~/node_modules
~/node_modules
cache
18 changes: 9 additions & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"editor.codeActionsOnSave": {
"source.addMissingImports": "explicit",
"source.organizeImports": "explicit",
"source.formatDocument": "explicit",
"source.fixAll": "explicit"
},
"editor.showUnused": true,
"editor.formatOnPaste": true,
"editor.formatOnSave": true
"editor.codeActionsOnSave": {
"source.addMissingImports": true,
"source.fixAll": true,
"source.formatDocument": true,
"source.organizeImports": true
},
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.showUnused": true
}
12 changes: 0 additions & 12 deletions forge/script/Counter.s.sol

This file was deleted.

14 changes: 0 additions & 14 deletions forge/src/Counter.sol

This file was deleted.

54 changes: 16 additions & 38 deletions forge/src/NegativeAmountHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,30 @@ import "openzeppelin-contracts/contracts/utils/cryptography/EIP712.sol";
import "openzeppelin-contracts/contracts/utils/cryptography/ECDSA.sol";

contract NegativeAmountHandler is EIP712 {
struct Payment {
address to;
int256 amount; // Can be negative to indicate deduction or penalty
uint256 nonce;
}

mapping(address => uint256) public nonces;
string private constant SIGNING_DOMAIN = "NegativeAmountHandler";
string private constant SIGNATURE_VERSION = "1";

// EIP712 Domain Separator initialization in constructor
constructor() EIP712("NegativeAmountHandler", "1") {}
struct Data {
int256 amount;
string message;
}

// Function to handle a negative amount logic
function handlePayment(Payment calldata payment, bytes calldata signature) external {
require(_verify(payment, _hash(payment), signature), "Invalid signature");
// require(payment.amount < 0, "Amount must be negative");

// Logic for handling negative amounts
emit PaymentHandled(payment.to, payment.amount, msg.sender);
constructor() EIP712(SIGNING_DOMAIN, SIGNATURE_VERSION) {}

// Increment nonce to prevent replay attacks
nonces[payment.to]++;
function verify(Data calldata data, bytes calldata signature) public view returns (bool) {
address signer = _verify(_hash(data), signature);
return signer == msg.sender; // Ensure that the signer is the sender of the message
}

// Create a hash of the payment details (EIP712 Typed Data)
function _hash(Payment calldata payment) internal view returns (bytes32) {
function _hash(Data calldata data) internal view returns (bytes32) {
return _hashTypedDataV4(keccak256(abi.encode(
keccak256("Payment(address to,int256 amount,uint256 nonce)"),
payment.to,
payment.amount,
payment.nonce
keccak256("Data(int256 amount,string message)"),
data.amount,
keccak256(bytes(data.message))
)));
}

// Verify the signature
function _verify(Payment calldata payment, bytes32 digest, bytes calldata signature) internal view returns (bool) {
address signer = ECDSA.recover(digest, signature);
return signer == msg.sender && nonces[signer] == payment.nonce;
function _verify(bytes32 digest, bytes memory signature) internal view returns (address) {
return ECDSA.recover(digest, signature);
}

event PaymentHandled(address indexed to, int256 amount, address indexed executor);
}
cast calldata "Payment(address to,int256 amount,uint256 nonce)" \
0x1234567890123456789012345678901234567890 -100 0

cast wallet sign 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 \
--typed-data domain "NegativeAmountHandler" 1.0.0 1 0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9 \
--typed-data Payment "address to" 0x1234567890123456789012345678901234567890 \
--typed-data Payment "int256 amount" -100 \
--typed-data Payment "uint256 nonce" 0
24 changes: 0 additions & 24 deletions forge/test/Counter.t.sol

This file was deleted.

Loading

0 comments on commit 71368b7

Please sign in to comment.