Skip to content

Commit

Permalink
Enable decoding and optional encoding of cashaddr
Browse files Browse the repository at this point in the history
Summary:
Wraps both cashaddr and base58 addresses into EncodeDestination and
DecodeDestination.

Test Plan: Added unittest

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Subscribers: deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D660
  • Loading branch information
dagurval committed Nov 14, 2017
1 parent eb096c1 commit 1a3a9d7
Show file tree
Hide file tree
Showing 31 changed files with 202 additions and 36 deletions.
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ BITCOIN_CORE_H = \
core_io.h \
core_memusage.h \
cuckoocache.h \
dstencode.h \
globals.h \
httprpc.h \
httpserver.h \
Expand Down Expand Up @@ -325,8 +326,11 @@ libbitcoin_common_a_SOURCES = \
cashaddr.cpp \
cashaddrenc.cpp \
chainparams.cpp \
config.cpp \
coins.cpp \
compressor.cpp \
dstencode.cpp \
globals.cpp \
core_read.cpp \
core_write.cpp \
key.cpp \
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ BITCOIN_TESTS =\
test/crypto_tests.cpp \
test/cuckoocache_tests.cpp \
test/DoS_tests.cpp \
test/dstencode_tests.cpp \
test/excessiveblock_tests.cpp \
test/getarg_tests.cpp \
test/hash_tests.cpp \
Expand Down
20 changes: 7 additions & 13 deletions src/base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ CTxDestination DecodeDestination(const std::string &str,
}
return CNoDestination();
}
} // namespace

} // namespace

Expand Down Expand Up @@ -291,19 +292,12 @@ bool CBitcoinSecret::SetString(const std::string &strSecret) {
return SetString(strSecret.c_str());
}

std::string EncodeDestination(const CTxDestination &dest) {
return boost::apply_visitor(DestinationEncoder(Params()), dest);
}

CTxDestination DecodeDestination(const std::string &str) {
return DecodeDestination(str, Params());
}

