wallet

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2024 License: MIT Imports: 15 Imported by: 1

Documentation

Index

Constants

View Source
const (
	EventTypeMinerPayout       = wallet.EventTypeMinerPayout
	EventTypeFoundationSubsidy = wallet.EventTypeFoundationSubsidy
	EventTypeSiafundClaim      = wallet.EventTypeSiafundClaim

	EventTypeV1Transaction        = wallet.EventTypeV1Transaction
	EventTypeV1ContractResolution = wallet.EventTypeV1ContractResolution

	EventTypeV2Transaction        = wallet.EventTypeV2Transaction
	EventTypeV2ContractResolution = wallet.EventTypeV2ContractResolution
)

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 ErrNotFound = errors.New("not found")

ErrNotFound is returned when a requested wallet or address is not found.

Functions

func SignTransaction

func SignTransaction(cs consensus.State, txn *types.Transaction, sigIndex int, key types.PrivateKey)

SignTransaction signs txn with the given key. The TransactionSignature object must already be present in txn at the given index.

func StandardTransactionSignature

func StandardTransactionSignature(id types.Hash256) types.TransactionSignature

StandardTransactionSignature is the most common form of TransactionSignature. It covers the entire transaction, references a sole public key, and has no timelock.

func UpdateChainState

func UpdateChainState(tx UpdateTx, reverted []chain.RevertUpdate, applied []chain.ApplyUpdate, indexMode IndexMode, log *zap.Logger) error

UpdateChainState atomically updates the state of a store with a set of updates from the chain manager.

Types

type Address

type Address struct {
	Address     types.Address      `json:"address"`
	Description string             `json:"description"`
	SpendPolicy *types.SpendPolicy `json:"spendPolicy,omitempty"`
	Metadata    json.RawMessage    `json:"metadata"`
}

A Address is an address associated with a wallet.

type AddressBalance

type AddressBalance struct {
	Address types.Address `json:"address"`
	Balance
}

AddressBalance pairs an address with its balance.

type AppliedState

type AppliedState struct {
	NumLeaves              uint64
	Events                 []Event
	CreatedSiacoinElements []types.SiacoinElement
	SpentSiacoinElements   []types.SiacoinElement
	CreatedSiafundElements []types.SiafundElement
	SpentSiafundElements   []types.SiafundElement
}

AppliedState contains all state changes made to a store after applying a chain update.

type Balance

type Balance struct {
	Siacoins         types.Currency `json:"siacoins"`
	ImmatureSiacoins types.Currency `json:"immatureSiacoins"`
	Siafunds         uint64         `json:"siafunds"`
}

Balance is a summary of a siacoin and siafund balance

type ChainManager

type ChainManager interface {
	PoolTransactions() []types.Transaction
	V2PoolTransactions() []types.V2Transaction

	Tip() types.ChainIndex
	BestIndex(height uint64) (types.ChainIndex, bool)

	OnReorg(fn func(types.ChainIndex)) (cancel func())
	UpdatesSince(index types.ChainIndex, max int) (rus []chain.RevertUpdate, aus []chain.ApplyUpdate, err error)
}

A ChainManager manages the consensus state

type ChainUpdate

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

A ChainUpdate is a set of changes to the consensus state.

type Event

type Event = wallet.Event

An Event is a record of a consensus event that affects the wallet.

func AppliedEvents

func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate, relevant func(types.Address) bool) (events []Event)

AppliedEvents extracts a list of relevant events from a chain update.

type EventData

type EventData = wallet.EventData

EventData is the data associated with an event.

type EventPayout

type EventPayout = wallet.EventPayout

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

type EventV1ContractResolution

type EventV1ContractResolution = wallet.EventV1ContractResolution

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

type EventV1Transaction

type EventV1Transaction = wallet.EventV1Transaction

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

type EventV2ContractResolution

type EventV2ContractResolution = wallet.EventV2ContractResolution

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

type EventV2Transaction

type EventV2Transaction = wallet.EventV2Transaction

