smart_wallet

package
v0.0.48 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: GPL-3.0 Imports: 14 Imported by: 0

README

SmartWallet GoLang library

Overview

SmartWallet is a golang library that provides helper functions to user operations.

Features

This package introduces the following types and helper functionality:

  • Config - A struct that holds the configuration for the SmartWallet.
  • Type - an enum that represents the type of the wallet. Currently only ZeroDev Kernel v2.2, Biconomy v2.0 and eth-infinitism SimpleAccount v0.6 (still in progress) are supported.
  • IsAccountDeployed(swAddress) - a function that checks if the smart wallet is deployed.
  • GetAccountAddress(owner, index) - a function that calculates the address of the smart wallet.
  • GetInitCode(smartWalletConfig) - a function that returns the init code of the smart wallet.
  • Get<SmartWalletType>InitCode(...) - a function that returns the init code for the supported types of smart wallets.
  • GetInitCodeFromFactoryCallData(smartWalletConfig, factoryCallData) - a function that builds the init code from the smart wallet config and factory call data.
  • GetFactoryCallData(smartWalletConfig) - a function that returns the call data for the smart wallet factory.
  • Get<SmartWalletType>FactoryCallData(...) - a function that returns the factory call data for the supported types of smart wallets.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// SimpleAccountType represents a smart wallet type for a Simple Account wallet.
	SimpleAccountType = Type{"simple_account"}
	// BiconomyType represents a type for BiconomyType Smart Account wallet.
	BiconomyType = Type{"biconomy"}
	// KernelType represents a type for Zerodev KernelType wallet.
	KernelType = Type{"kernel"}
)
View Source
var (
	// ErrNoSmartWalletType is returned when no smart wallet type is specified.
	// Make sure to specify the smart wallet type in the configuration.
	ErrNoSmartWalletType = errors.New("no smart wallet type specified (nil)")
	// ErrSmartWalletNotSupported is returned on attempt to perform some actions with an unsupported smart wallet type.
	// Make sure that the smart wallet type you are trying to use
	// has no `unsupported` or `untested` tags in the source files.
	ErrSmartWalletNotSupported = errors.New("smart wallet not supported")
)
View Source
var KernelExecuteABI = must(abi.JSON(strings.NewReader(`[
  {
    "inputs": [
      {
        "internalType": "address",
        "name": "to",
        "type": "address"
      },
      {
        "internalType": "uint256",
        "name": "value",
        "type": "uint256"
      },
      {
        "internalType": "bytes",
        "name": "data",
        "type": "bytes"
      },
      {
        "internalType": "enum Operation",
        "name": "",
        "type": "uint8"
      }
    ],
    "name": "execute",
    "outputs": [],
    "stateMutability": "payable",
    "type": "function"
  },
  {
    "inputs": [{
      "components": [
        {
          "internalType": "address",
          "name": "to",
          "type": "address"
        },
        {
          "internalType": "uint256",
          "name": "value",
          "type": "uint256"
        },
        {
          "internalType": "bytes",
          "name": "data",
          "type": "bytes"
        }
      ],
      "internalType": "struct Call[]",
      "name": "calls",
      "type": "tuple[]"
    }],
    "name": "executeBatch",
    "outputs": [],
    "stateMutability": "payable",
    "type": "function"
  }
]`)))

kernelExecuteABI is used to execute a transaction on Zerodev Kernel smart account.

Functions

func BuildCallData

func BuildCallData(swt Type, calls Calls) ([]byte, error)

func GetAccountAddress

func GetAccountAddress(ctx context.Context, provider ethereum.ContractCaller, config Config, entryPointAddress, owner common.Address, index decimal.Decimal) (common.Address, error)

func GetBiconomyFactoryCallData added in v0.0.40

func GetBiconomyFactoryCallData(owner common.Address, index decimal.Decimal, ecdsaValidator common.Address) ([]byte, error)

GetKernelInitData returns the calldata needed call the factory to deploy a Biconomy smart account.

func GetBiconomyInitCode

func GetBiconomyInitCode(owner common.Address, index decimal.Decimal, factory, ecdsaValidator common.Address) ([]byte, error)

GetBiconomyInitCode returns the init code for a Biconomy smart account. The init code deploys a smart account if it is not already deployed. NOTE: this was NOT tested. Use at your own risk or wait for the package to be updated.

func GetBiconomyInitCodeFromFactoryCallData added in v0.0.40

func GetBiconomyInitCodeFromFactoryCallData(factory common.Address, factoryCallData []byte) ([]byte, error)

GetBiconomyInitCodeFromFactoryCallData returns the init code for a Biconomy smart account, given the Biconomy Factory calldata. The init code deploys a smart account if it is not already deployed.

func GetFactoryCallData added in v0.0.40

func GetFactoryCallData(smartWalletConfig Config, ownerAddress common.Address, index decimal.Decimal) ([]byte, error)

