Skip to content

Commit

Permalink
- update Origination Typehashing to adhere properly to the eip-712 sp…
Browse files Browse the repository at this point in the history
…ecification

- use SafeTransferLib in the custodian
  • Loading branch information
androolloyd committed Dec 14, 2023
1 parent 7b6aa66 commit b0f98d4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/BNPLHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ contract BNPLHelper is IFlashLoanRecipient {
/* CONSTANTS AND IMMUTABLES */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

address private constant vault = address(0xBA12222222228d8Ba445958a75a0704d566BF2C8);
address private constant VAULT = address(0xBA12222222228d8Ba445958a75a0704d566BF2C8);

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STORAGE */
Expand All @@ -109,7 +109,7 @@ contract BNPLHelper is IFlashLoanRecipient {
function makeFlashLoan(address[] calldata tokens, uint256[] calldata amounts, bytes calldata userData) external {
activeUserDataHash = keccak256(userData);

IVault(vault).flashLoan(this, tokens, amounts, userData);
IVault(VAULT).flashLoan(this, tokens, amounts, userData);
}

function receiveFlashLoan(
Expand All @@ -118,7 +118,7 @@ contract BNPLHelper is IFlashLoanRecipient {
uint256[] calldata feeAmounts,
bytes calldata userData
) external override {
if (msg.sender != vault) {
if (msg.sender != VAULT) {
revert SenderNotVault();
}

Expand Down Expand Up @@ -147,7 +147,7 @@ contract BNPLHelper is IFlashLoanRecipient {
identifier: 0,
token: tokens[i],
from: execution.borrower,
to: vault,
to: VAULT,
amount: amounts[i] + feeAmounts[i]
});
unchecked {
Expand Down
5 changes: 3 additions & 2 deletions src/Custodian.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {ERC20} from "solady/src/tokens/ERC20.sol";
import {ERC721} from "solady/src/tokens/ERC721.sol";
import {ERC1155} from "solady/src/tokens/ERC1155.sol";
import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
import {SafeTransferLib} from "solady/src/utils/SafeTransferLib.sol";

contract Custodian is ERC721, ContractOffererInterface {
using {StarportLib.getId} for Starport.Loan;
Expand Down Expand Up @@ -357,7 +358,7 @@ contract Custodian is ERC721, ContractOffererInterface {
} else if (offer.itemType == ItemType.ERC1155) {
ERC1155(offer.token).setApprovalForAll(seaport, true);
} else if (offer.itemType == ItemType.ERC20) {
ERC20(offer.token).approve(seaport, type(uint256).max);
SafeTransferLib.safeApprove(offer.token, seaport, type(uint256).max);
}
}

Expand All @@ -384,7 +385,7 @@ contract Custodian is ERC721, ContractOffererInterface {
} else if (offer.itemType == ItemType.ERC1155) {
ERC1155(offer.token).safeTransferFrom(address(this), authorized, offer.identifier, offer.amount, "");
} else if (offer.itemType == ItemType.ERC20) {
ERC20(offer.token).transfer(authorized, offer.amount);
SafeTransferLib.safeTransfer(offer.token, authorized, offer.amount);
}
}

Expand Down
25 changes: 23 additions & 2 deletions src/Starport.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ contract Starport is PausableNonReentrant {
bytes32 public constant EIP_DOMAIN =
keccak256("EIP712Domain(string version,uint256 chainId,address verifyingContract)");
string public constant VERSION = "0";

bytes32 public constant INTENT_ORIGINATION_TYPEHASH = keccak256(
"Origination(address account,uint256 accountNonce,bool singleUse,bytes32 salt,uint256 deadline,bytes32 caveatHash"
"Origination(address account,uint256 accountNonce,bool singleUse,bytes32 salt,uint256 deadline,Caveat[] caveats)"
);
bytes32 public constant CAVEAT_TYPEHASH = keccak256("Caveat(address enforcer,bytes data)");

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STRUCTS */
Expand Down Expand Up @@ -409,6 +411,14 @@ contract Starport is PausableNonReentrant {
uint256 deadline,
CaveatEnforcer.Caveat[] calldata caveats
) public view virtual returns (bytes32) {
bytes32[] memory caveatHashes = new bytes32[](caveats.length);
uint256 i = 0;
for (; i < caveats.length;) {
caveatHashes[i] = _hashCaveat(caveats[i]);
unchecked {
++i;
}
}
return keccak256(
abi.encodePacked(
bytes1(0x19),
Expand All @@ -422,13 +432,24 @@ contract Starport is PausableNonReentrant {
singleUse,
salt,
deadline,
keccak256(abi.encode(caveats))
keccak256(abi.encodePacked(caveatHashes))
)
)
)
);
}

/**
* @dev Internal view function to derive the EIP-712 hash for a caveat
*
* @param caveat The caveat to hash.
*
* @return The hash.
*/
function _hashCaveat(CaveatEnforcer.Caveat memory caveat) internal view returns (bytes32) {
return keccak256(abi.encode(CAVEAT_TYPEHASH, caveat.enforcer, caveat.data));
}

/**
* @dev Helper to check if a loan is active
* @param loanId The id of the loan
Expand Down
2 changes: 2 additions & 0 deletions src/lib/PausableNonReentrant.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
// ↑↑↑↑
// ↑↑↑↑

pragma solidity ^0.8.17;

import {Ownable} from "solady/src/auth/Ownable.sol";

abstract contract PausableNonReentrant is Ownable {
Expand Down
5 changes: 3 additions & 2 deletions src/originators/StrategistOriginator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ contract StrategistOriginator is Ownable, Originator {
Starport public immutable SP;

// Define the EIP712 domain and typehash constants for generating signatures
bytes32 constant EIP_DOMAIN = keccak256("EIP712Domain(string version,uint256 chainId,address verifyingContract)");
bytes32 public constant EIP_DOMAIN =
keccak256("EIP712Domain(string version,uint256 chainId,address verifyingContract)");
bytes32 public constant ORIGINATOR_DETAILS_TYPEHASH = keccak256("Origination(uint256 nonce,bytes32 hash)");
bytes32 constant VERSION = keccak256("0");
bytes32 public constant VERSION = keccak256("0");
bytes32 internal immutable _DOMAIN_SEPARATOR;

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
Expand Down

0 comments on commit b0f98d4

Please sign in to comment.