Skip to main content
Version: Next

Namespace: typedData

References​

TypedDataRevision​

Re-exports TypedDataRevision


StarknetEnumType​

Re-exports StarknetEnumType


StarknetMerkleType​

Re-exports StarknetMerkleType


StarknetType​

Re-exports StarknetType


StarknetDomain​

Re-exports StarknetDomain


TypedData​

Re-exports TypedData

Functions​

prepareSelector​

â–¸ prepareSelector(selector): string

Prepares the selector for later use, if it's not already in correct format. The selector in correct format is the starknet_keccak hash of the function name, encoded in ASCII.

Parameters​

NameTypeDescription
selectorstringThe selector to be prepared.

Returns​

string

The prepared selector.

Example

const result1 = prepareSelector('0xc14cfe23f3fa7ce7b1f8db7d7682305b1692293f71a61cc06637f0d8d8b6c8');
// result1 = '0xc14cfe23f3fa7ce7b1f8db7d7682305b1692293f71a61cc06637f0d8d8b6c8'

const result2 = prepareSelector('myFunction');
// result2 = '0xc14cfe23f3fa7ce7b1f8db7d7682305b1692293f71a61cc06637f0d8d8b6c8'

Defined in​

src/utils/typedData.ts:122


isMerkleTreeType​

â–¸ isMerkleTreeType(type): type is StarknetMerkleType

Checks if the given Starknet type is a Merkle tree type.

Parameters​

NameTypeDescription
typeStarknetTypeThe StarkNet type to check.

Returns​

type is StarknetMerkleType

  • True if the type is a Merkle tree type, false otherwise.

Example

const type = { name: 'test', type: 'merkletree',};
const result1 = isMerkleTreeType(type);
// result1 = true

const type2 = {name: 'test', type: 'non-merkletree',};
const result2 = isMerkleTreeType(type2);
// result2 = false

Defined in​

src/utils/typedData.ts:144


getDependencies​

â–¸ getDependencies(types, type, dependencies?, contains?, revision?): string[]

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.

Parameters​

NameTypeDefault valueDescription
typesRecord<string, StarknetType[]>undefinedThe types object containing all defined types.
typestringundefinedThe name of the type to get dependencies for.
dependencies?string[][]The array to store dependencies.
contains?string''The type contained within the struct.
revision?TypedDataRevisionRevision.LEGACYThe revision of the TypedData.

Returns​

string[]

The array of dependencies.

Defined in​

src/utils/typedData.ts:160


encodeType​

â–¸ encodeType(types, type, revision?): string

Encode a type to a string. All dependent types are alphabetically sorted.

Parameters​

NameTypeDefault valueDescription
typesRecord<string, StarknetType[]>undefinedThe types object containing all defined types.
typestringundefinedThe name of the type to encode.
revision?TypedDataRevisionRevision.LEGACYThe revision of the TypedData.

Returns​

string

The encoded string.

Example

import typedDataExample from '../../__mocks__/typedData/baseExample.json';

const result = encodeType(typedDataExample.types, 'Mail');
// result = "Mail(from:Person,to:Person,contents:felt)Person(name:felt,wallet:felt)";

Defined in​

src/utils/typedData.ts:232


getTypeHash​

â–¸ getTypeHash(types, type, revision?): string

Get a type string as hash.

Parameters​

NameTypeDefault valueDescription
typesRecord<string, StarknetType[]>undefinedThe types object containing all defined types.
typestringundefinedThe name of the type to hash.
revision?TypedDataRevisionRevision.LEGACYThe revision of the TypedData.

Returns​

string

The hash.

Example

import typedDataExample from '../../__mocks__/typedData/baseExample.json';

const result = getTypeHash(typedDataExample.types, 'StarkNetDomain');
// result = "0x1bfc207425a47a5dfa1a50a4f5241203f50624ca5fdf5e18755765416b8e288";

Defined in​

src/utils/typedData.ts:291


encodeValue​

â–¸ encodeValue(types, type, data, ctx?, revision?): [string, string]