GetFactoryCallData returns the calldata for a smart wallet factory to deploy a wallet of a given type for a given smart wallet owner with a given wallet index. The smart wallet factory is later called with this calldata to deploy the smart wallet.

NOTE: this does NOT constitute an InitCode (defined by ERC-4337) sufficient to deploy a smart wallet in a user operation. For this purpose, use `GetInitCode` instead.

Parameters: - smartWalletConfig: the configuration for the smart wallet - ownerAddress: the address of the owner of the smart wallet - index: the index of the smart wallet

Returns: - the calldata for the smart wallet factory - an error if the calldata could not be built

func GetInitCode

func GetInitCode(smartWalletConfig Config, ownerAddress common.Address, index decimal.Decimal) ([]byte, error)

GetInitCode builds the InitCode (defined in the ERC-4337) for a smart wallet. The InitCode is used to deploy a smart wallet if it is not already deployed.

Parameters: - smartWalletConfig: the configuration for the smart wallet - ownerAddress: the address of the owner of the smart wallet - index: the index of the smart wallet

Returns: - the InitCode for the smart wallet - an error if the InitCode could not be built

func GetInitCodeFromFactoryCallData added in v0.0.40

func GetInitCodeFromFactoryCallData(smartWalletConfig Config, factoryCallData []byte) ([]byte, error)

GetInitCodeFromFactoryCallData builds the InitCode (defined in the ERC-4337) from a factory calldata, that contains the deployment data for the smart wallet. This is another way to get the InitCode for a smart wallet, as owner and index are basically included in the factory call data.

Parameters: - smartWalletConfig: the configuration for the smart wallet - factoryCallData: the calldata for the factory

Returns: - the InitCode for the smart wallet - an error if the InitCode could not be built

func GetKernelFactoryCallData added in v0.0.40

func GetKernelFactoryCallData(owner common.Address, index decimal.Decimal, accountLogic, ecdsaValidator common.Address) ([]byte, error)

GetKernelInitData returns the calldata needed call the factory to deploy a Zerodev Kernel smart account.

func GetKernelInitCode

func GetKernelInitCode(owner common.Address, index decimal.Decimal, factory, accountLogic, ecdsaValidator common.Address) ([]byte, error)

GetKernelInitCode the init code for a Zerodev Kernel smart account. The init code deploys a smart account if it is not already deployed.

func GetKernelInitCodeFromFactoryCallData added in v0.0.40

func GetKernelInitCodeFromFactoryCallData(factory common.Address, factoryCallData []byte) ([]byte, error)

GetKernelInitCodeFromFactoryCallData returns the init code for a Zerodev Kernel smart account, given the Kernel Factory calldata. The init code deploys a smart account if it is not already deployed.

func IsAccountDeployed

func IsAccountDeployed(ctx context.Context, provider ethereum.ChainStateReader, swAddress common.Address) (bool, error)

Types

type Call

type Call struct {
	To       common.Address
	Value    *big.Int // wei
	CallData []byte
}

Call represents sufficient data to build a single transaction, which is a part of a user operation to be executed in a batch with other ones.

type Calls

type Calls []Call

func UnpackCallsForKernel

func UnpackCallsForKernel(data []byte) (Calls, error)

UnpackCallsForKernel unpacks CallData for Zerodev Kernel smart wallet.

func UnpackCallsForSimpleAccount

func UnpackCallsForSimpleAccount(data []byte) (Calls, error)

UnpackCallsForSimpleAccount unpacks CallData for SimpleAccount smart wallet.

func (Calls) PackForKernel

func (calls Calls) PackForKernel() ([]byte, error)

handleCallKernel packs calldata for Zerodev Kernel smart wallet.

func (Calls) PackForSimpleAccount

func (calls Calls) PackForSimpleAccount() ([]byte, error)

PackForSimpleAccount packs CallData for SimpleAccount smart wallet.

type Config

type Config struct {
	Type           *Type          `yaml:"type" env:"SMART_WALLET_TYPE"`
	ECDSAValidator common.Address `yaml:"ecdsa_validator" env:"SMART_WALLET_ECDSA_VALIDATOR"`
	Logic          common.Address `yaml:"logic" env:"SMART_WALLET_LOGIC"`
	Factory        common.Address `yaml:"factory" env:"SMART_WALLET_FACTORY"`
}

SmartWalletConfig represents the configuration for the smart wallet to be used with the client.

func (*Config) Init

func (sw *Config) Init()

type Type

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

Type represents an enum for supported ERC-4337 smart wallets that can be used with the client to send user operations from.

func (*Type) SetValue

func (t *Type) SetValue(s string) error

SetValue implements the cleanenv.Setter interface.

func (Type) String

func (t Type) String() string

String returns the string representation of a SmartWalletType.

func (*Type) UnmarshalJSON

func (t *Type) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the JSON representation of a SmartWalletType.

func (*Type) UnmarshalYAML

func (t *Type) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML unmarshals the YAML representation of a SmartWalletType.

Jump to

Keyboard shortcuts

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