typedata

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	RevisionV0 revision
	RevisionV1 revision
)

Functions

func EncodeData

func EncodeData(
	typeDef *TypeDefinition,
	td *TypedData,
	context ...string,
) (enc []*felt.Felt, err error)

EncodeData encodes the given type definition using the TypedData struct.

func GetRevision

func GetRevision(version uint8) (rev *revision, err error)

Types

type Domain

type Domain struct {
	Name     string `json:"name"`
	Version  string `json:"version"`
	ChainID  string `json:"chainId"`
	Revision uint8  `json:"revision,omitempty"`

	HasStringRevision bool `json:"-"`
	// contains filtered or unexported fields
}

func (Domain) MarshalJSON

func (domain Domain) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for Domain. Some logic was added to marshal the `Domain` exactly as it is in the original JSON.

func (*Domain) UnmarshalJSON

func (domain *Domain) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Domain

type RevisionTypes

type RevisionTypes struct {
	Basic  []string
	Preset map[string]TypeDefinition
}

type TypeDefinition

type TypeDefinition struct {
	Name               string     `json:"-"`
	Encoding           *felt.Felt `json:"-"`
	EncoddingString    string     `json:"-"`
	SingleEncString    string     `json:"-"`
	ReferencedTypesEnc []string   `json:"-"`
	Parameters         []TypeParameter
}

func (TypeDefinition) MarshalJSON

func (td TypeDefinition) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for TypeDefinition

type TypeParameter

type TypeParameter struct {
	Name     string `json:"name"`
	Type     string `json:"type"`
	Contains string `json:"contains,omitempty"`
}

type TypedData

type TypedData struct {
	Types       map[string]TypeDefinition `json:"types"`
	PrimaryType string                    `json:"primaryType"`
	Domain      Domain                    `json:"domain"`
	Message     map[string]any            `json:"message"`
	Revision    *revision                 `json:"-"`
}

func NewTypedData

func NewTypedData(
	types []TypeDefinition,
	primaryType string,
	domain Domain,
	message []byte,
) (td *TypedData, err error)

NewTypedData creates a new instance of TypedData.

Parameters:

  • types: a slice of TypeDefinition representing the types used in the TypedData.
  • primaryType: a string representing the primary type of the TypedData.
  • domain: a Domain struct representing the domain information of the TypedData.
  • message: a byte slice containing the JSON-encoded message.

Returns:

  • td: a pointer to the newly created TypedData instance.
  • err: an error if any occurred during the creation of the TypedData.

func (*TypedData) GetMessageHash

func (td *TypedData) GetMessageHash(account string) (hash *felt.Felt, err error)

GetMessageHash calculates the hash of a typed message for a given account using the StarkCurve.

(ref: https://github.com/starknet-io/SNIPs/blob/5d5a42c654c27b377d8b7f90b453065fd19ec2eb/SNIPS/snip-12.md#specification)

Parameters:

  • account: A string representing the account.

Returns:

  • hash: A pointer to a felt.Felt representing the calculated hash.

func (*TypedData) GetStructHash

func (td *TypedData) GetStructHash(
	typeName string,
	context ...string,
) (hash *felt.Felt, err error)

GetStructHash calculates the hash of a struct type and its respective data.

Parameters:

  • typeName: the name of the type to be hashed.
  • context: optional context strings to be included in the hash calculation.

You can use 'context' to specify the path of the type you want to hash. Example: if you want to hash the type "ExampleInner" that is within the "Example" primary type with the name of "example_inner", you can specify the context as ["example_inner"]. If "ExampleInner" has a parameter with the name of "example_inner_inner" that you want to know the hash, you can specify the context as ["example_inner", "example_inner_inner"].

Returns:

  • hash: A pointer to a felt.Felt representing the calculated hash.
  • err: an error if any occurred during the hash calculation.

func (*TypedData) GetTypeHash

func (td *TypedData) GetTypeHash(typeName string) (*felt.Felt, error)

GetTypeHash returns the hash of the given type.

Parameters:

  • typeName: the name of the type to hash

Returns:

  • hash: A pointer to a felt.Felt representing the calculated hash.
  • err: an error if any occurred during the hash calculation.

func (*TypedData) UnmarshalJSON

func (td *TypedData) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for TypedData

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL