money

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: AGPL-3.0, ISC Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const WalletFileName = "wallet.db"

Variables

View Source
var (
	ErrSwapInProgress       = errors.New("swap is in progress, synchronize your wallet to complete the process")
	ErrInsufficientBalance  = errors.New("insufficient balance for transaction")
	ErrInvalidPubKey        = errors.New("invalid public key, public key must be in compressed secp256k1 format")
	ErrInvalidAmount        = errors.New("invalid amount")
	ErrInvalidAccountIndex  = errors.New("invalid account index")
	ErrInvalidPassword      = errors.New("invalid password")
	ErrInvalidBlockSystemID = errors.New("invalid system identifier")
	ErrTxFailedToConfirm    = errors.New("transaction(s) failed to confirm")
)

Functions

func IsEncrypted

func IsEncrypted(config WalletConfig) (bool, error)

IsEncrypted returns true if wallet exists and is encrypted and or false if wallet exists and is not encrypted, returns error if wallet does not exist.

func OpenDb

func OpenDb(config WalletConfig) (*wdb, error)

Types

type Bill added in v0.1.1

type Bill struct {
	Id         *uint256.Int `json:"id"`
	Value      uint64       `json:"value"`
	TxHash     []byte       `json:"txHash"`
	BlockProof *BlockProof  `json:"blockProof"`

	// dc bill specific fields
	IsDcBill  bool   `json:"dcBill"`
	DcTimeout uint64 `json:"dcTimeout"`
	DcNonce   []byte `json:"dcNonce"`
	// DcExpirationTimeout blockHeight when dc bill gets removed from state tree
	DcExpirationTimeout uint64 `json:"dcExpirationTimeout"`
}

func (*Bill) GetID added in v0.1.1

func (b *Bill) GetID() []byte

GetID returns bill id in 32-byte big endian array

func (*Bill) ToProto added in v0.1.1

func (b *Bill) ToProto() *block.Bill

type BlockProof added in v0.1.1

type BlockProof struct {
	Tx          *txsystem.Transaction `json:"tx"`
	Proof       *block.BlockProof     `json:"proof"`
	BlockNumber uint64                `json:"blockNumber"`
}

func NewBlockProof added in v0.1.1

func NewBlockProof(tx *txsystem.Transaction, proof *block.BlockProof, blockNumber uint64) (*BlockProof, error)

func (*BlockProof) ToSchema added in v0.1.1

func (b *BlockProof) ToSchema() *block.TxProof

func (*BlockProof) Verify added in v0.1.1

func (b *BlockProof) Verify(unitID []byte, verifiers map[string]abcrypto.Verifier, hashAlgorithm crypto.Hash) error

type Db

type Db interface {
	Do() TxContext
	WithTransaction(func(tx TxContext) error) error
	Close()
	DeleteDb()
}

type GetBalanceCmd added in v0.1.1

type GetBalanceCmd struct {
	AccountIndex uint64
	CountDCBills bool
}

type SendCmd added in v0.1.1

type SendCmd struct {
	ReceiverPubKey      []byte
	Amount              uint64
	WaitForConfirmation bool
	AccountIndex        uint64
}

type TxContext

type TxContext interface {
	AddAccount(accountIndex uint64, key *wallet.AccountKey) error
	GetAccountKey(accountIndex uint64) (*wallet.AccountKey, error)
	GetAccountKeys() ([]*wallet.AccountKey, error)
	GetMaxAccountIndex() (uint64, error)
	SetMaxAccountIndex(accountIndex uint64) error

	GetMasterKey() (string, error)
	SetMasterKey(masterKey string) error

	GetMnemonic() (string, error)
	SetMnemonic(mnemonic string) error

	IsEncrypted() (bool, error)
	SetEncrypted(encrypted bool) error
	VerifyPassword() (bool, error)

	GetBlockNumber() (uint64, error)
	SetBlockNumber(blockNumber uint64) error

	GetBill(accountIndex uint64, id []byte) (*Bill, error)
	SetBill(accountIndex uint64, bill *Bill) error
	ContainsBill(accountIndex uint64, id *uint256.Int) (bool, error)
	RemoveBill(accountIndex uint64, id *uint256.Int) error
	GetBills(accountIndex uint64) ([]*Bill, error)
	GetAllBills() ([][]*Bill, error)
	GetBalance(cmd GetBalanceCmd) (uint64, error)
	GetBalances(cmd GetBalanceCmd) ([]uint64, error)

	GetDcMetadataMap(accountIndex uint64) (map[uint256.Int]*dcMetadata, error)
	GetDcMetadata(accountIndex uint64, nonce []byte) (*dcMetadata, error)
	SetDcMetadata(accountIndex uint64, nonce []byte, dcMetadata *dcMetadata) error
}

type TxConverter added in v0.1.1

type TxConverter struct {
}

func (*TxConverter) ConvertTx added in v0.1.1

type Wallet

type Wallet struct {
	*wallet.Wallet
	// contains filtered or unexported fields
}

func CreateNewWallet

func CreateNewWallet(mnemonic string, config WalletConfig) (*Wallet, error)

CreateNewWallet creates a new wallet. To synchronize wallet with a node call Sync. Shutdown needs to be called to release resources used by wallet. If mnemonic seed is empty then new mnemonic will ge generated, otherwise wallet is restored using given mnemonic.

func LoadExistingWallet

func LoadExistingWallet(config WalletConfig) (*Wallet, error)

func (*Wallet) AddAccount added in v0.1.1

