goether

package module
v1.1.9 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 16 Imported by: 26

README

goether

A simple Ethereum wallet implementation and utilities in Golang.

Install

go get -u github.com/everFinance/goether

Examples

More examples...

Send Eth with data

prvHex := "your prvkey"
rpc := "https://infura.io/v3/{{InfuraKey}}"

testWallet, err := goether.NewWallet(prvHex, rpc)
if err != nil {
  panic(err)
}
// default send DynamicFeeTx
txHash, err := testWallet.SendTx(
  common.HexToAddress("0xa06b79E655Db7D7C3B3E7B2ccEEb068c3259d0C9"), // To
  goether.EthToBN(0.12), // Value
  []byte("123"),         // Data
  nil)

// send with opts
nonce := int(1)
gasLimit := int(999999)
txHash, err := testWallet.SendTx(
  common.HexToAddress("0xa06b79E655Db7D7C3B3E7B2ccEEb068c3259d0C9"),
  goether.EthToBN(0.12),
  []byte("123"),
  &goether.TxOpts{ // Configure nonce/gas yourself
    Nonce: &nonce,
    GasLimit: &gasLimit,
    GasPrice: goether.GweiToBN(10),
  })

Contract Interaction

abi := `[{"constant": true,"inputs": [{"name": "","type": "address"}],"name": "balanceOf","outputs": [{"name": "","type": "uint256"}],"payable": false,"stateMutability": "view","type": "function"},{"constant": false,"inputs": [{"name": "dst","type": "address"},{"name": "wad","type": "uint256"}],"name": "transfer","outputs": [{"name": "","type": "bool"}],"payable": false,"stateMutability": "nonpayable","type": "function"}]`
contractAddr := common.HexToAddress("contract address")
prvHex := "your prvkey"
rpc := "https://kovan.infura.io/v3/{{InfuraKey}}"

// init contract instance with wallet
testWallet, _ := goether.NewWallet(prvHex, rpc)
testContract, err := goether.NewContract(contractAddr, abi, rpc, testWallet)
if err != nil {
  panic(err)
}

// ERC20 BalanceOf
amount, err := testContract.CallMethod(
  "balanceOf", // Method name
  "latest", // Tag
  common.HexToAddress("0xa06b79e655db7d7c3b3e7b2cceeb068c3259d0c9")) // Args

// ERC20 Transfer
txHash, err := testContract.ExecMethod(
  "transfer", // Method name
  &goether.TxOpts{ // Configure nonce/gas yourself, nil load params from eth-node
    Nonce: &nonce,
    GasLimit: &gasLimit,
    GasPrice: goether.GweiToBN(10),
  },
  common.HexToAddress("0xab6c371B6c466BcF14d4003601951e5873dF2AcA"), // Args
  big.NewInt(100))

Modules

Signer

Ethereum Account which can be used to sign messages and transactions.

  • NewSignerFromMnemonic
  • SignTx
  • SignMsg
  • SignTypedData
  • GetPublicKey
  • GetPublicKeyHex
  • GetPrivateKey
  • Decrypt
Wallet

Connect to Ethereum Network, execute state changing operations.

  • SendTx
  • SendLegacyTx
  • GetAddress
  • GetBalance
  • GetNonce
  • GetPendingNonce
Contract

Creating Contract Instance for call & execute contract.

  • CallMethod
  • ExecMethod
  • EncodeData
  • EncodeDataHex
  • DecodeData
  • DecodeDataHex
  • DecodeEvent
  • DecodeEventHex
Utils
  • EthToBN
  • GweiToBN
  • EIP712Hash
  • Ecrecover
  • Encrypt

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EIP712Hash added in v1.1.3

func EIP712Hash(typedData apitypes.TypedData) (hash []byte, err error)

func Ecrecover

func Ecrecover(hash, signature []byte) (publicBy []byte, address common.Address, err error)

func Encrypt added in v1.1.5

func Encrypt(publicKey string, message []byte) ([]byte, error)

Encrypt encrypt

func EthToBN

func EthToBN(amount float64) (bn *big.Int)

func GweiToBN

func GweiToBN(amount float64) (bn *big.Int)

Types

type Contract

type Contract struct {
	Address common.Address
	ABI     abi.ABI

	Wallet *Wallet
	Client *ethrpc.EthRPC
}

func NewContract

func NewContract(address common.Address, abiStr, rpc string, wallet *Wallet) (*Contract, error)

func (*Contract) CallMethod

