tbsdk

package module
v0.1.5-beta.2 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 12 Imported by: 0

README

TB-SDK: THE BUNDLER SDK

This golang SDK is designed to aid in the integration with the ERC-4773 Account Abstractions Bundler component. The TB-SDK provides a comprehensive suite of functions to interact directly with a bundler instance, streamlining the process of preparing, executing, and managing user operations on the blockchain.

Supported Methods

eth_ Namespace

Methods defined by the ERC-4337 spec.

Method Supported
eth_chainId
eth_supportedEntryPoints
eth_estimateUserOperationGas
eth_sendUserOperation
eth_getUserOperationByHash
eth_getUserOperationReceipt
debug_ Namespace

Method defined by the ERC-4337 spec. Used only for debugging/testing and should be disabled on production APIs.

Method Supported
debug_bundler_clearState 🚧
debug_bundler_dumpMempool 🚧
debug_bundler_sendBundleNow 🚧
debug_bundler_setBundlingMode 🚧
debug_bundler_setReputation 🚧
debug_bundler_dumpReputation 🚧
debug_bundler_addUserOps 🚧

Sample Usage

- Bundler
package main

import (
    tbsdk "github.com/e4coder/tb-sdk"
)

func main() {
    bundler := tbsdk.NewBundler()
    err := bundler.Init("http://localhost:3000")
    if err != nil {
        panic(err)
    }

    userOp := tbsdk.NewOperationBuilder().
        Nonce(big.NewInt(123)).
        Sender(sender).
        CallData(data).
        FactoryAndData(&factoryAddress, factoryData).
        Signature(signature).
        Build()

    // RPC -> eth_estimateUserOperationGas
    rpcResponse, err := bundler.Eth_estimateUserOperationGas(userOp)
    if err != nil {
        panic(err)
    }

    fmt.Println(rpcResponse.Result)
}

- CounterfactualAddress
package main

import (
    tbsdk "github.com/e4coder/tb-sdk"
)

func main() {
	owner, _ := hex.DecodeString("494E8f1c10bb14Bea02C2f16cFB33a84BC57ef74")
	factory, _ := hex.DecodeString("4BBa2E1c4856228c0572f7b64f14916E2F091391")
	ar, _ := hex.DecodeString("F235B58DC3b2169136A857B06aaedcE1aEC4c667")
	ep, _ := hex.DecodeString("8024A70A99d35FF24Cacc861e946945530ee96A3")
	salt, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000001")
	creationCode, _ := hex.DecodeString(TestAccountContractCreationCode) // look at create2_test.go

	initCode := tbsdk.AbiEncodePacked(creationCode, tbsdk.AbiEncode(
		owner,
		ar,
		ep,
		factory,
	))

	address := tbsdk.ComputeCounterfactualAddress(factory, salt, initCode)

	// https://mumbai.polygonscan.com/address/0xd40aeab1d9e7c57523c2f5381f79c9738a73fe2d#internaltx
	deployedAddress := "0xd40aeab1d9e7c57523c2f5381f79c9738a73fe2d"

	if address != deployedAddress {
        panic("error")
	}
}

Contributing

Feel free to open pull requests and issues, the project is in active development any and all help is very much appreciated

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Client *http.Client = &http.Client{}
View Source
var ErrRequestCreationFailed = errors.New("request object failure")

Functions

func AbiEncode

func AbiEncode(args ...[]byte) []byte

TODO: input type ...interface{} output ([]byte, error)

func AbiEncodePacked

func AbiEncodePacked(args ...[]byte) []byte

TODO: input type ...interface{} output ([]byte, error)

func ComputeCounterfactualAddress

func ComputeCounterfactualAddress(sender, salt, initCode []byte) string

initCode = creationCode + packed(constructorArguments)

func DecodeResult

func DecodeResult[T Decoder](r interface{}, dest T) error

func GetUserOperationHash

func GetUserOperationHash(request *PackedUserOp, chainID int64, entryPointAddress common.Address) ([]byte, error)

func PackUserOperation

func PackUserOperation(request *PackedUserOp) ([]byte, error)

func PrepareRPCCall

func PrepareRPCCall(endpoint, method string, params interface{}) (*http.Request, error)

func SignDataWithEthereumPrivateKey

func SignDataWithEthereumPrivateKey(data []byte, privKey *ecdsa.PrivateKey) ([]byte, error)

func SignUserOp

func SignUserOp()

Types

type Adapter

