domain

package
v0.1.15 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2023 License: MIT Imports: 11 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

This section is empty.

Types

type Account

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

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

type AccountInfo

type AccountInfo struct {
	Key            AccountKey
	Xpub           string
	DerivationPath string
}

AccountInfo holds basic info about an account.

type AccountKey

type AccountKey struct {
	Name  string
	Index uint32
}

AccountKey holds the unique info of an account: name and HD index.

func (*AccountKey) String

func (ak *AccountKey) String() string

type AddressInfo

type AddressInfo struct {
	AccountKey     AccountKey
	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 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
	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)

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 successfull.
	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 successfull.
	ConfirmTransaction(
		ctx context.Context, txid, blockHash string, blockheight uint64,
	) (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) 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) 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 have been spent.

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(status UtxoStatus) error

Spend marks the utxos as spent.

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
)

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".
	// Generates a UtxoSpent event if successfull.
	SpendUtxos(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
	AccountsByKey       map[string]*Account
	AccountKeysByIndex  map[uint32]string
	AccountKeysByName   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(name string, birthdayBlock uint32) (*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) GetMasterBlindingKey

func (w *Wallet) GetMasterBlindingKey() (string, error)

GetMnemonic safely returns the master blinding key.

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) 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,
	) (*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