Skip to content

Commit

Permalink
differential eip 712 signing test added
Browse files Browse the repository at this point in the history
  • Loading branch information
androolloyd committed Dec 20, 2023
1 parent 25d6810 commit 1831127
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
33 changes: 15 additions & 18 deletions ffi-scripts/test-origination-hash.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Address, hashTypedData, pad, Hex, hexToString } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { decodeAbiParameters } from 'viem'

import { Address, hashTypedData, pad, Hex, hexToString, keccak256 } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { decodeAbiParameters, parseAbiParameters } from "viem";


const types = {
Expand Down Expand Up @@ -44,16 +43,16 @@ const types = {
};


const domain = (verifyingContract: Address, chainId: any) => ({
version: "0" ,
chainId,
verifyingContract: verifyingContract
const domain = (verifyingContract: Address, chainId: number) => ({
version: "0",
chainId: chainId,
verifyingContract
});

type caveatType = [`0x${string}`, `0x${string}`];

const typeDataMessage = (account: Address, accountNonce: string, singleUse: boolean, salt: Hex, deadline: string, caveats: caveatType) => ({
account: account, accountNonce: accountNonce, singleUse: true, salt: salt, deadline: deadline, caveats: caveats
const typeDataMessage = (account: Address, accountNonce: string, singleUse: boolean, salt: Hex, deadline: string, caveats: any) => ({
account: account, accountNonce: parseInt(accountNonce), singleUse: singleUse, salt: salt, deadline: deadline, caveats: caveats[0]
});

//verifying contract
Expand All @@ -69,18 +68,16 @@ const main = async () => {
const [signerKeyRaw, verifyingContract, account, accountNonce, singleUse, salt, deadline, caveatsRaw, chainId] = args;
const signerKey: any = `${signerKeyRaw}`;
// const signer = privateKeyToAccount(signerKey);//anvil account 1
const caveats : unknown = decodeAbiParameters([{ name: 'enforcer', type: 'address' },
{ name: 'data', type: 'bytes' },], caveatsRaw as `0x${string}`);

const hashData : any = {
domain: domain(verifyingContract as Address, parseInt(chainId as Hex).toString()),
const caveats: any = decodeAbiParameters(parseAbiParameters("(address enforcer,bytes data)[]"), caveatsRaw as `0x${string}`);
const hashData: any = {
domain: domain(verifyingContract as Address, parseInt(chainId as Hex)),
types,
primaryType: "Origination",
message: typeDataMessage(account as Address, "0", !!parseInt(singleUse as Hex), pad(salt as Hex, {size: 32}), pad(deadline as Hex, {size: 32}), [] as any)
message: typeDataMessage(account as Address, accountNonce, !!parseInt(singleUse as Hex), pad(salt as Hex, {size: 32}), parseInt(deadline as Hex).toString(), caveats as any)
};
const dataHash = hashTypedData(hashData);

const dataHash: Hex = hashTypedData(hashData);
console.log(dataHash);
}
};

main();
17 changes: 7 additions & 10 deletions src/Starport.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,16 @@ contract Starport is PausableNonReentrant {
address public immutable defaultCustodian;
bytes32 public immutable DEFAULT_CUSTODIAN_CODE_HASH;

bytes32 internal immutable _DOMAIN_SEPARATOR;

// Define the EIP712 domain and typehash constants for generating signatures
// Define the EIP712 domain and typeHash constants for generating signatures
bytes32 public constant EIP_DOMAIN =
keccak256("EIP712Domain(string version,uint256 chainId,address verifyingContract)");
string public constant VERSION = "0";
keccak256("EIP712Domain(" "string version," "uint256 chainId," "address verifyingContract" ")");
bytes32 public constant VERSION = keccak256(bytes("0"));

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

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* STRUCTS */
Expand Down Expand Up @@ -176,7 +174,6 @@ contract Starport is PausableNonReentrant {
}
defaultCustodian = payable(custodian);
DEFAULT_CUSTODIAN_CODE_HASH = defaultCustodianCodeHash;
_DOMAIN_SEPARATOR = keccak256(abi.encode(EIP_DOMAIN, VERSION, block.chainid, address(this)));
_initializeOwner(msg.sender);
}

Expand Down Expand Up @@ -452,7 +449,7 @@ contract Starport is PausableNonReentrant {
* @return The hash.
*/
function _hashCaveat(CaveatEnforcer.Caveat memory caveat) internal view returns (bytes32) {
return keccak256(abi.encode(CAVEAT_TYPEHASH, caveat.enforcer, caveat.data));
return keccak256(abi.encode(CAVEAT_TYPEHASH, caveat.enforcer, keccak256(caveat.data)));
}

/**
Expand Down
13 changes: 4 additions & 9 deletions test/unit-testing/TestStarport.sol
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,15 @@ contract TestStarport is StarportTest, DeepEq {
loan.toStorage(activeLoan);
}

function testEIP721Signing() public {
function testEIP712Signing() public {
CaveatEnforcer.SignedCaveats memory empty = _emptyCaveat();

empty.caveats = new CaveatEnforcer.Caveat[](2);
empty.caveats[0] = CaveatEnforcer.Caveat({enforcer: address(1), data: abi.encode(uint256(1))});
empty.caveats[1] = CaveatEnforcer.Caveat({enforcer: address(2), data: abi.encode(uint256(2))});
bytes32 hashToTest =
SP.hashCaveatWithSaltAndNonce(borrower.addr, empty.singleUse, empty.salt, empty.deadline, empty.caveats);

console2.log("account", borrower.addr);
console2.log("singleUse", empty.singleUse);
console2.log("salt");
console2.logBytes32(empty.salt);
console2.log("deadline", empty.deadline);
console2.log("starport", address(SP));
console2.log("nonce", SP.caveatNonces(borrower.addr));
console2.log("chainId", block.chainid);
string[] memory commands = new string[](11);
commands[0] = "ts-node";
commands[1] = "ffi-scripts/test-origination-hash.ts";
Expand Down

0 comments on commit 1831127

Please sign in to comment.