Skip to content

Commit 4b15ffe

Browse files
committed
Merge #20832: rpc: Better error messages for invalid addresses
8f0b64f Better error messages for invalid addresses (Bezdrighin) Pull request description: This PR addresses #20809. We add more detailed error messages in case an invalid address is provided inside the 'validateaddress' and 'getaddressinfo' RPC calls. This also covers the case when a user provides an address from a wrong network. We also add a functional test to test the new error messages. ACKs for top commit: kristapsk: ACK 8f0b64f meshcollider: Code review ACK 8f0b64f Tree-SHA512: ca0f806ab573e96b79e98d9f8c810b81fa99c638d9b5e4d99dc18c8bd2568e6a802ec305fdfb2983574a97a19a46fd53b77645f8078fb77e9deb24ad2a22cf93
2 parents 52d84a4 + 8f0b64f commit 4b15ffe

File tree

7 files changed

+141
-14
lines changed

7 files changed

+141
-14
lines changed

src/key_io.cpp

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include <assert.h>
1313
#include <string.h>
1414

15+
/// Maximum witness length for Bech32 addresses.
16+
static constexpr std::size_t BECH32_WITNESS_PROG_MAX_LEN = 40;
17+
1518
namespace {
1619
class DestinationEncoder
1720
{
@@ -65,10 +68,11 @@ class DestinationEncoder
6568
std::string operator()(const CNoDestination& no) const { return {}; }
6669
};
6770

68-
CTxDestination DecodeDestination(const std::string& str, const CChainParams& params)
71+
CTxDestination DecodeDestination(const std::string& str, const CChainParams& params, std::string& error_str)
6972
{
7073
std::vector<unsigned char> data;
7174
uint160 hash;
75+
error_str = "";
7276
if (DecodeBase58Check(str, data, 21)) {
7377
// base58-encoded Bitcoin addresses.
7478
// Public-key-hash-addresses have version 0 (or 111 testnet).
@@ -85,10 +89,21 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
8589
std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin());
8690
return ScriptHash(hash);
8791
}
92+
93+
// Set potential error message.
94+
// This message may be changed if the address can also be interpreted as a Bech32 address.
95+
error_str = "Invalid prefix for Base58-encoded address";
8896
}
8997
data.clear();
9098
auto bech = bech32::Decode(str);
91-
if (bech.second.size() > 0 && bech.first == params.Bech32HRP()) {
99+
if (bech.second.size() > 0) {
100+
error_str = "";
101+
102+
if (bech.first != params.Bech32HRP()) {
103+
error_str = "Invalid prefix for Bech32 address";
104+
return CNoDestination();
105+
}
106+
92107
// Bech32 decoding
93108
int version = bech.second[0]; // The first 5 bit symbol is the witness version (0-16)
94109
// The rest of the symbols are converted witness program bytes.
@@ -109,18 +124,32 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
109124
return scriptid;
110125
}
111126
}
127+
128+
error_str = "Invalid Bech32 v0 address data size";
129+
return CNoDestination();
130+
}
131+
132+
if (version > 16) {
133+
error_str = "Invalid Bech32 address witness version";
112134
return CNoDestination();
113135
}
114-
if (version > 16 || data.size() < 2 || data.size() > 40) {
136+
137+
if (data.size() < 2 || data.size() > BECH32_WITNESS_PROG_MAX_LEN) {
138+
error_str = "Invalid Bech32 address data size";
115139
return CNoDestination();
116140
}
141+
117142
WitnessUnknown unk;
118143
unk.version = version;
119144
std::copy(data.begin(), data.end(), unk.program);
120145
unk.length = data.size();
121146
return unk;
122147
}
123148
}
149+
150+
// Set error message if address can't be interpreted as Base58 or Bech32.
151+
if (error_str.empty()) error_str = "Invalid address format";
152+
124153
return CNoDestination();
125154
}
126155
} // namespace
@@ -208,14 +237,21 @@ std::string EncodeDestination(const CTxDestination& dest)
208237
return std::visit(DestinationEncoder(Params()), dest);
209238
}
210239

