smartaccount

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package smartaccount provides ERC-7579 modular smart account management with session key-based controlled autonomy for blockchain agents.

Architecture:

  • Account Manager: Safe deployment and UserOp construction
  • Session Manager: Hierarchical session key lifecycle
  • Policy Engine: Off-chain pre-flight validation
  • Module Registry: ERC-7579 module management
  • Bundler Client: External bundler RPC communication

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAccountNotDeployed     = errors.New("smart account not deployed")
	ErrSessionExpired         = errors.New("session key expired")
	ErrSessionRevoked         = errors.New("session key revoked")
	ErrPolicyViolation        = errors.New("session policy violation")
	ErrModuleNotInstalled     = errors.New("module not installed")
	ErrSpendLimitExceeded     = errors.New("spend limit exceeded")
	ErrInvalidSessionKey      = errors.New("invalid session key")
	ErrSessionNotFound        = errors.New("session key not found")
	ErrTargetNotAllowed       = errors.New("target address not allowed")
	ErrFunctionNotAllowed     = errors.New("function not allowed")
	ErrInvalidUserOp          = errors.New("invalid user operation")
	ErrBundlerError           = errors.New("bundler RPC error")
	ErrModuleAlreadyInstalled = errors.New("module already installed")
)

Functions

func ComputeUserOpHash added in v0.6.0

func ComputeUserOpHash(
	op *UserOperation,
	entryPoint common.Address,
	chainID int64,
) []byte

ComputeUserOpHash computes the hash of a UserOp for signing per the ERC-4337 v0.7 PackedUserOperation format. Inner hash: keccak256(abi.encode(sender, nonce, keccak256(initCode),

keccak256(callData), accountGasLimits, preVerificationGas,
gasFees, keccak256(paymasterAndData)))

Final hash: keccak256(abi.encode(innerHash, entryPoint, chainId))

Types

type AccountInfo

type AccountInfo struct {
	Address      common.Address `json:"address"`
	IsDeployed   bool           `json:"isDeployed"`
	Modules      []ModuleInfo   `json:"modules"`
	OwnerAddress common.Address `json:"ownerAddress"`
	ChainID      int64          `json:"chainId"`
	EntryPoint   common.Address `json:"entryPoint"`
}

AccountInfo holds smart account metadata.

type AccountManager

type AccountManager interface {
	// GetOrDeploy returns the account address, deploying if needed.
	GetOrDeploy(ctx context.Context) (*AccountInfo, error)
	// Info returns account metadata without deploying.
	Info(ctx context.Context) (*AccountInfo, error)
	// InstallModule installs an ERC-7579 module.
	InstallModule(
		ctx context.Context,
		moduleType ModuleType,
		addr common.Address,
		initData []byte,
	) (string, error)
	// UninstallModule removes an ERC-7579 module.
	UninstallModule(
		ctx context.Context,
		moduleType ModuleType,
		addr common.Address,
		deInitData []byte,
	) (string, error)
	// Execute submits a UserOperation via bundler.
	Execute(ctx context.Context, calls []ContractCall) (string, error)
}

AccountManager defines the smart account management interface.

type ContractCall

type ContractCall struct {
	Target      common.Address `json:"target"`
	Value       *big.Int       `json:"value"`
	Data        []byte         `json:"data"`
	FunctionSig string         `json:"functionSig,omitempty"`
}

ContractCall represents a call to be executed via the smart account.

type Factory

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

Factory handles Safe smart account deployment.

func NewFactory

func NewFactory(
	caller contract.ContractCaller,
	rpc *ethclient.Client,
	factoryAddr common.Address,
	singletonAddr common.Address,
	safe7579Addr common.Address,
	fallbackAddr common.Address,
	chainID int64,
) *Factory

NewFactory creates a smart account factory. singletonAddr is the Safe L2 implementation contract (the proxy delegates to this). safe7579Addr is the ERC-7579 adapter, called via delegate call during setup.

func (*Factory) ComputeAddress

func (f *Factory) ComputeAddress(
	ctx context.Context,
	owner common.Address,
	salt *big.Int,
) (common.Address, error)

ComputeAddress computes the counterfactual Safe address via CREATE2. Uses the SafeProxyFactory's salt derivation:

deploymentSalt = keccak256(keccak256(initializer) ++ saltNonce)

and the proxy initCode hash for the CREATE2 formula.

func (*Factory) Deploy

func (f *Factory) Deploy(
	ctx context.Context,
	owner common.Address,
	salt *big.Int,
) (common.Address, string, error)

Deploy deploys a new Safe account with ERC-7579 adapter. Returns the deployed account address and transaction hash.

func (*Factory) IsDeployed

func (f *Factory) IsDeployed(
	ctx context.Context,
	addr common.Address,
) (bool, error)

IsDeployed checks if the account has code deployed at its address by calling eth_getCode via the RPC client.

type Manager

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

Manager implements AccountManager for Safe-based smart accounts with ERC-7579 module support and ERC-4337 UserOp submission.

func NewManager

func NewManager(
	factory *Factory,
	bundlerClient *bundler.Client,
	caller contract.ContractCaller,
	wp wallet.WalletProvider,
	chainID int64,
	entryPoint common.Address,
) *Manager

NewManager creates a smart account manager.

func (*Manager) Execute

func (m *Manager) Execute(
	ctx context.Context,
	calls []ContractCall,
) (string, error)

Execute builds and submits a UserOp for contract calls.

func (*Manager) GetOrDeploy

func (m *Manager) GetOrDeploy(
	ctx context.Context,
) (*AccountInfo, error)

