Skip to content

Commit

Permalink
Better error messages for invalid addresses
Browse files Browse the repository at this point in the history
This commit addresses bitcoin#20809.

We add an additional 'error' property in the result of 'validateaddress' in case the address is not valid that gives a short description of why the address in invalid. We also change the error message returned by 'getaddressinfo' in case the address is invalid.

Github-Pull: bitcoin#20832
Rebased-From: 8f0b64f
  • Loading branch information
Bezdrighin authored and luke-jr committed Jun 26, 2021
1 parent 194b9b8 commit 333927f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 25 deletions.
46 changes: 41 additions & 5 deletions src/key_io.cpp
Expand Up @@ -15,6 +15,9 @@
#include <string.h>
#include <algorithm>

/// Maximum witness length for Bech32 addresses.
static constexpr std::size_t BECH32_WITNESS_PROG_MAX_LEN = 40;

namespace
{
class DestinationEncoder : public boost::static_visitor<std::string>
Expand Down Expand Up @@ -69,10 +72,11 @@ class DestinationEncoder : public boost::static_visitor<std::string>
std::string operator()(const CNoDestination& no) const { return {}; }
};

CTxDestination DecodeDestination(const std::string& str, const CChainParams& params)
CTxDestination DecodeDestination(const std::string& str, const CChainParams& params, std::string& error_str)
{
std::vector<unsigned char> data;
uint160 hash;
error_str = "";
if (DecodeBase58Check(str, data, 21)) {
// base58-encoded Bitcoin addresses.
// Public-key-hash-addresses have version 0 (or 111 testnet).
Expand All @@ -89,10 +93,21 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin());
return ScriptHash(hash);
}

// Set potential error message.
// This message may be changed if the address can also be interpreted as a Bech32 address.
error_str = "Invalid prefix for Base58-encoded address";
}
data.clear();
const auto dec = bech32::Decode(str);
if ((dec.encoding == bech32::Encoding::BECH32 || dec.encoding == bech32::Encoding::BECH32M) && dec.data.size() > 0 && dec.hrp == params.Bech32HRP()) {
if ((dec.encoding == bech32::Encoding::BECH32 || dec.encoding == bech32::Encoding::BECH32M) && dec.data.size() > 0) {
error_str = "";

if (dec.hrp != params.Bech32HRP()) {
error_str = "Invalid prefix for Bech32 address";
return CNoDestination();
}

// Bech32 decoding
int version = dec.data[0]; // The first 5 bit symbol is the witness version (0-16)
if (version == 0 && dec.encoding != bech32::Encoding::BECH32) {
Expand All @@ -119,18 +134,32 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
return scriptid;
}
}

error_str = "Invalid Bech32 v0 address data size";
return CNoDestination();
}
if (version > 16 || data.size() < 2 || data.size() > 40) {

if (version > 16) {
error_str = "Invalid Bech32 address witness version";
return CNoDestination();
}

if (data.size() < 2 || data.size() > BECH32_WITNESS_PROG_MAX_LEN) {
error_str = "Invalid Bech32 address data size";
return CNoDestination();
}

WitnessUnknown unk;
unk.version = version;
std::copy(data.begin(), data.end(), unk.program);
unk.length = data.size();
return unk;
}
}

// Set error message if address can't be interpreted as Base58 or Bech32.
if (error_str.empty()) error_str = "Invalid address format";

return CNoDestination();
}
} // namespace
Expand Down Expand Up @@ -218,14 +247,21 @@ std::string EncodeDestination(const CTxDestination& dest)
return boost::apply_visitor(DestinationEncoder(Params()), dest);
}

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

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

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

bool IsValidDestinationString(const std::string& str)
Expand Down
1 change: 1 addition & 0 deletions src/key_io.h
Expand Up @@ -23,6 +23,7 @@ std::string EncodeExtPubKey(const CExtPubKey& extpubkey);

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

Expand Down
15 changes: 10 additions & 5 deletions src/rpc/misc.cpp
Expand Up @@ -39,13 +39,14 @@ static RPCHelpMan validateaddress()
RPCResult{
RPCResult::Type::OBJ, "", "",
{
{RPCResult::Type::BOOL, "isvalid", "If the address is valid or not. If not, this is the only property returned."},
{RPCResult::Type::BOOL, "isvalid", "If the address is valid or not"},
{RPCResult::Type::STR, "address", "The bitcoin address validated"},
{RPCResult::Type::STR_HEX, "scriptPubKey", "The hex-encoded scriptPubKey generated by the address"},
{RPCResult::Type::BOOL, "isscript", "If the key is a script"},
{RPCResult::Type::BOOL, "iswitness", "If the address is a witness address"},
{RPCResult::Type::NUM, "witness_version", /* optional */ true, "The version number of the witness program"},
{RPCResult::Type::STR_HEX, "witness_program", /* optional */ true, "The hex value of the witness program"},
{RPCResult::Type::STR, "error", /* optional */ true, "Error message, if any"},
}
},
RPCExamples{
Expand All @@ -54,13 +55,14 @@ static RPCHelpMan validateaddress()
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
CTxDestination dest = DecodeDestination(request.params[0].get_str());
bool isValid = IsValidDestination(dest);
std::string error_msg;
CTxDestination dest = DecodeDestination(request.params[0].get_str(), error_msg);
const bool isValid = IsValidDestination(dest);
CHECK_NONFATAL(isValid == error_msg.empty());

UniValue ret(UniValue::VOBJ);
ret.pushKV("isvalid", isValid);
if (isValid)
{
if (isValid) {
std::string currentAddress = EncodeDestination(dest);
ret.pushKV("address", currentAddress);

Expand All @@ -69,7 +71,10 @@ static RPCHelpMan validateaddress()

UniValue detail = DescribeAddress(dest);
ret.pushKVs(detail);
} else {
ret.pushKV("error", error_msg);
}

return ret;
},
};
Expand Down
12 changes: 9 additions & 3 deletions src/wallet/rpcwallet.cpp
Expand Up @@ -3824,13 +3824,19 @@ RPCHelpMan getaddressinfo()

