Skip to content

TypedDataDomain

EIP712 typed data domain. Spec is defined here

Properties

struct {
  chainId: ?u64 = null
  name: ?[]const u8 = null
  verifyingContract: ?[]const u8 = null
  version: ?[]const u8 = null
  salt: ?[]const u8 = null
}

MessageProperty

EIP712 message property type. Spec is defined here

Properties

struct {
  name: []const u8
  type: []const u8
}

EncodeTypeErrors

Set of possible errors when encoding struct into human-readable format.

Allocator.Error || error{InvalidPrimaryType}

EIP712Errors

Set of possible errors when encoding the struct values.

EncodeTypeErrors || ParamErrors || error{ UnexpectTypeFound, NoSpaceLeft, InvalidLength }

HashTypedData

Performs hashing of EIP712 according to the expecification https://eips.ethereum.org/EIPS/eip-712

types parameter is expected to be a struct where the struct keys are used to grab the solidity type information so that the encoding and hashing can happen based on it. See the specification for more details.

primary_type is the expected main type that you want to hash this message. Compilation will fail if the provided string doesn't exist on the types parameter

domain is the values of the defined EIP712Domain. Currently it doesnt not support custom domain types.

message is expected to be a struct where the solidity types are transalated to the native zig types. I.E string -> []const u8 or int256 -> i256 and so on. In the future work will be done where the compiler will offer more clearer types base on a meta programming type function.

Example
const domain: TypedDataDomain = .{
    .name = "Ether Mail",
    .version = "1",
    .chainId = 1,
    .verifyingContract = "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
};
 
const types = .{
    .EIP712Domain = &.{
        .{ .type = "string", .name = "name" },
        .{ .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" },
    },
};
 
const hash = try hashTypedData(testing.allocator, types, "Mail", domain, .{
    .from = .{
        .name = "Cow",
        .wallet = "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
    },
    .to = .{
        .name = "Bob",
        .wallet = "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
    },
    .contents = "Hello, Bob!",
});

Signature

pub fn hashTypedData(
    allocator: Allocator,
    comptime types: anytype,
    comptime primary_type: []const u8,
    domain: ?TypedDataDomain,
    message: anytype,
) EIP712Errors![Keccak256.digest_length]u8

HashStruct

Performs hashing of EIP712 structs according to the expecification https://eips.ethereum.org/EIPS/eip-712

types parameter is expected to be a struct where the struct keys are used to grab the solidity type information so that the encoding and hashing can happen based on it.
See the specification for more details.

primary_type is the expected main type that you want to hash this message.
Compilation will fail if the provided string doesn't exist on the types parameter

data is expected to be a struct where the solidity types are transalated to the native zig types. I.E string -> []const u8 or int256 -> i256 and so on.

In the future work will be done where the compiler will offer more clearer types base on a meta programming type function.

Example
const domain: TypedDataDomain = .{
    .name = "Ether Mail",
    .version = "1",
    .chainId = 1,
    .verifyingContract = "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
};
const types = .{
    .EIP712Domain = &.{
        .{ .type = "string", .name = "name" },
        .{ .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" },
    },
};
 
const hash = try hashStruct(testing.allocator, types, "EIP712Domain", domain);

Signature

pub fn hashStruct(
    allocator: Allocator,
    comptime types: anytype,
    comptime primary_type: []const u8,
    data: anytype,
) EIP712Errors![Keccak256.digest_length]u8

EncodeStruct

Performs encoding of EIP712 structs according to the expecification https://eips.ethereum.org/EIPS/eip-712

types parameter is expected to be a struct where the struct keys are used to grab the solidity type information so that the encoding and hashing can happen based on it. See the specification for more details.

primary_type is the expected main type that you want to hash this message. Compilation will fail if the provided string doesn't exist on the types parameter

data is expected to be a struct where the solidity types are transalated to the native zig types. I.E string -> []const u8 or int256 -> i256 and so on. In the future work will be done where the compiler will offer more clearer types base on a meta programming type function.

Slices, arrays, strings and bytes will all be encoded as "bytes32" instead of their usual encoded values.

Signature

pub fn encodeStruct(
    allocator: Allocator,
    comptime types: anytype,
    comptime primary_type: []const u8,
    data: anytype,
    writer: anytype,
) EIP712Errors!void

EncodeStructField

Encodes a singular struct field.

Signature

pub fn encodeStructField(
    allocator: Allocator,
    comptime types: anytype,
    comptime primary_type: []const u8,
    value: anytype,
    writer: anytype,
) EIP712Errors!void

HashType

Hash the main types and it's nested children

Signature

pub fn hashType(
    allocator: Allocator,
    comptime types_fields: anytype,
    comptime primary_type: []const u8,
) EncodeTypeErrors![Keccak256.digest_length]u8

EncodeType

Encodes the main type from a struct into a "human-readable" format.

Ex: struct { Mail: []const struct {type: "address", name: "foo"}} into "Mail(address foo)"

Signature

pub fn encodeType(
    allocator: Allocator,
    comptime types_fields: anytype,
    comptime primary_type: []const u8,
    writer: anytype,
) !void

FindTypeDependencies

Finds the main type child type and recursivly checks their children as well.

Signature

pub fn findTypeDependencies(
    comptime types_fields: anytype,
    comptime primary_type: []const u8,
    result: *std.StringArrayHashMap(void),
) Allocator.Error!void