Skip to content

Commit ebd82fa

Browse files
committed
Merge bitcoin/bitcoin#30532: refactor: remove deprecated TxidFromString() in favour of transaction_identifier::FromHex()
f553e6d refactor: remove TxidFromString (stickies-v) 285ab50 test: replace WtxidFromString with Wtxid::FromHex (stickies-v) 9a0b2a6 fuzz: increase FromHex() coverage (stickies-v) 526a87b test: add uint256::FromHex unittest coverage (stickies-v) Pull request description: Since bitcoin/bitcoin@fab6ddb, `TxidFromString()` has been deprecated because it is less robust than the `transaction_identifier::FromHex()` introduced in [the same PR](bitcoin/bitcoin#30482). Specifically, it tries to recover from length-mismatches, recover from untrimmed whitespace, 0x-prefix and garbage at the end, instead of simply requiring exactly 64 hex-only characters. In this PR, `TxidFromString` is removed completely to clean up the code and prevent further unsafe usage. Unit and fuzz test coverage on `uint256::FromHex()` and functions that wrap it is increased. Note: `TxidFromSring` allowed users to prefix strings with "0x", this is no longer allowed for `transaction_identifier::FromHex()`, so a helper function for input validation may prove helpful in the future _(this overlaps with the `uint256::uint256S()` vs `uint256::FromHex()` future cleanup)_. It is not relevant to this PR, though, besides the fact that this unused (except for in tests) functionality is removed. The only users of `TxidFromString` are: - `test`, where it is straightforward to drop in the new `FromHex()` methods without much further concern - `qt` coincontrol. There is no need for input validation here, but txids are not guaranteed to be 64 characters. This is already handled by the existing code, so again, using `FromHex()` here seems quite straightforward. Addresses @maflcko's suggestion: bitcoin/bitcoin#30482 (comment) Also removes `WtxidFromString()`, which is a test-only helper function. ### Testing GUI changes To test the GUI coincontrol affected lines, `regtest` is probably the easiest way to quickly get some test coins, you can use e.g. ``` alias cli="./src/bitcoin-cli -regtest" cli createwallet "coincontrol" # generate 10 spendable outputs on 1 address cli generatetoaddress 10 $(cli -rpcwallet=coincontrol getnewaddress) # generate 10 spendable outputs on another address cli generatetoaddress 10 $(cli -rpcwallet=coincontrol getnewaddress) # make previous outputs spendable cli generatetoaddress 100 $(cli -rpcwallet=coincontrol getnewaddress) ``` ACKs for top commit: maflcko: re-ACK f553e6d 🔻 hodlinator: ACK f553e6d paplorinc: ACK f553e6d TheCharlatan: Nice, ACK f553e6d Tree-SHA512: c1c7e6ea4cbf05cf660ba178ffc4f35f0328f7aa6ad81872e2462fb91a6a22e4681ff64b3d0202a5a9abcb650c939561585cd309164a69ab6081c0765ee271ef
2 parents dc605cf + f553e6d commit ebd82fa

8 files changed

+93
-57
lines changed

src/qt/coincontroldialog.cpp

+11-15
Original file line numberDiff line numberDiff line change
@@ -174,22 +174,17 @@ void CoinControlDialog::showMenu(const QPoint &point)
174174
contextMenuItem = item;
175175

176176
// disable some items (like Copy Transaction ID, lock, unlock) for tree roots in context menu
177-
if (item->data(COLUMN_ADDRESS, TxHashRole).toString().length() == 64) // transaction hash is 64 characters (this means it is a child node, so it is not a parent node in tree mode)
178-
{
177+
auto txid{Txid::FromHex(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString())};
178+
if (txid) { // a valid txid means this is a child node, and not a parent node in tree mode
179179
m_copy_transaction_outpoint_action->setEnabled(true);
180-
if (model->wallet().isLockedCoin(COutPoint(TxidFromString(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), item->data(COLUMN_ADDRESS, VOutRole).toUInt())))
181-
{
180+
if (model->wallet().isLockedCoin(COutPoint(*txid, item->data(COLUMN_ADDRESS, VOutRole).toUInt()))) {
182181
lockAction->setEnabled(false);
183182
unlockAction->setEnabled(true);
184-
}
185-
else
186-
{
183+
} else {
187184
lockAction->setEnabled(true);
188185
unlockAction->setEnabled(false);
189186
}
190-
}
191-
else // this means click on parent node in tree mode -> disable all
192-
{
187+
} else { // this means click on parent node in tree mode -> disable all
193188
m_copy_transaction_outpoint_action->setEnabled(false);
194189
lockAction->setEnabled(false);
195190
unlockAction->setEnabled(false);
@@ -240,7 +235,7 @@ void CoinControlDialog::lockCoin()
240235
if (contextMenuItem->checkState(COLUMN_CHECKBOX) == Qt::Checked)
241236
contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
242237

243-
COutPoint outpt(TxidFromString(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
238+
COutPoint outpt(Txid::FromHex(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()).value(), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
244239
model->wallet().lockCoin(outpt, /* write_to_db = */ true);
245240
contextMenuItem->setDisabled(true);
246241
contextMenuItem->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
@@ -250,7 +245,7 @@ void CoinControlDialog::lockCoin()
250245
// context menu action: unlock coin
251246
void CoinControlDialog::unlockCoin()
252247
{
253-
COutPoint outpt(TxidFromString(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
248+
COutPoint outpt(Txid::FromHex(contextMenuItem->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()).value(), contextMenuItem->data(COLUMN_ADDRESS, VOutRole).toUInt());
254249
model->wallet().unlockCoin(outpt);
255250
contextMenuItem->setDisabled(false);
256251
contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon());
@@ -340,9 +335,10 @@ void CoinControlDialog::radioListMode(bool checked)
340335
// checkbox clicked by user
341336
void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
342337
{
343-
if (column == COLUMN_CHECKBOX && item->data(COLUMN_ADDRESS, TxHashRole).toString().length() == 64) // transaction hash is 64 characters (this means it is a child node, so it is not a parent node in tree mode)
344-
{
345-
COutPoint outpt(TxidFromString(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString()), item->data(COLUMN_ADDRESS, VOutRole).toUInt());
338+
if (column != COLUMN_CHECKBOX) return;
339+
auto txid{Txid::FromHex(item->data(COLUMN_ADDRESS, TxHashRole).toString().toStdString())};
340+
if (txid) { // a valid txid means this is a child node, and not a parent node in tree mode
341+
COutPoint outpt(*txid, item->data(COLUMN_ADDRESS, VOutRole).toUInt());
346342

347343
if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked)
348344
m_coin_control.UnSelect(outpt);

src/test/bloom_tests.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ BOOST_AUTO_TEST_CASE(bloom_match)
138138
BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match output address");
139139

140140
filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);
141-
filter.insert(COutPoint(TxidFromString("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0));
141+
filter.insert(COutPoint(Txid::FromHex("90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b").value(), 0));
142142
BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match COutPoint");
143143

144144
filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);
145-
COutPoint prevOutPoint(TxidFromString("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0);
145+
COutPoint prevOutPoint(Txid::FromHex("90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b").value(), 0);
146146
{
147147
std::vector<unsigned char> data(32 + sizeof(unsigned int));
148148
memcpy(data.data(), prevOutPoint.hash.begin(), 32);
@@ -160,11 +160,11 @@ BOOST_AUTO_TEST_CASE(bloom_match)
160160
BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched random address");
161161

162162
filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);
163-
filter.insert(COutPoint(TxidFromString("0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 1));
163+
filter.insert(COutPoint(Txid::FromHex("90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b").value(), 1));
164164
BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched COutPoint for an output we didn't care about");
165165

166166
filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);
167-
filter.insert(COutPoint(TxidFromString("0x000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"), 0));
167+
filter.insert(COutPoint(Txid::FromHex("000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b").value(), 0));
168168
BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched COutPoint for an output we didn't care about");
169169
}
170170

@@ -426,9 +426,9 @@ BOOST_AUTO_TEST_CASE(merkle_block_4_test_p2pubkey_only)
426426
BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash());
427427

428428
// We should match the generation outpoint
429-
BOOST_CHECK(filter.contains(COutPoint(TxidFromString("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0)));
429+
BOOST_CHECK(filter.contains(COutPoint(Txid::FromHex("147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b").value(), 0)));
430430
// ... but not the 4th transaction's output (its not pay-2-pubkey)
431-
BOOST_CHECK(!filter.contains(COutPoint(TxidFromString("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0)));
431+
BOOST_CHECK(!filter.contains(COutPoint(Txid::FromHex("02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041").value(), 0)));
432432
}
433433

434434
BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none)
@@ -451,8 +451,8 @@ BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none)
451451
BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash());
452452

453453
// We shouldn't match any outpoints (UPDATE_NONE)
454-
BOOST_CHECK(!filter.contains(COutPoint(TxidFromString("0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"), 0)));
455-
BOOST_CHECK(!filter.contains(COutPoint(TxidFromString("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"), 0)));
454+
BOOST_CHECK(!filter.contains(COutPoint(Txid::FromHex("147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b").value(), 0)));
455+
BOOST_CHECK(!filter.contains(COutPoint(Txid::FromHex("02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041").value(), 0)));
456456
}
457457

458458
static std::vector<unsigned char> RandomData()

src/test/fuzz/hex.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <uint256.h>
1111
#include <univalue.h>
1212
#include <util/strencodings.h>
13+
#include <util/transaction_identifier.h>
1314

1415
#include <cassert>
1516
#include <cstdint>
@@ -27,7 +28,11 @@ FUZZ_TARGET(hex)
2728
assert(ToLower(random_hex_string) == hex_data);
2829
}
2930
(void)IsHexNumber(random_hex_string);
30-
(void)uint256::FromHex(random_hex_string);
31+
if (uint256::FromHex(random_hex_string)) {
32+
assert(random_hex_string.length() == 64);
33+
assert(Txid::FromHex(random_hex_string));
34+
assert(Wtxid::FromHex(random_hex_string));
35+
}
3136
(void)uint256S(random_hex_string);
3237
try {
3338
(void)HexToPubKey(random_hex_string);

src/test/merkleblock_tests.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_found)
2424
std::set<Txid> txids;
2525

2626
// Last txn in block.
27-
Txid txhash1{TxidFromString("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")};
27+
Txid txhash1{Txid::FromHex("74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20").value()};
2828

2929
// Second txn in block.
30-
Txid txhash2{TxidFromString("0xf9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07")};
30+
Txid txhash2{Txid::FromHex("f9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07").value()};
3131

3232
txids.insert(txhash1);
3333
txids.insert(txhash2);
@@ -63,7 +63,7 @@ BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_not_found)
6363
CBlock block = getBlock13b8a();
6464

6565
std::set<Txid> txids2;
66-
txids2.insert(TxidFromString("0xc0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"));
66+
txids2.insert(Txid::FromHex("c0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20").value());
6767
CMerkleBlock merkleBlock(block, txids2);
6868

6969
BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex());

src/test/transaction_tests.cpp

+3-11
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ BOOST_AUTO_TEST_CASE(tx_valid)
223223
fValid = false;
224224
break;
225225
}
226-
COutPoint outpoint{TxidFromString(vinput[0].get_str()), uint32_t(vinput[1].getInt<int>())};
226+
COutPoint outpoint{Txid::FromHex(vinput[0].get_str()).value(), uint32_t(vinput[1].getInt<int>())};
227227
mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str());
228228
if (vinput.size() >= 4)
229229
{
@@ -311,7 +311,7 @@ BOOST_AUTO_TEST_CASE(tx_invalid)
311311
fValid = false;
312312
break;
313313
}
314-
COutPoint outpoint{TxidFromString(vinput[0].get_str()), uint32_t(vinput[1].getInt<int>())};
314+
COutPoint outpoint{Txid::FromHex(vinput[0].get_str()).value(), uint32_t(vinput[1].getInt<int>())};
315315
mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str());
316316
if (vinput.size() >= 4)
317317
{
@@ -542,7 +542,7 @@ BOOST_AUTO_TEST_CASE(test_big_witness_transaction)
542542
// create a big transaction of 4500 inputs signed by the same key
543543
for(uint32_t ij = 0; ij < 4500; ij++) {
544544
uint32_t i = mtx.vin.size();
545-
COutPoint outpoint(TxidFromString("0000000000000000000000000000000000000000000000000000000000000100"), i);
545+
COutPoint outpoint(Txid::FromHex("0000000000000000000000000000000000000000000000000000000000000100").value(), i);
546546

547547
mtx.vin.resize(mtx.vin.size() + 1);
548548
mtx.vin[i].prevout = outpoint;
@@ -1028,12 +1028,4 @@ BOOST_AUTO_TEST_CASE(test_IsStandard)
10281028
}
10291029
}
10301030

1031-
BOOST_AUTO_TEST_CASE(test_TxidFromString)
1032-
{
1033-
// Make sure TxidFromString respects string_view length and stops reading at
1034-
// end of the substring.
1035-
BOOST_CHECK_EQUAL(TxidFromString(std::string_view("ABCD1234", 4)).ToString(),
1036-
"000000000000000000000000000000000000000000000000000000000000abcd");
1037-
}
1038-
10391031
BOOST_AUTO_TEST_SUITE_END()

src/test/txpackage_tests.cpp

+6-13
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ inline CTransactionRef create_placeholder_tx(size_t num_inputs, size_t num_outpu
4343
}
4444
return MakeTransactionRef(mtx);
4545
}
46-
47-
// Create a Wtxid from a hex string
48-
inline Wtxid WtxidFromString(std::string_view str)
49-
{
50-
return Wtxid::FromUint256(uint256S(str));
51-
}
52-
5346
BOOST_FIXTURE_TEST_CASE(package_hash_tests, TestChain100Setup)
5447
{
5548
// Random real segwit transaction
@@ -74,9 +67,9 @@ BOOST_FIXTURE_TEST_CASE(package_hash_tests, TestChain100Setup)
7467
CTransactionRef ptx_3{MakeTransactionRef(tx_3)};
7568

7669
// It's easy to see that wtxids are sorted in lexicographical order:
77-
Wtxid wtxid_1{WtxidFromString("0x85cd1a31eb38f74ed5742ec9cb546712ab5aaf747de28a9168b53e846cbda17f")};
78-
Wtxid wtxid_2{WtxidFromString("0xb4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b")};
79-
Wtxid wtxid_3{WtxidFromString("0xe065bac15f62bb4e761d761db928ddee65a47296b2b776785abb912cdec474e3")};
70+
Wtxid wtxid_1{Wtxid::FromHex("85cd1a31eb38f74ed5742ec9cb546712ab5aaf747de28a9168b53e846cbda17f").value()};
71+
Wtxid wtxid_2{Wtxid::FromHex("b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b").value()};
72+
Wtxid wtxid_3{Wtxid::FromHex("e065bac15f62bb4e761d761db928ddee65a47296b2b776785abb912cdec474e3").value()};
8073
BOOST_CHECK_EQUAL(tx_1.GetWitnessHash(), wtxid_1);
8174
BOOST_CHECK_EQUAL(tx_2.GetWitnessHash(), wtxid_2);
8275
BOOST_CHECK_EQUAL(tx_3.GetWitnessHash(), wtxid_3);
@@ -85,9 +78,9 @@ BOOST_FIXTURE_TEST_CASE(package_hash_tests, TestChain100Setup)
8578
BOOST_CHECK(wtxid_2.GetHex() < wtxid_3.GetHex());
8679

8780
// The txids are not (we want to test that sorting and hashing use wtxid, not txid):
88-
Txid txid_1{TxidFromString("0xbd0f71c1d5e50589063e134fad22053cdae5ab2320db5bf5e540198b0b5a4e69")};
89-
Txid txid_2{TxidFromString("0xb4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b")};
90-
Txid txid_3{TxidFromString("0xee707be5201160e32c4fc715bec227d1aeea5940fb4295605e7373edce3b1a93")};
81+
Txid txid_1{Txid::FromHex("bd0f71c1d5e50589063e134fad22053cdae5ab2320db5bf5e540198b0b5a4e69").value()};
82+
Txid txid_2{Txid::FromHex("b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b").value()};
83+
Txid txid_3{Txid::FromHex("ee707be5201160e32c4fc715bec227d1aeea5940fb4295605e7373edce3b1a93").value()};
9184
BOOST_CHECK_EQUAL(tx_1.GetHash(), txid_1);
9285
BOOST_CHECK_EQUAL(tx_2.GetHash(), txid_2);
9386
BOOST_CHECK_EQUAL(tx_3.GetHash(), txid_3);

src/test/uint256_tests.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
#include <streams.h>
77
#include <test/util/setup_common.h>
88
#include <uint256.h>
9+
#include <util/strencodings.h>
10+
#include <util/transaction_identifier.h>
911

1012
#include <boost/test/unit_test.hpp>
1113

1214
#include <iomanip>
1315
#include <sstream>
1416
#include <string>
17+
#include <string_view>
1518
#include <vector>
1619

1720
BOOST_AUTO_TEST_SUITE(uint256_tests)
@@ -330,6 +333,59 @@ BOOST_AUTO_TEST_CASE(parse)
330333
}
331334
}
332335

336+
/**
337+
* Implemented as a templated function so it can be reused by other classes that have a FromHex()
338+
* method that wraps base_blob::FromHex(), such as transaction_identifier::FromHex().
339+
*/
340+
template <typename T>
341+
void TestFromHex()
342+
{
343+
constexpr unsigned int num_chars{T::size() * 2};
344+
static_assert(num_chars <= 64); // this test needs to be modified to allow for more than 64 hex chars
345+
const std::string valid_64char_input{"0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF"};
346+
const auto valid_input{valid_64char_input.substr(0, num_chars)};
347+
{
348+
// check that lower and upper case hex characters are accepted
349+
auto valid_result{T::FromHex(valid_input)};
350+
BOOST_REQUIRE(valid_result);
351+
BOOST_CHECK_EQUAL(valid_result->ToString(), ToLower(valid_input));
352+
}
353+
{
354+
// check that only strings of size num_chars are accepted
355+
BOOST_CHECK(!T::FromHex(""));
356+
BOOST_CHECK(!T::FromHex("0"));
357+
BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars / 2)));
358+
BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars - 1)));
359+
BOOST_CHECK(!T::FromHex(valid_input + "0"));
360+
}
361+
{
362+
// check that non-hex characters are not accepted
363+
std::string invalid_chars{R"( !"#$%&'()*+,-./:;<=>?@GHIJKLMNOPQRSTUVWXYZ[\]^_`ghijklmnopqrstuvwxyz{|}~)"};
364+
for (auto c : invalid_chars) {
365+
BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars - 1) + c));
366+
}
367+
// 0x prefixes are invalid
368+
std::string invalid_prefix{"0x" + valid_input};
369+
BOOST_CHECK(!T::FromHex(std::string_view(invalid_prefix.data(), num_chars)));
370+
BOOST_CHECK(!T::FromHex(invalid_prefix));
371+
}
372+
{
373+
// check that string_view length is respected
374+
std::string chars_68{valid_64char_input + "0123"};
375+
BOOST_CHECK_EQUAL(T::FromHex(std::string_view(chars_68.data(), num_chars)).value().ToString(), ToLower(valid_input));
376+
BOOST_CHECK(!T::FromHex(std::string_view(chars_68.data(), num_chars - 1))); // too short
377+
BOOST_CHECK(!T::FromHex(std::string_view(chars_68.data(), num_chars + 1))); // too long
378+
}
379+
}
380+
381+
BOOST_AUTO_TEST_CASE(from_hex)
382+
{
383+
TestFromHex<uint160>();
384+
TestFromHex<uint256>();
385+
TestFromHex<Txid>();
386+
TestFromHex<Wtxid>();
387+
}
388+
333389
BOOST_AUTO_TEST_CASE( check_ONE )
334390
{
335391
uint256 one = uint256S("0000000000000000000000000000000000000000000000000000000000000001");

src/util/transaction_identifier.h

-6
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,4 @@ using Txid = transaction_identifier<false>;
7272
/** Wtxid commits to all transaction fields including the witness. */
7373
using Wtxid = transaction_identifier<true>;
7474

75-
/** DEPRECATED due to missing length-check and hex-check, please use the safer FromHex, or FromUint256 */
76-
inline Txid TxidFromString(std::string_view str)
77-
{
78-
return Txid::FromUint256(uint256S(str));
79-
}
80-
8175
#endif // BITCOIN_UTIL_TRANSACTION_IDENTIFIER_H

0 commit comments

Comments
 (0)