LOCK(pwallet->cs_wallet);

UniValue ret(UniValue::VOBJ);
CTxDestination dest = DecodeDestination(request.params[0].get_str());
std::string error_msg;
CTxDestination dest = DecodeDestination(request.params[0].get_str(), error_msg);

// Make sure the destination is valid
if (!IsValidDestination(dest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
// Set generic error message in case 'DecodeDestination' didn't set it
if (error_msg.empty()) error_msg = "Invalid address";

throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error_msg);
}

UniValue ret(UniValue::VOBJ);

std::string currentAddress = EncodeDestination(dest);
ret.pushKV("address", currentAddress);

Expand Down
24 changes: 13 additions & 11 deletions test/functional/rpc_invalid_address_message.py
Expand Up @@ -6,7 +6,10 @@

from test_framework.test_framework import BitcoinTestFramework

from test_framework.util import assert_raises_rpc_error
from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
)

BECH32_VALID = 'bcrt1qtmp74ayg7p24uslctssvjm06q5phz4yrxucgnv'
BECH32_INVALID_BECH32 = 'bcrt1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqdmchcc'
Expand Down Expand Up @@ -35,18 +38,15 @@ def test_validateaddress(self):
# Bech32
info = node.validateaddress(BECH32_INVALID_SIZE)
assert not info['isvalid']
assert_equal(info['error'], 'Invalid Bech32 address data size')

info = node.validateaddress(BECH32_INVALID_PREFIX)
assert not info['isvalid']

info = node.validateaddress(BECH32_INVALID_BECH32)
assert not info['isvalid']

info = node.validateaddress(BECH32_INVALID_BECH32M)
assert not info['isvalid']
assert_equal(info['error'], 'Invalid prefix for Bech32 address')

info = node.validateaddress(BECH32_INVALID_V0_SIZE)
assert not info['isvalid']
assert_equal(info['error'], 'Invalid Bech32 v0 address data size')

info = node.validateaddress(BECH32_VALID)
assert info['isvalid']
Expand All @@ -55,6 +55,7 @@ def test_validateaddress(self):
# Base58
info = node.validateaddress(BASE58_INVALID_PREFIX)
assert not info['isvalid']
assert_equal(info['error'], 'Invalid prefix for Base58-encoded address')

info = node.validateaddress(BASE58_VALID)
assert info['isvalid']
Expand All @@ -63,17 +64,18 @@ def test_validateaddress(self):
# Invalid address format
info = node.validateaddress(INVALID_ADDRESS)
assert not info['isvalid']
assert_equal(info['error'], 'Invalid address format')

def test_getaddressinfo(self):
node = self.nodes[0]

assert_raises_rpc_error(-5, "Invalid address", node.getaddressinfo, BECH32_INVALID_SIZE)
assert_raises_rpc_error(-5, "Invalid Bech32 address data size", node.getaddressinfo, BECH32_INVALID_SIZE)

assert_raises_rpc_error(-5, "Invalid address", node.getaddressinfo, BECH32_INVALID_PREFIX)
assert_raises_rpc_error(-5, "Invalid prefix for Bech32 address", node.getaddressinfo, BECH32_INVALID_PREFIX)

assert_raises_rpc_error(-5, "Invalid address", node.getaddressinfo, BASE58_INVALID_PREFIX)
assert_raises_rpc_error(-5, "Invalid prefix for Base58-encoded address", node.getaddressinfo, BASE58_INVALID_PREFIX)

assert_raises_rpc_error(-5, "Invalid address", node.getaddressinfo, INVALID_ADDRESS)
assert_raises_rpc_error(-5, "Invalid address format", node.getaddressinfo, INVALID_ADDRESS)

def run_test(self):
self.test_validateaddress()
Expand Down
1 change: 1 addition & 0 deletions test/functional/test_runner.py
Expand Up @@ -134,6 +134,7 @@
'wallet_keypool_topup.py --descriptors',
'feature_fee_estimation.py',
'interface_zmq.py',
'rpc_invalid_address_message.py',
'interface_bitcoin_cli.py',
'mempool_resurrect.py',
'wallet_txn_doublespend.py --mineblock',
Expand Down
2 changes: 1 addition & 1 deletion test/functional/wallet_basic.py
Expand Up @@ -586,7 +586,7 @@ def run_test(self):
assert_equal(total_txs, len(self.nodes[0].listtransactions("*", 99999)))

# Test getaddressinfo on external address. Note that these addresses are taken from disablewallet.py
assert_raises_rpc_error(-5, "Invalid address", self.nodes[0].getaddressinfo, "3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy")
assert_raises_rpc_error(-5, "Invalid prefix for Base58-encoded address", self.nodes[0].getaddressinfo, "3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy")
address_info = self.nodes[0].getaddressinfo("mneYUmWYsuk7kySiURxCi3AGxrAqZxLgPZ")
assert_equal(address_info['address'], "mneYUmWYsuk7kySiURxCi3AGxrAqZxLgPZ")
assert_equal(address_info["scriptPubKey"], "76a9144e3854046c7bd1594ac904e4793b6a45b36dea0988ac")
Expand Down

0 comments on commit 333927f

Please sign in to comment.