Skip to content

Commit a3a4d19

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

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
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

+2-4
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,7 +131,6 @@ 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();
135135
s << OP_1 << anchor_bytes;
136136
BOOST_CHECK_EQUAL(Solver(s, solutions), TxoutType::ANCHOR);
@@ -208,7 +208,7 @@ BOOST_AUTO_TEST_CASE(script_standard_Solver_failure)
208208

209209
// TxoutType::ANCHOR but wrong witness version
210210
s.clear();
211-
s << OP_2 << std::vector<unsigned char>{0x4e, 0x73};
211+
s << OP_2 << anchor_bytes;
212212
BOOST_CHECK(!s.IsPayToAnchor());
213213
BOOST_CHECK_EQUAL(Solver(s, solutions), TxoutType::WITNESS_UNKNOWN);
214214

@@ -280,7 +280,6 @@ BOOST_AUTO_TEST_CASE(script_standard_ExtractDestination)
280280
BOOST_CHECK(std::get<WitnessV1Taproot>(address) == WitnessV1Taproot(xpk));
281281

282282
// TxoutType::ANCHOR
283-
std::vector<unsigned char> anchor_bytes{0x4e, 0x73};
284283
s.clear();
285284
s << OP_1 << anchor_bytes;
286285
BOOST_CHECK(ExtractDestination(s, address));
@@ -368,7 +367,6 @@ BOOST_AUTO_TEST_CASE(script_standard_GetScriptFor_)
368367
BOOST_CHECK(result == expected);
369368

370369
// PayToAnchor
371-
std::vector<unsigned char> anchor_bytes{0x4e, 0x73};
372370
expected.clear();
373371
expected << OP_1 << anchor_bytes;
374372
result = GetScriptForDestination(PayToAnchor());

src/test/transaction_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ BOOST_AUTO_TEST_CASE(test_IsStandard)
10381038
}
10391039

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

0 commit comments

Comments
 (0)