PHPackages                             ironchoi/eip712-hash-sign - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ironchoi/eip712-hash-sign

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ironchoi/eip712-hash-sign
=========================

Generate Eip712 Hashes for signing

v1.0(2y ago)029MITPHP

Since Apr 26Pushed 2y agoCompare

[ Source](https://github.com/ironchoi/eip712-hash-sign)[ Packagist](https://packagist.org/packages/ironchoi/eip712-hash-sign)[ RSS](/packages/ironchoi-eip712-hash-sign/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (7)Versions (3)Used By (0)

`eip-712`
=========

[](#eip-712)

[![PHP](https://github.com/ironchoi/eip712-hash-sign/actions/workflows/tests.yml/badge.svg)](https://github.com/ironchoi/eip712-hash-sign/actions/workflows/php.yml)[![codecov](https://camo.githubusercontent.com/b2b8a1197c5b63f264bb66939301b38679e48b452c7cc8d0305ac287631ea10b/68747470733a2f2f636f6465636f762e696f2f67682f69726f6e63686f692f6569703731322d686173682d7369676e2f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d43504a4348585a544e32)](https://codecov.io/gh/ironchoi/eip712-hash-sign)[![Licensed under the MIT License](https://camo.githubusercontent.com/08cef40a9105b6526ca22088bc514fbfdbc9aac1ddbf8d4e6c750e3a88a44dca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d626c75652e737667)](https://github.com/ironchoi/eip712-hash-sign/blob/master/LICENSE)

This is a library laravel / php to help generata an EIP712 Hash for signing and verifying [EIP-712](https://eips.ethereum.org/EIPS/eip-712) based messages. It is fully written for php 8.0, and is currently only compatible with the latest specification of EIP-712 ([eth\_signTypedData\_v4](https://docs.metamask.io/guide/signing-data.html#sign-typed-data-v4)).

Note that this library currently does not handle the signing itself. For this, you can use something like `kornrunner\Secp256k1`. For some examples please see below.

Installation
------------

[](#installation)

```
$ composer require ironchoi/eip712-hash-sign
```

### Getting Started

[](#getting-started)

First, define your typed data as a JSON string or PHP array, according to the JSON schema specified by EIP-712. For example:

```
{
	"types": {
		"EIP712Domain": [
			{ "name": "name", "type": "string" },
			{ "name": "version", "type": "string" },
			{ "name": "chainId", "type": "uint256" },
			{ "name": "verifyingContract", "type": "address" }
		],
		"Person": [
			{ "name": "name", "type": "string" },
			{ "name": "wallet", "type": "address" }
		],
		"Mail": [
			{ "name": "from", "type": "Person" },
			{ "name": "to", "type": "Person" },
			{ "name": "contents", "type": "string" }
		]
	},
	"primaryType": "Mail",
	"domain": {
		"name": "Ether Mail",
		"version": "1",
		"chainId": 1,
		"verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
	},
	"message": {
		"from": {
			"name": "Cow",
			"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
		},
		"to": {
			"name": "Bob",
			"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
		},
		"contents": "Hello, Bob!"
	}
}
```

### Example

[](#example)

```
use SleepFinance\Eip712;
use kornrunner\Secp256k1;

// import the EIP-712 json fil;
$mailTypedJson = file_get_contents('path/to/your-json-file.json');

$eip712 = new Eip712($mailTypedJson);

$hashToSign = $eip712->hashTypedDataV4();

//signing with account 0xf3022686aa43B98362c989659561b9B348977897
$pvk="0x2870b52bfe2401ac0eed7f62fd4bd03eb579c61369c6b4dd6931fb4a57d71b09";

$secp256k1 = new Secp256k1();

$signed   = $secp256k1->sign($hashToSign, $pvk);

//Hex
$signatureToSubmit = $signed->toHex();
```

It allows arrays

```
use SleepFinance\Eip712;
use kornrunner\Secp256k1;

$mailTypedData = [
    "types" => [
        "EIP712Domain" => [
            [
                "name" => "name",
                "type" => "string"
            ],
            [
                "name" => "version",
                "type" => "string"
            ],
            [
                "name" => "chainId",
                "type" => "uint256"
            ],
            [
                "name" => "verifyingContract",
                "type" => "address"
            ]
        ],
        "Person" => [
            [
                "name" => "name",
                "type" => "string"
            ],
            [
                "name" => "wallet",
                "type" => "address"
            ]
        ],
        "Mail" => [
            [
                "name" => "from",
                "type" => "Person"
            ],
            [
                "name" => "to",
                "type" => "Person"
            ],
            [
                "name" => "contents",
                "type" => "string"
            ]
        ]
    ],
    "primaryType" => "Mail",
    "domain" => [
        "name" => "Ether Mail",
        "version" => "1",
        "chainId" => 1,
        "verifyingContract" => "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
    ],
    "message" => [
        "from" => [
            "name" => "Cow",
            "wallet" => "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
        ],
        "to" => [
            "name" => "Bob",
            "wallet" => "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
        ],
        "contents" => "Hello, Bob!"
    ]
];
$eip712 = new Eip712($mailTypedData);

$hashToSign = $eip712->hashTypedDataV4();

//signing with account 0xf3022686aa43B98362c989659561b9B348977897
$pvk="0x2870b52bfe2401ac0eed7f62fd4bd03eb579c61369c6b4dd6931fb4a57d71b09";

$secp256k1 = new Secp256k1();

$signed   = $secp256k1->sign($hashToSign, $pvk);

//Hex
$signatureToSubmit = $signed->toHex();
```

### You many need to recompose signature!!

[](#you-many-need-to-recompose-signature)

Sometimes ` $signatureToSubmit = $signed->toHex();` hex doesnt work. You may need to recompose signature

```
$signed   = $secp256k1->sign($hashToSign, $pvk);

//$signatureToSubmit = $signed->toHex();

$r   = $this->hexup(gmp_strval($signed->getR(), 16));

$s   = $this->hexup(gmp_strval($signed->getS(), 16));

$v   = dechex((int) $signed->getRecoveryParam() + 27);

$signatureToSubmit = "0x$r$s$v";

----

function hexup(string $value): string
{
    return strlen($value) % 2 === 0 ? $value : "0{$value}";
}
```

### Encoder Functions

[](#encoder-functions)

Here is a brief description of the functions available in the encoder. For more detailed examples, you can refer to [`src/tests`](https://github.com/ironchoi/eip712-hash-sign/blob/master/tests).

#### `Encoder::encode(SleepFinance\Eip712 $typedData)`

[](#encoderencodesleepfinanceeip712-typeddata)

This function will return the full EIP-191 encoded message to be signed hashed using Keccak256.

```
use SleepFinance\Encoder;

$mailTypedJson = file_get_contents('path/to/your-json-file.json');

$eip712 = new Eip712($mailTypedJson);

$hashToSign = Encoder::encode($eip712);

dump($hashToSign);
//be609aee343fb3c4b28e1df9e632fca64fcfaede20f02e86244efddf30957bd2
```

#### `Encoder::getStructHash(SleepFinance\Eip712 $typedData, $type, $data)`

[](#encodergetstructhashsleepfinanceeip712-typeddata-type-data)

This function returns a Keccak-256 hash for a single struct type (e.g. EIP712Domain, Person or Mail).

```
use SleepFinance\Encoder;

$mailTypedJson = file_get_contents('path/to/your-json-file.json');

$eip712 = new Eip712($mailTypedJson);

$hash = Encoder::getStructHash($eip712, "EIP712Domain", $eip712->domain);

dump($hash);
 // f2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f
```

#### `Encoder::encodeData(Eip712 $typedData, string $type, data)`

[](#encoderencodedataeip712-typeddata-string-type-data)

This function returns the raw ABI encoded data for the struct type.

```
use SleepFinance\Encoder;

$mailTypedJson = file_get_contents('path/to/your-json-file.json');

$eip712 = new Eip712($mailTypedJson);

$abiEncodedData = Encoder::encodeData($eip712, "EIP712Domain", $eip712->domain);

dump($abiEncodedData);
 // 8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400fc70ef06638535b4881fafcac8287e210e3769ff1a8e91f1b95d6246e61e4d3c6c89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc60000000000000000000000000000000000000000000000000000000000000001000000000000000000000000cccccccccccccccccccccccccccccccccccccccc
```

#### `Encoder::getTypeHash(Eip712 $typedData, string $type)`

[](#encodergettypehasheip712-typeddata-string-type)

This function returns the type hash for a struct type. This is the same as `Keccak256(EIP712Domain(string name,string version,uint256 chainId,address verifyingContract))`, with support optional sub-types automatically included too.

```
use SleepFinance\Encoder;

$mailTypedJson = file_get_contents('path/to/your-json-file.json');

$eip712 = new Eip712($mailTypedJson);

$typeHash = Encoder::getTypeHash($eip712, "EIP712Domain");

dump($typeHash);
 // 8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f
```

#### `Encoder::encodeType(Eip712 $typedData, string $type)`

[](#encoderencodetypeeip712-typeddata-string-type)

This function returns the type string before hashing it, e.g. `EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)`, with optional sub-types automatically included too.

```
use SleepFinance\Encoder;

$mailTypedJson = file_get_contents('path/to/your-json-file.json');

$eip712 = new Eip712($mailTypedJson);

$encodedType = Encoder::encodeType($eip712, "EIP712Domain");

dump($encodedType);
 // EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)
```

### Non-standard domains are currently not tested!

[](#non-standard-domains-are-currently-not-tested)

It's possible to use a custom domain format, like from the CIP-23 specification, if you want to use a custom implementation of EIP-712.

To do this, Intialize EIP172 with your custom domain; schema validation will be skipped!

```
$eip712 = new Eip712($mailTypedJson, 'MyCustomDomain');
```

Telegram

see you on the flipside!
------------------------

[](#see-you-on-the-flipside)

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 60% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

746d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4b633967344a6e9c06d0602395ed4fccd6a7a7ae9044786a8f4754d66ef3aa3b?d=identicon)[caitiemin](/maintainers/caitiemin)

---

Top Contributors

[![ofumbi](https://avatars.githubusercontent.com/u/4081256?v=4)](https://github.com/ofumbi "ofumbi (15 commits)")[![ironchoi](https://avatars.githubusercontent.com/u/23722737?v=4)](https://github.com/ironchoi "ironchoi (9 commits)")[![sleepfinance](https://avatars.githubusercontent.com/u/122859883?v=4)](https://github.com/sleepfinance "sleepfinance (1 commits)")

### Embed Badge

![Health badge](/badges/ironchoi-eip712-hash-sign/health.svg)

```
[![Health](https://phpackages.com/badges/ironchoi-eip712-hash-sign/health.svg)](https://phpackages.com/packages/ironchoi-eip712-hash-sign)
```

###  Alternatives

[illuminate/events

The Illuminate Events package.

13454.3M1.8k](/packages/illuminate-events)[illuminate/session

The Illuminate Session package.

9937.4M753](/packages/illuminate-session)[illuminate/pagination

The Illuminate Pagination package.

10532.5M862](/packages/illuminate-pagination)[illuminate/broadcasting

The Illuminate Broadcasting package.

7126.5M178](/packages/illuminate-broadcasting)[illuminate/redis

The Illuminate Redis package.

8314.0M314](/packages/illuminate-redis)[illuminate/cookie

The Illuminate Cookie package.

224.3M122](/packages/illuminate-cookie)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
