domain

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWalletMissingMnemonic         = fmt.Errorf("missing mnemonic")
	ErrWalletMissingPassword         = fmt.Errorf("missing password")
	ErrWalletMissingNetwork          = fmt.Errorf("missing network name")
	ErrWalletMissingBirthdayBlock    = fmt.Errorf("missing birthday block height")
	ErrWalletLocked                  = fmt.Errorf("wallet is locked")
	ErrWalletUnlocked                = fmt.Errorf("wallet must be locked")
	ErrWalletMaxAccountNumberReached = fmt.Errorf("reached max number of accounts")
	ErrWalletInvalidPassword         = fmt.Errorf("wrong password")
	ErrWalletInvalidNetwork          = fmt.Errorf("unknown network")
	ErrAccountNotFound               = fmt.Errorf("account not found in wallet")
)
View Source
var (
	ErrUtxoAlreadyLocked = fmt.Errorf("utxo is already locked")
)

Functions

func GetAccountNamespace added in v0.1.16

func GetAccountNamespace(rootPath string, index uint32) string

GetAccountNamespace generates a unique account namespace from the given root path and account derivation index.

Types

type Account

type Account struct {
	AccountInfo
	Index                  uint32
	BirthdayBlock          uint32
	NextExternalIndex      uint
	NextInternalIndex      uint
	DerivationPathByScript map[string]string
	Unconf                 bool
}

Account defines the entity data struture for a derived account of the daemon's HD wallet

type AccountInfo

type AccountInfo struct {
	Namespace      string
	Label          string
	Xpub           string
	DerivationPath string
}

AccountInfo holds basic info about an account.

func (*AccountInfo) GetMasterBlindingKey added in v0.1.16

func (i *AccountInfo) GetMasterBlindingKey() (string, error)

type AddressInfo

type AddressInfo struct {
	Account        string
	Address        string
	BlindingKey    []byte
	DerivationPath string
	Script         string
}

AddressInfo holds useful info about a derived address.

type Balance

type Balance struct {
	Confirmed   uint64
	Unconfirmed uint64
	Locked      uint64
}

Balance holds info about the balance of a list of utxos with the same asset.

func (*Balance) Total

func (b *Balance) Total() uint64

type ExternalScriptEvent added in v0.2.0

type ExternalScriptEvent struct {
	EventType ExternalScriptEventType
	Info      AddressInfo
}

ExternalScriptEvent holds info about an event occured within the repository.

type ExternalScriptEventType added in v0.2.0

type ExternalScriptEventType int
const (
	ExternalScriptAdded ExternalScriptEventType = iota
	ExternalScriptDeleted
)

func (ExternalScriptEventType) String added in v0.2.0

func (t ExternalScriptEventType) String() string

type ExternalScriptRepository added in v0.2.0

type ExternalScriptRepository interface {
	// AddScripts persists the given external script by preventing duplicates.
	AddScript(ctx context.Context, script AddressInfo) (bool, error)
	// GetAllScripts returns all the persisted external scripts.
	GetAllScripts(ctx context.Context) ([]AddressInfo, error)
	// DeleteScript removes an external script idenitified by its hash from the store.
	DeleteScript(ctx context.Context, scriptHash string) (bool, error)
}

ExternalScriptRepository is the abstraction for any kind of database intended to persist external scripts as AddressInfo.

type IMnemonicCypher

type IMnemonicCypher interface {
	Encrypt(mnemonic, password []byte) ([]byte, error)
	Decrypt(encryptedMnemonic, password []byte) ([]byte, error)
}

IMnemonicCipher defines the methods a cypher must implement to encrypt or decrypt a mnemonic with a password.

var MnemonicCypher IMnemonicCypher

type IMnemonicStore

type IMnemonicStore interface {
	Set(mnemonic string)
	Unset()
	IsSet() bool
	Get() []string
}

IMnemonicStore defines the methods a store storing a mnemonic in plaintext must implement to either set, unset or get it.

var MnemonicStore IMnemonicStore

type Transaction

type Transaction struct {
	TxID        string
	TxHex       string
	BlockHash   string
	BlockHeight uint64
	BlockTime   int64
	Accounts    map[string]struct{}
}

