eip712

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package eip712 provides functionality for computing EIP-712 hashes and signatures for CREC operations without requiring network connectivity or API client dependencies.

This package is useful when you need to:

  • Generate operation hashes for verification
  • Sign operations offline
  • Implement custom signing workflows

Usage

Create an EIP-712 handler without network dependencies:

handler, err := eip712.NewHandler(&eip712.Options{
    Logger: logger, // optional
})

// Hash an operation
hash, err := handler.HashOperation(operation, chainSelector)

// Sign an operation
hash, signature, err := handler.SignOperation(ctx, operation, signer, chainSelector)

The handler can be used standalone or embedded in higher-level clients that need signing capabilities.

Example

This example demonstrates using the EIP-712 handler independently for offline signing without requiring any network dependencies.

package main

import (
	"context"
	"fmt"
	"log"
	"math/big"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/crypto"

	"github.com/smartcontractkit/crec-sdk/transact/eip712"
	"github.com/smartcontractkit/crec-sdk/transact/signer/local"
	"github.com/smartcontractkit/crec-sdk/transact/types"
)

func main() {
	// Create an EIP-712 handler without any API dependencies
	handler, err := eip712.NewHandler(nil)
	if err != nil {
		log.Fatal(err)
	}

	// Create a test operation
	operation := &types.Operation{
		ID:       big.NewInt(12345),
		Account:  common.HexToAddress("0x1234567890123456789012345678901234567890"),
		Deadline: big.NewInt(0),
		Transactions: []types.Transaction{
			{
				To:    common.HexToAddress("0xabcdefabcdefabcdefabcdefabcdefabcdefabcd"),
				Value: big.NewInt(0),
				Data:  []byte{0x01, 0x02, 0x03},
			},
		},
	}

	// Base Sepolia chain selector
	chainSelector := "10344971235874465080"

	// Hash the operation
	hash, err := handler.HashOperation(operation, chainSelector)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Operation hash: %s\n", hash.Hex())

	// Create a signer (in production, use a secure key management solution)
	privateKey, err := crypto.HexToECDSA("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
	if err != nil {
		log.Fatal(err)
	}
	signer := local.NewSigner(privateKey)

	// Sign the operation
	opHash, signature, err := handler.SignOperation(context.Background(), operation, signer, chainSelector)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Signed operation: %s\n", opHash.Hex())
	fmt.Printf("Signature length: %d bytes\n", len(signature))
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrOperationRequired is returned when the operation parameter is nil.
	ErrOperationRequired = errors.New("operation is required")
	// ErrSignerRequired is returned when the signer parameter is nil.
	ErrSignerRequired = errors.New("signer is required")

	// ErrParseChainSelector is returned when the chain selector cannot be parsed.
	ErrParseChainSelector = errors.New("failed to parse chain selector")
	// ErrGetChainFamily is returned when the chain family cannot be determined from the selector.
	ErrGetChainFamily = errors.New("failed to get chain family")
	// ErrUnsupportedChainFamily is returned when the chain family is not EVM.
	ErrUnsupportedChainFamily = errors.New("chain family is not supported")
	// ErrGetChainID is returned when the chain ID cannot be retrieved from the selector.
	ErrGetChainID = errors.New("failed to get chain ID from selector")
	// ErrCreateTypedData is returned when creating EIP-712 typed data for the operation fails.
	ErrCreateTypedData = errors.New("failed to create typed data for operation")
	// ErrComputeOperationHash is returned when computing the EIP-712 hash fails.
	ErrComputeOperationHash = errors.New("failed to compute operation hash")
	// ErrHashOperation is returned when hashing the operation fails.
	ErrHashOperation = errors.New("failed to hash operation")
	// ErrSignOperation is returned when signing the operation fails.
	ErrSignOperation = errors.New("failed to sign operation")
)

Functions

func GetChainIDFromSelector

func GetChainIDFromSelector(chainSelector string) (*big.Int, error)

GetChainIDFromSelector is a utility function that extracts the chain ID from a chain selector string. This is useful for applications that need the numeric chain ID for other purposes.

  • chainSelector: The chain selector string to parse.

Returns the chain ID as a big.Int or an error if the selector is invalid or unsupported.

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler provides operations for hashing and signing CREC operations. It handles EIP-712 typed data generation and signing without requiring network access or API client dependencies.

func NewHandler

func NewHandler(opts *Options) (*Handler, error)

NewHandler creates a new EIP-712 handler with the provided options. The handler can compute operation hashes and signatures independently of network operations.

  • opts: Options for configuring the handler. Can be nil for defaults.

func (*Handler) HashOperation

func (h *Handler) HashOperation(op *types.Operation, chainSelector string) (common.Hash, error)

HashOperation computes the EIP-712 digest of the given operation.

  • op: The operation to hash.
  • chainSelector: chainSelector of the blockchain network in which the operation is being executed.

Fetches chainID corresponding to the chain selector from smartcontractkit/chain-selectors package.

func (*Handler) SignOperation

func (h *Handler) SignOperation(
	ctx context.Context,
	op *types.Operation,
	signer signer.Signer,
	chainSelector string,
) (common.Hash, []byte, error)

SignOperation signs the given operation using the provided signer, returning the operation hash and the signature over the hash.

  • ctx: The context for the request.
  • op: The operation to sign.
  • signer: The signer to use for signing the operation. See signer.Signer for details.
  • chainSelector: chainSelector of the blockchain network in which the operation is being executed.

Fetches chainID corresponding to the chain selector from smartcontractkit/chain-selectors package.

func (*Handler) SignOperationHash

func (h *Handler) SignOperationHash(
	ctx context.Context,
	opHash common.Hash,
	signer signer.Signer,
) ([]byte, error)

SignOperationHash signs the given operation hash using the provided signer, returning the signature.

  • ctx: The context for the request.
  • opHash: The operation hash to sign.
  • signer: The signer to use for signing the operation. See signer.Signer for details.

type Options

type Options struct {
	Logger *slog.Logger
}

Options defines the options for creating a new EIP-712 handler. The handler is used to compute EIP-712 hashes and signatures for operations without requiring network connectivity.

  • Logger: Optional logger instance for logging messages.

Jump to

Keyboard shortcuts

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