eth

package
v0.0.0-...-948650a Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package eth provides general functionality for interacting with the Ethereum network, smart contracts, etc.

Index

Constants

View Source
const DefaultHDPathPrefix = HDPathPrefix("m/44'/60'/0'/0/")

DefaultHDPathPrefix is the default format for derived accounts when using SignerFromSeedPhrase().

View Source
const DialerFlag = "dialer_eth_node_url"

DialerFlag is the flag name configured by NewDialerFromFlags.

View Source
const Symbol = `Ξ`

Symbol is the ETH symbol.

Variables

View Source
var ErrBlockNotFound = errors.New("block not found")

ErrBlockNotFound is returned by LastBlockBy if no block was mined by the requested time.

Functions

func AddressPerLine

func AddressPerLine(r io.Reader) ([]common.Address, error)

AddressPerLine returns AddressesFromReader(r, bufio.ScanLines). It therefore reads each non-empty line of r and parses it as an Address. Empty lines are skipped.

func AddressesFromReader

func AddressesFromReader(r io.Reader, split bufio.SplitFunc) ([]common.Address, error)

AddressesFromReader scans the Reader, splitting according to the split function, and parses each non-empty chunk as an address after trimming surrounding space (i.e. the split function does not have to trim). Empty chunks are skipped.

func Ether

func Ether(e int64) *big.Int

Ether returns e in Wei.

func EtherFraction

func EtherFraction(numerator, denominator int64) *big.Int

EtherFraction returns numerator/denominator ETH in Wei.

func LastBlockBy

func LastBlockBy(ctx context.Context, blocks BlockFetcher, minedBy uint64, hint *BlockRange) (_ *types.Block, retErr error)

LastBlockBy performs a binary search to find and return the last block mined by the specified unix timestamp, inclusive. If a nil hint is provided, the search defaults to [0,blocks.BlockNumber()]. If hint.Last == 0, it defaults to the latest block.

func NewMnemonic

func NewMnemonic(bitSize int) (string, error)

NewMnemonic is a convenience wrapper around go-bip39 entropy and mnemonic creation.

func RWDemuxBackend

func RWDemuxBackend(r, w bind.ContractBackend) bind.ContractBackend

An RWDemuxBackend splits calls to ContractBackend methods to a read-only and write-only backend. This is useful when using a regular node (e.g. Infura) for reading, but Flashbots for writing.

Everything except for SendTransaction() and EstimateGas() are sent to the read backend. EstimateGas is included as a "write" because it may leak sensitive information.

func RandFromHash

func RandFromHash(h common.Hash) *rand.Rand

RandFromHash returns a new *rand.Rand seeded by the Hash. The Hash is converted to a uint256 and each of its four uint64 words are folded into one by xor. The raw bits of the resulting uint64 are treated as an int64 passed to rand.NewSource().

func WithPersonalMessagePrefix

func WithPersonalMessagePrefix(message []byte) []byte

WithPersonalMessagePrefix converts a given message to conform to the signed data standard according to EIP-191.

Types

type BlockFetcher

type BlockFetcher interface {
	BlockNumber(context.Context) (uint64, error)
	BlockByNumber(context.Context, *big.Int) (*types.Block, error)
}

A BlockFetcher can fetch information about blocks. Typically this would be an *ethclient.Client.

type BlockRange

type BlockRange struct {
	First, Last uint64
}

BlockRange is an inclusive range of block numbers.

type Dialer

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

A Dialer dials an Ethereum node URL that it sources from a secret store. This is to protect API keys that may be stored in the URL.

func MustNewDialerFromFlag

func MustNewDialerFromFlag(fs *flag.FlagSet, defaultNodeURL *secrets.Secret, opts ...secrets.Option) *Dialer

func NewDialer

func NewDialer(nodeURL *secrets.Secret, opts ...secrets.Option) *Dialer

NewDialer returns a Dialer that sources its node URL from the given Secret.

func NewDialerFromFlag

func NewDialerFromFlag(fs *flag.FlagSet, defaultNodeURL *secrets.Secret, opts ...secrets.Option) (*Dialer, error)

NewDialerFromFlag returns a Dialer that is configurable via command-line flags; see DialerFlag. The Options are propagated when Fetch()ing the Secret.

func (*Dialer) Dial

func (c *Dialer) Dial(ctx context.Context) (*ethclient.Client, error)

Dial Fetch()es the Dialer's secret node URL and returns ethclient.DialContext(ctx, [secret]).

type HDPathPrefix

type HDPathPrefix string

An HDPathPrefix is a prefix for use in deriving private keys from BIP39 mnemonics. It is appended with the account number. Values MUST include a trailing slash.

func (HDPathPrefix) SignerFromPRF

func (hdp HDPathPrefix) SignerFromPRF(src prf.PRF, input []byte, account uint) (*Signer, error)

SignerFromPRF deterministically derives a private key from the pseudo-random function and the input bytes. By definition, the output of a PRF is indistinguishable from a random function.

The input parameter allows for different sets of HD wallets to be derived from the same underlying PRF key. Only the PRF key need be secret.