func (w *Wallet) AddAccount() (uint64, []byte, error)

AddAccount adds the next account in account key series to the wallet. New accounts are indexed only from the time of creation and not backwards in time. Returns new account's index and public key.

func (*Wallet) AddBill added in v0.1.1

func (w *Wallet) AddBill(accountIndex uint64, bill *Bill) error

AddBill adds bill to wallet. Given bill must have a valid transaction with P2PKH predicate for given account. Block proof is not verified, but transaction is required. Overwrites existing bill with the same ID, if one exists.

func (*Wallet) CollectDust

func (w *Wallet) CollectDust(ctx context.Context) error

CollectDust starts the dust collector process for all accounts in the wallet. Wallet needs to be synchronizing using Sync or SyncToMaxBlockNumber in order to receive transactions and finish the process. The function blocks until dust collector process is finished or timed out. Skips account if the account already has only one or no bills.

func (*Wallet) DeleteDb

func (w *Wallet) DeleteDb()

DeleteDb deletes the wallet database.

func (*Wallet) GetAccountKey added in v0.1.1

func (w *Wallet) GetAccountKey(accountIndex uint64) (*wallet.AccountKey, error)

func (*Wallet) GetAccountKeys added in v0.1.1

func (w *Wallet) GetAccountKeys() ([]*wallet.AccountKey, error)

func (*Wallet) GetAllBills added in v0.1.1

func (w *Wallet) GetAllBills() ([][]*Bill, error)

GetAllBills returns all bills owned by the wallet for all accounts.

func (*Wallet) GetBalance

func (w *Wallet) GetBalance(cmd GetBalanceCmd) (uint64, error)

GetBalance returns sum value of all bills currently owned by the wallet, for given account. The value returned is the smallest denomination of alphabills.

func (*Wallet) GetBalances added in v0.1.1

func (w *Wallet) GetBalances(cmd GetBalanceCmd) ([]uint64, error)

GetBalances returns sum value of all bills currently owned by the wallet, for all accounts. The value returned is the smallest denomination of alphabills.

func (*Wallet) GetBill added in v0.1.1

func (w *Wallet) GetBill(accountIndex uint64, billId []byte) (*Bill, error)

GetBill returns bill for the given bill id. If bill does not exist returns error "bill does not exist".

func (*Wallet) GetBills added in v0.1.1

func (w *Wallet) GetBills(accountIndex uint64) ([]*Bill, error)

GetBills returns all bills owned by the wallet for the given account.

func (*Wallet) GetConfig added in v0.1.1

func (w *Wallet) GetConfig() WalletConfig

func (*Wallet) GetMnemonic

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

GetMnemonic returns mnemonic seed of the wallet

func (*Wallet) GetPublicKey

func (w *Wallet) GetPublicKey(accountIndex uint64) ([]byte, error)

GetPublicKey returns public key of the wallet (compressed secp256k1 key 33 bytes)

func (*Wallet) GetPublicKeys added in v0.1.1

func (w *Wallet) GetPublicKeys() ([][]byte, error)

GetPublicKeys returns public keys of the wallet, indexed by account indexes

func (*Wallet) ProcessBlock

func (w *Wallet) ProcessBlock(b *block.Block) error

func (*Wallet) Send

func (w *Wallet) Send(ctx context.Context, cmd SendCmd) ([]*Bill, error)

Send creates, signs and broadcasts transactions, in total for the given amount, to the given public key, the public key must be in compressed secp256k1 format. Sends one transaction per bill, prioritzing larger bills. Returns list of bills including transaction and proof data, if waitForConfirmation=true, otherwise nil.

func (*Wallet) Shutdown

func (w *Wallet) Shutdown()

Shutdown terminates connection to alphabill node, closes wallet db, cancels dust collector job and any background goroutines.

func (*Wallet) StartDustCollectorJob

func (w *Wallet) StartDustCollectorJob() error

StartDustCollectorJob starts the dust collector background process that runs every hour until wallet is shut down. Wallet needs to be synchronizing using Sync or SyncToMaxBlockNumber in order to receive transactions and finish the process. Returns error if the job failed to start.

func (*Wallet) Sync

func (w *Wallet) Sync(ctx context.Context) error

Sync synchronises wallet from the last known block number with the given alphabill node. The function blocks forever or until alphabill connection is terminated. Returns immediately if already synchronizing.

func (*Wallet) SyncToMaxBlockNumber

func (w *Wallet) SyncToMaxBlockNumber(ctx context.Context) error

SyncToMaxBlockNumber synchronises wallet from the last known block number with the given alphabill node. The function blocks until maximum block height, calculated at the start of the process, is reached. Returns immediately with ErrWalletAlreadySynchronizing if already synchronizing.

type WalletConfig

type WalletConfig struct {
	// Directory where default boltdb wallet database is created, only used when Db is not set,
	// if empty then 'home/.alphabill/wallet' directory is used.
	DbPath string

	// Custom database implementation, if set then DbPath is not used,
	// if not set then boltdb is created at DbPath.
	Db Db

	// WalletPass used to encrypt/decrypt sensitive information. If empty then wallet will not be encrypted.
	WalletPass string

	// Configuration options for connecting to alphabill nodes.
	AlphabillClientConfig client.AlphabillClientConfig
}

func (*WalletConfig) GetWalletDir

func (c *WalletConfig) GetWalletDir() (string, error)

GetWalletDir returns wallet directory, if DbPath is set then returns DbPath, if DbPath is not set then returns 'home/.alphabill/wallet'.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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