Skip to content

Commit 8284229

Browse files
theStackinstagibbs
andcommitted
refactor: deduplicate anchor witness program bytes (0x4e,0x73)
Co-authored-by: Gregory Sanders <[email protected]>
1 parent 41f2f05 commit 8284229

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

src/addresstype.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,13 @@ struct WitnessUnknown
117117
}
118118
};
119119

120+
/** Witness program for Pay-to-Anchor output script type */
121+
static const std::vector<unsigned char> ANCHOR_BYTES{0x4e, 0x73};
122+
120123
struct PayToAnchor : public WitnessUnknown
121124
{
122-
PayToAnchor() : WitnessUnknown(1, {0x4e, 0x73}) {
123-
Assume(CScript::IsPayToAnchor(1, {0x4e, 0x73}));
125+
PayToAnchor() : WitnessUnknown(1, ANCHOR_BYTES) {
126+
Assume(CScript::IsPayToAnchor(1, ANCHOR_BYTES));
124127
};
125128
};
126129

src/test/script_standard_tests.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <test/data/bip341_wallet_vectors.json.h>
66

7+
#include <addresstype.h>
78
#include <key.h>
89
#include <key_io.h>
910
#include <script/script.h>
@@ -130,9 +131,8 @@ BOOST_AUTO_TEST_CASE(script_standard_Solver_success)
130131
BOOST_CHECK(solutions[1] == ToByteVector(uint256::ONE));
131132

132133
// TxoutType::ANCHOR
133-
std::vector<unsigned char> anchor_bytes{0x4e, 0x73};
134134
s.clear();
135-
s << OP_1 << anchor_bytes;
135+
s << OP_1 << ANCHOR_BYTES;
136136
BOOST_CHECK_EQUAL(Solver(s, solutions), TxoutType::ANCHOR);
137137
BOOST_CHECK(solutions.empty());
138138

@@ -212,7 +212,7 @@ BOOST_AUTO_TEST_CASE(script_standard_Solver_failure)
212212

213213
// TxoutType::ANCHOR but wrong witness version
214214
s.clear();
215-
s << OP_2 << std::vector<unsigned char>{0x4e, 0x73};
215+
s << OP_2 << ANCHOR_BYTES;
216216
BOOST_CHECK(!s.IsPayToAnchor());
217217
BOOST_CHECK_EQUAL(Solver(s, solutions), TxoutType::WITNESS_UNKNOWN);
218218

@@ -284,9 +284,8 @@ BOOST_AUTO_TEST_CASE(script_standard_ExtractDestination)
284284
BOOST_CHECK(std::get<WitnessV1Taproot>(address) == WitnessV1Taproot(xpk));
285285

286286
// TxoutType::ANCHOR
287-
std::vector<unsigned char> anchor_bytes{0x4e, 0x73};
288287
s.clear();
289-
s << OP_1 << anchor_bytes;
288+
s << OP_1 << ANCHOR_BYTES;
290289
BOOST_CHECK(ExtractDestination(s, address));
291290
BOOST_CHECK(std::get<PayToAnchor>(address) == PayToAnchor());
292291

@@ -379,9 +378,8 @@ BOOST_AUTO_TEST_CASE(script_standard_GetScriptFor_)
379378
BOOST_CHECK(result == expected);
380379

381380
// PayToAnchor
382-
std::vector<unsigned char> anchor_bytes{0x4e, 0x73};
383381
expected.clear();
384-
expected << OP_1 << anchor_bytes;
382+
expected << OP_1 << ANCHOR_BYTES;
385383
result = GetScriptForDestination(PayToAnchor());
386384
BOOST_CHECK(result == expected);
387385
}

src/test/transaction_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ BOOST_AUTO_TEST_CASE(test_IsStandard)
10401040
}
10411041

10421042
// Check anchor outputs
1043-
t.vout[0].scriptPubKey = CScript() << OP_1 << std::vector<unsigned char>{0x4e, 0x73};
1043+
t.vout[0].scriptPubKey = CScript() << OP_1 << ANCHOR_BYTES;
10441044
BOOST_CHECK(t.vout[0].scriptPubKey.IsPayToAnchor());
10451045
t.vout[0].nValue = 240;
10461046
CheckIsStandard(t);

0 commit comments

Comments
 (0)