Comparing version 0.3.0 to 0.4.0
@@ -82,3 +82,3 @@ "use strict"; | ||
if (typedData.types[type]) { | ||
return ['bytes32', (0, _utils.keccak256)(encodeData(typedData, type, data))]; | ||
return ['bytes32', getStructHash(typedData, type, data)]; | ||
} | ||
@@ -118,4 +118,10 @@ | ||
const getMessage = typedData => { | ||
return Buffer.concat([EIP_191_PREFIX, getStructHash(typedData, 'EIP712Domain', typedData.domain), getStructHash(typedData, typedData.primaryType, typedData.message)]); | ||
const getMessage = (typedData, hash) => { | ||
const message = Buffer.concat([EIP_191_PREFIX, getStructHash(typedData, 'EIP712Domain', typedData.domain), getStructHash(typedData, typedData.primaryType, typedData.message)]); | ||
if (hash) { | ||
return (0, _utils.keccak256)(message); | ||
} | ||
return message; | ||
}; | ||
@@ -122,0 +128,0 @@ |
@@ -8,6 +8,4 @@ "use strict"; | ||
var _ajv = _interopRequireDefault(require("ajv")); | ||
var _schemasafe = require("@exodus/schemasafe"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const EIP_712_SCHEMA = { | ||
@@ -54,4 +52,3 @@ type: 'object', | ||
const validateTypedData = data => { | ||
const ajv = new _ajv.default(); | ||
return ajv.validate(EIP_712_SCHEMA, data); | ||
return (0, _schemasafe.validator)(EIP_712_SCHEMA)(data); | ||
}; | ||
@@ -58,0 +55,0 @@ |
@@ -64,3 +64,3 @@ import { defaultAbiCoder } from '@ethersproject/abi'; | ||
if (typedData.types[type]) { | ||
return ['bytes32', keccak256(encodeData(typedData, type, data))]; | ||
return ['bytes32', getStructHash(typedData, type, data)]; | ||
} | ||
@@ -94,4 +94,10 @@ | ||
}; | ||
export const getMessage = typedData => { | ||
return Buffer.concat([EIP_191_PREFIX, getStructHash(typedData, 'EIP712Domain', typedData.domain), getStructHash(typedData, typedData.primaryType, typedData.message)]); | ||
export const getMessage = (typedData, hash) => { | ||
const message = Buffer.concat([EIP_191_PREFIX, getStructHash(typedData, 'EIP712Domain', typedData.domain), getStructHash(typedData, typedData.primaryType, typedData.message)]); | ||
if (hash) { | ||
return keccak256(message); | ||
} | ||
return message; | ||
}; | ||
@@ -98,0 +104,0 @@ export const asArray = (typedData, type = typedData.primaryType, data = typedData.message) => { |
@@ -1,2 +0,2 @@ | ||
import Ajv from 'ajv'; | ||
import { validator } from '@exodus/schemasafe'; | ||
const EIP_712_SCHEMA = { | ||
@@ -42,5 +42,4 @@ type: 'object', | ||
export const validateTypedData = data => { | ||
const ajv = new Ajv(); | ||
return ajv.validate(EIP_712_SCHEMA, data); | ||
return validator(EIP_712_SCHEMA)(data); | ||
}; | ||
//# sourceMappingURL=json.js.map |
{ | ||
"name": "eip-712", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Utility functions that can help with signing and verifying EIP-712 based messages", | ||
@@ -48,3 +48,3 @@ "author": "Maarten Zuidhoorn <maarten@zuidhoorn.com>", | ||
"@ethersproject/abi": "^5.0.3", | ||
"ajv": "^6.12.4", | ||
"@exodus/schemasafe": "^1.0.0-rc.2", | ||
"keccak": "^3.0.1" | ||
@@ -69,2 +69,4 @@ }, | ||
"eslint-plugin-jest": "^23.20.0", | ||
"ethereumjs-util": "^7.0.4", | ||
"ethers": "^5.0.9", | ||
"husky": "^4.2.5", | ||
@@ -71,0 +73,0 @@ "jest": "^26.4.2", |
@@ -5,6 +5,8 @@ # `eip-712` | ||
This is a library for Node.js and web browsers with some utility functions that can help with signing and verifying [EIP-712](https://eips.ethereum.org/EIPS/eip-712) based messages. It is fully written in TypeScript. | ||
This is a library for Node.js and web browsers with some utility functions that can help with signing and verifying [EIP-712](https://eips.ethereum.org/EIPS/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://docs.metamask.io/guide/signing-data.html#sign-typed-data-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`](https://github.com/Mrtenz/eip-712/blob/master/examples) folder. | ||
## Installation | ||
@@ -70,7 +72,7 @@ | ||
Here is a brief description of the functions available in this library. For more detailed examples, you can refer to [`src/eip-712.test.ts`](https://github.com/Mrtenz/eip-712/blob/master/src/eip-712.test.ts). | ||
Here is a brief description of the functions available in this library. For more detailed examples, you can refer to [`src/eip-712.test.ts`](https://github.com/Mrtenz/eip-712/blob/master/src/eip-712.test.ts), or to the examples in the [`examples`](https://github.com/Mrtenz/eip-712/blob/master/examples) folder. | ||
#### `getMessage(typedData)` | ||
#### `getMessage(typedData, hash?)` | ||
This function will return the full EIP-191 encoded message to be signed as Buffer, for the typed data specified. | ||
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. | ||
@@ -82,2 +84,3 @@ ```js | ||
console.log(getMessage(typedData).toString('hex')); // 1901f2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090fc52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e | ||
console.log(getMessage(typedData, true).toString('hex')); // be609aee343fb3c4b28e1df9e632fca64fcfaede20f02e86244efddf30957bd2 | ||
``` | ||
@@ -84,0 +87,0 @@ |
@@ -210,2 +210,11 @@ import invalidArrayLength from './__fixtures__/invalid-array-length.json'; | ||
it('hashes the message with Keccak-256', () => { | ||
expect(getMessage(mailTypedData, true).toString('hex')).toBe( | ||
'be609aee343fb3c4b28e1df9e632fca64fcfaede20f02e86244efddf30957bd2' | ||
); | ||
expect(getMessage(approvalTypedData, true).toString('hex')).toBe( | ||
'ee0cdea747f4a81355be92dbf30e209dbd2954a82d5a82482b7c7800089c7f57' | ||
); | ||
}); | ||
it('throws for invalid JSON data', () => { | ||
@@ -212,0 +221,0 @@ // @ts-expect-error |
@@ -8,3 +8,4 @@ import { defaultAbiCoder } from '@ethersproject/abi'; | ||
/** | ||
* Get the dependencies of a type. | ||
* Get the dependencies of a struct type. If a struct has the same dependency multiple times, it's only included once | ||
* in the resulting array. | ||
* | ||
@@ -114,3 +115,3 @@ * @param {TypedData} typedData | ||
if (typedData.types[type]) { | ||
return ['bytes32', keccak256(encodeData(typedData, type, data as Record<string, unknown>))]; | ||
return ['bytes32', getStructHash(typedData, type, data as Record<string, unknown>)]; | ||
} | ||
@@ -174,9 +175,11 @@ | ||
/** | ||
* Get the EIP-191 encoded message to sign, from the typedData object. | ||
* Get the EIP-191 encoded message to sign, from the typedData object. If `hash` is enabled, the message will be hashed | ||
* with Keccak256. | ||
* | ||
* @param {TypedData} typedData | ||
* @param {boolean} hash | ||
* @return {Buffer} | ||
*/ | ||
export const getMessage = (typedData: TypedData): Buffer => { | ||
return Buffer.concat([ | ||
export const getMessage = (typedData: TypedData, hash?: boolean): Buffer => { | ||
const message = Buffer.concat([ | ||
EIP_191_PREFIX, | ||
@@ -186,2 +189,8 @@ getStructHash(typedData, 'EIP712Domain', typedData.domain as Record<string, unknown>), | ||
]); | ||
if (hash) { | ||
return keccak256(message); | ||
} | ||
return message; | ||
}; | ||
@@ -188,0 +197,0 @@ |
@@ -0,1 +1,10 @@ | ||
/** | ||
* A single type, as part of a struct. The `type` field can be any of the EIP-712 supported types. Currently those are: | ||
* - Atomic types: bytes1..32, uint8..256, int8..256, bool, address | ||
* - Dynamic types: bytes, string | ||
* - Reference types: array type (e.g. uint8[], SomeStruct[]), struct type (e.g. SomeStruct) | ||
* | ||
* Note that the `uint` and `int` aliases like in Solidity, and fixed point numbers are not supported by the EIP-712 | ||
* standard. | ||
*/ | ||
export interface EIP712Type { | ||
@@ -6,2 +15,5 @@ name: string; | ||
/** | ||
* The EIP712 domain struct. Any of these fields are optional, but it must contain at least one field. | ||
*/ | ||
export interface EIP712Domain { | ||
@@ -15,2 +27,5 @@ name?: string; | ||
/** | ||
* The complete typed data, with all the structs, domain data, primary type of the message, and the message itself. | ||
*/ | ||
export interface TypedData { | ||
@@ -17,0 +32,0 @@ types: { |
@@ -1,2 +0,2 @@ | ||
import Ajv from 'ajv'; | ||
import { validator } from '@exodus/schemasafe'; | ||
import { TypedData } from '../types'; | ||
@@ -33,3 +33,3 @@ | ||
required: ['types', 'primaryType', 'domain', 'message'] | ||
} as const; | ||
}; | ||
@@ -43,4 +43,3 @@ /** | ||
export const validateTypedData = (data: unknown): data is TypedData => { | ||
const ajv = new Ajv(); | ||
return ajv.validate(EIP_712_SCHEMA, data) as boolean; | ||
return validator(EIP_712_SCHEMA)(data); | ||
}; |
/// <reference types="node" /> | ||
import { TypedData } from './types'; | ||
/** | ||
* Get the dependencies of a type. | ||
* Get the dependencies of a struct type. If a struct has the same dependency multiple times, it's only included once | ||
* in the resulting array. | ||
* | ||
@@ -49,8 +50,10 @@ * @param {TypedData} typedData | ||
/** | ||
* Get the EIP-191 encoded message to sign, from the typedData object. | ||
* Get the EIP-191 encoded message to sign, from the typedData object. If `hash` is enabled, the message will be hashed | ||
* with Keccak256. | ||
* | ||
* @param {TypedData} typedData | ||
* @param {boolean} hash | ||
* @return {Buffer} | ||
*/ | ||
export declare const getMessage: (typedData: TypedData) => Buffer; | ||
export declare const getMessage: (typedData: TypedData, hash?: boolean | undefined) => Buffer; | ||
/** | ||
@@ -57,0 +60,0 @@ * Get the typed data as array. This can be useful for encoding the typed data with the contract ABI. |
/// <reference types="node" /> | ||
/** | ||
* A single type, as part of a struct. The `type` field can be any of the EIP-712 supported types. Currently those are: | ||
* - Atomic types: bytes1..32, uint8..256, int8..256, bool, address | ||
* - Dynamic types: bytes, string | ||
* - Reference types: array type (e.g. uint8[], SomeStruct[]), struct type (e.g. SomeStruct) | ||
* | ||
* Note that the `uint` and `int` aliases like in Solidity, and fixed point numbers are not supported by the EIP-712 | ||
* standard. | ||
*/ | ||
export interface EIP712Type { | ||
@@ -6,2 +15,5 @@ name: string; | ||
} | ||
/** | ||
* The EIP712 domain struct. Any of these fields are optional, but it must contain at least one field. | ||
*/ | ||
export interface EIP712Domain { | ||
@@ -14,2 +26,5 @@ name?: string; | ||
} | ||
/** | ||
* The complete typed data, with all the structs, domain data, primary type of the message, and the message itself. | ||
*/ | ||
export interface TypedData { | ||
@@ -16,0 +31,0 @@ types: { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
111079
60
1888
151
26
+ Added@exodus/schemasafe@1.3.0(transitive)
- Removedajv@^6.12.4
- Removedajv@6.12.6(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedpunycode@2.3.1(transitive)
- Removeduri-js@4.4.1(transitive)