type Adapter func(...AdapterArgs) string
var ADDRESS_ADAPTER Adapter = func(aa ...AdapterArgs) string {
	address, ok := aa[0].(*common.Address)
	if !ok || address == nil {
		return common.Address{}.Hex()
	}

	return address.Hex()
}
var ADDRESS_PACKED_DATA_ADAPTER Adapter = func(aa ...AdapterArgs) string {
	factoryAddress, ok := aa[0].(*common.Address)
	if !ok || factoryAddress == nil {
		return "0x"
	}

	factoryHex := factoryAddress.Hex()

	if len(aa) != 2 {
		return factoryHex
	}

	args, ok := aa[1].([]interface{})
	if !ok || len(args) != 1 {
		return factoryHex
	}

	factoryAddressData, ok := args[0].([]byte)
	if !ok || len(factoryAddressData) == 0 {
		return factoryHex
	}

	return strings.Join([]string{factoryHex, hex.EncodeToString(factoryAddressData)}, "")
}
var BIG_INT_ADAPTER Adapter = func(aa ...AdapterArgs) string {
	nonce, ok := aa[0].(*big.Int)
	if !ok || nonce == nil {
		return ""
	}

	return "0x" + nonce.Text(16)
}
var PACKED_DATA_ADAPTER Adapter = func(aa ...AdapterArgs) string {
	callData, ok := aa[0].([]byte)
	if !ok || len(callData) == 0 {
		return "0x"
	}

	return strings.Join([]string{"0x", hex.EncodeToString(callData)}, "")
}

type AdapterArgs

type AdapterArgs interface{}

type AddressArrayEntryPoints

type AddressArrayEntryPoints struct {
	Value []common.Address
}

func (*AddressArrayEntryPoints) Decode

func (i *AddressArrayEntryPoints) Decode(rawResult interface{}) error

type Bundler

type Bundler struct {
	RpcUri      string
	ChainId     *big.Int
	EntryPoints []common.Address
}

func NewBundler

func NewBundler() *Bundler

func (*Bundler) Debug_clearState

func (b *Bundler) Debug_clearState()

func (*Bundler) Debug_dumpMempool

func (b *Bundler) Debug_dumpMempool()

func (*Bundler) Debug_dumpReputation

func (b *Bundler) Debug_dumpReputation()

func (*Bundler) Debug_sendBundleNow

func (b *Bundler) Debug_sendBundleNow()

func (*Bundler) Debug_setBundlingMode

func (b *Bundler) Debug_setBundlingMode()

func (*Bundler) Debug_setReputation

func (b *Bundler) Debug_setReputation()

func (*Bundler) Eth_chainId

func (b *Bundler) Eth_chainId() (*RpcResponse, error)

func (*Bundler) Eth_estimateUserOperationGas

func (b *Bundler) Eth_estimateUserOperationGas(userOp *PackedUserOp, entrypoint *common.Address) (*RpcResponse, error)

func (*Bundler) Eth_getUserOperationByHash

func (b *Bundler) Eth_getUserOperationByHash(userOpHash string) (*RpcResponse, error)

func (*Bundler) Eth_getUserOperationReceipt

func (b *Bundler) Eth_getUserOperationReceipt(userOpHash string) (*RpcResponse, error)

func (*Bundler) Eth_sendUserOperation

func (b *Bundler) Eth_sendUserOperation(userOp *PackedUserOp, entrypoint *common.Address) (*RpcResponse, error)

func (*Bundler) Eth_supportedEntryPoints

func (b *Bundler) Eth_supportedEntryPoints() (*RpcResponse, error)

func (*Bundler) Init

func (b *Bundler) Init(rpcUri string) error

func (*Bundler) Rundler_maxPriorityFeePerGas

func (b *Bundler) Rundler_maxPriorityFeePerGas() (*RpcResponse, error)

type Config

type Config struct {
}

type Decoder

type Decoder interface {
	Decode(interface{}) error
}

type Int64ChainId

type Int64ChainId struct {
	Value int64
}

func (*Int64ChainId) Decode

func (i *Int64ChainId) Decode(rawResult interface{}) error

type OperationBuilder

type OperationBuilder struct {
	Operation UserOp
}

func NewOperationBuilder

func NewOperationBuilder() *OperationBuilder

func (*OperationBuilder) Adapt

func (ob *OperationBuilder) Adapt(adapter Adapter, val interface{}, args ...interface{}) string

func (*OperationBuilder) Build

func (ob *OperationBuilder) Build() *PackedUserOp

func (*OperationBuilder) BuildWithOperationHash

func (ob *OperationBuilder) BuildWithOperationHash(chainId *big.Int, entryPoint common.Address) (*PackedUserOp, []byte, error)

func (*OperationBuilder) BuildWithSignature

func (ob *OperationBuilder) BuildWithSignature(chainId *big.Int, entryPoint common.Address, privKey *ecdsa.PrivateKey) (*PackedUserOp, error)

