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

@confluxfans/cip-23

v0.1.0

Published

Tiny library with utility functions that can help with signing and verifying CIP-23 based messages

Downloads

3

Readme

cip-23

Version License Actions Status codecov

This is a library for Node.js and web browsers with some utility functions that can help with signing and verifying EIP-712 based messages. It is fully written in TypeScript, and is currently only compatible with the latest specification of EIP-712 (eth_signTypedData_v4).

https://eips.ethereum.org/EIPS/eip-712

Note that this library currently does not handle the signing itself. For this, you can use something like Ethers.js or ethereumjs-util. For examples, please see the examples folder.

Installation

You can install this library with Yarn or NPM:

$ yarn add cip-23
$ npm install cip-23

There is a CommonJS version as well as an ES6 version available. Most tools should automatically pick the right version (e.g. Node.js, Webpack).

Getting Started

First, define your typed data as a JSON object, according to the JSON schema specified by cip-23. For example:

{
  "types": {
    "CIP23Domain": [
      { "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!"
  }
}

Functions

Here is a brief description of the functions available in this library. For more detailed examples, you can refer to src/cip-23.test.ts, or to the examples in the examples folder.

getMessage(typedData, hash?)

This function will return the full EIP-191 encoded message to be signed as Buffer, for the typed data specified. If hash is enabled, the message will be hashed using Keccak256.

import { getMessage } from 'cip-23';

const typedData = { /*...*/ };
console.log(getMessage(typedData).toString('hex')); // 1901f2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090fc52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e
console.log(getMessage(typedData, true).toString('hex')); // be609aee343fb3c4b28e1df9e632fca64fcfaede20f02e86244efddf30957bd2

asArray(typedData)

This function returns the typed data as an array. This can be useful for encoding typed data as ABI.

import { asArray } from 'cip-23';

const typedData = { /*...*/ };
console.log(asArray(typedData)); // [ ['Cow', '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826'], ['Bob', '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB'], 'Hello, Bob!' ]

getStructHash(typedData, type, data)

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

import { getStructHash } from 'cip-23';

const typedData = { /*...*/ };
console.log(getStructHash(typedData, 'cip-23', typedData.domain).toString('hex')); // f2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f

encodeData(typedData, type, data)

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

import { encodeData } from 'cip-23';

const typedData = { /*...*/ };
console.log(encodeData(typedData, 'CIP23Domain', typedData.domain).toString('hex')); // 8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400fc70ef06638535b4881fafcac8287e210e3769ff1a8e91f1b95d6246e61e4d3c6c89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc60000000000000000000000000000000000000000000000000000000000000001000000000000000000000000cccccccccccccccccccccccccccccccccccccccc

getTypeHash(typedData, type)

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

import { getTypeHash } from 'cip-23';

const typedData = { /*...*/ };
console.log(getTypeHash(typedData, 'CIP23Domain').toString('hex')); // 8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f

encodeType(typedData, type)

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

import { encodeType } from 'cip-23';

const typedData = { /*...*/ };
console.log(encodeType(typedData, 'CIP23Domain')); // CIP23Domain(string name,string version,uint256 chainId,address verifyingContract)

getDependencies(typedData, type)

This function returns all sub-types used by a struct as a string array. If the struct has no sub-types (like CIP23Domain), an array with only the type itself is returned.

import { getDependencies } from 'cip-23';

const typedData = { /*...*/ };
console.log(getDependencies(typedData, 'CIP23Domain')); // ['CIP23Domain']
console.log(getDependencies(typedData, 'Mail')); // ['Mail', 'Person']