GetOrDeploy returns the account info, deploying if needed.

func (*Manager) Info

func (m *Manager) Info(
	ctx context.Context,
) (*AccountInfo, error)

Info returns current account metadata without deploying.

func (*Manager) InstallModule

func (m *Manager) InstallModule(
	ctx context.Context,
	moduleType ModuleType,
	addr common.Address,
	initData []byte,
) (string, error)

InstallModule installs an ERC-7579 module on the smart account.

func (*Manager) SetPaymasterFunc

func (m *Manager) SetPaymasterFunc(fn PaymasterDataFunc)

SetPaymasterFunc sets the paymaster callback for gasless transactions.

func (*Manager) UninstallModule

func (m *Manager) UninstallModule(
	ctx context.Context,
	moduleType ModuleType,
	addr common.Address,
	deInitData []byte,
) (string, error)

UninstallModule removes a module from the smart account.

type ModuleInfo

type ModuleInfo struct {
	Address     common.Address `json:"address"`
	Type        ModuleType     `json:"type"`
	Name        string         `json:"name"`
	InstalledAt time.Time      `json:"installedAt"`
}

ModuleInfo describes an installed ERC-7579 module.

type ModuleType

type ModuleType uint8

ModuleType represents ERC-7579 module types.

const (
	ModuleTypeValidator ModuleType = 1
	ModuleTypeExecutor  ModuleType = 2
	ModuleTypeFallback  ModuleType = 3
	ModuleTypeHook      ModuleType = 4
)

func (ModuleType) String

func (t ModuleType) String() string

String returns the module type name.

type PaymasterDataFunc

type PaymasterDataFunc func(ctx context.Context, op *UserOperation, stub bool) ([]byte, *PaymasterGasOverrides, error)

PaymasterDataFunc obtains paymasterAndData for a UserOp. When stub is true, returns temporary data for gas estimation. When stub is false, returns final signed data with optional gas overrides.

type PaymasterGasOverrides

type PaymasterGasOverrides struct {
	CallGasLimit         *big.Int
	VerificationGasLimit *big.Int
	PreVerificationGas   *big.Int
}

PaymasterGasOverrides allows paymaster to override gas estimates.

type PolicyViolationError

type PolicyViolationError struct {
	SessionID string
	Reason    string
}

PolicyViolationError provides details about why a policy check failed.

func (*PolicyViolationError) Error

func (e *PolicyViolationError) Error() string

func (*PolicyViolationError) Unwrap

func (e *PolicyViolationError) Unwrap() error

type SessionKey

type SessionKey struct {
	ID            string         `json:"id"`
	PublicKey     []byte         `json:"publicKey"`
	Address       common.Address `json:"address"`
	PrivateKeyRef string         `json:"privateKeyRef"` // CryptoProvider key ID
	Policy        SessionPolicy  `json:"policy"`
	ParentID      string         `json:"parentId,omitempty"` // empty = master session
	CreatedAt     time.Time      `json:"createdAt"`
	ExpiresAt     time.Time      `json:"expiresAt"`
	Revoked       bool           `json:"revoked"`
}

SessionKey represents a session key with its associated policy.

func (*SessionKey) IsActive

func (sk *SessionKey) IsActive() bool

IsActive returns true if the session key is usable.

func (*SessionKey) IsExpired

func (sk *SessionKey) IsExpired() bool

IsExpired returns true if the session key has expired.

func (*SessionKey) IsMaster

func (sk *SessionKey) IsMaster() bool

IsMaster returns true if this is a master (root) session key.

type SessionPolicy

type SessionPolicy struct {
	AllowedTargets    []common.Address `json:"allowedTargets"`
	AllowedFunctions  []string         `json:"allowedFunctions"` // 4-byte hex selectors
	SpendLimit        *big.Int         `json:"spendLimit"`
	SpentAmount       *big.Int         `json:"spentAmount,omitempty"`
	ValidAfter        time.Time        `json:"validAfter"`
	ValidUntil        time.Time        `json:"validUntil"`
	Active            bool             `json:"active"`
	AllowedPaymasters []common.Address `json:"allowedPaymasters,omitempty"`
}

SessionPolicy defines the constraints for a session key.

type UserOperation

type UserOperation struct {
	Sender               common.Address `json:"sender"`
	Nonce                *big.Int       `json:"nonce"`
	InitCode             []byte         `json:"initCode"`
	CallData             []byte         `json:"callData"`
	CallGasLimit         *big.Int       `json:"callGasLimit"`
	VerificationGasLimit *big.Int       `json:"verificationGasLimit"`
	PreVerificationGas   *big.Int       `json:"preVerificationGas"`
	MaxFeePerGas         *big.Int       `json:"maxFeePerGas"`
	MaxPriorityFeePerGas *big.Int       `json:"maxPriorityFeePerGas"`
	PaymasterAndData     []byte         `json:"paymasterAndData"`
	Signature            []byte         `json:"signature"`
}

UserOperation represents an ERC-4337 UserOperation.

Directories

Path Synopsis
Package bindings provides Go ABI bindings for smart account contracts.
Package bindings provides Go ABI bindings for smart account contracts.
Package bundler provides an ERC-4337 bundler JSON-RPC client.
Package bundler provides an ERC-4337 bundler JSON-RPC client.
Package paymaster provides ERC-4337 paymaster integration for gasless transactions.
Package paymaster provides ERC-4337 paymaster integration for gasless transactions.
permit
Package permit implements EIP-2612 permit signing for USDC paymaster interactions.
Package permit implements EIP-2612 permit signing for USDC paymaster interactions.

Jump to

Keyboard shortcuts

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