2017-04-17 21:10:47 +02:00
|
|
|
// Copyright (c) 2018 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2018-04-07 09:42:02 +02:00
|
|
|
#ifndef BITCOIN_INTERFACES_WALLET_H
|
|
|
|
#define BITCOIN_INTERFACES_WALLET_H
|
2017-04-17 21:10:47 +02:00
|
|
|
|
2017-04-18 00:56:44 +02:00
|
|
|
#include <amount.h> // For CAmount
|
2018-04-19 11:13:09 +02:00
|
|
|
#include <pubkey.h> // For CKeyID and CScriptID (definitions needed in CTxDestination instantiation)
|
2017-04-18 00:56:44 +02:00
|
|
|
#include <script/ismine.h> // For isminefilter, isminetype
|
|
|
|
#include <script/standard.h> // For CTxDestination
|
|
|
|
#include <support/allocators/secure.h> // For SecureString
|
|
|
|
#include <ui_interface.h> // For ChangeType
|
|
|
|
|
2017-04-17 21:10:47 +02:00
|
|
|
#include <functional>
|
2017-04-18 00:56:44 +02:00
|
|
|
#include <map>
|
2017-04-17 21:10:47 +02:00
|
|
|
#include <memory>
|
2017-04-18 00:56:44 +02:00
|
|
|
#include <stdint.h>
|
2017-04-17 21:10:47 +02:00
|
|
|
#include <string>
|
2017-04-18 01:46:08 +02:00
|
|
|
#include <tuple>
|
2017-04-18 00:56:44 +02:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2017-04-17 21:10:47 +02:00
|
|
|
|
2017-04-18 00:56:44 +02:00
|
|
|
class CCoinControl;
|
2018-04-07 18:12:46 +02:00
|
|
|
class CFeeRate;
|
2017-04-18 00:56:44 +02:00
|
|
|
class CKey;
|
2017-04-17 21:10:47 +02:00
|
|
|
class CWallet;
|
2018-04-07 18:12:46 +02:00
|
|
|
enum class FeeReason;
|
2017-04-18 00:56:44 +02:00
|
|
|
enum class OutputType;
|
|
|
|
struct CRecipient;
|
2017-04-17 21:10:47 +02:00
|
|
|
|
2018-04-07 09:42:02 +02:00
|
|
|
namespace interfaces {
|
2017-04-17 21:10:47 +02:00
|
|
|
|
|
|
|
class Handler;
|
2017-04-18 00:56:44 +02:00
|
|
|
class PendingWalletTx;
|
2017-04-18 19:01:23 +02:00
|
|
|
struct WalletAddress;
|
2017-04-18 00:56:44 +02:00
|
|
|
struct WalletBalances;
|
2017-04-18 22:42:30 +02:00
|
|
|
struct WalletTx;
|
2017-04-18 01:46:08 +02:00
|
|
|
struct WalletTxOut;
|
2017-04-18 22:42:30 +02:00
|
|
|
struct WalletTxStatus;
|
2017-04-18 00:56:44 +02:00
|
|
|
|
|
|
|
using WalletOrderForm = std::vector<std::pair<std::string, std::string>>;
|
|
|
|
using WalletValueMap = std::map<std::string, std::string>;
|
2017-04-17 21:10:47 +02:00
|
|
|
|
|
|
|
//! Interface for accessing a wallet.
|
|
|
|
class Wallet
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~Wallet() {}
|
|
|
|
|
2017-04-18 00:56:44 +02:00
|
|
|
//! Encrypt wallet.
|
|
|
|
virtual bool encryptWallet(const SecureString& wallet_passphrase) = 0;
|
|
|
|
|
|
|
|
//! Return whether wallet is encrypted.
|
|
|
|
virtual bool isCrypted() = 0;
|
|
|
|
|
|
|
|
//! Lock wallet.
|
|
|
|
virtual bool lock() = 0;
|
|
|
|
|
|
|
|
//! Unlock wallet.
|
|
|
|
virtual bool unlock(const SecureString& wallet_passphrase) = 0;
|
|
|
|
|
|
|
|
//! Return whether wallet is locked.
|
|
|
|
virtual bool isLocked() = 0;
|
|
|
|
|
|
|
|
//! Change wallet passphrase.
|
|
|
|
virtual bool changeWalletPassphrase(const SecureString& old_wallet_passphrase,
|
|
|
|
const SecureString& new_wallet_passphrase) = 0;
|
|
|
|
|
2018-03-31 22:02:58 +02:00
|
|
|
//! Abort a rescan.
|
|
|
|
virtual void abortRescan() = 0;
|
|
|
|
|
2017-04-18 00:56:44 +02:00
|
|
|
//! Back up wallet.
|
|
|
|
virtual bool backupWallet(const std::string& filename) = 0;
|
|
|
|
|
|
|
|
//! Get wallet name.
|
|
|
|
virtual std::string getWalletName() = 0;
|
|
|
|
|
2017-04-18 19:01:23 +02:00
|
|
|
// Get key from pool.
|
|
|
|
virtual bool getKeyFromPool(bool internal, CPubKey& pub_key) = 0;
|
|
|
|
|
2017-04-18 00:56:44 +02:00
|
|
|
//! Get public key.
|
|
|
|
virtual bool getPubKey(const CKeyID& address, CPubKey& pub_key) = 0;
|
|
|
|
|
|
|
|
//! Get private key.
|
|
|
|
virtual bool getPrivKey(const CKeyID& address, CKey& key) = 0;
|
|
|
|
|
|
|
|
//! Return whether wallet has private key.
|
|
|
|
virtual bool isSpendable(const CTxDestination& dest) = 0;
|
|
|
|
|
|
|
|
//! Return whether wallet has watch only keys.
|
|
|
|
virtual bool haveWatchOnly() = 0;
|
|
|
|
|
|
|
|
//! Add or update address.
|
|
|
|
virtual bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::string& purpose) = 0;
|
|
|
|
|
2017-04-18 19:01:23 +02:00
|
|
|
// Remove address.
|
|
|
|
virtual bool delAddressBook(const CTxDestination& dest) = 0;
|
|
|
|
|
2017-04-18 00:56:44 +02:00
|
|
|
//! Look up address in wallet, return whether exists.
|
|
|
|
virtual bool getAddress(const CTxDestination& dest,
|
2018-04-10 17:50:10 +02:00
|
|
|
std::string* name,
|
|
|
|
isminetype* is_mine,
|
|
|
|
std::string* purpose) = 0;
|
2017-04-18 00:56:44 +02:00
|
|
|
|
2017-04-18 19:01:23 +02:00
|
|
|
//! Get wallet address list.
|
|
|
|
virtual std::vector<WalletAddress> getAddresses() = 0;
|
|
|
|
|
|
|
|
//! Add scripts to key store so old so software versions opening the wallet
|
|
|
|
//! database can detect payments to newer address types.
|
|
|
|
virtual void learnRelatedScripts(const CPubKey& key, OutputType type) = 0;
|
|
|
|
|
2017-04-18 00:56:44 +02:00
|
|
|
//! Add dest data.
|
|
|
|
virtual bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) = 0;
|
|
|
|
|
|
|
|
//! Erase dest data.
|
|
|
|
virtual bool eraseDestData(const CTxDestination& dest, const std::string& key) = 0;
|
|
|
|
|
|
|
|
//! Get dest values with prefix.
|
|
|
|
virtual std::vector<std::string> getDestValues(const std::string& prefix) = 0;
|
|
|
|
|
|
|
|
//! Lock coin.
|
|
|
|
virtual void lockCoin(const COutPoint& output) = 0;
|
|
|
|
|
|
|
|
//! Unlock coin.
|
|
|
|
virtual void unlockCoin(const COutPoint& output) = 0;
|
|
|
|
|
|
|
|
//! Return whether coin is locked.
|
|
|
|
virtual bool isLockedCoin(const COutPoint& output) = 0;
|
|
|
|
|
|
|
|
//! List locked coins.
|
|
|
|
virtual void listLockedCoins(std::vector<COutPoint>& outputs) = 0;
|
|
|
|
|
|
|
|
//! Create transaction.
|
|
|
|
virtual std::unique_ptr<PendingWalletTx> createTransaction(const std::vector<CRecipient>& recipients,
|
|
|
|
const CCoinControl& coin_control,
|
|
|
|
bool sign,
|
|
|
|
int& change_pos,
|
|
|
|
CAmount& fee,
|
|
|
|
std::string& fail_reason) = 0;
|
|
|
|
|
|
|
|
//! Return whether transaction can be abandoned.
|
|
|
|
virtual bool transactionCanBeAbandoned(const uint256& txid) = 0;
|
|
|
|
|
|
|
|
//! Abandon transaction.
|
|
|
|
virtual bool abandonTransaction(const uint256& txid) = 0;
|
|
|
|
|
|
|
|
//! Return whether transaction can be bumped.
|
|
|
|
virtual bool transactionCanBeBumped(const uint256& txid) = 0;
|
|
|
|
|
|
|
|
//! Create bump transaction.
|
|
|
|
virtual bool createBumpTransaction(const uint256& txid,
|
|
|
|
const CCoinControl& coin_control,
|
|
|
|
CAmount total_fee,
|
|
|
|
std::vector<std::string>& errors,
|
|
|
|
CAmount& old_fee,
|
|
|
|
CAmount& new_fee,
|
|
|
|
CMutableTransaction& mtx) = 0;
|
|
|
|
|
|
|
|
//! Sign bump transaction.
|
|
|
|
virtual bool signBumpTransaction(CMutableTransaction& mtx) = 0;
|
|
|
|
|
|
|
|
//! Commit bump transaction.
|
|
|
|
virtual bool commitBumpTransaction(const uint256& txid,
|
|
|
|
CMutableTransaction&& mtx,
|
|
|
|
std::vector<std::string>& errors,
|
|
|
|
uint256& bumped_txid) = 0;
|
|
|
|
|
2017-04-18 22:42:30 +02:00
|
|
|
//! Get a transaction.
|
|
|
|
virtual CTransactionRef getTx(const uint256& txid) = 0;
|
|
|
|
|
|
|
|
//! Get transaction information.
|
|
|
|
virtual WalletTx getWalletTx(const uint256& txid) = 0;
|
|
|
|
|
|
|
|
//! Get list of all wallet transactions.
|
|
|
|
virtual std::vector<WalletTx> getWalletTxs() = 0;
|
|
|
|
|
|
|
|
//! Try to get updated status for a particular transaction, if possible without blocking.
|
|
|
|
virtual bool tryGetTxStatus(const uint256& txid,
|
|
|
|
WalletTxStatus& tx_status,
|
|
|
|
int& num_blocks,
|
|
|
|
int64_t& adjusted_time) = 0;
|
|
|
|
|
|
|
|
//! Get transaction details.
|
|
|
|
virtual WalletTx getWalletTxDetails(const uint256& txid,
|
|
|
|
WalletTxStatus& tx_status,
|
|
|
|
WalletOrderForm& order_form,
|
|
|
|
bool& in_mempool,
|
|
|
|
int& num_blocks,
|
|
|
|
int64_t& adjusted_time) = 0;
|
|
|
|
|
2017-04-18 00:56:44 +02:00
|
|
|
//! Get balances.
|
|
|
|
virtual WalletBalances getBalances() = 0;
|
|
|
|
|
|
|
|
//! Get balances if possible without blocking.
|
|
|
|
virtual bool tryGetBalances(WalletBalances& balances, int& num_blocks) = 0;
|
|
|
|
|
|
|
|
//! Get balance.
|
|
|
|
virtual CAmount getBalance() = 0;
|
|
|
|
|
|
|
|
//! Get available balance.
|
|
|
|
virtual CAmount getAvailableBalance(const CCoinControl& coin_control) = 0;
|
|
|
|
|
2017-04-18 22:42:30 +02:00
|
|
|
//! Return whether transaction input belongs to wallet.
|
|
|
|
virtual isminetype txinIsMine(const CTxIn& txin) = 0;
|
|
|
|
|
|
|
|
//! Return whether transaction output belongs to wallet.
|
|
|
|
virtual isminetype txoutIsMine(const CTxOut& txout) = 0;
|
|
|
|
|
|
|
|
//! Return debit amount if transaction input belongs to wallet.
|
|
|
|
virtual CAmount getDebit(const CTxIn& txin, isminefilter filter) = 0;
|
|
|
|
|
|
|
|
//! Return credit amount if transaction input belongs to wallet.
|
|
|
|
virtual CAmount getCredit(const CTxOut& txout, isminefilter filter) = 0;
|
|
|
|
|
2017-04-18 01:46:08 +02:00
|
|
|
//! Return AvailableCoins + LockedCoins grouped by wallet address.
|
|
|
|
//! (put change in one group with wallet address)
|
|
|
|
using CoinsList = std::map<CTxDestination, std::vector<std::tuple<COutPoint, WalletTxOut>>>;
|
|
|
|
virtual CoinsList listCoins() = 0;
|
|
|
|
|
|
|
|
//! Return wallet transaction output information.
|
|
|
|
virtual std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) = 0;
|
|
|
|
|
2018-04-07 18:12:46 +02:00
|
|
|
//! Get required fee.
|
|
|
|
virtual CAmount getRequiredFee(unsigned int tx_bytes) = 0;
|
|
|
|
|
|
|
|
//! Get minimum fee.
|
|
|
|
virtual CAmount getMinimumFee(unsigned int tx_bytes,
|
|
|
|
const CCoinControl& coin_control,
|
|
|
|
int* returned_target,
|
|
|
|
FeeReason* reason) = 0;
|
|
|
|
|
|
|
|
//! Get tx confirm target.
|
|
|
|
virtual unsigned int getConfirmTarget() = 0;
|
|
|
|
|
2017-04-18 00:56:44 +02:00
|
|
|
// Return whether HD enabled.
|
|
|
|
virtual bool hdEnabled() = 0;
|
|
|
|
|
2017-02-01 13:54:28 +01:00
|
|
|
// check if a certain wallet flag is set.
|
|
|
|
virtual bool IsWalletFlagSet(uint64_t flag) = 0;
|
|
|
|
|
2017-04-18 00:56:44 +02:00
|
|
|
// Get default address type.
|
|
|
|
virtual OutputType getDefaultAddressType() = 0;
|
|
|
|
|
|
|
|
// Get default change type.
|
|
|
|
virtual OutputType getDefaultChangeType() = 0;
|
|
|
|
|
2018-06-05 12:17:28 +02:00
|
|
|
//! Register handler for unload message.
|
|
|
|
using UnloadFn = std::function<void()>;
|
|
|
|
virtual std::unique_ptr<Handler> handleUnload(UnloadFn fn) = 0;
|
|
|
|
|
2017-04-17 21:10:47 +02:00
|
|
|
//! Register handler for show progress messages.
|
|
|
|
using ShowProgressFn = std::function<void(const std::string& title, int progress)>;
|
|
|
|
virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
|
2017-04-18 00:56:44 +02:00
|
|
|
|
|
|
|
//! Register handler for status changed messages.
|
|
|
|
using StatusChangedFn = std::function<void()>;
|
|
|
|
virtual std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) = 0;
|
|
|
|
|
|
|
|
//! Register handler for address book changed messages.
|
|
|
|
using AddressBookChangedFn = std::function<void(const CTxDestination& address,
|
|
|
|
const std::string& label,
|
|
|
|
bool is_mine,
|
|
|
|
const std::string& purpose,
|
|
|
|
ChangeType status)>;
|
|
|
|
virtual std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) = 0;
|
|
|
|
|
|
|
|
//! Register handler for transaction changed messages.
|
|
|
|
using TransactionChangedFn = std::function<void(const uint256& txid, ChangeType status)>;
|
|
|
|
virtual std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) = 0;
|
|
|
|
|
|
|
|
//! Register handler for watchonly changed messages.
|
|
|
|
using WatchOnlyChangedFn = std::function<void(bool have_watch_only)>;
|
|
|
|
virtual std::unique_ptr<Handler> handleWatchOnlyChanged(WatchOnlyChangedFn fn) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
//! Tracking object returned by CreateTransaction and passed to CommitTransaction.
|
|
|
|
class PendingWalletTx
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~PendingWalletTx() {}
|
|
|
|
|
|
|
|
//! Get transaction data.
|
|
|
|
virtual const CTransaction& get() = 0;
|
|
|
|
|
|
|
|
//! Get virtual transaction size.
|
|
|
|
virtual int64_t getVirtualSize() = 0;
|
|
|
|
|
|
|
|
//! Send pending transaction and commit to wallet.
|
|
|
|
virtual bool commit(WalletValueMap value_map,
|
|
|
|
WalletOrderForm order_form,
|
|
|
|
std::string& reject_reason) = 0;
|
|
|
|
};
|
|
|
|
|
2017-04-18 19:01:23 +02:00
|
|
|
//! Information about one wallet address.
|
|
|
|
struct WalletAddress
|
|
|
|
{
|
|
|
|
CTxDestination dest;
|
|
|
|
isminetype is_mine;
|
|
|
|
std::string name;
|
|
|
|
std::string purpose;
|
|
|
|
|
|
|
|
WalletAddress(CTxDestination dest, isminetype is_mine, std::string name, std::string purpose)
|
|
|
|
: dest(std::move(dest)), is_mine(is_mine), name(std::move(name)), purpose(std::move(purpose))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-18 00:56:44 +02:00
|
|
|
//! Collection of wallet balances.
|
|
|
|
struct WalletBalances
|
|
|
|
{
|
|
|
|
CAmount balance = 0;
|
|
|
|
CAmount unconfirmed_balance = 0;
|
|
|
|
CAmount immature_balance = 0;
|
|
|
|
bool have_watch_only = false;
|
|
|
|
CAmount watch_only_balance = 0;
|
|
|
|
CAmount unconfirmed_watch_only_balance = 0;
|
|
|
|
CAmount immature_watch_only_balance = 0;
|
|
|
|
|
|
|
|
bool balanceChanged(const WalletBalances& prev) const
|
|
|
|
{
|
|
|
|
return balance != prev.balance || unconfirmed_balance != prev.unconfirmed_balance ||
|
|
|
|
immature_balance != prev.immature_balance || watch_only_balance != prev.watch_only_balance ||
|
|
|
|
unconfirmed_watch_only_balance != prev.unconfirmed_watch_only_balance ||
|
|
|
|
immature_watch_only_balance != prev.immature_watch_only_balance;
|
|
|
|
}
|
2017-04-17 21:10:47 +02:00
|
|
|
};
|
|
|
|
|
2017-04-18 22:42:30 +02:00
|
|
|
// Wallet transaction information.
|
|
|
|
struct WalletTx
|
|
|
|
{
|
|
|
|
CTransactionRef tx;
|
|
|
|
std::vector<isminetype> txin_is_mine;
|
|
|
|
std::vector<isminetype> txout_is_mine;
|
|
|
|
std::vector<CTxDestination> txout_address;
|
|
|
|
std::vector<isminetype> txout_address_is_mine;
|
|
|
|
CAmount credit;
|
|
|
|
CAmount debit;
|
|
|
|
CAmount change;
|
|
|
|
int64_t time;
|
|
|
|
std::map<std::string, std::string> value_map;
|
|
|
|
bool is_coinbase;
|
|
|
|
};
|
|
|
|
|
|
|
|
//! Updated transaction status.
|
|
|
|
struct WalletTxStatus
|
|
|
|
{
|
|
|
|
int block_height;
|
|
|
|
int blocks_to_maturity;
|
|
|
|
int depth_in_main_chain;
|
|
|
|
unsigned int time_received;
|
|
|
|
uint32_t lock_time;
|
|
|
|
bool is_final;
|
|
|
|
bool is_trusted;
|
|
|
|
bool is_abandoned;
|
|
|
|
bool is_coinbase;
|
|
|
|
bool is_in_main_chain;
|
|
|
|
};
|
|
|
|
|
2017-04-18 01:46:08 +02:00
|
|
|
//! Wallet transaction output.
|
|
|
|
struct WalletTxOut
|
|
|
|
{
|
|
|
|
CTxOut txout;
|
|
|
|
int64_t time;
|
|
|
|
int depth_in_main_chain = -1;
|
|
|
|
bool is_spent = false;
|
|
|
|
};
|
|
|
|
|
2018-09-12 16:05:00 +02:00
|
|
|
//! Return implementation of Wallet interface. This function is defined in
|
|
|
|
//! dummywallet.cpp and throws if the wallet component is not compiled.
|
2018-05-22 17:18:07 +02:00
|
|
|
std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet>& wallet);
|
2017-04-17 21:10:47 +02:00
|
|
|
|
2018-04-07 09:42:02 +02:00
|
|
|
} // namespace interfaces
|
2017-04-17 21:10:47 +02:00
|
|
|
|
2018-04-07 09:42:02 +02:00
|
|
|
#endif // BITCOIN_INTERFACES_WALLET_H
|