wallet

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: MIT Imports: 14 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDifferentSeed = errors.New("seed differs from wallet seed")

ErrDifferentSeed is returned when a different seed is provided to NewSingleAddressWallet than was used to initialize the wallet

View Source
var (
	// ErrNotEnoughFunds is returned when there are not enough unspent outputs
	// to fund a transaction.
	ErrNotEnoughFunds = errors.New("not enough funds")
)

Functions

func ApplyChainUpdates added in v0.0.2

func ApplyChainUpdates(tx ApplyTx, address types.Address, updates []*chain.ApplyUpdate) error

ApplyChainUpdates atomically applies a batch of wallet updates

func IsRelevantTransaction

func IsRelevantTransaction(txn types.Transaction, addr types.Address) bool

IsRelevantTransaction returns true if the v1 transaction is relevant to the address

func KeyFromSeed

func KeyFromSeed(seed *[32]byte, index uint64) types.PrivateKey

KeyFromSeed returns the Ed25519 key derived from the supplied seed and index.

func NewSeedPhrase

func NewSeedPhrase() string

NewSeedPhrase generates a random seed phrase.

func RevertChainUpdate added in v0.0.2

func RevertChainUpdate(tx RevertTx, address types.Address, cru *chain.RevertUpdate) error

RevertChainUpdate atomically reverts a chain update from a wallet

func SeedFromPhrase

func SeedFromPhrase(seed *[32]byte, phrase string) error

SeedFromPhrase derives a 32-byte seed from the supplied phrase.

func SumOutputs

func SumOutputs(outputs []types.SiacoinElement) (sum types.Currency)

SumOutputs returns the total value of the supplied outputs.

Types

type ApplyTx added in v0.0.2

type ApplyTx interface {
	// WalletStateElements returns all state elements related to the wallet. It is used
	// to update the proofs of all state elements affected by the update.
	WalletStateElements() ([]types.StateElement, error)
	// UpdateStateElements updates the proofs of all state elements affected by the
	// update.
	UpdateStateElements([]types.StateElement) error

	// AddEvents is called with all relevant events added in the update.
	AddEvents([]Event) error
	// AddSiacoinElements is called with all new siacoin elements in the
	// update. Ephemeral siacoin elements are not included.
	AddSiacoinElements([]SiacoinElement) error
	// RemoveSiacoinElements is called with all siacoin elements that were
	// spent in the update.
	RemoveSiacoinElements([]types.SiacoinOutputID) error
}

ApplyTx is an interface for atomically applying a chain update to a single address wallet.

type Balance added in v0.0.2

type Balance struct {
	Spendable   types.Currency `json:"spendable"`
	Confirmed   types.Currency `json:"confirmed"`
	Unconfirmed types.Currency `json:"unconfirmed"`
	Immature    types.Currency `json:"immature"`
}

Balance is the balance of a wallet.

type ChainManager

type ChainManager interface {
	TipState() consensus.State
	BestIndex(height uint64) (types.ChainIndex, bool)

	PoolTransactions() []types.Transaction

	AddSubscriber(chain.Subscriber, types.ChainIndex) error
	RemoveSubscriber(chain.Subscriber)
}

A ChainManager manages the current state of the blockchain.

type Event added in v0.0.2

type Event struct {
	ID             types.Hash256     `json:"id"`
	Index          types.ChainIndex  `json:"index"`
	Inflow         types.Currency    `json:"inflow"`
	Outflow        types.Currency    `json:"outflow"`
	Transaction    types.Transaction `json:"transaction"`
	Source         EventSource       `json:"source"`
	MaturityHeight uint64            `json:"maturityHeight"`
	Timestamp      time.Time         `json:"timestamp"`
}

An Event is a transaction or other event that affects the wallet including miner payouts, siafund claims, and file contract payouts.

func (*Event) DecodeFrom added in v0.0.2

func (t *Event) DecodeFrom(d *types.Decoder)

DecodeFrom implements types.DecoderFrom.

func (Event) EncodeTo added in v0.0.2

func (t Event) EncodeTo(e *types.Encoder)

EncodeTo implements types.EncoderTo.

type EventSource added in v0.0.2

type EventSource string

An EventSource is a string indicating the source of a transaction.

const (
	EventSourceTransaction      EventSource = "transaction"
	EventSourceMinerPayout      EventSource = "miner"
	EventSourceSiafundClaim     EventSource = "siafundClaim"
	EventSourceValidContract    EventSource = "validContract"
	EventSourceMissedContract   EventSource = "missedContract"
	EventSourceFoundationPayout EventSource = "foundation"
)

transaction sources indicate the source of a transaction. Transactions can either be created by sending Siacoins between unlock hashes or they can be created by consensus (e.g. a miner payout, a siafund claim, or a contract).

type Option

type Option func(*config)

An Option is a configuration option for a wallet.

func WithDefragThreshold

func WithDefragThreshold(n int) Option

WithDefragThreshold sets the transaction defrag threshold.

func WithLogger

func WithLogger(l *zap.Logger) Option

WithLogger sets the logger for the wallet

func WithMaxDefragUTXOs

func WithMaxDefragUTXOs(n int) Option

WithMaxDefragUTXOs sets the maximum number of additional utxos that will be added to a transaction when defragging

func WithMaxInputsForDefrag

func WithMaxInputsForDefrag(n int) Option

