Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

Commit

Permalink
EIP712 Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
boringcrypto committed Mar 16, 2021
1 parent 2438e4e commit 293b476
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 23 deletions.
6 changes: 4 additions & 2 deletions contracts/MasterContractManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ contract MasterContractManager is BoringOwnable, BoringFactory {
assembly {
chainId := chainid()
}
DOMAIN_SEPARATOR = keccak256(abi.encode(DOMAIN_SEPARATOR_SIGNATURE_HASH, "BentoBox V2", chainId, address(this)));
DOMAIN_SEPARATOR = keccak256(abi.encode(DOMAIN_SEPARATOR_SIGNATURE_HASH, keccak256("BentoBox V1"), chainId, address(this)));
}

/// @notice Other contracts need to register with this master contract so that users can approve them for the BentoBox.
Expand Down Expand Up @@ -97,7 +97,9 @@ contract MasterContractManager is BoringOwnable, BoringFactory {
keccak256(
abi.encode(
APPROVAL_SIGNATURE_HASH,
approved ? "Give FULL access to funds in (and approved to) BentoBox?" : "Revoke access to BentoBox?",
approved
? keccak256("Give FULL access to funds in (and approved to) BentoBox?")
: keccak256("Revoke access to BentoBox?"),
user,
masterContract,
approved,
Expand Down
6 changes: 4 additions & 2 deletions contracts/flat/BentoBoxFlat.sol
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ contract MasterContractManager is BoringOwnable, BoringFactory {
assembly {
chainId := chainid()
}
DOMAIN_SEPARATOR = keccak256(abi.encode(DOMAIN_SEPARATOR_SIGNATURE_HASH, "BentoBox V2", chainId, address(this)));
DOMAIN_SEPARATOR = keccak256(abi.encode(DOMAIN_SEPARATOR_SIGNATURE_HASH, keccak256("BentoBox V1"), chainId, address(this)));
}

function registerProtocol() public {
Expand Down Expand Up @@ -496,7 +496,9 @@ contract MasterContractManager is BoringOwnable, BoringFactory {
keccak256(
abi.encode(
APPROVAL_SIGNATURE_HASH,
approved ? "Give FULL access to funds in (and approved to) BentoBox?" : "Revoke access to BentoBox?",
approved
? keccak256("Give FULL access to funds in (and approved to) BentoBox?")
: keccak256("Revoke access to BentoBox?"),
user,
masterContract,
approved,
Expand Down
47 changes: 36 additions & 11 deletions contracts/samples/salary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ contract Salary is BoringBatchable {
BentoBox public bentoBox;

event LogCreate(
address indexed funder, address indexed recipient,
address indexed funder,
address indexed recipient,
IERC20 indexed token,
uint32 cliffTimestamp, uint32 endTimestamp, uint32 cliffPercent, uint128 totalShares, uint256 salaryId
uint32 cliffTimestamp,
uint32 endTimestamp,
uint32 cliffPercent,
uint128 totalShares,
uint256 salaryId
);
event LogWithdraw(uint256 indexed salaryId, address indexed to, uint256 shares);
event LogCancel(uint256 indexed salaryId, address indexed to, uint256 shares);
Expand All @@ -27,10 +32,16 @@ contract Salary is BoringBatchable {
}

// Included to be able to approve BentoBox and create in the same transaction (using batch)
function setBentoBoxApproval(address user, bool approved, uint8 v, bytes32 r, bytes32 s) public {
bentoBox.setMasterContractApproval(user, address(this), approved, v, r, s);
function setBentoBoxApproval(
address user,
bool approved,
uint8 v,
bytes32 r,
bytes32 s
) public {
bentoBox.setMasterContractApproval(user, address(this), approved, v, r, s);
}

/// now cliffTimestamp
/// | | endTimestamp
/// V V |
Expand Down Expand Up @@ -67,13 +78,19 @@ contract Salary is BoringBatchable {
/// The funder of each salary, separated out for gas optimization
address[] public funder;

uint8 private constant MODE_BENTO = 0; // Use BentoBox balance
uint8 private constant MODE_ERC20_SKIM = 1; // Use ERC20 tokens deposited onto the BentoBox contract
uint8 private constant MODE_ERC20 = 2; // Use ERC20 tokens in the users wallet (transferFrom with approval)
uint8 private constant MODE_BENTO = 0; // Use BentoBox balance
uint8 private constant MODE_ERC20_SKIM = 1; // Use ERC20 tokens deposited onto the BentoBox contract
uint8 private constant MODE_ERC20 = 2; // Use ERC20 tokens in the users wallet (transferFrom with approval)

/// Create a salary
function create(
address recipient, IERC20 token, uint32 cliffTimestamp, uint32 endTimestamp, uint32 cliffPercent, uint8 mode, uint128 amount
address recipient,
IERC20 token,
uint32 cliffTimestamp,
uint32 endTimestamp,
uint32 cliffPercent,
uint8 mode,
uint128 amount
) public returns (uint256 salaryId, uint256 shares) {
// Check that the end if after or equal to the cliff
// If they are equal, all shares become payable at once, use this for a fixed term lockup
Expand Down Expand Up @@ -145,7 +162,11 @@ contract Salary is BoringBatchable {
}

// Withdraw the maximum amount possible for a salaryId
function withdraw(uint256 salaryId, address to, bool toBentoBox) public {
function withdraw(
uint256 salaryId,
address to,
bool toBentoBox
) public {
UserSalary memory salary = salaries[salaryId];
// Only pay out to the recipient
require(salary.recipient == msg.sender, "Salary: not recipient");
Expand All @@ -167,7 +188,11 @@ contract Salary is BoringBatchable {
}

// Cancel a salary, can only be done by the funder
function cancel(uint256 salaryId, address to, bool toBentoBox) public onlyFunder(salaryId) {
function cancel(
uint256 salaryId,
address to,
bool toBentoBox
) public onlyFunder(salaryId) {
uint256 sharesLeft = uint256(salaries[salaryId].shares).sub(salaries[salaryId].withdrawnShares);
if (toBentoBox) {
bentoBox.transfer(salaries[salaryId].token, address(this), to, sharesLeft);
Expand Down
15 changes: 11 additions & 4 deletions test/utilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,26 @@ function getApprovalMsg(tokenAddress, approve, nonce, deadline) {
function getBentoBoxDomainSeparator(address, chainId) {
return keccak256(
defaultAbiCoder.encode(
["bytes32", "string", "uint256", "address"],
[keccak256(toUtf8Bytes("EIP712Domain(string name,uint256 chainId,address verifyingContract)")), "BentoBox V2", chainId, address]
["bytes32", "bytes32", "uint256", "address"],
[
keccak256(toUtf8Bytes("EIP712Domain(string name,uint256 chainId,address verifyingContract)")),
keccak256(toUtf8Bytes("BentoBox V1")),
chainId,
address,
]
)
)
}

function getBentoBoxApprovalDigest(bentoBox, user, masterContractAddress, approved, nonce, chainId = 1) {
const DOMAIN_SEPARATOR = getBentoBoxDomainSeparator(bentoBox.address, chainId)
const msg = defaultAbiCoder.encode(
["bytes32", "string", "address", "address", "bool", "uint256"],
["bytes32", "bytes32", "address", "address", "bool", "uint256"],
[
BENTOBOX_MASTER_APPROVAL_TYPEHASH,
approved ? "Give FULL access to funds in (and approved to) BentoBox?" : "Revoke access to BentoBox?",
approved
? keccak256(toUtf8Bytes("Give FULL access to funds in (and approved to) BentoBox?"))
: keccak256(toUtf8Bytes("Revoke access to BentoBox?")),
user.address,
masterContractAddress,
approved,
Expand Down
10 changes: 6 additions & 4 deletions test/utilities/lendingpair.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const BENTOBOX_MASTER_APPROVAL_TYPEHASH = keccak256(
function getBentoBoxDomainSeparator(address, chainId) {
return keccak256(
defaultAbiCoder.encode(
["bytes32", "string", "uint256", "address"],
["bytes32", "bytes32", "uint256", "address"],
[
keccak256(ethers.utils.toUtf8Bytes("EIP712Domain(string name,uint256 chainId,address verifyingContract)")),
"BentoBox V2",
keccak256(ethers.utils.toUtf8Bytes("BentoBox V1")),
chainId,
address,
]
Expand All @@ -32,10 +32,12 @@ function getBentoBoxDomainSeparator(address, chainId) {
function getBentoBoxApprovalDigest(bentoBox, user, masterContractAddress, approved, nonce, chainId = 1) {
const DOMAIN_SEPARATOR = getBentoBoxDomainSeparator(bentoBox.address, chainId)
const msg = defaultAbiCoder.encode(
["bytes32", "string", "address", "address", "bool", "uint256"],
["bytes32", "bytes32", "address", "address", "bool", "uint256"],
[
BENTOBOX_MASTER_APPROVAL_TYPEHASH,
approved ? "Give FULL access to funds in (and approved to) BentoBox?" : "Revoke access to BentoBox?",
approved
? keccak256(ethers.utils.toUtf8Bytes("Give FULL access to funds in (and approved to) BentoBox?"))
: keccak256(ethers.utils.toUtf8Bytes("Revoke access to BentoBox?")),
user.address,
masterContractAddress,
approved,
Expand Down

0 comments on commit 293b476

Please sign in to comment.