Encodes a single value to an ABI serialisable string, number or Buffer. Returns the data as a tuple, which consists of an array of ABI compatible types, and an array of corresponding values.

Parameters​

NameTypeDefault valueDescription
typesRecord<string, StarknetType[]>undefinedThe types object containing all defined types.
typestringundefinedThe name of the type to encode.
dataunknownundefinedThe data to encode.
ctx?Context{}The context of the encoding process.
revision?TypedDataRevisionRevision.LEGACYThe revision of the TypedData.

Returns​

[string, string]

The ABI compatible type and corresponding value.

Example

import { getSelectorFromName } from '../../src/utils/hash';

const selector = 'transfer';
const selectorHash = getSelectorFromName(selector);
const result1 = encodeValue({}, 'felt', selectorHash);

// result1 = ['felt', '0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e']

Defined in​

src/utils/typedData.ts:322


encodeData​

â–¸ encodeData<T>(types, type, data, revision?): string[][]

Encode the data to an ABI encoded Buffer. The data should be a key -> value object with all the required values. All dependent types are automatically encoded.

Type parameters​

NameType
Textends TypedData

Parameters​

NameTypeDefault valueDescription
typesT["types"]undefinedThe types object containing all defined types.
typestringundefinedThe name of the type to encode.
dataT["message"]undefinedThe data to encode.
revision?TypedDataRevisionRevision.LEGACYThe revision of the TypedData.

Returns​

string[][]

The ABI compatible types and corresponding values.

Defined in​

src/utils/typedData.ts:460


getStructHash​

â–¸ getStructHash<T>(types, type, data, revision?): string

Get encoded data as a hash. The data should be a key -> value object with all the required values. All dependent types are automatically encoded.

Type parameters​

NameType
Textends TypedData

Parameters​

NameTypeDefault valueDescription
typesT["types"]undefinedThe types object containing all defined types.
typestringundefinedThe name of the type to hash.
dataT["message"]undefinedThe data to hash.
revision?TypedDataRevisionRevision.LEGACYThe revision of the TypedData.

Returns​

string

The hash of the encoded data.

Example

import exampleBaseTypes from '../../__mocks__/typedData/example_baseTypes.json';

const result = getStructHash(
exampleBaseTypes.types,
'StarknetDomain',
exampleBaseTypes.domain as StarknetDomain,
TypedDataRevision.ACTIVE
);
// result = "0x555f72e550b308e50c1a4f8611483a174026c982a9893a05c185eeb85399657";

Defined in​

src/utils/typedData.ts:515


getMessageHash​

â–¸ getMessageHash(typedData, account): string

Get the SNIP-12 encoded message to sign, from the typedData object.

Parameters​

NameTypeDescription
typedDataTypedDataThe TypedData object.
accountBigNumberishThe account to sign the message.

Returns​

string

The hash of the message to sign.

Throws

Will throw an error if the typedData does not match the JSON schema.

Example

const exampleAddress = "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826";
const typedDataStringExample = {
types: {
StarkNetDomain: [
{ name: 'name', type: 'felt' },
{ name: 'version', type: 'felt' },
{ name: 'chainId', type: 'felt' },
],
Person: [
{ name: 'name', type: 'felt' },
{ name: 'wallet', type: 'felt' },
],
String: [
{ name: 'len', type: 'felt' },
{ name: 'data', type: 'felt*' },
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'String' },
],
},
primaryType: 'Mail',
domain: {
name: 'StarkNet Mail',
version: '1',
chainId: 1,
},
message: {
from: {
name: 'Cow',
wallet: exampleAddress,
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: stringToStringStruct(
'this is way longer than just 32 characters, to test if that is possible within a typedData struct.'
),
},
};

const result = getMessageHash(typedDataStringExample, exampleAddress);
// result = "0x70338fb11b8f70b68b261de8a322bcb004bd85e88ac47d9147982c7f5ac66fd"

Defined in​

src/utils/typedData.ts:582