Skip to content

Commit 9d098af

Browse files
committed
Merge bitcoin/bitcoin#27747: rpc: Use 'byte'/'bytes' for bech32(m) validation error message
3d0a5c3 use 'byte'/'bytes' for bech32(m) validation error (Reese Russell) Pull request description: This PR rectifies a linguistic inconsistency found in merged PR [27727](bitcoin/bitcoin#27727). It addresses the improper usage of the term 'byte' in error reports. As it stands, PR [27727](bitcoin/bitcoin#27727) exclusively utilizes 'byte' in error messages, regardless of the context, as demonstrated below: Currently: ```Invalid Bech32 v0 address program size (16 byte), per BIP141``` This modification enhances the accuracy of error reporting in most scenarios users are likely to encounter by checking for a plural or singular number of bytes. This PR **16 Bytes program size error** : ``` ( "BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P", "Invalid Bech32 v0 address program size (16 bytes), per BIP141", [], ) ``` **1 Byte program size error** ``` ( "bc1pw5dgrnzv", "Invalid Bech32 address program size (1 byte)", [] ), ``` Thank you ACKs for top commit: MarcoFalke: lgtm ACK 3d0a5c3 Tree-SHA512: 55069194a6a33a37559cf14b59b6ac05b1160f57f14d1415aef8e76c916c7c7f343310916ae85b3fa895937802449c1dddb2f653340d0f39203f06aee10f6fce
2 parents e434320 + 3d0a5c3 commit 9d098af

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

src/key_io.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
146146
// The rest of the symbols are converted witness program bytes.
147147
data.reserve(((dec.data.size() - 1) * 5) / 8);
148148
if (ConvertBits<5, 8, false>([&](unsigned char c) { data.push_back(c); }, dec.data.begin() + 1, dec.data.end())) {
149+
150+
std::string_view byte_str{data.size() == 1 ? "byte" : "bytes"};
151+
149152
if (version == 0) {
150153
{
151154
WitnessV0KeyHash keyid;
@@ -162,7 +165,7 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
162165
}
163166
}
164167

165-
error_str = strprintf("Invalid Bech32 v0 address program size (%s byte), per BIP141", data.size());
168+
error_str = strprintf("Invalid Bech32 v0 address program size (%d %s), per BIP141", data.size(), byte_str);
166169
return CNoDestination();
167170
}
168171

@@ -179,7 +182,7 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
179182
}
180183

181184
if (data.size() < 2 || data.size() > BECH32_WITNESS_PROG_MAX_LEN) {
182-
error_str = strprintf("Invalid Bech32 address program size (%s byte)", data.size());
185+
error_str = strprintf("Invalid Bech32 address program size (%d %s)", data.size(), byte_str);
183186
return CNoDestination();
184187
}
185188

test/functional/rpc_invalid_address_message.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ def check_invalid(self, addr, error_str, error_locations=None):
6363

6464
def test_validateaddress(self):
6565
# Invalid Bech32
66-
self.check_invalid(BECH32_INVALID_SIZE, "Invalid Bech32 address program size (41 byte)")
66+
self.check_invalid(BECH32_INVALID_SIZE, "Invalid Bech32 address program size (41 bytes)")
6767
self.check_invalid(BECH32_INVALID_PREFIX, 'Invalid or unsupported Segwit (Bech32) or Base58 encoding.')
6868
self.check_invalid(BECH32_INVALID_BECH32, 'Version 1+ witness address must use Bech32m checksum')
6969
self.check_invalid(BECH32_INVALID_BECH32M, 'Version 0 witness address must use Bech32 checksum')
7070
self.check_invalid(BECH32_INVALID_VERSION, 'Invalid Bech32 address witness version')
71-
self.check_invalid(BECH32_INVALID_V0_SIZE, "Invalid Bech32 v0 address program size (21 byte), per BIP141")
71+
self.check_invalid(BECH32_INVALID_V0_SIZE, "Invalid Bech32 v0 address program size (21 bytes), per BIP141")
7272
self.check_invalid(BECH32_TOO_LONG, 'Bech32 string too long', list(range(90, 108)))
7373
self.check_invalid(BECH32_ONE_ERROR, 'Invalid Bech32 checksum', [9])
7474
self.check_invalid(BECH32_TWO_ERRORS, 'Invalid Bech32 checksum', [22, 43])
@@ -105,7 +105,7 @@ def test_validateaddress(self):
105105
def test_getaddressinfo(self):
106106
node = self.nodes[0]
107107

108-
assert_raises_rpc_error(-5, "Invalid Bech32 address program size (41 byte)", node.getaddressinfo, BECH32_INVALID_SIZE)
108+
assert_raises_rpc_error(-5, "Invalid Bech32 address program size (41 bytes)", node.getaddressinfo, BECH32_INVALID_SIZE)
109109
assert_raises_rpc_error(-5, "Invalid or unsupported Segwit (Bech32) or Base58 encoding.", node.getaddressinfo, BECH32_INVALID_PREFIX)
110110
assert_raises_rpc_error(-5, "Invalid or unsupported Base58-encoded address.", node.getaddressinfo, BASE58_INVALID_PREFIX)
111111
assert_raises_rpc_error(-5, "Invalid or unsupported Segwit (Bech32) or Base58 encoding.", node.getaddressinfo, INVALID_ADDRESS)

test/functional/rpc_validateaddress.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
),
3434
(
3535
"BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P",
36-
"Invalid Bech32 v0 address program size (16 byte), per BIP141",
36+
"Invalid Bech32 v0 address program size (16 bytes), per BIP141",
3737
[],
3838
),
3939
(
@@ -101,12 +101,12 @@
101101
("bc1pw5dgrnzv", "Invalid Bech32 address program size (1 byte)", []),
102102
(
103103
"bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v8n0nx0muaewav253zgeav",
104-
"Invalid Bech32 address program size (41 byte)",
104+
"Invalid Bech32 address program size (41 bytes)",
105105
[],
106106
),
107107
(
108108
"BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P",
109-
"Invalid Bech32 v0 address program size (16 byte), per BIP141",
109+
"Invalid Bech32 v0 address program size (16 bytes), per BIP141",
110110
[],
111111
),
112112
(

0 commit comments

Comments
 (0)