A TypedDataEncode prepares and encodes EIP-712 payloads for signed typed data.

This is useful for those that wish to compute various components of a typed data hash, primary types, or sub-components, but generally the higher level Signer.signTypedData is more useful.

Constructors

new TypedDataEncoder()

new TypedDataEncoder(types): TypedDataEncoder

Create a new TypedDataEncoder for types.

This performs all necessary checking that types are valid and do not violate the EIP-712 structural constraints as well as computes the primaryType.

Parameters

ParameterType
typesRecord<string, TypedDataField[]>

Returns

TypedDataEncoder

Source

hash/typed-data.ts:260

Properties

PropertyModifierTypeDescription
primaryTypereadonlystringThe primary type for the structured types.

This is derived automatically from the types, since no recursion is possible, once the DAG for
the types is consturcted internally, the primary type must be the only remaining type with no parent nodes.

Accessors

types

get types(): Record<string, TypedDataField[]>

The types.

Returns

Record<string, TypedDataField[]>

Source

hash/typed-data.ts:245

Methods

encode()

encode(value): string

Return the fulled encoded value for the types.

Parameters

ParameterTypeDescription
valueRecord<string, any>The value to encode.

Returns

string

The encoded value.

Source

hash/typed-data.ts:471


encodeData()

encodeData(type, value): string

Return the encoded value for the type.

Parameters

ParameterTypeDescription
typestringThe type to encode the value for.
valueanyThe value to encode.

Returns

string

The encoded value.

Source

hash/typed-data.ts:448


encodeType()

encodeType(name): string

Return the full type for name.

Parameters

ParameterTypeDescription
namestringThe name to get the full type for.

Returns

string

The full type.

Source

hash/typed-data.ts:434


getEncoder()

getEncoder(type): (value) => string

Returnthe encoder for the specific type.

Parameters

ParameterTypeDescription
typestringThe type to get the encoder for.

Returns

Function

The encoder for the type.

Parameters
ParameterType
valueany
Returns

string

Source

hash/typed-data.ts:367


hash()

hash(value): string

Return the hash of the fully encoded value for the types.

Parameters

ParameterTypeDescription
valueRecord<string, any>The value to hash.

Returns

string

The hash of the value.

Source

hash/typed-data.ts:482


hashStruct()

hashStruct(name, value): string

Returns the hash of value for the type of name.

Parameters

ParameterTypeDescription
namestringThe name of the type.
valueRecord<string, any>The value to hash.

Returns

string

The hash of the value.

Source

hash/typed-data.ts:460


visit()

visit(value, callback): any

Call calback for each value in value, passing the type and component within value.

This is useful for replacing addresses or other transformation that may be desired on each component, based on its type.

Parameters

ParameterTypeDescription
valueRecord<string, any>The value to visit.
callback(type, data) => anyThe callback to call for each value.

Returns

any

The result of the callback.

Source

hash/typed-data.ts:536


encode()

static encode(
   domain, 
   types, 
   value): string

Return the fully encoded EIP-712 value for types with domain.

Parameters

ParameterTypeDescription
domainTypedDataDomainThe domain to use.
typesRecord<string, TypedDataField[]>The types to encode.
valueRecord<string, any>The value to encode.

Returns

string

The encoded value.

Source

hash/typed-data.ts:612


from()

static from(types): TypedDataEncoder

Create a new TypedDataEncoder for types.

Parameters

ParameterTypeDescription
typesRecord<string, TypedDataField[]>The types to encode.

Returns

TypedDataEncoder

The encoder for the types.

Throws

If the types are invalid.

Source

hash/typed-data.ts:548


getPayload()

static getPayload(
   domain, 
   types, 
   value): any

Returns the JSON-encoded payload expected by nodes which implement the JSON-RPC EIP-712 method.

Parameters

ParameterTypeDescription
domainTypedDataDomainThe domain to use.
typesRecord<string, TypedDataField[]>The types to encode.
valueRecord<string, any>The value to encode.

Returns

any

The JSON-encoded payload.

Source

hash/typed-data.ts:648


getPrimaryType()

static getPrimaryType(types): string

Return the primary type for types.

Parameters

ParameterTypeDescription
typesRecord<string, TypedDataField[]>The types to get the primary type for.

Returns

string

The primary type.

Throws

If the types are invalid.

Source

hash/typed-data.ts:560


hash()

static hash(
   domain, 
   types, 
   value): string

Return the hash of the fully encoded EIP-712 value for types with domain.

Parameters

ParameterTypeDescription
domainTypedDataDomainThe domain to use.
typesRecord<string, TypedDataField[]>The types to hash.
valueRecord<string, any>The value to hash.

Returns

string

The hash of the value.

Source

hash/typed-data.ts:630


hashDomain()

static hashDomain(domain): string

Return the domain hash for domain.

Parameters

ParameterTypeDescription
domainTypedDataDomainThe domain to hash.

Returns

string

The hash of the domain.

Throws

If the domain is invalid.

Source

hash/typed-data.ts:585


hashStruct()

static hashStruct(
   name, 
   types, 
   value): string

Return the hashed struct for value using types and name.

Parameters

ParameterTypeDescription
namestringThe name of the type.
typesRecord<string, TypedDataField[]>The types to hash.
valueRecord<string, any>The value to hash.

Returns

string

The hash of the value.

Source

hash/typed-data.ts:573