Transaction is the data structure representing an Elements tx with extra info like whether it is conifirmed/unconfirmed and the name of the accounts owning one or more of its inputs.

func (*Transaction) AddAccount

func (t *Transaction) AddAccount(accountName string)

AddAccount adds the given account to the map of those involved in the tx.

func (*Transaction) Confirm

func (t *Transaction) Confirm(
	blockHash string, blockHeight uint64, blockTime int64,
)

Confirm marks the tx as confirmed.

func (*Transaction) GetAccounts

func (t *Transaction) GetAccounts() []string

GetAccounts returns the account map as a slice of account names.

func (*Transaction) HasAccounts

func (t *Transaction) HasAccounts(tx *Transaction) bool

HasAccounts returns whether the current tx contains all account names of the provided one.

func (*Transaction) IsConfirmed

func (t *Transaction) IsConfirmed() bool

IsConfirmed returns whther the tx is included in the blockchain.

type TransactionEvent

type TransactionEvent struct {
	EventType   TransactionEventType
	Transaction *Transaction
}

TransactionEvent holds info about an event occured within the repository.

type TransactionEventType

type TransactionEventType int
const (
	TransactionAdded TransactionEventType = iota
	TransactionUnconfirmed
	TransactionConfirmed
)

func (TransactionEventType) String

func (t TransactionEventType) String() string

type TransactionRepository

type TransactionRepository interface {
	// AddTransaction adds the provided transaction to the repository by
	// preventing duplicates.
	// Generates a TransactionAdded event if successful.
	AddTransaction(ctx context.Context, tx *Transaction) (bool, error)
	// ConfirmTransaction adds the given blockhash and block height to the
	// Transaction identified by the given txid.
	// Generates a TransactionConfirmed event if successful.
	ConfirmTransaction(
		ctx context.Context,
		txid, blockHash string, blockheight uint64, blocktime int64,
	) (bool, error)
	// GetTransaction returns the Transaction identified by the given txid.
	GetTransaction(ctx context.Context, txid string) (*Transaction, error)
	// UpdateTransaction allows to commit multiple changes to the same
	// Transaction in a transactional way.
	UpdateTransaction(
		ctx context.Context, txid string,
		updateFn func(tx *Transaction) (*Transaction, error),
	) error
	// GetEventChannel retunrs the channel of TransactionEvents.
	GetEventChannel() chan TransactionEvent
}

TransactionRepository is the abstraction for any kind of database intended to persist Transactions.

type Utxo

type Utxo struct {
	UtxoKey
	Value               uint64
	Asset               string
	ValueCommitment     []byte
	AssetCommitment     []byte
	ValueBlinder        []byte
	AssetBlinder        []byte
	Script              []byte
	Nonce               []byte
	RangeProof          []byte
	SurjectionProof     []byte
	AccountName         string
	LockTimestamp       int64
	LockExpiryTimestamp int64
	SpentStatus         UtxoStatus
	ConfirmedStatus     UtxoStatus
}

Utxo is the data structure representing an Elements UTXO with extra info like whether it is spent/utxo, confirmed/unconfirmed or locked/unlocked and the name of the account owning it.

func (*Utxo) CanUnlock

func (u *Utxo) CanUnlock() bool

CanUnlock reutrns whether a locked utxo can be unlocked.

func (*Utxo) Confirm

func (u *Utxo) Confirm(status UtxoStatus) error

Confirm marks the utxos as confirmed.

func (*Utxo) ConfirmSpend added in v0.2.3

func (u *Utxo) ConfirmSpend(status UtxoStatus) error

ConfirmSpend adds confirmation (block) info to a spent utxo.

func (*Utxo) Info

func (u *Utxo) Info() UtxoInfo

Info returns a light view of the current utxo.

func (*Utxo) IsConfidential

func (u *Utxo) IsConfidential() bool

IsConfidential returns whether the utxo is a confidential one.

func (*Utxo) IsConfirmed

func (u *Utxo) IsConfirmed() bool

IsConfirmed returns whether the utxo is confirmed.

func (*Utxo) IsConfirmedSpent added in v0.2.3

