npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eip712-helpers

v1.0.2

Published

Helpers to implement EIP-712

Downloads

9

Readme

Summary

Helpers to implement EIP-712. Including js helpers for creating EIP-712 signatures and a solidity contract for validating EIP-712 signatures.

EIP-712 is a standard for Ethereum typed structured data hashing and signing. For more information, read the EIP-712

Usages

Step 1 - define message types

You should list all types that are used for signing the signature. And you should also pick one of them as the primary type where your message is defined.

For example:

const DEMO_TYPES = {
    EIP712Demo: [
        {
            type: "address",
            name: "whose",
        },
        {
            type: "Container",
            name: "container",
        },
    ],

    Container: [
        {
            type: "int256",
            name: "val"
        }
    ]
}

In this case, EIP712Demo is the primary type.

Step 2 - create the message hash and signature

Import this helper library as EIP712.

Create the type data using the createTypeData helper function:

const typedData = EIP712.createTypeData(
    DEMO_TYPES,
    "EIP712Demo",
    new EIP712.DomainData(
        "EIP712Demo.Set", // domain name
        "v1", // domain version
        chainId,
        demo.address,
        "0xb225c57bf2111d6955b97ef0f55525b5a400dc909a5506e34b102e193dd53406"
    ), {
        whose: accounts[1],
        container: {
            val: 42
        }
    });

Sign the typed data signature with the signTypedData helper function:

const sig = await EIP712.signTypedData(web3, accounts[1], typedData);
// You should now use: sig.v, sig.r, sig.s,

Step 3 - validate the message signature in solidity

Import EIP712 from the EIP712.sol

Build domain separator once in the constructor:

    // EIP712 domain separtor
    bytes32 private constant DEMO_DOMAIN_SALT = 0xb225c57bf2111d6955b97ef0f55525b5a400dc909a5506e34b102e193dd53406;
    bytes32 private constant DEMO_DOMAIN_NAME_HASH = keccak256("EIP712Demo.Set");
    bytes32 private constant DEMO_DOMAIN_VERSION_HASH = keccak256("v1");
    bytes32 private DEMO_DOMAIN_SEPARATOR;

    constructor(uint256 chainId) public {
        DEMO_DOMAIN_SEPARATOR = EIP712.buildDomainSeparator(
            DEMO_DOMAIN_NAME_HASH,
            DEMO_DOMAIN_VERSION_HASH,
            chainId,
            address(this),
            DEMO_DOMAIN_SALT);
    }

Note that the domain name, the domain version, the salt have to match the ones you used in the javascript.

Define types hashes:

// EIP712 type definitions
bytes32 private constant CONTAINER_TYPE_HASH = keccak256("Container(int256 val)");
bytes32 private constant DEMO_TYPE_HASH = keccak256("EIP712Demo(address whose,Container container)Container(int256 val)");

Note that as a compound type, EIP712Demo has to include its composite types according to the standard: "If the struct type references other struct types (and these in turn reference even more struct types), then the set of referenced struct types is collected, sorted by name and appended to the encoding. An example encoding is Transaction(Person from,Person to,Asset tx)Asset(address token,uint256 amount)Person(address wallet,string name)."

Create message hashes and validate the signature:

bytes32 containerHash =  keccak256(abi.encode(
    CONTAINER_TYPE_HASH,
    val));
bytes32 demoHash =  keccak256(abi.encode(
    DEMO_TYPE_HASH,
    whose,
    containerHash));
require(EIP712.validateMessageSignature(DEMO_DOMAIN_SEPARATOR, demoHash, v, r, s, whose), "Invalid signature");

Example

Checkout the test code for an actual example: test/eip712.test.js