From 1831127727d9c7f87278a993f620bdb902c85b95 Mon Sep 17 00:00:00 2001 From: Andrew Redden Date: Wed, 20 Dec 2023 01:58:46 +0100 Subject: [PATCH] differential eip 712 signing test added --- ffi-scripts/test-origination-hash.ts | 33 +++++++++++++--------------- src/Starport.sol | 17 ++++++-------- test/unit-testing/TestStarport.sol | 13 ++++------- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/ffi-scripts/test-origination-hash.ts b/ffi-scripts/test-origination-hash.ts index 70a1bdc7..50e97082 100644 --- a/ffi-scripts/test-origination-hash.ts +++ b/ffi-scripts/test-origination-hash.ts @@ -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 = { @@ -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 @@ -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(); \ No newline at end of file diff --git a/src/Starport.sol b/src/Starport.sol index 0588f2e1..66c7bc25 100644 --- a/src/Starport.sol +++ b/src/Starport.sol @@ -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 */ @@ -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); } @@ -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))); } /** diff --git a/test/unit-testing/TestStarport.sol b/test/unit-testing/TestStarport.sol index 7b09191d..4e37c6eb 100644 --- a/test/unit-testing/TestStarport.sol +++ b/test/unit-testing/TestStarport.sol @@ -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";