240+
CTxDestination DecodeDestination(const std::string& str, std::string& error_msg)
241+
{
242+
return DecodeDestination(str, Params(), error_msg);
243+
}
244+
211245
CTxDestination DecodeDestination(const std::string& str)
212246
{
213-
return DecodeDestination(str, Params());
247+
std::string error_msg;
248+
return DecodeDestination(str, error_msg);
214249
}
215250

216251
bool IsValidDestinationString(const std::string& str, const CChainParams& params)
217252
{
218-
return IsValidDestination(DecodeDestination(str, params));
253+
std::string error_msg;
254+
return IsValidDestination(DecodeDestination(str, params, error_msg));
219255
}
220256

221257
bool IsValidDestinationString(const std::string& str)

src/key_io.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ std::string EncodeExtPubKey(const CExtPubKey& extpubkey);
2323

2424
std::string EncodeDestination(const CTxDestination& dest);
2525
CTxDestination DecodeDestination(const std::string& str);
26+
CTxDestination DecodeDestination(const std::string& str, std::string& error_msg);
2627
bool IsValidDestinationString(const std::string& str);
2728
bool IsValidDestinationString(const std::string& str, const CChainParams& params);
2829

src/rpc/misc.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ static RPCHelpMan validateaddress()
3939
RPCResult{
4040
RPCResult::Type::OBJ, "", "",
4141
{
42-
{RPCResult::Type::BOOL, "isvalid", "If the address is valid or not. If not, this is the only property returned."},
42+
{RPCResult::Type::BOOL, "isvalid", "If the address is valid or not"},
4343
{RPCResult::Type::STR, "address", "The bitcoin address validated"},
4444
{RPCResult::Type::STR_HEX, "scriptPubKey", "The hex-encoded scriptPubKey generated by the address"},
4545
{RPCResult::Type::BOOL, "isscript", "If the key is a script"},
4646
{RPCResult::Type::BOOL, "iswitness", "If the address is a witness address"},
4747
{RPCResult::Type::NUM, "witness_version", /* optional */ true, "The version number of the witness program"},
4848
{RPCResult::Type::STR_HEX, "witness_program", /* optional */ true, "The hex value of the witness program"},
49+
{RPCResult::Type::STR, "error", /* optional */ true, "Error message, if any"},
4950
}
5051
},
5152
RPCExamples{
@@ -54,13 +55,14 @@ static RPCHelpMan validateaddress()
5455
},
5556
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
5657
{
57-
CTxDestination dest = DecodeDestination(request.params[0].get_str());
58-
bool isValid = IsValidDestination(dest);
58+
std::string error_msg;
59+
CTxDestination dest = DecodeDestination(request.params[0].get_str(), error_msg);
60+
const bool isValid = IsValidDestination(dest);
61+
CHECK_NONFATAL(isValid == error_msg.empty());
5962

6063
UniValue ret(UniValue::VOBJ);
6164
ret.pushKV("isvalid", isValid);
62-
if (isValid)
63-
{
65+
if (isValid) {
6466
std::string currentAddress = EncodeDestination(dest);
6567
ret.pushKV("address", currentAddress);
6668

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

7072
UniValue detail = DescribeAddress(dest);
7173
ret.pushKVs(detail);
74+
} else {
75+
ret.pushKV("error", error_msg);
7276
}
77+
7378
return ret;
7479
},
7580
};

src/wallet/rpcwallet.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3820,13 +3820,19 @@ RPCHelpMan getaddressinfo()
38203820

38213821
LOCK(pwallet->cs_wallet);
38223822

3823-
UniValue ret(UniValue::VOBJ);
3824-
CTxDestination dest = DecodeDestination(request.params[0].get_str());
3823+
std::string error_msg;
3824+
CTxDestination dest = DecodeDestination(request.params[0].get_str(), error_msg);
3825+
38253826
// Make sure the destination is valid
38263827
if (!IsValidDestination(dest)) {
3827-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
3828+
// Set generic error message in case 'DecodeDestination' didn't set it
3829+
if (error_msg.empty()) error_msg = "Invalid address";
3830+
3831+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error_msg);
38283832
}
38293833