func (u *Utxo) IsConfirmedSpent() bool

IsConfirmedSpent returns whether the utxo is spent by a tx in blockchain.

func (*Utxo) IsLocked

func (u *Utxo) IsLocked() bool

IsLocked returns whether the utxo is locked.

func (*Utxo) IsRevealed

func (u *Utxo) IsRevealed() bool

IsRevealed returns whether the utxo is confidential and its blinded data (value, asset and relative blinders) have been revealed.

func (*Utxo) IsSpent

func (u *Utxo) IsSpent() bool

IsSpent returns whether the utxo is spent by a tx in mempool.

func (*Utxo) Key

func (u *Utxo) Key() UtxoKey

Key returns the UtxoKey of the current utxo.

func (*Utxo) Lock

func (u *Utxo) Lock(timestamp, expiryTimestamp int64)

Lock marks the current utxo as locked.

func (*Utxo) Spend

func (u *Utxo) Spend(txid string) error

Spend marks the utxos as spent and resets the lock timestamp.

func (*Utxo) Unlock

func (u *Utxo) Unlock()

Unlock marks the current locked utxo as unlocked.

type UtxoEvent

type UtxoEvent struct {
	EventType UtxoEventType
	Utxos     []UtxoInfo
}

UtxoEvent holds info about an event occured within the repository.

type UtxoEventType

type UtxoEventType int
const (
	UtxoAdded UtxoEventType = iota
	UtxoConfirmed
	UtxoLocked
	UtxoUnlocked
	UtxoSpent
	UtxoConfirmedSpend
)

func (UtxoEventType) String

func (t UtxoEventType) String() string

type UtxoInfo

type UtxoInfo struct {
	UtxoKey
	Value           uint64
	Asset           string
	Script          []byte
	ValueBlinder    []byte
	AssetBlinder    []byte
	AccountName     string
	SpentStatus     UtxoStatus
	ConfirmedStatus UtxoStatus
}

UtxoInfo holds sensitive info about the utxo. For confidential utxos. they must be revealed to return useful UtxoInfo.

func (UtxoInfo) Key

func (i UtxoInfo) Key() UtxoKey

type UtxoKey

type UtxoKey struct {
	TxID string
	VOut uint32
}

UtxoKey represents the key of an Utxo, composed by its txid and vout.

func (UtxoKey) Hash

func (k UtxoKey) Hash() string

func (UtxoKey) String

func (k UtxoKey) String() string

type UtxoRepository

type UtxoRepository interface {
	// AddUtxos adds the provided utxos to the repository by preventing
	// duplicates.
	// Generates a UtxoAdded event if successfull.
	AddUtxos(ctx context.Context, utxos []*Utxo) (int, error)
	// GetUtxosByKey returns the utxos identified by the given keys.
	GetUtxosByKey(ctx context.Context, utxoKeys []UtxoKey) ([]*Utxo, error)
	// GetAllUtxos returns the entire UTXO set, included those locked or
	// already spent.
	GetAllUtxos(ctx context.Context) ([]*Utxo, error)
	// GetSpendableUtxos returns all unlocked utxo UTXOs.
	GetSpendableUtxos(ctx context.Context) ([]*Utxo, error)
	// GetAllUtxosForAccount returns the list of all utxos for the given
	// account.
	GetAllUtxosForAccount(ctx context.Context, account string) ([]*Utxo, error)
	// GetSpendableUtxosForAccount returns the list of spendable utxos for the
	// given account. The list incldues only confirmed and unlocked utxos.
	GetSpendableUtxosForAccount(ctx context.Context, account string) ([]*Utxo, error)
	// GetLockedUtxosForAccount returns the list of all currently locked utxos
	// for the given account.
	GetLockedUtxosForAccount(ctx context.Context, account string) ([]*Utxo, error)
	// GetBalanceForAccount returns the confirmed, unconfirmed and locked
	// balances per each asset for the given account.
	GetBalanceForAccount(ctx context.Context, account string) (map[string]*Balance, error)
	// SpendUtxos updates the status of the given list of utxos to "spent" by the given txid.
	// Generates a UtxoSpent event if successfull.
	SpendUtxos(ctx context.Context, utxoKeys []UtxoKey, txid string) (int, error)
	// ConfirmSpendUtxos updates the status of the given list of utxos to "confirmed spend".
	// Generates a UtxoConfirmedSpend event if successfull.
	ConfirmSpendUtxos(ctx context.Context, utxoKeys []UtxoKey, status UtxoStatus) (int, error)
	// ConfirmUtxos updates the status of the given list of utxos to "confirmed".
	// Generates a UtxoConfirmed event if successfull.
	ConfirmUtxos(ctx context.Context, utxoKeys []UtxoKey, status UtxoStatus) (int, error)
	// LockUtxos updates the status of the given list of utxos to "locked".
	// Generates a UtxoLocked event if successfull.
	LockUtxos(ctx context.Context, utxoKeys []UtxoKey, timestamp, expiryTimestamp int64) (int, error)
	// UnlockUtxos updates the status of the given list of utxos to "unlocked".
	// Generates a UtxoUnlocked event if successfull.
	UnlockUtxos(ctx context.Context, utxoKeys []UtxoKey) (int, error)
	// DeleteUtxosForAccount deletes every utxo associated to the given account
	// from the repository.
	DeleteUtxosForAccount(ctx context.Context, accountName string) error
	// GetEventChannel returns the channel of UtxoEvents.
	GetEventChannel() chan UtxoEvent
}

