-
Notifications
You must be signed in to change notification settings - Fork 932
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
141 additions
and
6 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
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
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,19 @@ | ||
pragma solidity >=0.5.0 <0.6.0; | ||
|
||
/// @title Handler Context - allows to extract calling context | ||
/// @author Richard Meissner - <[email protected]> | ||
/// @notice based on https://github.com/OpenZeppelin/openzeppelin-contracts/blob/f8cc8b844a9f92f63dc55aa581f7d643a1bc5ac1/contracts/metatx/ERC2771Context.sol | ||
contract HandlerContext { | ||
|
||
// This function does not rely on a trusted forwarder. Use the returned value only to check information against the calling manager. | ||
function _msgSender() internal pure returns (address sender) { | ||
// The assembly code is more direct than the Solidity version using `abi.decode`. | ||
assembly { sender := shr(96, calldataload(sub(calldatasize(), 20))) } | ||
} | ||
|
||
// Function do differentiate more clearly between msg.sender and the calling manager | ||
function _manager() internal view returns (address) { | ||
return msg.sender; | ||
} | ||
|
||
} |
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,7 @@ | ||
pragma solidity >=0.5.0 <0.6.0; | ||
import "../handler/HandlerContext.sol"; | ||
contract TestHandler is HandlerContext { | ||
function dudududu() external returns (address sender, address manager) { | ||
return (_msgSender(), _manager()); | ||
} | ||
} |
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
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,45 @@ | ||
import { expect } from "chai"; | ||
import hre, { deployments, waffle } from "hardhat"; | ||
import "@nomiclabs/hardhat-ethers"; | ||
import { AddressZero } from "@ethersproject/constants"; | ||
import { getSafeTemplate } from "../utils/setup"; | ||
|
||
describe("HandlerContext", async () => { | ||
|
||
const [user1, user2] = waffle.provider.getWallets(); | ||
|
||
const setup = deployments.createFixture(async ({ deployments }) => { | ||
await deployments.fixture(); | ||
const TestHandler = await hre.ethers.getContractFactory("TestHandler"); | ||
const handler = await TestHandler.deploy(); | ||
return { | ||
safe: await getSafeTemplate(), | ||
handler | ||
} | ||
}) | ||
|
||
it('parses information correctly', async () => { | ||
const { handler } = await setup(); | ||
const response = await user1.call({ | ||
to: handler.address, | ||
data: handler.interface.encodeFunctionData("dudududu") + user2.address.slice(2) | ||
}) | ||
expect( | ||
handler.interface.decodeFunctionResult("dudududu", response) | ||
).to.be.deep.eq([user2.address, user1.address]) | ||
}) | ||
|
||
it('works with the Safe', async () => { | ||
const { safe, handler } = await setup(); | ||
await safe.setup([user1.address, user2.address], 1, AddressZero, "0x", handler.address, AddressZero, 0, AddressZero) | ||
|
||
const response = await user1.call({ | ||
to: safe.address, | ||
data: handler.interface.encodeFunctionData("dudududu") | ||
}) | ||
|
||
expect( | ||
handler.interface.decodeFunctionResult("dudududu", response) | ||
).to.be.deep.eq([user1.address, safe.address]) | ||
}) | ||
}) |