func (c *Contract) CallMethod(methodName, tag string, args ...interface{}) (res string, err error)

CallMethod Only read contract status tag:

	HEX String - an integer block number
 String "earliest" for the earliest/genesis block
 String "latest" - for the latest mined block
 String "pending" - for the pending state/transactions

func (*Contract) DecodeData added in v1.1.2

func (c *Contract) DecodeData(data []byte) (methodName string, params map[string]interface{}, err error)

func (*Contract) DecodeDataHex added in v1.1.2

func (c *Contract) DecodeDataHex(dataHex string) (methodName string, params map[string]interface{}, err error)

func (*Contract) DecodeEvent added in v1.1.2

func (c *Contract) DecodeEvent(topics []common.Hash, data []byte) (eventName string, values map[string]interface{}, err error)

func (*Contract) DecodeEventHex added in v1.1.2

func (c *Contract) DecodeEventHex(topicsHex []string, dataHex string) (eventName string, values map[string]interface{}, err error)

func (*Contract) EncodeData added in v1.1.2

func (c *Contract) EncodeData(methodName string, args ...interface{}) ([]byte, error)

func (*Contract) EncodeDataHex added in v1.1.2

func (c *Contract) EncodeDataHex(methodName string, args ...interface{}) (hex string, err error)

func (*Contract) ExecMethod

func (c *Contract) ExecMethod(methodName string, opts *TxOpts, args ...interface{}) (txHash string, err error)

ExecMethod Execute tx

func (*Contract) GetAddress

func (c *Contract) GetAddress() string

type Signer

type Signer struct {
	Address common.Address
	// contains filtered or unexported fields
}

func NewSigner

func NewSigner(prvHex string) (*Signer, error)

func NewSignerFromPath

func NewSignerFromPath(prvPath string) (*Signer, error)

func (Signer) Decrypt added in v1.1.5

func (s Signer) Decrypt(ct []byte) ([]byte, error)

Decrypt decrypt

func (Signer) GetPrivateKey added in v1.1.5

func (s Signer) GetPrivateKey() *ecdsa.PrivateKey

func (Signer) GetPublicKey added in v1.1.5

func (s Signer) GetPublicKey() []byte

func (Signer) GetPublicKeyHex added in v1.1.5

func (s Signer) GetPublicKeyHex() string

func (*Signer) SignLegacyTx added in v1.1.7

func (s *Signer) SignLegacyTx(
	nonce int, to common.Address, amount *big.Int,
	gasLimit int, gasPrice *big.Int,
	data []byte, chainID *big.Int,
) (tx *types.Transaction, err error)

func (Signer) SignMsg

func (s Signer) SignMsg(msg []byte) (sig []byte, err error)

func (*Signer) SignTx

func (s *Signer) SignTx(
	nonce int, to common.Address, amount *big.Int,
	gasLimit int, gasTipCap *big.Int, gasFeeCap *big.Int,
	data []byte, chainID *big.Int,
) (tx *types.Transaction, err error)

SignTx DynamicFeeTx

func (Signer) SignTypedData added in v1.1.3

func (s Signer) SignTypedData(typedData apitypes.TypedData) (sig []byte, err error)

type TxOpts

type TxOpts struct {
	Nonce     *int
	GasLimit  *int
	GasPrice  *big.Int
	GasTipCap *big.Int
	GasFeeCap *big.Int
}

type Wallet

type Wallet struct {
	Address common.Address
	ChainID *big.Int

	Signer *Signer
	Client *ethrpc.EthRPC
}

func NewWallet

func NewWallet(prvHex, rpc string) (*Wallet, error)

func NewWalletFromPath

func NewWalletFromPath(prvPath, rpc string) (*Wallet, error)

func (*Wallet) GetAddress

func (w *Wallet) GetAddress() string

func (*Wallet) GetBalance

func (w *Wallet) GetBalance() (balance big.Int, err error)

func (*Wallet) GetNonce

func (w *Wallet) GetNonce() (nonce int, err error)

func (*Wallet) GetPendingNonce

func (w *Wallet) GetPendingNonce() (nonce int, err error)

func (*Wallet) SendLegacyTx added in v1.1.7

func (w *Wallet) SendLegacyTx(to common.Address, amount *big.Int, data []byte, opts *TxOpts) (txHash string, err error)

func (*Wallet) SendTx

func (w *Wallet) SendTx(to common.Address, amount *big.Int, data []byte, opts *TxOpts) (txHash string, err error)

Jump to

Keyboard shortcuts

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