dsa-periphery-contract/forge-test/InstadappAdapter.t.sol

200 lines
6.4 KiB
Solidity
Raw Permalink Normal View History

2023-05-01 05:28:29 +00:00
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {IDSA} from "../contracts/interfaces/IDSA.sol";
import {InstadappAdapter} from "../contracts/InstadappAdapter.sol";
import {TestHelper} from "./utils/TestHelper.sol";
contract MockInstadappReceiver is InstadappAdapter {
constructor() {}
function tryAuthCast(
address dsaAddress,
2023-05-12 00:03:38 +00:00
bytes memory authcastCallData
2023-05-01 05:28:29 +00:00
) external payable {
2023-05-12 00:03:38 +00:00
authCast(dsaAddress, authcastCallData);
2023-05-01 05:28:29 +00:00
}
2023-05-04 07:38:56 +00:00
function tryVerify(
address auth,
bytes memory signature,
CastData memory castData,
bytes32 salt,
uint256 deadline
) external returns (bool) {
return verify(auth, signature, castData, salt, deadline);
}
2023-05-01 05:28:29 +00:00
}
contract InstadappAdapterTest is TestHelper {
// ============ Storage ============
address dsa = address(1);
2023-05-01 05:38:01 +00:00
address instadappReceiver = 0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f;
uint256 deadline = 100;
uint256 timestamp = 90;
2023-05-01 05:28:29 +00:00
// ============ Test set up ============
function setUp() public override {
super.setUp();
2023-05-01 05:38:01 +00:00
MockInstadappReceiver _instadappReceiver = new MockInstadappReceiver();
vm.etch(instadappReceiver, address(_instadappReceiver).code);
2023-05-01 05:28:29 +00:00
}
// ============ Utils ============
function utils_dsaMocks(bool isAuth) public {
vm.mockCall(dsa, abi.encodeWithSelector(IDSA.isAuth.selector), abi.encode(isAuth));
vm.mockCall(dsa, abi.encodeWithSelector(IDSA.cast.selector), abi.encode(bytes32(abi.encode(1))));
}
// ============ InstadappAdapter.authCast ============
function test_InstadappAdapter__authCast_shouldRevertIfInvalidAuth() public {
utils_dsaMocks(false);
address originSender = address(0x123);
string[] memory _targetNames = new string[](2);
_targetNames[0] = "target1";
_targetNames[1] = "target2";
bytes[] memory _datas = new bytes[](2);
_datas[0] = bytes("data1");
_datas[1] = bytes("data2");
address _origin = originSender;
InstadappAdapter.CastData memory castData = InstadappAdapter.CastData(_targetNames, _datas, _origin);
bytes memory signature = bytes("0x111");
address auth = originSender;
bytes32 salt = bytes32(abi.encode(1));
2023-05-12 00:03:38 +00:00
bytes memory authcastCallData = abi.encode(
auth,
signature,
castData,
salt,
deadline
);
2023-05-01 05:28:29 +00:00
vm.expectRevert(bytes("Invalid Auth"));
2023-05-12 00:03:38 +00:00
MockInstadappReceiver(instadappReceiver).tryAuthCast(dsa, authcastCallData);
2023-05-01 05:28:29 +00:00
}
function test_InstadappAdapter__authCast_shouldRevertIfInvalidSignature() public {
utils_dsaMocks(true);
address originSender = address(0x123);
string[] memory _targetNames = new string[](2);
_targetNames[0] = "target1";
_targetNames[1] = "target2";
bytes[] memory _datas = new bytes[](2);
_datas[0] = bytes("data1");
_datas[1] = bytes("data2");
address _origin = originSender;
InstadappAdapter.CastData memory castData = InstadappAdapter.CastData(_targetNames, _datas, _origin);
bytes
memory signature = hex"e91f49cb8bf236eafb590ba328a6ca75f4d189fa51bfce2ac774541801c17d3f2d3df798f18c0520db5a98d33362d507f890d5904c2aea1dd059a9b0f05fb3ad1c";
address auth = originSender;
bytes32 salt = bytes32(abi.encode(1));
vm.expectRevert(bytes("Invalid signature"));
2023-05-12 00:03:38 +00:00
bytes memory authcastCallData = abi.encode(
auth,
signature,
castData,
salt,
deadline
);
MockInstadappReceiver(instadappReceiver).tryAuthCast(dsa, authcastCallData);
2023-05-01 05:28:29 +00:00
}
function test_InstadappAdapter__authCast_shouldWork() public {
utils_dsaMocks(true);
address originSender = address(0xc1aAED5D41a3c3c33B1978EA55916f9A3EE1B397);
string[] memory _targetNames = new string[](3);
_targetNames[0] = "target111";
_targetNames[1] = "target222";
_targetNames[2] = "target333";
bytes[] memory _datas = new bytes[](3);
_datas[0] = bytes("0x111");
_datas[1] = bytes("0x222");
_datas[2] = bytes("0x333");
address _origin = originSender;
bytes32 salt = bytes32(abi.encode(1));
InstadappAdapter.CastData memory castData = InstadappAdapter.CastData(_targetNames, _datas, _origin);
bytes
2023-05-04 08:30:34 +00:00
memory signature = hex"ec163cca0d31ea58537dfff8377fdbd957fb0ba58088f74436af944bf1c3248148910f14a13b7d0b6d707fb7478cfd7f9ae3830b02d0a5e5584ef7648460a8d71c";
2023-05-01 05:28:29 +00:00
address auth = originSender;
2023-05-01 05:38:01 +00:00
vm.warp(timestamp);
2023-05-12 00:03:38 +00:00
bytes memory authcastCallData = abi.encode(
auth,
signature,
castData,
salt,
deadline
);
MockInstadappReceiver(instadappReceiver).tryAuthCast(dsa, authcastCallData);
2023-05-01 05:28:29 +00:00
}
// ============ InstadappAdapter.verify ============
function test_InstadappAdapter__verify_shouldReturnTrue() public {
utils_dsaMocks(true);
address originSender = address(0xc1aAED5D41a3c3c33B1978EA55916f9A3EE1B397);
string[] memory _targetNames = new string[](3);
_targetNames[0] = "target111";
_targetNames[1] = "target222";
_targetNames[2] = "target333";
bytes[] memory _datas = new bytes[](3);
_datas[0] = bytes("0x111");
_datas[1] = bytes("0x222");
_datas[2] = bytes("0x333");
address _origin = originSender;
bytes32 salt = bytes32(abi.encode(1));
InstadappAdapter.CastData memory castData = InstadappAdapter.CastData(_targetNames, _datas, _origin);
bytes
2023-05-04 08:30:34 +00:00
memory signature = hex"ec163cca0d31ea58537dfff8377fdbd957fb0ba58088f74436af944bf1c3248148910f14a13b7d0b6d707fb7478cfd7f9ae3830b02d0a5e5584ef7648460a8d71c";
2023-05-01 05:28:29 +00:00
address auth = originSender;
2023-05-01 05:38:01 +00:00
vm.warp(timestamp);
2023-05-04 07:38:56 +00:00
assertEq(MockInstadappReceiver(instadappReceiver).tryVerify(auth, signature, castData, salt, deadline), true);
2023-05-01 05:28:29 +00:00
}
function test_InstadappAdapter__verify_shouldReturnFalse() public {
utils_dsaMocks(true);
address originSender = address(0x123);
string[] memory _targetNames = new string[](2);
_targetNames[0] = "target1";
_targetNames[1] = "target2";
bytes[] memory _datas = new bytes[](2);
_datas[0] = bytes("data1");
_datas[1] = bytes("data2");
address _origin = originSender;
InstadappAdapter.CastData memory castData = InstadappAdapter.CastData(_targetNames, _datas, _origin);
bytes
memory signature = hex"e91f49cb8bf236eafb590ba328a6ca75f4d189fa51bfce2ac774541801c17d3f2d3df798f18c0520db5a98d33362d507f890d5904c2aea1dd059a9b0f05fb3ad1c";
address auth = originSender;
bytes32 salt = bytes32(abi.encode(1));
2023-05-04 07:38:56 +00:00
assertEq(MockInstadappReceiver(instadappReceiver).tryVerify(auth, signature, castData, salt, deadline), false);
2023-05-01 05:28:29 +00:00
}
}