UtxoRepository is the abstraction for any kind of database intended to persist Utxos.

type UtxoStatus

type UtxoStatus struct {
	Txid        string
	BlockHeight uint64
	BlockTime   int64
	BlockHash   string
}

type Wallet

type Wallet struct {
	EncryptedMnemonic   []byte
	PasswordHash        []byte
	BirthdayBlockHeight uint32
	RootPath            string
	NetworkName         string
	Accounts            map[string]*Account
	AccountsByLabel     map[string]string
	NextAccountIndex    uint32
}

Wallet is the data structure representing a secure HD wallet, ie. protected by a password that encrypts/decrypts the mnemonic seed.

func NewWallet

func NewWallet(
	mnemonic []string, password, rootPath, network string,
	birthdayBlock uint32, accounts []Account,
) (*Wallet, error)

NewWallet encrypts the provided mnemonic with the passhrase and returns a new Wallet initialized with the encrypted mnemonic, the hash of the password, the given root path, network and possible a list of accounts for an already used one. The Wallet is locked by default since it is initialized without the mnemonic in plain text.

func (*Wallet) AllDerivedAddressesForAccount

func (w *Wallet) AllDerivedAddressesForAccount(
	accountName string,
) ([]AddressInfo, error)

AllDerivedAddressesForAccount returns info about all derived receiving and change addresses derived so far for the given account.

func (*Wallet) AllDerivedExternalAddressesForAccount

func (w *Wallet) AllDerivedExternalAddressesForAccount(
	accountName string,
) ([]AddressInfo, error)

AllDerivedExternalAddressesForAccount returns info about all derived receiving addresses derived so far for the given account.

func (*Wallet) ChangePassword

func (w *Wallet) ChangePassword(currentPassword, newPassword string) error

ChangePassword attempts to unlock the wallet with the given currentPassword, then encrypts the plaintext mnemonic again with new password, stores its hash and, finally, locks the Wallet again.

func (*Wallet) CreateAccount

func (w *Wallet) CreateAccount(label string, birthdayBlock uint32, unconf bool) (*Account, error)

CreateAccount creates a new account with the given name by preventing collisions with existing ones. If successful, returns the Account created.

func (*Wallet) DeleteAccount

func (w *Wallet) DeleteAccount(accountName string) error

DeleteAccount safely removes an Account and all related stored info from the Wallet.

func (*Wallet) DeriveNextExternalAddressForAccount

func (w *Wallet) DeriveNextExternalAddressForAccount(
	accountName string,
) (*AddressInfo, error)

DeriveNextExternalAddressForAccount returns all useful info about the next new receiving address for the given account.

func (*Wallet) DeriveNextInternalAddressForAccount

func (w *Wallet) DeriveNextInternalAddressForAccount(
	accountName string,
) (*AddressInfo, error)

