Skip to content
Lattice

ERC7739Lib

Git Source

Title: ERC7739Lib

Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/draft-ERC7739Utils.sol)

Cross-checked against Solady src/accounts/ERC1271.sol for the wire layout.

Pure ERC-7739 ("defensive rehashing") helpers: nested EIP-712 (TypedDataSign) and PersonalSign struct hashing + wire decoding. No state, no assembly. Consumed by ERC1271SignatureLib.

These rebind an incoming signature to a specific EIP-712 domain so a signature valid for one account cannot be replayed against another account that shares the same owner key.

Constants

PERSONAL_SIGN_TYPEHASH

keccak256("PersonalSign(bytes prefixed)").

bytes32 internal constant PERSONAL_SIGN_TYPEHASH =
        0x983e65e5148e570cd828ead231ee759a8d7958721a768f93bc4483ba005c32de;

Functions

personalSignStructHash

keccak256(abi.encode(PERSONAL_SIGN_TYPEHASH, contents)).

function personalSignStructHash(bytes32 contents) internal pure returns (bytes32);

Parameters

NameTypeDescription
contentsbytes32

Returns

NameTypeDescription
<none>bytes32

typedDataSignTypehash

TypedDataSign(contentsName contents,string name,string version,uint256 chainId,address verifyingContract,bytes32 salt)contentsType``.

function typedDataSignTypehash(string calldata contentsName, string calldata contentsType)
        internal
        pure
        returns (bytes32);

Parameters

NameTypeDescription
contentsNamestring
contentsTypestring

Returns

NameTypeDescription
<none>bytes32

typedDataSignStructHash

keccak256(typehash || contentsHash || domainBytes); an empty contents name yields bytes32(0), which can never match a real outer hash (so a malformed descriptor fails closed).

function typedDataSignStructHash(
        string calldata contentsName,
        string calldata contentsType,
        bytes32 contentsHash,
        bytes memory domainBytes
    ) internal pure returns (bytes32);

Parameters

NameTypeDescription
contentsNamestring
contentsTypestring
contentsHashbytes32
domainBytesbytes

Returns

NameTypeDescription
<none>bytes32

typedDataSignStructHash

Overload that splits contentsDescr into name + type first.

function typedDataSignStructHash(string calldata contentsDescr, bytes32 contentsHash, bytes memory domainBytes)
        internal
        pure
        returns (bytes32);

Parameters

NameTypeDescription
contentsDescrstring
contentsHashbytes32
domainBytesbytes

Returns

NameTypeDescription
<none>bytes32

decodeTypedDataSig

Layout: signature ‖ appSeparator(32) ‖ contentsHash(32) ‖ contentsDescr(N) ‖ uint16(N). A too-short / inconsistent envelope decodes to empty values (which then fail validation).

function decodeTypedDataSig(bytes calldata encodedSignature)
        internal
        pure
        returns (bytes calldata signature, bytes32 appSeparator, bytes32 contentsHash, string calldata contentsDescr);

Parameters

NameTypeDescription
encodedSignaturebytes

Returns

NameTypeDescription
signaturebytes
appSeparatorbytes32
contentsHashbytes32
contentsDescrstring

decodeContentsDescr

Splits a contents descriptor into (contentsName, contentsType).

  • Implicit form (descriptor ends with )): type == descriptor, name = prefix up to the first (.
  • Explicit form: the name is appended after the type's closing ). Returns empty strings on any malformed descriptor (which then fails closed).
function decodeContentsDescr(string calldata contentsDescr)
        internal
        pure
        returns (string calldata contentsName, string calldata contentsType);

Parameters

NameTypeDescription
contentsDescrstring

Returns

NameTypeDescription
contentsNamestring
contentsTypestring

_isForbiddenChar

function _isForbiddenChar(bytes1 c) private pure returns (bool);

Parameters

NameTypeDescription
cbytes1

Returns

NameTypeDescription
<none>bool

_emptyBytes

function _emptyBytes() private pure returns (bytes calldata r);

Returns

NameTypeDescription
rbytes

_emptyString

function _emptyString() private pure returns (string calldata r);

Returns

NameTypeDescription
rstring