3834+
UniValue ret(UniValue::VOBJ);
3835+
38303836
std::string currentAddress = EncodeDestination(dest);
38313837
ret.pushKV("address", currentAddress);
38323838

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2020 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test error messages for 'getaddressinfo' and 'validateaddress' RPC commands."""
6+
7+
from test_framework.test_framework import BitcoinTestFramework
8+
9+
from test_framework.util import (
10+
assert_equal,
11+
assert_raises_rpc_error,
12+
)
13+
14+
BECH32_VALID = 'bcrt1qtmp74ayg7p24uslctssvjm06q5phz4yrxucgnv'
15+
BECH32_INVALID_SIZE = 'bcrt1sqqpl9r5c'
16+
BECH32_INVALID_PREFIX = 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'
17+
18+
BASE58_VALID = 'mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn'
19+
BASE58_INVALID_PREFIX = '17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem'
20+
21+
INVALID_ADDRESS = 'asfah14i8fajz0123f'
22+
23+
class InvalidAddressErrorMessageTest(BitcoinTestFramework):
24+
def set_test_params(self):
25+
self.setup_clean_chain = True
26+
self.num_nodes = 1
27+
28+
def skip_test_if_missing_module(self):
29+
self.skip_if_no_wallet()
30+
31+
def test_validateaddress(self):
32+
node = self.nodes[0]
33+
34+
# Bech32
35+
info = node.validateaddress(BECH32_INVALID_SIZE)
36+
assert not info['isvalid']
37+
assert_equal(info['error'], 'Invalid Bech32 address data size')
38+
39+
info = node.validateaddress(BECH32_INVALID_PREFIX)
40+
assert not info['isvalid']
41+
assert_equal(info['error'], 'Invalid prefix for Bech32 address')
42+
43+
info = node.validateaddress(BECH32_VALID)
44+
assert info['isvalid']
45+
assert 'error' not in info
46+
47+
# Base58
48+
info = node.validateaddress(BASE58_INVALID_PREFIX)
49+
assert not info['isvalid']
50+
assert_equal(info['error'], 'Invalid prefix for Base58-encoded address')
51+
52+
info = node.validateaddress(BASE58_VALID)
53+
assert info['isvalid']
54+
assert 'error' not in info
55+
56+
# Invalid address format
57+
info = node.validateaddress(INVALID_ADDRESS)
58+
assert not info['isvalid']
59+
assert_equal(info['error'], 'Invalid address format')
60+
61+
def test_getaddressinfo(self):
62+
node = self.nodes[0]
63+
64+
assert_raises_rpc_error(-5, "Invalid Bech32 address data size", node.getaddressinfo, BECH32_INVALID_SIZE)
65+
66+
assert_raises_rpc_error(-5, "Invalid prefix for Bech32 address", node.getaddressinfo, BECH32_INVALID_PREFIX)
67+
68+
assert_raises_rpc_error(-5, "Invalid prefix for Base58-encoded address", node.getaddressinfo, BASE58_INVALID_PREFIX)
69+
70+
assert_raises_rpc_error(-5, "Invalid address format", node.getaddressinfo, INVALID_ADDRESS)
71+
72+
def run_test(self):
73+
self.test_validateaddress()
74+
self.test_getaddressinfo()
75+
76+
77+
if __name__ == '__main__':
78+
InvalidAddressErrorMessageTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
'wallet_keypool_topup.py --descriptors',
135135
'feature_fee_estimation.py',
136136
'interface_zmq.py',
137+
'rpc_invalid_address_message.py',
137138
'interface_bitcoin_cli.py',
138139
'mempool_resurrect.py',
139140
'wallet_txn_doublespend.py --mineblock',

test/functional/wallet_basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ def run_test(self):
586586
assert_equal(total_txs, len(self.nodes[0].listtransactions("*", 99999)))
587587

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

0 commit comments

Comments
 (0)