bool IsValidDestinationString(const std::string &str,
const CChainParams &params) {
return IsValidDestination(DecodeDestination(str, params));
std::string EncodeLegacyAddr(const CTxDestination &dest,
const CChainParams &params) {
return boost::apply_visitor(DestinationEncoder(params), dest);
}

bool IsValidDestinationString(const std::string &str) {
return IsValidDestination(DecodeDestination(str, Params()));
CTxDestination DecodeLegacyAddr(const std::string &str,
const CChainParams &params) {
return DecodeDestination(str, params);
}
7 changes: 2 additions & 5 deletions src/base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ typedef CBitcoinExtKeyBase<CExtPubKey, BIP32_EXTKEY_SIZE,
CChainParams::EXT_PUBLIC_KEY>
CBitcoinExtPubKey;

std::string EncodeDestination(const CTxDestination &dest);
CTxDestination DecodeDestination(const std::string &str);
bool IsValidDestinationString(const std::string &str);
bool IsValidDestinationString(const std::string &str,
const CChainParams &params);
std::string EncodeLegacyAddr(const CTxDestination &dest, const CChainParams &);
CTxDestination DecodeLegacyAddr(const std::string &str, const CChainParams &);

#endif // BITCOIN_BASE58_H
2 changes: 2 additions & 0 deletions src/bitcoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
#endif

#include "base58.h"
#include "chainparams.h"
#include "clientversion.h"
#include "coins.h"
#include "consensus/consensus.h"
#include "core_io.h"
#include "dstencode.h"
#include "keystore.h"
#include "policy/policy.h"
#include "primitives/transaction.h"
Expand Down
9 changes: 9 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "consensus/consensus.h"
#include "globals.h"

GlobalConfig::GlobalConfig() : useCashAddr(false) {}

bool GlobalConfig::SetMaxBlockSize(uint64_t maxBlockSize) {
// Do not allow maxBlockSize to be set below historic 1MB limit
// It cannot be equal either because of the "must be big" UAHF rule.
Expand Down Expand Up @@ -44,3 +46,10 @@ static GlobalConfig gConfig;
const Config &GetConfig() {
return gConfig;
}

void GlobalConfig::SetCashAddrEncoding(bool c) {
useCashAddr = c;
}
bool GlobalConfig::UseCashAddrEncoding() const {
return useCashAddr;
}
8 changes: 8 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ class Config : public boost::noncopyable {
SetBlockPriorityPercentage(int64_t blockPriorityPercentage) = 0;
virtual uint8_t GetBlockPriorityPercentage() const = 0;
virtual const CChainParams &GetChainParams() const = 0;
virtual void SetCashAddrEncoding(bool) = 0;
virtual bool UseCashAddrEncoding() const = 0;
};

class GlobalConfig final : public Config {
public:
GlobalConfig();
bool SetMaxBlockSize(uint64_t maxBlockSize);
uint64_t GetMaxBlockSize() const;
bool SetBlockPriorityPercentage(int64_t blockPriorityPercentage);
uint8_t GetBlockPriorityPercentage() const;
const CChainParams &GetChainParams() const;
void SetCashAddrEncoding(bool) override;
bool UseCashAddrEncoding() const override;

private:
bool useCashAddr;
};

// Temporary woraround.
Expand Down
2 changes: 1 addition & 1 deletion src/core_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "core_io.h"

#include "base58.h"
#include "dstencode.h"
#include "primitives/transaction.h"
#include "script/script.h"
#include "script/standard.h"
Expand Down
41 changes: 41 additions & 0 deletions src/dstencode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2017 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "dstencode.h"
#include "base58.h"
#include "cashaddrenc.h"
#include "chainparams.h"
#include "config.h"
#include "script/standard.h"

std::string EncodeDestination(const CTxDestination &dst,
const CChainParams &params, const Config &cfg) {
return cfg.UseCashAddrEncoding() ? EncodeCashAddr(dst, params)
: EncodeLegacyAddr(dst, params);
}

CTxDestination DecodeDestination(const std::string &addr,
const CChainParams &params) {
CTxDestination dst = DecodeCashAddr(addr, params);
if (IsValidDestination(dst)) {
return dst;
}
return DecodeLegacyAddr(addr, params);
}

bool IsValidDestinationString(const std::string &addr,
const CChainParams &params) {
return IsValidDestination(DecodeDestination(addr, params));
}

std::string EncodeDestination(const CTxDestination &dst) {
return EncodeDestination(dst, Params(), GetConfig());
}

CTxDestination DecodeDestination(const std::string &addr) {
return DecodeDestination(addr, Params());
}

bool IsValidDestinationString(const std::string &addr) {
return IsValidDestinationString(addr, Params());
}
26 changes: 26 additions & 0 deletions src/dstencode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef BITCOIN_DSTENCODE_H
#define BITCOIN_DSTENCODE_H

// key.h and pubkey.h are not used here, but gcc doesn't want to instantiate
// CTxDestination if types are unknown
#include "key.h"
#include "pubkey.h"
#include "script/standard.h"
#include <string>

class Config;
class CChainParams;

std::string EncodeDestination(const CTxDestination &, const CChainParams &,
const Config &);
CTxDestination DecodeDestination(const std::string &addr, const CChainParams &);
bool IsValidDestinationString(const std::string &addr,
const CChainParams &params);

// Temporary workaround. Don't rely on global state, pass all parameters in new
// code.
std::string EncodeDestination(const CTxDestination &);
CTxDestination DecodeDestination(const std::string &addr);
bool IsValidDestinationString(const std::string &addr);

#endif // BITCOIN_DSTENCODE_H
1 change: 1 addition & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,7 @@ bool AppInitParameterInteraction(Config &config) {
}
}
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/qt/addresstablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "guiutil.h"
#include "walletmodel.h"

#include "base58.h"
#include "dstencode.h"
#include "wallet/wallet.h"

#include <QDebug>
Expand Down
2 changes: 1 addition & 1 deletion src/qt/bitcoinaddressvalidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "bitcoinaddressvalidator.h"

#include "base58.h"
#include "dstencode.h"

/* Base58 characters are:
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Expand Down
1 change: 1 addition & 0 deletions src/qt/coincontroldialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "txmempool.h"
#include "walletmodel.h"

#include "dstencode.h"
#include "init.h"
#include "policy/policy.h"
#include "validation.h" // For mempool
Expand Down
1 change: 1 addition & 0 deletions src/qt/guiutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "qvalidatedlineedit.h"
#include "walletmodel.h"

#include "dstencode.h"
#include "init.h"
#include "policy/policy.h"
#include "primitives/transaction.h"
Expand Down
2 changes: 1 addition & 1 deletion src/qt/paymentserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include "guiutil.h"
#include "optionsmodel.h"

#include "base58.h"
#include "chainparams.h"
#include "dstencode.h"
#include "policy/policy.h"
#include "ui_interface.h"
#include "util.h"
Expand Down
2 changes: 1 addition & 1 deletion src/qt/sendcoinsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include "sendcoinsentry.h"
#include "walletmodel.h"

#include "base58.h"
#include "chainparams.h"
#include "dstencode.h"
#include "txmempool.h"
#include "ui_interface.h"
#include "validation.h" // mempool and minRelayTxFee
Expand Down
2 changes: 1 addition & 1 deletion src/qt/signverifymessagedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "platformstyle.h"
#include "walletmodel.h"

#include "base58.h"
#include "dstencode.h"
#include "init.h"
#include "validation.h" // For strMessageMagic
#include "wallet/wallet.h"
Expand Down
2 changes: 1 addition & 1 deletion src/qt/transactiondesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include "paymentserver.h"
#include "transactionrecord.h"

#include "base58.h"
#include "consensus/consensus.h"
#include "dstencode.h"
#include "script/script.h"
#include "timedata.h"
#include "util.h"
Expand Down
2 changes: 1 addition & 1 deletion src/qt/transactionrecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include "transactionrecord.h"

#include "base58.h"
#include "consensus/consensus.h"
#include "dstencode.h"
#include "timedata.h"
#include "validation.h"
#include "wallet/finaltx.h"
Expand Down
2 changes: 1 addition & 1 deletion src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "recentrequeststablemodel.h"
#include "transactiontablemodel.h"

#include "base58.h"
#include "dstencode.h"
#include "keystore.h"
#include "net.h" // for g_connman
#include "sync.h"
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "amount.h"
#include "base58.h"
#include "chain.h"
#include "chainparams.h"
#include "config.h"
#include "consensus/consensus.h"
#include "consensus/params.h"
#include "consensus/validation.h"
#include "core_io.h"
#include "dstencode.h"
#include "init.h"
#include "miner.h"
#include "net.h"
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "rpc/misc.h"

#include "base58.h"
#include "clientversion.h"
#include "config.h"
#include "dstencode.h"
#include "init.h"
#include "net.h"
#include "netbase.h"
Expand Down
1 change: 1 addition & 0 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "config.h"
#include "consensus/validation.h"
#include "core_io.h"
#include "dstencode.h"
#include "init.h"
#include "keystore.h"
#include "merkleblock.h"
Expand Down
8 changes: 4 additions & 4 deletions src/test/base58_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_parse) {
"key mismatch:" + strTest);

// Private key must be invalid public key
destination = DecodeDestination(exp_base58string);
destination = DecodeLegacyAddr(exp_base58string, Params());
BOOST_CHECK_MESSAGE(!IsValidDestination(destination),
"IsValid privkey as pubkey:" + strTest);
} else {
// "script" or "pubkey"
std::string exp_addrType =
find_value(metadata, "addrType").get_str();
// Must be valid public key
destination = DecodeDestination(exp_base58string);
destination = DecodeLegacyAddr(exp_base58string, Params());
BOOST_CHECK_MESSAGE(IsValidDestination(destination),
"!IsValid:" + strTest);
BOOST_CHECK_MESSAGE((boost::get<CScriptID>(&destination) !=
Expand Down Expand Up @@ -236,7 +236,7 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_gen) {
BOOST_ERROR("Bad addrtype: " << strTest);
continue;
}
std::string address = EncodeDestination(dest);
std::string address = EncodeLegacyAddr(dest, Params());
BOOST_CHECK_MESSAGE(address == exp_base58string,
"mismatch: " + strTest);
}
Expand Down Expand Up @@ -267,7 +267,7 @@ BOOST_AUTO_TEST_CASE(base58_keys_invalid) {
std::string exp_base58string = test[0].get_str();

// must be invalid as public and as private key
destination = DecodeDestination(exp_base58string);
destination = DecodeLegacyAddr(exp_base58string, Params());
BOOST_CHECK_MESSAGE(!IsValidDestination(destination),
"IsValid pubkey:" + strTest);
secret.SetString(exp_base58string);
Expand Down
Loading

0 comments on commit 1a3a9d7

Please sign in to comment.