TypedDataEncoder
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()
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
Parameter | Type |
---|---|
types | Record <string , TypedDataField []> |
Returns
Source
Properties
Property | Modifier | Type | Description |
---|---|---|---|
primaryType | readonly | string | The 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
The types.
Returns
Record
<string
, TypedDataField
[]>
Source
Methods
encode()
Return the fulled encoded value
for the types.
Parameters
Parameter | Type | Description |
---|---|---|
value | Record <string , any > | The value to encode. |
Returns
string
The encoded value.
Source
encodeData()
Return the encoded value
for the type
.
Parameters
Parameter | Type | Description |
---|---|---|
type | string | The type to encode the value for. |
value | any | The value to encode. |
Returns
string
The encoded value.
Source
encodeType()
Return the full type for name
.
Parameters
Parameter | Type | Description |
---|---|---|
name | string | The name to get the full type for. |
Returns
string
The full type.
Source
getEncoder()
Returnthe encoder for the specific type
.
Parameters
Parameter | Type | Description |
---|---|---|
type | string | The type to get the encoder for. |
Returns
Function
The encoder for the type.
Parameters
Parameter | Type |
---|---|
value | any |
Returns
string
Source
hash()
Return the hash of the fully encoded value
for the types.
Parameters
Parameter | Type | Description |
---|---|---|
value | Record <string , any > | The value to hash. |
Returns
string
The hash of the value.
Source
hashStruct()
Returns the hash of value
for the type of name
.
Parameters
Parameter | Type | Description |
---|---|---|
name | string | The name of the type. |
value | Record <string , any > | The value to hash. |
Returns
string
The hash of the value.
Source
visit()
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
Parameter | Type | Description |
---|---|---|
value | Record <string , any > | The value to visit. |
callback | (type , data ) => any | The callback to call for each value. |
Returns
any
The result of the callback.
Source
encode()
Return the fully encoded EIP-712 value
for types
with domain
.
Parameters
Parameter | Type | Description |
---|---|---|
domain | TypedDataDomain | The domain to use. |
types | Record <string , TypedDataField []> | The types to encode. |
value | Record <string , any > | The value to encode. |
Returns
string
The encoded value.
Source
from()
Create a new TypedDataEncoder for types
.
Parameters
Parameter | Type | Description |
---|---|---|
types | Record <string , TypedDataField []> | The types to encode. |
Returns
The encoder for the types.
Throws
If the types are invalid.
Source
getPayload()
Returns the JSON-encoded payload expected by nodes which implement the JSON-RPC EIP-712 method.
Parameters
Parameter | Type | Description |
---|---|---|
domain | TypedDataDomain | The domain to use. |
types | Record <string , TypedDataField []> | The types to encode. |
value | Record <string , any > | The value to encode. |
Returns
any
The JSON-encoded payload.
Source
getPrimaryType()
Return the primary type for types
.
Parameters
Parameter | Type | Description |
---|---|---|
types | Record <string , TypedDataField []> | The types to get the primary type for. |
Returns
string
The primary type.
Throws
If the types are invalid.
Source
hash()
Return the hash of the fully encoded EIP-712 value
for types
with
domain
.
Parameters
Parameter | Type | Description |
---|---|---|
domain | TypedDataDomain | The domain to use. |
types | Record <string , TypedDataField []> | The types to hash. |
value | Record <string , any > | The value to hash. |
Returns
string
The hash of the value.
Source
hashDomain()
Return the domain hash for domain
.
Parameters
Parameter | Type | Description |
---|---|---|
domain | TypedDataDomain | The domain to hash. |
Returns
string
The hash of the domain.
Throws
If the domain is invalid.
Source
hashStruct()
Return the hashed struct for value
using types
and name
.
Parameters
Parameter | Type | Description |
---|---|---|
name | string | The name of the type. |
types | Record <string , TypedDataField []> | The types to hash. |
value | Record <string , any > | The value to hash. |
Returns
string
The hash of the value.
Source
Was this page helpful?