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))
}
Output:
Index ¶
- Variables
- func GetChainIDFromSelector(chainSelector string) (*big.Int, error)
- type Handler
- func (h *Handler) HashOperation(op *types.Operation, chainSelector string) (common.Hash, error)
- func (h *Handler) SignOperation(ctx context.Context, op *types.Operation, signer signer.Signer, ...) (common.Hash, []byte, error)
- func (h *Handler) SignOperationHash(ctx context.Context, opHash common.Hash, signer signer.Signer) ([]byte, error)
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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.