WithMaxInputsForDefrag sets the maximum number of inputs a transaction can have to be considered for defragging

func WithReservationDuration

func WithReservationDuration(d time.Duration) Option

WithReservationDuration sets the duration that a reservation will be held on spent utxos

type RevertTx added in v0.0.2

type RevertTx interface {
	// WalletStateElements returns all state elements in the database. It is used
	// to update the proofs of all state elements affected by the update.
	WalletStateElements() ([]types.StateElement, error)
	// UpdateStateElements updates the proofs of all state elements affected by the
	// update.
	UpdateStateElements([]types.StateElement) error

	// RevertIndex is called with the chain index that is being reverted.
	// Any transactions and siacoin elements that were created by the index
	// should be removed.
	RevertIndex(types.ChainIndex) error
	// AddSiacoinElements is called with all siacoin elements that are
	// now unspent due to the revert.
	AddSiacoinElements([]SiacoinElement) error
}

RevertTx is an interface for atomically reverting a chain update from a single address wallet.

type SiacoinElement added in v0.0.2

type SiacoinElement struct {
	types.SiacoinElement
	Index types.ChainIndex `json:"index"`
}

A SiacoinElement is a siacoin output paired with its chain index

type SingleAddressStore

type SingleAddressStore interface {
	chain.Subscriber

	// Tip returns the consensus change ID and block height of
	// the last wallet change.
	Tip() (types.ChainIndex, error)
	// UnspentSiacoinElements returns a list of all unspent siacoin outputs
	// including immature outputs.
	UnspentSiacoinElements() ([]SiacoinElement, error)
	// WalletEvents returns a paginated list of transactions ordered by
	// maturity height, descending. If no more transactions are available,
	// (nil, nil) should be returned.
	WalletEvents(offset, limit int) ([]Event, error)
	// WalletEventCount returns the total number of events relevant to the
	// wallet.
	WalletEventCount() (uint64, error)
}

A SingleAddressStore stores the state of a single-address wallet. Implementations are assumed to be thread safe.

type SingleAddressWallet

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

A SingleAddressWallet is a hot wallet that manages the outputs controlled by a single address.

func NewSingleAddressWallet

func NewSingleAddressWallet(priv types.PrivateKey, cm ChainManager, store SingleAddressStore, opts ...Option) (*SingleAddressWallet, error)

NewSingleAddressWallet returns a new SingleAddressWallet using the provided private key and store.

func (*SingleAddressWallet) Address

func (sw *SingleAddressWallet) Address() types.Address

Address returns the address of the wallet.

func (*SingleAddressWallet) Balance

func (sw *SingleAddressWallet) Balance() (balance Balance, err error)

Balance returns the balance of the wallet.

func (*SingleAddressWallet) Close

func (sw *SingleAddressWallet) Close() error

Close closes the wallet

func (*SingleAddressWallet) EventCount added in v0.0.2

func (sw *SingleAddressWallet) EventCount() (uint64, error)

EventCount returns the total number of events relevant to the wallet.

func (*SingleAddressWallet) Events added in v0.0.2

func (sw *SingleAddressWallet) Events(offset, limit int) ([]Event, error)

Events returns a paginated list of events, ordered by maturity height, descending. If no more events are available, (nil, nil) is returned.

func (*SingleAddressWallet) FundTransaction

func (sw *SingleAddressWallet) FundTransaction(txn *types.Transaction, amount types.Currency, useUnconfirmed bool) ([]types.Hash256, error)

FundTransaction adds siacoin inputs worth at least amount to the provided transaction. If necessary, a change output will also be added. The inputs will not be available to future calls to FundTransaction unless ReleaseInputs is called.

func (*SingleAddressWallet) Redistribute

func (sw *SingleAddressWallet) Redistribute(outputs int, amount, feePerByte types.Currency) (txns []types.Transaction, toSign []types.Hash256, err error)

Redistribute returns a transaction that redistributes money in the wallet by selecting a minimal set of inputs to cover the creation of the requested outputs. It also returns a list of output IDs that need to be signed.

func (*SingleAddressWallet) ReleaseInputs

func (sw *SingleAddressWallet) ReleaseInputs(txns ...types.Transaction)

ReleaseInputs is a helper function that releases the inputs of txn for use in other transactions. It should only be called on transactions that are invalid or will never be broadcast.

func (*SingleAddressWallet) SignTransaction

func (sw *SingleAddressWallet) SignTransaction(txn *types.Transaction, toSign []types.Hash256, cf types.CoveredFields)

SignTransaction adds a signature to each of the specified inputs.

func (*SingleAddressWallet) SpendableOutputs

func (sw *SingleAddressWallet) SpendableOutputs() ([]SiacoinElement, error)

SpendableOutputs returns a list of spendable siacoin outputs, a spendable output is an unspent output that's not locked, not currently in the transaction pool and that has matured.

func (*SingleAddressWallet) Tip

Tip returns the block height the wallet has scanned to.

func (*SingleAddressWallet) UnconfirmedTransactions

func (sw *SingleAddressWallet) UnconfirmedTransactions() ([]Event, error)

UnconfirmedTransactions returns all unconfirmed transactions relevant to the wallet.

func (*SingleAddressWallet) UnlockConditions

func (sw *SingleAddressWallet) UnlockConditions() types.UnlockConditions

UnlockConditions returns the unlock conditions of the wallet.

Jump to

Keyboard shortcuts

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