SignerFromPRF can be thought of as a method for securely creating new mnemonic seed phrases from a single underlying key and different input parameters. Although the resulting mnemonic is accessible, SignerFromPRF is intended for use in an automated environment, which is why it relies on Google Tink.

func (HDPathPrefix) SignerFromPRFSet

func (hdp HDPathPrefix) SignerFromPRFSet(set *prf.Set, input []byte, account uint) (*Signer, error)

SignerFromPRFSet returns hdp.SifnerFromPRF() using the set's primary PRF. This is simply a convenience function as the prf package doesn't accomodate direct creation of a prf.PRF.

func (HDPathPrefix) SignerFromSeedPhrase

func (hdp HDPathPrefix) SignerFromSeedPhrase(mnemonic, password string, account uint) (*Signer, error)

SignerFromSeedPhrase confirms that the mnemonic is valid under BIP39 and then uses it to derive a private key (see HDPathF)

type NullableAddress

type NullableAddress struct {
	common.Address
	Valid bool
}

A NullableAddress contains a common.Address that can be flagged as being Valid or not (i.e. null). Unlike a pointer, a NullableAddress can be reliably marshalled to and from JSON and CSV. The zero value is equivalent to null.

func (NullableAddress) MarshalCSV

func (n NullableAddress) MarshalCSV() (string, error)

MarshalCSV marshals the Address to a hex string. It returns ("", nil) if Null.

func (NullableAddress) MarshalJSON

func (n NullableAddress) MarshalJSON() ([]byte, error)

MarshalJSON marshals the Address to JSON. It returns []byte("null"), nil if Null (i.e. explicit JSON null). A non-Null marshalled value is a hex string.

func (*NullableAddress) UnmarshalCSV

func (n *NullableAddress) UnmarshalCSV(s string) error

UnmarshalCSV unmarshals the Address from a hex string.

func (*NullableAddress) UnmarshalJSON

func (n *NullableAddress) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the Address from JSON.

type NullableUint256

type NullableUint256 struct {
	uint256.Int
	Valid bool
}

A NullableUint256 contains a uint256.Int that can be flagged as being Valid or not (i.e. null). Unlike a pointer, a NullableUint256 can be reliably marshalled to and from JSON and CSV. The zero value is equivalent to null.

func (NullableUint256) MarshalCSV

func (n NullableUint256) MarshalCSV() (string, error)

MarshalCSV marshals the Int to a hex string. It returns ("", nil) if Null.

func (NullableUint256) MarshalJSON

func (n NullableUint256) MarshalJSON() ([]byte, error)

MarshalJSON marshals the Int to JSON. It returns []byte("null"), nil if Null (i.e. explicit JSON null). A non-Null marshalled value is a hex string.

func (*NullableUint256) UnmarshalCSV

func (n *NullableUint256) UnmarshalCSV(s string) error

UnmarshalCSV unmarshals the Int from a hex string.

func (*NullableUint256) UnmarshalJSON

func (n *NullableUint256) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the Int from JSON.

type Signer

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

A Signer abstracts signing of arbitrary messages by wrapping an ECDSA private key and, optionally, its associated BIP39 mnemonic.

func NewSigner

func NewSigner(bitSize int) (*Signer, error)

NewSigner is equivalent to DefaultHDPathPrefix.SignerFromSeedPhrase(NewMnemonic(), "", 0).

func (*Signer) Address

func (s *Signer) Address() common.Address

Address returns the Signer's public key converted to an Ethereum address.

func (*Signer) Mnemonic

func (s *Signer) Mnemonic() string

Mnemonic returns the mnemonic used to derive the Signer's private key. USE WITH CAUTION.

func (*Signer) PersonalSign

func (s *Signer) PersonalSign(buf []byte) ([]byte, error)

PersonalSign returns an EIP-191 conform personal ECDSA signature of buf Convenience wrapper for s.CompactSign(WithPersonalMessagePrefix(buf))

func (*Signer) PersonalSignAddress

func (s *Signer) PersonalSignAddress(addr common.Address) ([]byte, error)

SignAddress is a convenience wrapper for s.PersonalSign(addr.Bytes()).

func (*Signer) PersonalSignWithNonce

func (s *Signer) PersonalSignWithNonce(buf []byte) ([]byte, [32]byte, error)

PersonalSignWithNonce generates a 32-byte nonce with crypto/rand and returns s.PersonalSign(append(buf, nonce)).

func (*Signer) RawSign

func (s *Signer) RawSign(buf []byte) ([]byte, error)

RawSign returns an ECDSA signature of buf. USE WITH CAUTION as signed data SHOULD be hashed first to avoid chosen-plaintext attacks. Prefer Signer.Sign().

func (*Signer) Sign

func (s *Signer) Sign(buf []byte) ([]byte, error)

Sign returns an ECDSA signature of keccak256(buf).

func (*Signer) String

func (s *Signer) String() string

String returns s.Address() as a string.

func (*Signer) TransactorWithChainID

func (s *Signer) TransactorWithChainID(chainID *big.Int) (*bind.TransactOpts, error)

TransactorWithChainID returns bind.NewKeyedTransactorWithChainID(<key>, chainID) where <key> is the Signer's private key.

Jump to

Keyboard shortcuts

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