wallet

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: MIT Imports: 15 Imported by: 14

Documentation

Index

Constants

View Source
const (
	EventTypeMinerPayout       = "miner"
	EventTypeFoundationSubsidy = "foundation"
	EventTypeSiafundClaim      = "siafundClaim"

	EventTypeV1Transaction        = "v1Transaction"
	EventTypeV1ContractResolution = "v1ContractResolution"

	EventTypeV2Transaction        = "v2Transaction"
	EventTypeV2ContractResolution = "v2ContractResolution"
)

event types indicate the source of an event. Events can either be created by sending Siacoins between addresses or they can be created by consensus (e.g. a miner payout, a siafund claim, or a contract).

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 ExplicitCoveredFields added in v0.0.4

func ExplicitCoveredFields(txn types.Transaction) (cf types.CoveredFields)

ExplicitCoveredFields returns a CoveredFields that covers all elements present in txn.

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 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 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
	V2PoolTransactions() []types.V2Transaction
	OnReorg(func(types.ChainIndex)) func()
}

A ChainManager manages the current state of the blockchain.

type ChainUpdate added in v0.0.4

type ChainUpdate interface {
	ForEachSiacoinElement(func(sce types.SiacoinElement, spent bool))
	ForEachSiafundElement(func(sfe types.SiafundElement, spent bool))
	ForEachFileContractElement(func(fce types.FileContractElement, rev *types.FileContractElement, resolved, valid bool))
	ForEachV2FileContractElement(func(fce types.V2FileContractElement, rev *types.V2FileContractElement, res types.V2FileContractResolutionType))
}

A ChainUpdate is an interface for iterating over the elements in a chain update.

type Event added in v0.0.2

type Event struct {
	ID             types.Hash256    `json:"id"`
	Index          types.ChainIndex `json:"index"`
	Confirmations  uint64           `json:"confirmations"`
	Type           string           `json:"type"`
	Data           EventData        `json:"data"`
	MaturityHeight uint64           `json:"maturityHeight"`
	Timestamp      time.Time        `json:"timestamp"`
	Relevant       []types.Address  `json:"relevant,omitempty"`
}

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 (ev *Event) DecodeFrom(d *types.Decoder)

DecodeFrom implements types.DecoderFrom

func (*Event) EncodeTo added in v0.0.2

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

EncodeTo implements types.EncoderTo

func (*Event) SiacoinInflow added in v0.2.4

func (e *Event) SiacoinInflow() types.Currency

SiacoinInflow calculates the sum of Siacoins that were received by relevant addresses

func (*Event) SiacoinOutflow added in v0.2.4

func (e *Event) SiacoinOutflow() types.Currency

SiacoinOutflow calculates the sum of Siacoins that were spent by relevant addresses

func (*Event) SiafundInflow added in v0.2.4

func (e *Event) SiafundInflow() uint64

SiafundInflow calculates the sum of Siafunds that were received by relevant addresses

func (*Event) SiafundOutflow added in v0.2.4

func (e *Event) SiafundOutflow() uint64

SiafundOutflow calculates the sum of Siafunds that were spent by relevant addresses

func (*Event) UnmarshalJSON added in v0.0.7

func (e *Event) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type EventData added in v0.0.4

type EventData interface {
	// contains filtered or unexported methods
}

EventData contains the data associated with an event.

type EventPayout added in v0.0.7

type EventPayout struct {
	SiacoinElement types.SiacoinElement `json:"siacoinElement"`
}

An EventPayout represents a miner payout, siafund claim, or foundation subsidy.

func (*EventPayout) DecodeFrom added in v0.6.0

func (ep *EventPayout) DecodeFrom(d *types.Decoder)

DecodeFrom implements types.DecoderFrom

func (EventPayout) EncodeTo added in v0.6.0

func (ep EventPayout) EncodeTo(e *types.Encoder)

EncodeTo implements types.EncoderTo

type EventV1ContractResolution added in v0.0.7

type EventV1ContractResolution struct {
	Parent         types.FileContractElement `json:"parent"`
	SiacoinElement types.SiacoinElement      `json:"siacoinElement"`
	Missed         bool                      `json:"missed"`
}

An EventV1ContractResolution represents a file contract payout from a v1 contract.

func (*EventV1ContractResolution) DecodeFrom added in v0.6.0

func (er *EventV1ContractResolution) DecodeFrom(d *types.Decoder)

DecodeFrom implements types.DecoderFrom

func (EventV1ContractResolution) EncodeTo added in v0.6.0

func (er EventV1ContractResolution) EncodeTo(e *types.Encoder)

EncodeTo implements types.EncoderTo

type EventV1Transaction added in v0.0.4

type EventV1Transaction struct {
	Transaction types.Transaction `json:"transaction"`
	// v1 siacoin inputs do not describe the value of the spent utxo
	SpentSiacoinElements []types.SiacoinElement `json:"spentSiacoinElements,omitempty"`
	// v1 siafund inputs do not describe the value of the spent utxo
	SpentSiafundElements []types.SiafundElement `json:"spentSiafundElements,omitempty"`
}

