accounts

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

The package is designed to be flexible and adaptable to various Ethereum-compatible networks like Ethereum mainnet, Binance Smart Chain (BSC), and Polygon. It provides a structured approach to handle accounts across these networks, making it easier for developers to interact with different blockchain environments through a unified interface.

Index

Constants

View Source
const (
	DEFAULT_GAS_LIMIT = uint64(90000)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account struct {
	*keystore.KeyStore `json:"-" yaml:"-"` // KeyStore for managing account keys
	Address            common.Address      `json:"address" yaml:"address"`         // Ethereum address of the account
	Type               utils.AccountType   `json:"type" yaml:"type"`               // Account type
	PrivateKey         string              `json:"private_key" yaml:"private_key"` // Private key of the account
	PublicKey          string              `json:"public_key" yaml:"public_key"`   // Public key of the account
	KeystoreAccount    account.Account     `json:"account" yaml:"account"`         // Ethereum account information
	Password           string              `json:"password" yaml:"password"`       // Account's password
	Network            utils.Network       `json:"network" yaml:"network"`         // Network information
	Tags               []string            `json:"tags" yaml:"tags"`               // Arbitrary tags for the account
	// contains filtered or unexported fields
}

Account represents an Ethereum account with extended functionalities. It embeds ClientPool for network interactions and KeyStore for account management. It also includes fields for account details, network information, and additional tags.

func LoadAccount

func LoadAccount(path string) (*Account, error)

LoadAccount loads an account from a JSON file located at a given path. The method reads the file, unmarshals the JSON content into an Account struct, and returns the populated Account instance or an error if the loading process fails.

func (*Account) Balance

func (a *Account) Balance(ctx context.Context, blockNum *big.Int) (*big.Int, error)

Balance retrieves the account's balance from the Ethereum network at a specified block number. Returns the balance as *big.Int or an error if the balance query fails.

func (*Account) DecodePassword

func (a *Account) DecodePassword() (string, error)

DecodePassword decodes the base64-encoded password. Returns the decoded password or an error if decoding fails.

func (*Account) GetAddress

func (a *Account) GetAddress() common.Address

GetAddress retrieves the Ethereum address of the account.

func (*Account) GetClient

func (a *Account) GetClient() *clients.Client

GetClient retrieves the assigned Ethereum client associated with the account.

func (*Account) HasTag

func (a *Account) HasTag(tag string) bool

HasTag checks if the account has a specific tag. Returns true if the tag exists, false otherwise.

func (*Account) SaveToPath

func (a *Account) SaveToPath(path string) error

SaveToPath serializes the account information and writes it to a specified file path. The account data is saved in JSON format. Returns an error if the writing process fails.

func (*Account) SetAccountBalance

func (a *Account) SetAccountBalance(ctx context.Context, amount *big.Int) error

SetAccountBalance sets the account's balance to a specific amount. This method is mainly used for testing purposes in simulation environments like Anvil. It does not affect the real balance on the Ethereum network.

func (*Account) SetClient

func (a *Account) SetClient(client *clients.Client)

SetClient assigns a specific Ethereum client to the account for network interactions.

func (*Account) TransactOpts

func (a *Account) TransactOpts(client *clients.Client, amount *big.Int, simulate bool) (*bind.TransactOpts, error)

TransactOpts generates transaction options for interacting with the Ethereum network. It configures nonce, gas price, gas limit, and value for the transaction. The method also handles different account types: simple and keystore-based accounts. For simple accounts, it directly uses the provided private key. For keystore accounts, it decrypts the key using the stored password. If 'simulate' is true, it returns transaction options without signing.

func (*Account) Transfer

func (a *Account) Transfer(ctx context.Context, to common.Address, value *big.Int) (*types.Transaction, error)

Transfer initiates an Ethereum transfer from this account to another address. It ensures the account has sufficient balance and then constructs and signs a transaction. The method uses the account's stored passphrase for signing. Returns the signed transaction or an error if the transfer process fails.

type Manager

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

Manager handles the account operations across various Ethereum networks. It maintains a map of keystores and accounts for each supported network.

func NewManager

func NewManager(ctx context.Context, client *clients.ClientPool, cfg *Options) (*Manager, error)

NewManager initializes a new Manager instance. It checks for the existence of keystore paths and supported networks, and loads existing accounts from the keystore.

func (*Manager) Create

func (m *Manager) Create(network utils.Network, password string, pin bool, tags ...string) (*Account, error)

Create creates a new account for a given network with a specified password and optional tags. It saves the account to the network's keystore path and adds it to the accounts map.

func (*Manager) Delete

func (m *Manager) Delete(network utils.Network, address common.Address) error

Delete removes an account for a given network by its address. It deletes the account from both the keystore and the Manager's accounts map. Returns an error if the account is not found or if there's an issue with deletion.

func (*Manager) Get

func (m *Manager) Get(network utils.Network, address common.Address) (*Account, error)

Get retrieves a specific account by its address for a given network. Returns an error if the account is not found.

func (*Manager) GetConfig

func (m *Manager) GetConfig() *Options

GetConfig returns the configuration options of the Manager.

func (*Manager) GetKeystore

func (m *Manager) GetKeystore(network utils.Network) (*keystore.KeyStore, error)

GetKeystore retrieves the keystore for a given network. Returns an error if the network is not supported.

func (*Manager) GetNetworkPath

func (m *Manager) GetNetworkPath(network utils.Network) string

GetNetworkPath returns the file path for a given network's keystore.

func (*Manager) List

func (m *Manager) List(network utils.Network, tags ...string) []*Account

List lists all accounts for a given network, optionally filtered by tags.

func (*Manager) Load

func (m *Manager) Load() error

Load loads accounts from the keystore for each network. Returns an error if it fails to load accounts for any network.

type Options

type Options struct {
	// KeystorePath specifies the file system path to the directory where the keystore files are stored.
	// The keystore is used to securely store the private keys of Ethereum accounts.
	KeystorePath string `json:"keystore_path" yaml:"keystore_path"`

	// SupportedNetworks lists the Ethereum based networks that the account manager will interact with.
	// Each network has a corresponding keystore and set of account configurations.
	SupportedNetworks []utils.Network `json:"supported_networks" yaml:"supported_networks"`
}

Options defines the configuration parameters for account management.

Jump to

Keyboard shortcuts

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