From f0836e5e91fbf77fb46e4f418e24b42b65e2353c Mon Sep 17 00:00:00 2001 From: Igor Line Date: Fri, 24 Mar 2023 00:03:58 +0100 Subject: [PATCH] wip: advertiser sig add --- remappings.txt | 2 +- src/crosschainAdvertiser/l1/Forwarder.sol | 6 ++-- src/crosschainAdvertiser/l2/L2Campaign.sol | 33 ++++++++++++++++++++-- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/remappings.txt b/remappings.txt index 298019e..740d4fe 100644 --- a/remappings.txt +++ b/remappings.txt @@ -1,5 +1,5 @@ ds-test/=lib/forge-std/lib/ds-test/src/ forge-std/=lib/forge-std/src/ interfaces/=lib/interfaces/ -openzeppelin-contracts/=lib/openzeppelin-contracts/ +@openzeppelin/=lib/openzeppelin-contracts/ solmate/=lib/solmate/src/ diff --git a/src/crosschainAdvertiser/l1/Forwarder.sol b/src/crosschainAdvertiser/l1/Forwarder.sol index 4ad1ea4..63ebc3f 100644 --- a/src/crosschainAdvertiser/l1/Forwarder.sol +++ b/src/crosschainAdvertiser/l1/Forwarder.sol @@ -32,7 +32,9 @@ contract AdForwarder { // The domain the campain contract is deployed uint32 destinationDomain, // The relayerFee that needs to be paid to the connext relayer - uint256 relayerFee + uint256 relayerFee, + // Signature of the advertiser + bytes32 signature ) external payable { (bool success, bytes memory result) = target.call{value: msg.value - relayerFee}(_calldata); require(success, "tx failed"); @@ -52,7 +54,7 @@ contract AdForwarder { msg.sender, // _delegate: address that can revert or forceLocal on destination 0, // _amount: 0 because no funds are being transferred 0, // _slippage: can be anything between 0-10000 because no funds are being transferred - abi.encode(adHash, advertiser) // _callData: the encoded calldata to send + abi.encode(adHash, advertiser, signature) // _callData: the encoded calldata to send ); // getting selector from calldata diff --git a/src/crosschainAdvertiser/l2/L2Campaign.sol b/src/crosschainAdvertiser/l2/L2Campaign.sol index cebd267..5b25037 100644 --- a/src/crosschainAdvertiser/l2/L2Campaign.sol +++ b/src/crosschainAdvertiser/l2/L2Campaign.sol @@ -1,6 +1,7 @@ pragma solidity ^0.8.15; import {getAdHash} from "../AdHash.sol"; +import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; contract L2Campaign { // The amount an advertiser will receive for a successful ad @@ -18,9 +19,14 @@ contract L2Campaign { // Deadline after which the owner can withdraw the funds uint256 public immutable deadline; + mapping (address advertiser => bytes32 agreement) public agreements; + mapping(bytes32 => address) claims; + uint public claimableRewards; + using ECDSA for bytes32; + constructor( uint256 _commission, address _target, @@ -71,14 +77,37 @@ contract L2Campaign { uint32 _origin, bytes memory _callData ) external onlySource(_originSender, _origin) returns (bytes memory) { - (bytes32 adHash, address advertiser) = abi.decode( + (bytes32 adHash, address advertiser, bytes32 signature) = abi.decode( _callData, - (bytes32, address) + (bytes32, address, bytes32) + ); + // TODO: Will relayer keep retrying if this fails? + // What if tx will never succeed? + require( + agreements[advertiser] == signature, + "Advertiser did not apply for this campaign" ); claims[adHash] = advertiser; claimableRewards += commission; } + function applyTo(bytes memory signature) external payable { + require( + msg.value >= commission, + "Payment cannot be less than commission" + ); + // TODO: return extra funds + require( + agreements[msg.sender] == 0x0, + "Advertiser already applied for this campaign" + ); + require( + keccak256(abi.encodePacked(target, msg.sender)).toEthSignedMessageHash().recover(signature) == msg.sender, + "Signature is not valid" + ); + agreements[msg.sender] = abi.encodePacked(signature); + } + function withdraw() external onlyOwner onlyAfter(deadline) { require( address(this).balance > claimableRewards,