EventV2Transaction is a transaction event that includes the transaction

type ID

type ID int64

An ID is a unique identifier for a wallet.

func (ID) MarshalText

func (w ID) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*ID) UnmarshalText

func (w *ID) UnmarshalText(buf []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type IndexMode

type IndexMode uint8

An IndexMode determines the chain state that the wallet manager stores.

const (
	IndexModePersonal IndexMode = iota
	IndexModeFull
	IndexModeNone
)

IndexMode represents the index mode of the wallet manager. The index mode determines how the wallet manager stores the consensus state.

IndexModePersonal - The wallet manager scans the blockchain starting at genesis. Only state from addresses that are registered with a wallet will be stored. If an address is added to a wallet after the scan completes, the manager will need to rescan.

IndexModeFull - The wallet manager scans the blockchain starting at genesis and stores the state of all addresses.

IndexModeNone - The wallet manager does not scan the blockchain. This is useful for multiple nodes sharing the same database. None should only be used when connecting to a database that is in "Full" mode.

func (IndexMode) MarshalText

func (i IndexMode) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (IndexMode) String

func (i IndexMode) String() string

String returns the string representation of the index mode.

func (*IndexMode) UnmarshalText

func (i *IndexMode) UnmarshalText(buf []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

type Manager

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

A Manager manages wallets.

func NewManager

func NewManager(cm ChainManager, store Store, opts ...Option) (*Manager, error)

NewManager creates a new wallet manager.

func (*Manager) AddAddress

func (m *Manager) AddAddress(walletID ID, addr Address) error

AddAddress adds the given address to the given wallet.

func (*Manager) AddWallet

func (m *Manager) AddWallet(w Wallet) (Wallet, error)

AddWallet adds the given wallet.

func (*Manager) AddressBalance

func (m *Manager) AddressBalance(address types.Address) (balance Balance, err error)

AddressBalance returns the balance of a single address.

func (*Manager) AddressEvents

func (m *Manager) AddressEvents(address types.Address, offset, limit int) (events []Event, err error)

AddressEvents returns the events of a single address.

func (*Manager) AddressSiacoinOutputs

func (m *Manager) AddressSiacoinOutputs(address types.Address, offset, limit int) (siacoins []types.SiacoinElement, err error)

AddressSiacoinOutputs returns the unspent siacoin outputs for an address.

func (*Manager) AddressSiafundOutputs

func (m *Manager) AddressSiafundOutputs(address types.Address, offset, limit int) (siafunds []types.SiafundElement, err error)

AddressSiafundOutputs returns the unspent siafund outputs for an address.

func (*Manager) AddressUnconfirmedEvents

func (m *Manager) AddressUnconfirmedEvents(address types.Address) ([]Event, error)

AddressUnconfirmedEvents returns the unconfirmed events for a single address.

func (*Manager) Addresses

func (m *Manager) Addresses(walletID ID) ([]Address, error)

Addresses returns the addresses of the given wallet.

func (*Manager) Close

func (m *Manager) Close() error

Close closes the wallet manager.

func (*Manager) DeleteWallet

func (m *Manager) DeleteWallet(walletID ID) error

DeleteWallet deletes the given wallet.

func (*Manager) Events

func (m *Manager) Events(eventIDs []types.Hash256) ([]Event, error)

Events returns the events with the given IDs.

func (*Manager) IndexMode

func (m *Manager) IndexMode() IndexMode

IndexMode returns the index mode of the wallet manager.

func (*Manager) RemoveAddress

func (m *Manager) RemoveAddress(walletID ID, addr types.Address) error

RemoveAddress removes the given address from the given wallet.

func (*Manager) Reserve

func (m *Manager) Reserve(ids []types.Hash256, duration time.Duration) error

Reserve reserves the given ids for the given duration.

func (*Manager) Scan

func (m *Manager) Scan(ctx context.Context, index types.ChainIndex) error

Scan rescans the chain starting from the given index. The scan will complete when the chain manager reaches the current tip or the context is canceled.

func (*Manager) SiacoinElement

func (m *Manager) SiacoinElement(id types.SiacoinOutputID) (types.SiacoinElement, error)

SiacoinElement returns the unspent siacoin element with the given id.

func (*Manager) SiafundElement

func (m *Manager) SiafundElement(id types.SiafundOutputID) (types.SiafundElement, error)

SiafundElement returns the unspent siafund element with the given id.

func (*Manager) Tip

func (m *Manager) Tip() (types.ChainIndex, error)

Tip returns the last scanned chain index of the manager.

func (*Manager) UnconfirmedEvents

func (m *Manager) UnconfirmedEvents() ([]Event, error)

UnconfirmedEvents returns all unconfirmed events in the transaction pool.

func (*Manager) UnspentSiacoinOutputs

func (m *Manager) UnspentSiacoinOutputs(walletID ID, offset, limit int) ([]types.SiacoinElement, error)

UnspentSiacoinOutputs returns a paginated list of matured siacoin outputs relevant to the wallet

func (*Manager) UnspentSiafundOutputs

func (m *Manager) UnspentSiafundOutputs(walletID ID, offset, limit int) ([]types.SiafundElement, error)

UnspentSiafundOutputs returns a paginated list of siafund outputs relevant to the wallet

func (*Manager) UpdateWallet

func (m *Manager) UpdateWallet(w Wallet) (Wallet, error)

UpdateWallet updates the given wallet.

func (*Manager) WalletBalance

func (m *Manager) WalletBalance(walletID ID) (Balance, error)

WalletBalance returns the balance of the given wallet.

func (*Manager) WalletEvents

func (m *Manager) WalletEvents(walletID ID, offset, limit int) ([]Event, error)

WalletEvents returns the events of the given wallet.

func (*Manager) WalletUnconfirmedEvents

func (m *Manager) WalletUnconfirmedEvents(walletID ID) ([]Event, error)

WalletUnconfirmedEvents returns the unconfirmed events of the given wallet.

func (*Manager) Wallets

func (m *Manager) Wallets() ([]Wallet, error)

Wallets returns the wallets of the wallet manager.

type Option

type Option func(*Manager)

An Option configures a wallet Manager.

func WithIndexMode

func WithIndexMode(mode IndexMode) Option

WithIndexMode sets the index mode used by the manager.

func WithLogger

func WithLogger(log *zap.Logger) Option

WithLogger sets the logger used by the manager.

func WithSyncBatchSize

func WithSyncBatchSize(size int) Option

WithSyncBatchSize sets the number of blocks to batch when scanning the blockchain. The default is 64. Increasing this value can improve performance at the cost of memory usage.

type RevertedState

type RevertedState struct {
	NumLeaves              uint64
	UnspentSiacoinElements []types.SiacoinElement
	DeletedSiacoinElements []types.SiacoinElement
	UnspentSiafundElements []types.SiafundElement
	DeletedSiafundElements []types.SiafundElement
}

RevertedState contains all state changes made to a store after reverting a chain update.

type Seed

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

A Seed is securely-generated entropy, used to derive an arbitrary number of keypairs.

func NewSeed

func NewSeed() Seed

NewSeed returns a random Seed.

func NewSeedFromEntropy

func NewSeedFromEntropy(entropy *[32]byte) Seed

NewSeedFromEntropy returns a the specified seed.

func (Seed) PrivateKey

func (s Seed) PrivateKey(index uint64) types.PrivateKey

PrivateKey derives the private key for the specified index.

func (Seed) PublicKey

func (s Seed) PublicKey(index uint64) (pk types.PublicKey)

PublicKey derives the public key for the specified index.

type SeedAddressVault

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

A SeedAddressVault generates and stores addresses from a seed.

func NewSeedAddressVault

func NewSeedAddressVault(seed Seed, initialAddrs, lookahead uint64) *SeedAddressVault

NewSeedAddressVault initializes a SeedAddressVault.

func (*SeedAddressVault) NewAddress

func (sav *SeedAddressVault) NewAddress(desc string) Address

NewAddress returns a new address derived from the seed, along with descriptive metadata.

func (*SeedAddressVault) OwnsAddress

func (sav *SeedAddressVault) OwnsAddress(addr types.Address) bool

OwnsAddress returns true if addr was derived from the seed.

func (*SeedAddressVault) SignTransaction

func (sav *SeedAddressVault) SignTransaction(cs consensus.State, txn *types.Transaction, toSign []types.Hash256) error

SignTransaction signs the specified transaction using keys derived from the wallet seed. If toSign is nil, SignTransaction will automatically add Signatures for each input owned by the seed. If toSign is not nil, it a list of IDs of Signatures already present in txn; SignTransaction will fill in the Signature field of each.

type Store

type Store interface {
	UpdateChainState(reverted []chain.RevertUpdate, applied []chain.ApplyUpdate) error

	WalletUnconfirmedEvents(id ID, index types.ChainIndex, timestamp time.Time, v1 []types.Transaction, v2 []types.V2Transaction) (annotated []Event, err error)
	WalletEvents(walletID ID, offset, limit int) ([]Event, error)
	AddWallet(Wallet) (Wallet, error)
	UpdateWallet(Wallet) (Wallet, error)
	DeleteWallet(walletID ID) error
	WalletBalance(walletID ID) (Balance, error)
	WalletSiacoinOutputs(walletID ID, index types.ChainIndex, offset, limit int) ([]types.SiacoinElement, error)
	WalletSiafundOutputs(walletID ID, offset, limit int) ([]types.SiafundElement, error)
	WalletAddresses(walletID ID) ([]Address, error)
	Wallets() ([]Wallet, error)

	AddWalletAddress(walletID ID, address Address) error
	RemoveWalletAddress(walletID ID, address types.Address) error

	AddressBalance(address types.Address) (balance Balance, err error)
	AddressEvents(address types.Address, offset, limit int) (events []Event, err error)
	AddressSiacoinOutputs(address types.Address, index types.ChainIndex, offset, limit int) (siacoins []types.SiacoinElement, err error)
	AddressSiafundOutputs(address types.Address, offset, limit int) (siafunds []types.SiafundElement, err error)

	Events(eventIDs []types.Hash256) ([]Event, error)
	AnnotateV1Events(index types.ChainIndex, timestamp time.Time, v1 []types.Transaction) (annotated []Event, err error)

	SiacoinElement(types.SiacoinOutputID) (types.SiacoinElement, error)
	SiafundElement(types.SiafundOutputID) (types.SiafundElement, error)

	SetIndexMode(IndexMode) error
	LastCommittedIndex() (types.ChainIndex, error)
}

A Store is a persistent store of wallet data.

type TreeNodeUpdate

type TreeNodeUpdate struct {
	Hash   types.Hash256
	Row    int
	Column int
}

A TreeNodeUpdate contains the hash of a Merkle tree node and its row and column indices.

type UpdateTx

type UpdateTx interface {
	SiacoinStateElements() ([]types.StateElement, error)
	UpdateSiacoinStateElements([]types.StateElement) error

	SiafundStateElements() ([]types.StateElement, error)
	UpdateSiafundStateElements([]types.StateElement) error

	UpdateStateTree([]TreeNodeUpdate) error

	AddressRelevant(types.Address) (bool, error)

	ApplyIndex(types.ChainIndex, AppliedState) error
	RevertIndex(types.ChainIndex, RevertedState) error
}

An UpdateTx atomically updates the state of a store.

type Wallet

type Wallet struct {
	ID          ID              `json:"id"`
	Name        string          `json:"name"`
	Description string          `json:"description"`
	DateCreated time.Time       `json:"dateCreated"`
	LastUpdated time.Time       `json:"lastUpdated"`
	Metadata    json.RawMessage `json:"metadata"`
}

A Wallet is a collection of addresses and metadata.

Jump to

Keyboard shortcuts

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