Wallet API: generalize 'bool testnet' to 'NetworkType nettype'

This commit is contained in:
stoffu 2018-03-05 23:59:16 +09:00
parent af773211cb
commit 0e7ad2e2c9
No known key found for this signature in database
GPG Key ID: 41DAB8343A9EC012
5 changed files with 103 additions and 42 deletions

View File

@ -233,16 +233,16 @@ bool Wallet::paymentIdValid(const string &paiment_id)
return false;
}
bool Wallet::addressValid(const std::string &str, bool testnet)
bool Wallet::addressValid(const std::string &str, NetworkType nettype)
{
cryptonote::address_parse_info info;
return get_account_address_from_str(info, testnet ? TESTNET : MAINNET, str);
return get_account_address_from_str(info, static_cast<cryptonote::network_type>(nettype), str);
}
bool Wallet::keyValid(const std::string &secret_key_string, const std::string &address_string, bool isViewKey, bool testnet, std::string &error)
bool Wallet::keyValid(const std::string &secret_key_string, const std::string &address_string, bool isViewKey, NetworkType nettype, std::string &error)
{
cryptonote::address_parse_info info;
if(!get_account_address_from_str(info, testnet ? TESTNET : MAINNET, address_string)) {
if(!get_account_address_from_str(info, static_cast<cryptonote::network_type>(nettype), address_string)) {
error = tr("Failed to parse address");
return false;
}
@ -275,10 +275,10 @@ bool Wallet::keyValid(const std::string &secret_key_string, const std::string &a
return true;
}
std::string Wallet::paymentIdFromAddress(const std::string &str, bool testnet)
std::string Wallet::paymentIdFromAddress(const std::string &str, NetworkType nettype)
{
cryptonote::address_parse_info info;
if (!get_account_address_from_str(info, testnet ? TESTNET : MAINNET, str))
if (!get_account_address_from_str(info, static_cast<cryptonote::network_type>(nettype), str))
return "";
if (!info.has_payment_id)
return "";
@ -300,7 +300,7 @@ void Wallet::debug(const std::string &str) {
}
///////////////////////// WalletImpl implementation ////////////////////////
WalletImpl::WalletImpl(bool testnet)
WalletImpl::WalletImpl(NetworkType nettype)
:m_wallet(nullptr)
, m_status(Wallet::Status_Ok)
, m_trustedDaemon(false)
@ -310,7 +310,7 @@ WalletImpl::WalletImpl(bool testnet)
, m_rebuildWalletCache(false)
, m_is_connected(false)
{
m_wallet = new tools::wallet2(testnet ? TESTNET : MAINNET);
m_wallet = new tools::wallet2(static_cast<cryptonote::network_type>(nettype));
m_history = new TransactionHistoryImpl(this);
m_wallet2Callback = new Wallet2CallbackImpl(this);
m_wallet->callback(m_wallet2Callback);

View File

@ -52,7 +52,7 @@ struct Wallet2CallbackImpl;
class WalletImpl : public Wallet
{
public:
WalletImpl(bool testnet = false);
WalletImpl(NetworkType nettype = MAINNET);
~WalletImpl();
bool create(const std::string &path, const std::string &password,
const std::string &language);
@ -115,7 +115,7 @@ public:
void setRecoveringFromSeed(bool recoveringFromSeed);
bool watchOnly() const;
bool rescanSpent();
bool testnet() const {return m_wallet->nettype() == cryptonote::TESTNET;}
NetworkType nettype() const {return static_cast<NetworkType>(m_wallet->nettype());}
void hardForkInfo(uint8_t &version, uint64_t &earliest_height) const;
bool useForkRules(uint8_t version, int64_t early_blocks) const;

View File

@ -40,6 +40,12 @@
// Public interface for libwallet library
namespace Monero {
enum NetworkType : uint8_t {
MAINNET = 0,
TESTNET,
STAGENET
};
namespace Utils {
bool isAddressLocal(const std::string &hostaddr);
void onStartup();
@ -358,7 +364,10 @@ struct Wallet
virtual std::string address(uint32_t accountIndex = 0, uint32_t addressIndex = 0) const = 0;
std::string mainAddress() const { return address(0, 0); }
virtual std::string path() const = 0;
virtual bool testnet() const = 0;
virtual NetworkType nettype() const = 0;
bool mainnet() const { return nettype() == MAINNET; }
bool testnet() const { return nettype() == TESTNET; }
bool stagenet() const { return nettype() == STAGENET; }
//! returns current hard fork info
virtual void hardForkInfo(uint8_t &version, uint64_t &earliest_height) const = 0;
//! check if hard fork rules should be used
@ -529,9 +538,21 @@ struct Wallet
static uint64_t amountFromDouble(double amount);
static std::string genPaymentId();
static bool paymentIdValid(const std::string &paiment_id);
static bool addressValid(const std::string &str, bool testnet);
static bool keyValid(const std::string &secret_key_string, const std::string &address_string, bool isViewKey, bool testnet, std::string &error);
static std::string paymentIdFromAddress(const std::string &str, bool testnet);
static bool addressValid(const std::string &str, NetworkType nettype);
static bool addressValid(const std::string &str, bool testnet) // deprecated
{
return addressValid(str, testnet ? MAINNET : TESTNET);
}
static bool keyValid(const std::string &secret_key_string, const std::string &address_string, bool isViewKey, NetworkType nettype, std::string &error);
static bool keyValid(const std::string &secret_key_string, const std::string &address_string, bool isViewKey, bool testnet, std::string &error) // deprecated
{
return keyValid(secret_key_string, address_string, isViewKey, testnet ? TESTNET : MAINNET, error);
}
static std::string paymentIdFromAddress(const std::string &str, NetworkType nettype);
static std::string paymentIdFromAddress(const std::string &str, bool testnet) // deprecated
{
return paymentIdFromAddress(str, testnet ? TESTNET : MAINNET);
}
static uint64_t maximumAllowedAmount();
// Easylogger wrapper
static void init(const char *argv0, const char *default_log_base_name);
@ -750,47 +771,66 @@ struct WalletManager
* \param path Name of wallet file
* \param password Password of wallet file
* \param language Language to be used to generate electrum seed mnemonic
* \param nettype Network type
* \return Wallet instance (Wallet::status() needs to be called to check if created successfully)
*/
virtual Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language, bool testnet = false) = 0;
virtual Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language, NetworkType nettype = MAINNET) = 0;
Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language, bool testnet = false) // deprecated
{
return createWallet(path, password, language, testnet ? TESTNET : MAINNET);
}
/*!
* \brief Opens existing wallet
* \param path Name of wallet file
* \param password Password of wallet file
* \param nettype Network type
* \return Wallet instance (Wallet::status() needs to be called to check if opened successfully)
*/
virtual Wallet * openWallet(const std::string &path, const std::string &password, bool testnet = false) = 0;
virtual Wallet * openWallet(const std::string &path, const std::string &password, NetworkType nettype = MAINNET) = 0;
Wallet * openWallet(const std::string &path, const std::string &password, bool testnet = false) // deprecated
{
return openWallet(path, password, testnet ? TESTNET : MAINNET);
}
/*!
* \brief recovers existing wallet using mnemonic (electrum seed)
* \param path Name of wallet file to be created
* \param password Password of wallet file
* \param mnemonic mnemonic (25 words electrum seed)
* \param testnet testnet
* \param nettype Network type
* \param restoreHeight restore from start height
* \return Wallet instance (Wallet::status() needs to be called to check if recovered successfully)
*/
virtual Wallet * recoveryWallet(const std::string &path, const std::string &password, const std::string &mnemonic,
bool testnet = false, uint64_t restoreHeight = 0) = 0;
NetworkType nettype = MAINNET, uint64_t restoreHeight = 0) = 0;
Wallet * recoveryWallet(const std::string &path, const std::string &password, const std::string &mnemonic,
bool testnet = false, uint64_t restoreHeight = 0) // deprecated
{
return recoveryWallet(path, password, mnemonic, testnet ? TESTNET : MAINNET, restoreHeight);
}
/*!
* \deprecated this method creates a wallet WITHOUT a passphrase, use the alternate recoverWallet() method
* \brief recovers existing wallet using mnemonic (electrum seed)
* \param path Name of wallet file to be created
* \param mnemonic mnemonic (25 words electrum seed)
* \param testnet testnet
* \param nettype Network type
* \param restoreHeight restore from start height
* \return Wallet instance (Wallet::status() needs to be called to check if recovered successfully)
*/
virtual Wallet * recoveryWallet(const std::string &path, const std::string &mnemonic, bool testnet = false, uint64_t restoreHeight = 0) = 0;
virtual Wallet * recoveryWallet(const std::string &path, const std::string &mnemonic, NetworkType nettype = MAINNET, uint64_t restoreHeight = 0) = 0;
Wallet * recoveryWallet(const std::string &path, const std::string &mnemonic, bool testnet = false, uint64_t restoreHeight = 0) // deprecated
{
return recoveryWallet(path, mnemonic, testnet ? TESTNET : MAINNET, restoreHeight);
}
/*!
* \brief recovers existing wallet using keys. Creates a view only wallet if spend key is omitted
* \param path Name of wallet file to be created
* \param password Password of wallet file
* \param language language
* \param testnet testnet
* \param nettype Network type
* \param restoreHeight restore from start height
* \param addressString public address
* \param viewKeyString view key
@ -800,18 +840,29 @@ struct WalletManager
virtual Wallet * createWalletFromKeys(const std::string &path,
const std::string &password,
const std::string &language,
bool testnet,
NetworkType nettype,
uint64_t restoreHeight,
const std::string &addressString,
const std::string &viewKeyString,
const std::string &spendKeyString = "") = 0;
Wallet * createWalletFromKeys(const std::string &path,
const std::string &password,
const std::string &language,
bool testnet,
uint64_t restoreHeight,
const std::string &addressString,
const std::string &viewKeyString,
const std::string &spendKeyString = "") // deprecated
{
return createWalletFromKeys(path, password, language, testnet ? TESTNET : MAINNET, restoreHeight, addressString, viewKeyString, spendKeyString);
}
/*!
* \deprecated this method creates a wallet WITHOUT a passphrase, use createWalletFromKeys(..., password, ...) instead
* \brief recovers existing wallet using keys. Creates a view only wallet if spend key is omitted
* \param path Name of wallet file to be created
* \param language language
* \param testnet testnet
* \param nettype Network type
* \param restoreHeight restore from start height
* \param addressString public address
* \param viewKeyString view key
@ -820,11 +871,21 @@ struct WalletManager
*/
virtual Wallet * createWalletFromKeys(const std::string &path,
const std::string &language,
bool testnet,
NetworkType nettype,
uint64_t restoreHeight,
const std::string &addressString,
const std::string &viewKeyString,
const std::string &spendKeyString = "") = 0;
Wallet * createWalletFromKeys(const std::string &path,
const std::string &language,
bool testnet,
uint64_t restoreHeight,
const std::string &addressString,
const std::string &viewKeyString,
const std::string &spendKeyString = "") // deprecated
{
return createWalletFromKeys(path, language, testnet ? TESTNET : MAINNET, restoreHeight, addressString, viewKeyString, spendKeyString);
}
/*!
* \brief Closes wallet. In case operation succeded, wallet object deleted. in case operation failed, wallet object not deleted

View File

@ -60,46 +60,46 @@ namespace {
namespace Monero {
Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::string &password,
const std::string &language, bool testnet)
const std::string &language, NetworkType nettype)
{
WalletImpl * wallet = new WalletImpl(testnet);
WalletImpl * wallet = new WalletImpl(nettype);
wallet->create(path, password, language);
return wallet;
}
Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string &password, bool testnet)
Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string &password, NetworkType nettype)
{
WalletImpl * wallet = new WalletImpl(testnet);
WalletImpl * wallet = new WalletImpl(nettype);
wallet->open(path, password);
//Refresh addressBook
wallet->addressBook()->refresh();
return wallet;
}
Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &mnemonic, bool testnet, uint64_t restoreHeight)
Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &mnemonic, NetworkType nettype, uint64_t restoreHeight)
{
return recoveryWallet(path, "", mnemonic, testnet, restoreHeight);
return recoveryWallet(path, "", mnemonic, nettype, restoreHeight);
}
Wallet *WalletManagerImpl::createWalletFromKeys(const std::string &path,
const std::string &language,
bool testnet,
NetworkType nettype,
uint64_t restoreHeight,
const std::string &addressString,
const std::string &viewKeyString,
const std::string &spendKeyString)
{
return createWalletFromKeys(path, "", language, testnet, restoreHeight,
return createWalletFromKeys(path, "", language, nettype, restoreHeight,
addressString, viewKeyString, spendKeyString);
}
Wallet *WalletManagerImpl::recoveryWallet(const std::string &path,
const std::string &password,
const std::string &mnemonic,
bool testnet,
NetworkType nettype,
uint64_t restoreHeight)
{
WalletImpl * wallet = new WalletImpl(testnet);
WalletImpl * wallet = new WalletImpl(nettype);
if(restoreHeight > 0){
wallet->setRefreshFromBlockHeight(restoreHeight);
}
@ -110,13 +110,13 @@ Wallet *WalletManagerImpl::recoveryWallet(const std::string &path,
Wallet *WalletManagerImpl::createWalletFromKeys(const std::string &path,
const std::string &password,
const std::string &language,
bool testnet,
NetworkType nettype,
uint64_t restoreHeight,
const std::string &addressString,
const std::string &viewKeyString,
const std::string &spendKeyString)
{
WalletImpl * wallet = new WalletImpl(testnet);
WalletImpl * wallet = new WalletImpl(nettype);
if(restoreHeight > 0){
wallet->setRefreshFromBlockHeight(restoreHeight);
}

View File

@ -38,27 +38,27 @@ class WalletManagerImpl : public WalletManager
{
public:
Wallet * createWallet(const std::string &path, const std::string &password,
const std::string &language, bool testnet);
Wallet * openWallet(const std::string &path, const std::string &password, bool testnet);
const std::string &language, NetworkType nettype);
Wallet * openWallet(const std::string &path, const std::string &password, NetworkType nettype);
virtual Wallet * recoveryWallet(const std::string &path,
const std::string &password,
const std::string &mnemonic,
bool testnet,
NetworkType nettype,
uint64_t restoreHeight);
virtual Wallet * createWalletFromKeys(const std::string &path,
const std::string &password,
const std::string &language,
bool testnet,
NetworkType nettype,
uint64_t restoreHeight,
const std::string &addressString,
const std::string &viewKeyString,
const std::string &spendKeyString = "");
// next two methods are deprecated - use the above version which allow setting of a password
virtual Wallet * recoveryWallet(const std::string &path, const std::string &mnemonic, bool testnet, uint64_t restoreHeight);
virtual Wallet * recoveryWallet(const std::string &path, const std::string &mnemonic, NetworkType nettype, uint64_t restoreHeight);
// deprecated: use createWalletFromKeys(..., password, ...) instead
virtual Wallet * createWalletFromKeys(const std::string &path,
const std::string &language,
bool testnet,
NetworkType nettype,
uint64_t restoreHeight,
const std::string &addressString,
const std::string &viewKeyString,