DeriveNextInternalAddressForAccount returns all useful info about the next new change address for the given account.

func (*Wallet) GetAccount

func (w *Wallet) GetAccount(accountName string) (*Account, error)

GetAccount safely returns an Account identified by the given name.

func (*Wallet) GetMnemonic

func (w *Wallet) GetMnemonic() ([]string, error)

GetMnemonic safely returns the plaintext mnemonic.

func (*Wallet) IsInitialized

func (w *Wallet) IsInitialized() bool

IsInitialized returns wheter the wallet is initialized with an encrypted mnemonic.

func (*Wallet) IsLocked

func (w *Wallet) IsLocked() bool

IsLocked returns whether the wallet is initialized and the plaintext mnemonic is set in its store.

func (*Wallet) IsValidPassword added in v0.1.14

func (w *Wallet) IsValidPassword(password string) bool

func (*Wallet) Lock

func (w *Wallet) Lock(password string) error

Lock locks the Wallet by wiping the plaintext mnemonic from its store.

func (*Wallet) SetLabelForAccount added in v0.1.16

func (w *Wallet) SetLabelForAccount(accountName, label string) error

SetLabelForAccount changes the label for the given account

func (*Wallet) Unlock

func (w *Wallet) Unlock(password string) error

Unlock attempts to decrypt the encrypted mnemonic with the provided password.

type WalletEvent

type WalletEvent struct {
	EventType            WalletEventType
	AccountName          string
	AccountBirthdayBlock uint32
	AccountAddresses     []AddressInfo
}

WalletEvent holds info about an event occured within the repository.

type WalletEventType

type WalletEventType int
const (
	WalletCreated WalletEventType = iota
	WalletUnlocked
	WalletLocked
	WalletPasswordChanged
	WalletAccountCreated
	WalletAccountAddressesDerived
	WalletAccountDeleted
)

func (WalletEventType) String

func (t WalletEventType) String() string

type WalletRepository

type WalletRepository interface {
	// CreateWallet stores a new Wallet if not yet existing.
	// Generates a WalletCreated event if successfull.
	CreateWallet(ctx context.Context, wallet *Wallet) error
	// GetWallet returns the stored wallet, if existing.
	GetWallet(ctx context.Context) (*Wallet, error)
	// UnlockWallet attempts to update the status of the Wallet to "unlocked".
	// Generates a WalletUnlocked event if successfull.
	UnlockWallet(ctx context.Context, password string) error
	// LockkWallet updates the status of the Wallet to "locked".
	// Generates a WalletLocked event if successfull.
	LockWallet(ctx context.Context, password string) error
	// UpdateWallet allows to make multiple changes to the Wallet in a
	// transactional way.
	UpdateWallet(
		ctx context.Context, updateFn func(v *Wallet) (*Wallet, error),
	) error
	// CreateAccount creates a new wallet account with the given name and returns
	// its basic info.
	// Generates a WalletAccountCreated event if successfull.
	CreateAccount(
		ctx context.Context, accountName string, birthdayBlock uint32, unconf bool,
	) (*AccountInfo, error)
	// DeriveNextExternalAddressesForAccount returns one or more new receiving
	// addresses for the given account.
	// Generates a WalletAccountAddressesDerived event if successfull.
	DeriveNextExternalAddressesForAccount(
		ctx context.Context, accountName string, numOfAddresses uint64,
	) ([]AddressInfo, error)
	// DeriveNextInternalAddressesForAccount returns one or more new change
	// addresses for the given account.
	// Generates a WalletAccountAddressesDerived event if successfull.
	DeriveNextInternalAddressesForAccount(
		ctx context.Context, accountName string, numOfAddresses uint64,
	) ([]AddressInfo, error)
	// DeleteAccount deletes the wallet account with the given name.
	// Generates a WalletAccountDeleted event if successfull.
	DeleteAccount(ctx context.Context, accountName string) error
	// GetEventChannel returns the channel of WalletEvents.
	GetEventChannel() chan WalletEvent
}

WalletRepository is the abstraction for any kind of database intended to persist a Wallet.

Jump to

Keyboard shortcuts

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