diff --git a/src/Makefile.am b/src/Makefile.am index 4108110ee6..6b2a77012e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,568 +1,570 @@ # Copyright (c) 2013-2016 The Bitcoin Core developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. DIST_SUBDIRS = secp256k1 univalue AM_LDFLAGS = $(PTHREAD_CFLAGS) $(LIBTOOL_LDFLAGS) $(HARDENED_LDFLAGS) AM_CXXFLAGS = $(HARDENED_CXXFLAGS) $(ERROR_CXXFLAGS) AM_CPPFLAGS = $(HARDENED_CPPFLAGS) EXTRA_LIBRARIES = if EMBEDDED_UNIVALUE LIBUNIVALUE = univalue/libunivalue.la $(LIBUNIVALUE): $(wildcard univalue/lib/*) $(wildcard univalue/include/*) $(AM_V_at)$(MAKE) $(AM_MAKEFLAGS) -C $(@D) $(@F) else LIBUNIVALUE = $(UNIVALUE_LIBS) endif BITCOIN_CONFIG_INCLUDES=-I$(builddir)/config BITCOIN_INCLUDES=-I$(builddir) -I$(builddir)/obj $(BDB_CPPFLAGS) $(BOOST_CPPFLAGS) $(LEVELDB_CPPFLAGS) $(CRYPTO_CFLAGS) $(SSL_CFLAGS) BITCOIN_INCLUDES += -I$(srcdir)/secp256k1/include BITCOIN_INCLUDES += $(UNIVALUE_CFLAGS) BITCOIN_SEEDER_INCLUDES = -I$(srcdir)/seeder BITCOIN_SEEDER_INCLUDES += $(BITCOIN_INCLUDES) LIBBITCOIN_SERVER=libbitcoin_server.a LIBBITCOIN_COMMON=libbitcoin_common.a LIBBITCOIN_CONSENSUS=libbitcoin_consensus.a LIBBITCOIN_CLI=libbitcoin_cli.a LIBBITCOIN_UTIL=libbitcoin_util.a LIBBITCOIN_CRYPTO=crypto/libbitcoin_crypto.a LIBBITCOINQT=qt/libbitcoinqt.a LIBSECP256K1=secp256k1/libsecp256k1.la if ENABLE_ZMQ LIBBITCOIN_ZMQ=libbitcoin_zmq.a endif if BUILD_BITCOIN_LIBS LIBBITCOINCONSENSUS=libbitcoinconsensus.la endif if BUILD_BITCOIN_SEEDER LIBBITCOIN_SEEDER=libbitcoin_seeder.a endif if ENABLE_WALLET LIBBITCOIN_WALLET=libbitcoin_wallet.a endif $(LIBSECP256K1): $(wildcard secp256k1/src/*) $(wildcard secp256k1/include/*) $(AM_V_at)$(MAKE) $(AM_MAKEFLAGS) -C $(@D) $(@F) # Make is not made aware of per-object dependencies to avoid limiting building parallelization # But to build the less dependent modules first, we manually select their order here: EXTRA_LIBRARIES += \ $(LIBBITCOIN_CRYPTO) \ $(LIBBITCOIN_UTIL) \ $(LIBBITCOIN_COMMON) \ $(LIBBITCOIN_CONSENSUS) \ $(LIBBITCOIN_SERVER) \ $(LIBBITCOIN_CLI) \ $(LIBBITCOIN_SEEDER) \ $(LIBBITCOIN_WALLET) \ $(LIBBITCOIN_ZMQ) lib_LTLIBRARIES = $(LIBBITCOINCONSENSUS) bin_PROGRAMS = noinst_PROGRAMS = TESTS = BENCHMARKS = if BUILD_BITCOIND bin_PROGRAMS += bitcoind endif if BUILD_BITCOIN_SEEDER bin_PROGRAMS += bitcoin-seeder endif if BUILD_BITCOIN_UTILS bin_PROGRAMS += bitcoin-cli bitcoin-tx endif .PHONY: FORCE check-symbols check-security # bitcoin core # BITCOIN_CORE_H = \ addrdb.h \ addrman.h \ base58.h \ bloom.h \ blockencodings.h \ cashaddr.h \ + cashaddrenc.h \ chain.h \ chainparams.h \ chainparamsbase.h \ chainparamsseeds.h \ checkpoints.h \ checkqueue.h \ clientversion.h \ coins.h \ compat.h \ compat/byteswap.h \ compat/endian.h \ compat/sanity.h \ compressor.h \ config.h \ consensus/consensus.h \ core_io.h \ core_memusage.h \ cuckoocache.h \ globals.h \ httprpc.h \ httpserver.h \ indirectmap.h \ init.h \ key.h \ keystore.h \ dbwrapper.h \ limitedmap.h \ memusage.h \ merkleblock.h \ miner.h \ net.h \ net_processing.h \ netaddress.h \ netbase.h \ netmessagemaker.h \ noui.h \ policy/fees.h \ policy/policy.h \ pow.h \ protocol.h \ random.h \ reverselock.h \ rpc/blockchain.h \ rpc/client.h \ rpc/misc.h \ rpc/protocol.h \ rpc/server.h \ rpc/register.h \ scheduler.h \ script/scriptcache.h \ script/sigcache.h \ script/sign.h \ script/standard.h \ script/ismine.h \ streams.h \ support/allocators/secure.h \ support/allocators/zeroafterfree.h \ support/cleanse.h \ support/events.h \ support/lockedpool.h \ sync.h \ threadsafety.h \ threadinterrupt.h \ timedata.h \ torcontrol.h \ txdb.h \ txmempool.h \ ui_interface.h \ undo.h \ util.h \ utilmoneystr.h \ utiltime.h \ validation.h \ validationinterface.h \ versionbits.h \ wallet/coincontrol.h \ wallet/crypter.h \ wallet/db.h \ wallet/finaltx.h \ wallet/rpcdump.h \ wallet/rpcwallet.h \ wallet/wallet.h \ wallet/walletdb.h \ warnings.h \ zmq/zmqabstractnotifier.h \ zmq/zmqconfig.h\ zmq/zmqnotificationinterface.h \ zmq/zmqpublishnotifier.h obj/build.h: FORCE @$(MKDIR_P) $(builddir)/obj @$(top_srcdir)/share/genbuild.sh $(abs_top_builddir)/src/obj/build.h \ $(abs_top_srcdir) libbitcoin_util_a-clientversion.$(OBJEXT): obj/build.h # server: shared between bitcoind and bitcoin-qt libbitcoin_server_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS) libbitcoin_server_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_server_a_SOURCES = \ addrman.cpp \ addrdb.cpp \ bloom.cpp \ blockencodings.cpp \ chain.cpp \ checkpoints.cpp \ config.cpp \ globals.cpp \ httprpc.cpp \ httpserver.cpp \ init.cpp \ dbwrapper.cpp \ merkleblock.cpp \ miner.cpp \ net.cpp \ net_processing.cpp \ noui.cpp \ policy/fees.cpp \ policy/policy.cpp \ pow.cpp \ rest.cpp \ rpc/abc.cpp \ rpc/blockchain.cpp \ rpc/mining.cpp \ rpc/misc.cpp \ rpc/net.cpp \ rpc/rawtransaction.cpp \ rpc/server.cpp \ script/scriptcache.cpp \ script/sigcache.cpp \ script/ismine.cpp \ timedata.cpp \ torcontrol.cpp \ txdb.cpp \ txmempool.cpp \ ui_interface.cpp \ validation.cpp \ validationinterface.cpp \ versionbits.cpp \ $(BITCOIN_CORE_H) if ENABLE_ZMQ libbitcoin_zmq_a_CPPFLAGS = $(BITCOIN_INCLUDES) $(ZMQ_CFLAGS) libbitcoin_zmq_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_zmq_a_SOURCES = \ zmq/zmqabstractnotifier.cpp \ zmq/zmqnotificationinterface.cpp \ zmq/zmqpublishnotifier.cpp endif # wallet: shared between bitcoind and bitcoin-qt, but only linked # when wallet enabled libbitcoin_wallet_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) libbitcoin_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_wallet_a_SOURCES = \ wallet/crypter.cpp \ wallet/db.cpp \ wallet/finaltx.cpp \ wallet/rpcdump.cpp \ wallet/rpcwallet.cpp \ wallet/wallet.cpp \ wallet/walletdb.cpp \ $(BITCOIN_CORE_H) # crypto primitives library crypto_libbitcoin_crypto_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_CONFIG_INCLUDES) crypto_libbitcoin_crypto_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) crypto_libbitcoin_crypto_a_SOURCES = \ crypto/aes.cpp \ crypto/aes.h \ crypto/chacha20.h \ crypto/chacha20.cpp \ crypto/common.h \ crypto/hmac_sha256.cpp \ crypto/hmac_sha256.h \ crypto/hmac_sha512.cpp \ crypto/hmac_sha512.h \ crypto/ripemd160.cpp \ crypto/ripemd160.h \ crypto/sha1.cpp \ crypto/sha1.h \ crypto/sha256.cpp \ crypto/sha256.h \ crypto/sha512.cpp \ crypto/sha512.h # consensus: shared between all executables that validate any consensus rules. libbitcoin_consensus_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) libbitcoin_consensus_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_consensus_a_SOURCES = \ amount.h \ arith_uint256.cpp \ arith_uint256.h \ consensus/merkle.cpp \ consensus/merkle.h \ consensus/params.h \ consensus/validation.h \ hash.cpp \ hash.h \ prevector.h \ primitives/block.cpp \ primitives/block.h \ primitives/transaction.cpp \ primitives/transaction.h \ pubkey.cpp \ pubkey.h \ script/bitcoinconsensus.cpp \ script/interpreter.cpp \ script/interpreter.h \ script/script.cpp \ script/script.h \ script/script_error.cpp \ script/script_error.h \ serialize.h \ tinyformat.h \ uint256.cpp \ uint256.h \ utilstrencodings.cpp \ utilstrencodings.h \ version.h # common: shared between bitcoind, and bitcoin-qt and non-server tools libbitcoin_common_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) libbitcoin_common_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_common_a_SOURCES = \ amount.cpp \ base58.cpp \ cashaddr.cpp \ + cashaddrenc.cpp \ chainparams.cpp \ coins.cpp \ compressor.cpp \ core_read.cpp \ core_write.cpp \ key.cpp \ keystore.cpp \ netaddress.cpp \ netbase.cpp \ protocol.cpp \ scheduler.cpp \ script/sign.cpp \ script/standard.cpp \ warnings.cpp \ $(BITCOIN_CORE_H) # util: shared between all executables. # This library *must* be included to make sure that the glibc # backward-compatibility objects and their sanity checks are linked. libbitcoin_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) libbitcoin_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_util_a_SOURCES = \ support/lockedpool.cpp \ chainparamsbase.cpp \ clientversion.cpp \ compat/glibc_sanity.cpp \ compat/glibcxx_sanity.cpp \ compat/strnlen.cpp \ random.cpp \ rpc/protocol.cpp \ support/cleanse.cpp \ sync.cpp \ threadinterrupt.cpp \ util.cpp \ utilmoneystr.cpp \ utilstrencodings.cpp \ utiltime.cpp \ $(BITCOIN_CORE_H) if GLIBC_BACK_COMPAT libbitcoin_util_a_SOURCES += compat/glibc_compat.cpp endif # cli: shared between bitcoin-cli and bitcoin-qt libbitcoin_cli_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) libbitcoin_cli_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_cli_a_SOURCES = \ rpc/client.cpp \ $(BITCOIN_CORE_H) # seeder library libbitcoin_seeder_a_CPPFLAGS = $(AM_CPPFLAGS) $(PIE_FLAGS) $(BITCOIN_SEEDER_INCLUDES) libbitcoin_seeder_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_seeder_a_SOURCES = \ seeder/bitcoin.cpp \ seeder/bitcoin.h \ seeder/compat.h \ seeder/db.cpp \ seeder/db.h \ seeder/dns.c \ seeder/dns.h \ seeder/netbase.cpp \ seeder/netbase.h \ seeder/protocol.cpp \ seeder/protocol.h \ seeder/strlcpy.h \ seeder/util.cpp \ seeder/util.h nodist_libbitcoin_util_a_SOURCES = $(srcdir)/obj/build.h # # bitcoind binary # bitcoind_SOURCES = bitcoind.cpp bitcoind_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) bitcoind_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) bitcoind_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) if TARGET_WINDOWS bitcoind_SOURCES += bitcoind-res.rc endif bitcoind_LDADD = \ $(LIBBITCOIN_SERVER) \ $(LIBBITCOIN_COMMON) \ $(LIBUNIVALUE) \ $(LIBBITCOIN_UTIL) \ $(LIBBITCOIN_WALLET) \ $(LIBBITCOIN_ZMQ) \ $(LIBBITCOIN_CONSENSUS) \ $(LIBBITCOIN_CRYPTO) \ $(LIBLEVELDB) \ $(LIBLEVELDB_SSE42) \ $(LIBMEMENV) \ $(LIBSECP256K1) bitcoind_LDADD += $(BOOST_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(ZMQ_LIBS) # bitcoin-cli binary # bitcoin_cli_SOURCES = bitcoin-cli.cpp bitcoin_cli_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(EVENT_CFLAGS) bitcoin_cli_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) bitcoin_cli_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) if TARGET_WINDOWS bitcoin_cli_SOURCES += bitcoin-cli-res.rc endif bitcoin_cli_LDADD = \ $(LIBBITCOIN_CLI) \ $(LIBUNIVALUE) \ $(LIBBITCOIN_UTIL) \ $(LIBBITCOIN_CRYPTO) bitcoin_cli_LDADD += $(BOOST_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(EVENT_LIBS) # # bitcoin-seeder binary # bitcoin_seeder_SOURCES = seeder/main.cpp bitcoin_seeder_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_SEEDER_INCLUDES) bitcoin_seeder_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) bitcoin_seeder_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) bitcoin_seeder_LDADD = \ $(LIBBITCOIN_SEEDER) \ $(LIBBITCOIN_UTIL) \ $(LIBBITCOIN_CRYPTO) bitcoin_seeder_LDADD += $(BOOST_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(ZMQ_LIBS) # # bitcoin-tx binary # bitcoin_tx_SOURCES = bitcoin-tx.cpp bitcoin_tx_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) bitcoin_tx_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) bitcoin_tx_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) if TARGET_WINDOWS bitcoin_tx_SOURCES += bitcoin-tx-res.rc endif bitcoin_tx_LDADD = \ $(LIBUNIVALUE) \ $(LIBBITCOIN_COMMON) \ $(LIBBITCOIN_UTIL) \ $(LIBBITCOIN_CONSENSUS) \ $(LIBBITCOIN_CRYPTO) \ $(LIBSECP256K1) bitcoin_tx_LDADD += $(BOOST_LIBS) $(CRYPTO_LIBS) # # bitcoinconsensus library # if BUILD_BITCOIN_LIBS include_HEADERS = script/bitcoinconsensus.h libbitcoinconsensus_la_SOURCES = $(crypto_libbitcoin_crypto_a_SOURCES) $(libbitcoin_consensus_a_SOURCES) if GLIBC_BACK_COMPAT libbitcoinconsensus_la_SOURCES += compat/glibc_compat.cpp endif libbitcoinconsensus_la_LDFLAGS = $(AM_LDFLAGS) -no-undefined $(RELDFLAGS) libbitcoinconsensus_la_LIBADD = $(LIBSECP256K1) libbitcoinconsensus_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include -DBUILD_BITCOIN_INTERNAL libbitcoinconsensus_la_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) endif # CTAES_DIST = crypto/ctaes/bench.c CTAES_DIST += crypto/ctaes/ctaes.c CTAES_DIST += crypto/ctaes/ctaes.h CTAES_DIST += crypto/ctaes/README.md CTAES_DIST += crypto/ctaes/test.c CLEANFILES = $(EXTRA_LIBRARIES) CLEANFILES += *.gcda *.gcno CLEANFILES += compat/*.gcda compat/*.gcno CLEANFILES += consensus/*.gcda consensus/*.gcno CLEANFILES += crypto/*.gcda crypto/*.gcno CLEANFILES += policy/*.gcda policy/*.gcno CLEANFILES += primitives/*.gcda primitives/*.gcno CLEANFILES += script/*.gcda script/*.gcno CLEANFILES += support/*.gcda support/*.gcno CLEANFILES += univalue/*.gcda univalue/*.gcno CLEANFILES += wallet/*.gcda wallet/*.gcno CLEANFILES += wallet/test/*.gcda wallet/test/*.gcno CLEANFILES += zmq/*.gcda zmq/*.gcno DISTCLEANFILES = obj/build.h EXTRA_DIST = $(CTAES_DIST) clean-local: -$(MAKE) -C secp256k1 clean -$(MAKE) -C univalue clean -rm -f leveldb/*/*.gcda leveldb/*/*.gcno leveldb/helpers/memenv/*.gcda leveldb/helpers/memenv/*.gcno -rm -rf test/__pycache__ .rc.o: @test -f $(WINDRES) ## FIXME: How to get the appropriate modulename_CPPFLAGS in here? $(AM_V_GEN) $(WINDRES) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(CPPFLAGS) -DWINDRES_PREPROC -i $< -o $@ .mm.o: $(AM_V_CXX) $(OBJCXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ $(CPPFLAGS) $(AM_CXXFLAGS) $(QT_INCLUDES) $(AM_CXXFLAGS) $(PIE_FLAGS) $(CXXFLAGS) -c -o $@ $< check-symbols: $(bin_PROGRAMS) if GLIBC_BACK_COMPAT @echo "Checking glibc back compat..." $(AM_V_at) READELF=$(READELF) CPPFILT=$(CPPFILT) $(top_srcdir)/contrib/devtools/symbol-check.py < $(bin_PROGRAMS) endif check-security: $(bin_PROGRAMS) if HARDEN @echo "Checking binary security..." $(AM_V_at) READELF=$(READELF) OBJDUMP=$(OBJDUMP) $(top_srcdir)/contrib/devtools/security-check.py < $(bin_PROGRAMS) endif %.pb.cc %.pb.h: %.proto @test -f $(PROTOC) $(AM_V_GEN) $(PROTOC) --cpp_out=$(@D) --proto_path=$( "$@.new" && mv -f "$@.new" "$@" @echo "Generated $@" diff --git a/src/cashaddrenc.cpp b/src/cashaddrenc.cpp new file mode 100644 index 0000000000..85d6fe66d1 --- /dev/null +++ b/src/cashaddrenc.cpp @@ -0,0 +1,128 @@ +// 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 "cashaddrenc.h" +#include "cashaddr.h" +#include "chainparams.h" +#include "pubkey.h" +#include "script/script.h" +#include "utilstrencodings.h" +#include +#include +#include + +const uint8_t CASHADDR_VERSION_PUBKEY = 0; +const uint8_t CASHADDR_VERISON_SCRIPT = 8; + +// Size of data-part in a pubkey/script cash address. +// Consists of: 8 bits version + 160 bits hash. +const size_t CASHADDR_GROUPED_SIZE = 34; /* 5 bit representation */ +const size_t CASHADDR_BYTES = 21; /* 8 bit representation */ + +namespace { + +// Implements encoding of CTxDestination using cashaddr. +class CashAddrEncoder : public boost::static_visitor { +public: + CashAddrEncoder(const CChainParams &p); + + std::string operator()(const CKeyID &id) const; + std::string operator()(const CScriptID &id) const; + std::string operator()(const CNoDestination &) const; + +private: + const CChainParams ¶ms; +}; + +// Convert the data part to a 5 bit representation. +template +std::vector PackAddrData(const T &id, uint8_t version, + size_t expectedSize) { + std::vector data = {version}; + data.insert(data.end(), id.begin(), id.end()); + + const std::string errstr = "Error packing cashaddr"; + + std::vector converted; + if (!ConvertBits<8, 5, true>(converted, begin(data), end(data))) { + throw std::runtime_error(errstr); + } + + if (converted.size() != expectedSize) { + throw std::runtime_error(errstr); + } + + return converted; +} + +CashAddrEncoder::CashAddrEncoder(const CChainParams &p) : params(p) {} + +std::string CashAddrEncoder::operator()(const CKeyID &id) const { + std::vector data = + PackAddrData(id, CASHADDR_VERSION_PUBKEY, CASHADDR_GROUPED_SIZE); + return cashaddr::Encode(params.CashAddrPrefix(), data); +} + +std::string CashAddrEncoder::operator()(const CScriptID &id) const { + std::vector data = + PackAddrData(id, CASHADDR_VERISON_SCRIPT, CASHADDR_GROUPED_SIZE); + return cashaddr::Encode(params.CashAddrPrefix(), data); +} + +std::string CashAddrEncoder::operator()(const CNoDestination &) const { + return ""; +} + +} // anon ns + +std::string EncodeCashAddr(const CTxDestination &dst, + const CChainParams ¶ms) { + return boost::apply_visitor(CashAddrEncoder(params), dst); +} + +CTxDestination DecodeCashAddr(const std::string &addrstr, + const CChainParams ¶ms) { + std::pair> cashaddr = + cashaddr::Decode(addrstr); + + if (cashaddr.first != params.CashAddrPrefix()) { + return CNoDestination{}; + } + + if (cashaddr.second.empty()) { + return CNoDestination{}; + } + + std::vector data; + if (!ConvertBits<5, 8, true>(data, begin(cashaddr.second), + end(cashaddr.second))) { + return CNoDestination{}; + } + + // Both encoding and decoding add padding, so it's double padded. + // Truncate the double padding. + if (data.back() != 0) { + // Not padded, should be. + return CNoDestination{}; + } + data.pop_back(); + + // Check that we decoded the exact number of bytes we expected. + if (data.size() != CASHADDR_BYTES) { + return CNoDestination{}; + } + + uint160 hash; + std::copy(begin(data) + 1, end(data), hash.begin()); + + uint8_t version = data.at(0); + if (version == CASHADDR_VERSION_PUBKEY) { + return CKeyID(hash); + } + if (version == CASHADDR_VERISON_SCRIPT) { + return CScriptID(hash); + } + + // unknown version + return CNoDestination{}; +} diff --git a/src/cashaddrenc.h b/src/cashaddrenc.h new file mode 100644 index 0000000000..397359978f --- /dev/null +++ b/src/cashaddrenc.h @@ -0,0 +1,17 @@ +// 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. +#ifndef BITCOIN_CASHADDRENC_H +#define BITCOIN_CASHADDRENC_H + +#include "script/standard.h" +#include + +class CChainParams; + +CTxDestination DecodeCashAddr(const std::string &addr, + const CChainParams ¶ms); + +std::string EncodeCashAddr(const CTxDestination &, const CChainParams &); + +#endif diff --git a/src/test/cashaddrenc_tests.cpp b/src/test/cashaddrenc_tests.cpp new file mode 100644 index 0000000000..f89852dad2 --- /dev/null +++ b/src/test/cashaddrenc_tests.cpp @@ -0,0 +1,171 @@ +// 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 "cashaddr.h" +#include "cashaddrenc.h" +#include "chainparams.h" +#include "random.h" +#include "test/test_bitcoin.h" +#include "uint256.h" +#include + +namespace { + +std::vector GetNetworks() { + return {CBaseChainParams::MAIN, CBaseChainParams::TESTNET, + CBaseChainParams::REGTEST}; +} + +uint160 insecure_GetRandUInt160(FastRandomContext &rand) { + uint160 n; + for (uint8_t *c = n.begin(); c != n.end(); ++c) { + *c = static_cast(rand.rand32()); + } + return n; +} + +class DstTypeChecker : public boost::static_visitor { +public: + void operator()(const CKeyID &id) { isKey = true; } + void operator()(const CScriptID &id) { isScript = true; } + void operator()(const CNoDestination &) {} + + static bool IsScriptDst(const CTxDestination &d) { + DstTypeChecker checker; + boost::apply_visitor(checker, d); + return checker.isScript; + } + + static bool IsKeyDst(const CTxDestination &d) { + DstTypeChecker checker; + boost::apply_visitor(checker, d); + return checker.isKey; + } + +private: + DstTypeChecker() : isKey(false), isScript(false) {} + bool isKey; + bool isScript; +}; + +} // anon ns + +BOOST_FIXTURE_TEST_SUITE(cashaddrenc_tests, BasicTestingSetup) + +BOOST_AUTO_TEST_CASE(encode_decode) { + std::vector toTest = {CNoDestination{}, + CKeyID(uint160S("badf00d")), + CScriptID(uint160S("f00dbad"))}; + + for (auto dst : toTest) { + for (auto net : GetNetworks()) { + std::string encoded = EncodeCashAddr(dst, Params(net)); + CTxDestination decoded = DecodeCashAddr(encoded, Params(net)); + BOOST_CHECK(dst == decoded); + } + } +} + +// Check that an encoded cash address is not valid on another network. +BOOST_AUTO_TEST_CASE(invalid_on_wrong_network) { + + const CTxDestination dst = CKeyID(uint160S("c0ffee")); + const CTxDestination invalidDst = CNoDestination{}; + + for (auto net : GetNetworks()) { + for (auto otherNet : GetNetworks()) { + if (net == otherNet) continue; + + std::string encoded = EncodeCashAddr(dst, Params(net)); + CTxDestination decoded = DecodeCashAddr(encoded, Params(otherNet)); + BOOST_CHECK(decoded != dst); + BOOST_CHECK(decoded == invalidDst); + } + } +} + +BOOST_AUTO_TEST_CASE(random_dst) { + FastRandomContext rand(true); + + const size_t NUM_TESTS = 5000; + const CChainParams ¶ms = Params(CBaseChainParams::MAIN); + + for (size_t i = 0; i < NUM_TESTS; ++i) { + uint160 hash = insecure_GetRandUInt160(rand); + const CTxDestination dst_key = CKeyID(hash); + const CTxDestination dst_scr = CScriptID(hash); + + const std::string encoded_key = EncodeCashAddr(dst_key, params); + const CTxDestination decoded_key = DecodeCashAddr(encoded_key, params); + + const std::string encoded_scr = EncodeCashAddr(dst_scr, params); + const CTxDestination decoded_scr = DecodeCashAddr(encoded_scr, params); + + std::string err("cashaddr failed for hash: "); + err += hash.ToString(); + + BOOST_CHECK_MESSAGE(dst_key == decoded_key, err); + BOOST_CHECK_MESSAGE(dst_scr == decoded_scr, err); + + BOOST_CHECK_MESSAGE(DstTypeChecker::IsKeyDst(decoded_key), err); + BOOST_CHECK_MESSAGE(DstTypeChecker::IsScriptDst(decoded_scr), err); + } +} + +// Test that a invalid, specially crafted cashaddr stays invalid when truncated. +BOOST_AUTO_TEST_CASE(invalid_when_truncated) { + + // Cashaddr payload is 34 5-bit nibbles. The last one is padded. When + // converting back to bytes, there is additional padding. + // + // This extra padding truncated. But we should make sure that what we + // truncate is padding and not data. + // + // This test creates a invalid address that leaves data in the byte that + // should be padding. + uint8_t version = 0; + std::vector data = {version}; + for (size_t i = 0; i < 33; ++i) + data.push_back(1); + assert(data.size() == 34); + + const CChainParams params = Params(CBaseChainParams::MAIN); + + std::string fake = cashaddr::Encode(params.CashAddrPrefix(), data); + CTxDestination nodst = CNoDestination{}; + BOOST_CHECK(nodst == DecodeCashAddr(fake, params)); +} + +BOOST_AUTO_TEST_CASE(test_addresses) { + const CChainParams params = Params(CBaseChainParams::MAIN); + + std::vector> hash{ + {118, 160, 64, 83, 189, 160, 168, 139, 218, 81, + 119, 184, 106, 21, 195, 178, 159, 85, 152, 115}, + {203, 72, 18, 50, 41, 156, 213, 116, 49, 81, + 172, 75, 45, 99, 174, 25, 142, 123, 176, 169}, + {1, 31, 40, 228, 115, 201, 95, 64, 19, 215, + 213, 62, 197, 251, 195, 180, 45, 248, 237, 16}}; + + std::vector pubkey = { + "bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a", + "bitcoincash:qr95sy3j9xwd2ap32xkykttr4cvcu7as4y0qverfuy", + "bitcoincash:qqq3728yw0y47sqn6l2na30mcw6zm78dzqre909m2r"}; + std::vector script = { + "bitcoincash:ppm2qsznhks23z7629mms6s4cwef74vcwvn0h829pq", + "bitcoincash:pr95sy3j9xwd2ap32xkykttr4cvcu7as4yc93ky28e", + "bitcoincash:pqq3728yw0y47sqn6l2na30mcw6zm78dzq5ucqzc37"}; + + for (size_t i = 0; i < hash.size(); ++i) { + const CTxDestination dstKey = CKeyID(uint160(hash[i])); + const CTxDestination dstScript = CScriptID(uint160(hash[i])); + + BOOST_CHECK_EQUAL(pubkey[i], + EncodeCashAddr(CKeyID(uint160(hash[i])), params)); + BOOST_CHECK_EQUAL(script[i], + EncodeCashAddr(CScriptID(uint160(hash[i])), params)); + } +} + +BOOST_AUTO_TEST_SUITE_END()