func (*OperationBuilder) CallData

func (ob *OperationBuilder) CallData(callData []byte) *OperationBuilder

func (*OperationBuilder) CallGasLimit

func (ob *OperationBuilder) CallGasLimit(callGasLimit *big.Int) *OperationBuilder

func (*OperationBuilder) Factory

func (ob *OperationBuilder) Factory(factory *common.Address) *OperationBuilder

func (*OperationBuilder) FactoryAndData

func (ob *OperationBuilder) FactoryAndData(factory *common.Address, factoryCallData []byte) *OperationBuilder

func (*OperationBuilder) FactoryData

func (ob *OperationBuilder) FactoryData(factoryCallData []byte) *OperationBuilder

func (*OperationBuilder) MaxFeePerGas

func (ob *OperationBuilder) MaxFeePerGas(maxFeePerGas *big.Int) *OperationBuilder

func (*OperationBuilder) MaxPriorityFeePerGas

func (ob *OperationBuilder) MaxPriorityFeePerGas(maxPriorityFeePerGas *big.Int) *OperationBuilder

func (*OperationBuilder) Nonce

func (ob *OperationBuilder) Nonce(nonce *big.Int) *OperationBuilder

func (*OperationBuilder) Paymaster

func (ob *OperationBuilder) Paymaster(paymaster *common.Address) *OperationBuilder

func (*OperationBuilder) PaymasterAndData

func (ob *OperationBuilder) PaymasterAndData(paymaster *common.Address, paymasterCallData []byte) *OperationBuilder

func (*OperationBuilder) PaymasterData

func (ob *OperationBuilder) PaymasterData(paymasterCallData []byte) *OperationBuilder

func (*OperationBuilder) PaymasterPostOpGasLimit

func (ob *OperationBuilder) PaymasterPostOpGasLimit(paymasterPostOpGasLimit *big.Int) *OperationBuilder

func (*OperationBuilder) PaymasterVerificationGasLimit

func (ob *OperationBuilder) PaymasterVerificationGasLimit(paymasterVerificationGasLimit *big.Int) *OperationBuilder

func (*OperationBuilder) PreVerificationGas

func (ob *OperationBuilder) PreVerificationGas(preVerificationGas *big.Int) *OperationBuilder

func (*OperationBuilder) Sender

func (ob *OperationBuilder) Sender(sender *common.Address) *OperationBuilder

func (*OperationBuilder) Signature

func (ob *OperationBuilder) Signature(sig string) *OperationBuilder

func (*OperationBuilder) VerificationGasLimit

func (ob *OperationBuilder) VerificationGasLimit(verificationGasLimit *big.Int) *OperationBuilder

type PackedUserOp

type PackedUserOp struct {
	Sender               string `json:"sender"`
	Nonce                string `json:"nonce"`
	InitCode             string `json:"initCode"`
	CallData             string `json:"callData"`
	CallGasLimit         string `json:"callGasLimit,omitempty"`
	VerificationGasLimit string `json:"verificationGasLimit,omitempty"`
	PreVerificationGas   string `json:"preVerificationGas,omitempty"`
	MaxFeePerGas         string `json:"maxFeePerGas,omitempty"`
	MaxPriorityFeePerGas string `json:"maxPriorityFeePerGas,omitempty"`
	PaymasterAndData     string `json:"paymasterAndData"`
	Signature            string `json:"signature"`
}

type RpcError

type RpcError struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

type RpcRequest

type RpcRequest struct {
	Jsonrpc string      `json:"jsonrpc"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params"`
	Id      int         `json:"id"`
}

type RpcResponse

type RpcResponse struct {
	Jsonrpc string      `json:"jsonrpc"`
	Result  interface{} `json:"result"`
	Error   interface{} `json:"error"`
	Id      int         `json:"id"`
}

func HandleRpcRequest

func HandleRpcRequest(request *http.Request, client *http.Client) (*RpcResponse, error)

type UserOp

type UserOp struct {
	Sender                        *common.Address
	Nonce                         *big.Int
	Factory                       *common.Address
	FactoryData                   []byte
	CallData                      []byte
	CallGasLimit                  *big.Int
	VerificationGasLimit          *big.Int
	PreVerificationGas            *big.Int
	MaxFeePerGas                  *big.Int
	MaxPriorityFeePerGas          *big.Int
	Paymaster                     *common.Address
	PaymasterVerificationGasLimit *big.Int
	PaymasterPostOpGasLimit       *big.Int
	PaymasterData                 []byte
	Signature                     string
}

Jump to

Keyboard shortcuts

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