An EventV1Transaction pairs a v1 transaction with its spent siacoin and siafund elements.

func (*EventV1Transaction) DecodeFrom added in v0.6.0

func (et *EventV1Transaction) DecodeFrom(d *types.Decoder)

DecodeFrom implements types.DecoderFrom

func (EventV1Transaction) EncodeTo added in v0.6.0

func (et EventV1Transaction) EncodeTo(e *types.Encoder)

EncodeTo implements types.EncoderTo

type EventV2ContractResolution added in v0.0.7

type EventV2ContractResolution struct {
	Resolution     types.V2FileContractResolution `json:"resolution"`
	SiacoinElement types.SiacoinElement           `json:"siacoinElement"`
	Missed         bool                           `json:"missed"`
}

An EventV2ContractResolution represents a file contract payout from a v2 contract.

func (*EventV2ContractResolution) DecodeFrom added in v0.6.0

func (er *EventV2ContractResolution) DecodeFrom(d *types.Decoder)

DecodeFrom implements types.DecoderFrom

func (EventV2ContractResolution) EncodeTo added in v0.6.0

func (er EventV2ContractResolution) EncodeTo(e *types.Encoder)

EncodeTo implements types.EncoderTo

type EventV2Transaction added in v0.0.4

type EventV2Transaction types.V2Transaction

EventV2Transaction is a transaction event that includes the transaction

func (*EventV2Transaction) DecodeFrom added in v0.6.0

func (et *EventV2Transaction) DecodeFrom(d *types.Decoder)

DecodeFrom implements types.DecoderFrom

func (EventV2Transaction) EncodeTo added in v0.6.0

func (et EventV2Transaction) EncodeTo(e *types.Encoder)

EncodeTo implements types.EncoderTo

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 ProofUpdater added in v0.6.0

type ProofUpdater interface {
	UpdateElementProof(e *types.StateElement)
}

A ProofUpdater is an interface for updating the proof of a state element.

type SingleAddressStore

type SingleAddressStore interface {
	// 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() ([]types.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) FundV2Transaction added in v0.0.4

func (sw *SingleAddressWallet) FundV2Transaction(txn *types.V2Transaction, amount types.Currency, useUnconfirmed bool) (types.ChainIndex, []int, error)

FundV2Transaction 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.

The returned index should be used as the basis for AddV2PoolTransactions.

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) RedistributeV2 added in v0.2.2

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

RedistributeV2 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, v2txns []types.V2Transaction)

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) SignHash added in v0.0.4

SignHash signs the hash with the wallet's private key.

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) SignV2Inputs added in v0.0.4

func (sw *SingleAddressWallet) SignV2Inputs(txn *types.V2Transaction, toSign []int)

SignV2Inputs adds a signature to each of the specified siacoin inputs.

func (*SingleAddressWallet) SpendPolicy added in v0.0.4

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

SpendPolicy returns the wallet's default spend policy.

func (*SingleAddressWallet) SpendableOutputs

func (sw *SingleAddressWallet) SpendableOutputs() ([]types.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) UnconfirmedEvents added in v0.2.4

func (sw *SingleAddressWallet) UnconfirmedEvents() (annotated []Event, err error)

UnconfirmedEvents 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.

func (*SingleAddressWallet) UnspentSiacoinElements added in v0.2.4

func (sw *SingleAddressWallet) UnspentSiacoinElements() ([]types.SiacoinElement, error)

UnspentSiacoinElements returns the wallet's unspent siacoin outputs.

func (*SingleAddressWallet) UpdateChainState added in v0.2.2

func (sw *SingleAddressWallet) UpdateChainState(tx UpdateTx, reverted []chain.RevertUpdate, applied []chain.ApplyUpdate) error

UpdateChainState atomically applies and reverts chain updates to a single wallet store.

type UpdateTx added in v0.0.4

type UpdateTx interface {
	// UpdateWalletSiacoinElementProofs updates the proofs of all state elements
	// affected by the update. ProofUpdater.UpdateElementProof must be called
	// for each state element in the database.
	UpdateWalletSiacoinElementProofs(ProofUpdater) error

	// WalletApplyIndex is called with the chain index that is being applied.
	// Any transactions and siacoin elements that were created by the index
	// should be added and any siacoin elements that were spent should be
	// removed.
	//
	// timestamp is the timestamp of the block being applied.
	WalletApplyIndex(index types.ChainIndex, created, spent []types.SiacoinElement, events []Event, timestamp time.Time) error
	// WalletRevertIndex is called with the chain index that is being reverted.
	// Any transactions that were added by the index should be removed
	//
	// removed contains the siacoin elements that were created by the index
	// and should be deleted.
	//
	// unspent contains the siacoin elements that were spent and should be
	// recreated. They are not necessarily created by the index and should
	// not be associated with it.
	//
	// timestamp is the timestamp of the block being reverted
	WalletRevertIndex(index types.ChainIndex, removed, unspent []types.SiacoinElement, timestamp time.Time) error
}

UpdateTx is an interface for atomically applying chain updates to a single address wallet.

Jump to

Keyboard shortcuts

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