Skip to content

Commit 9ee09d3

Browse files
authored
Merge pull request #1808 from evoskuil/master
Generalize payment_address script derivation.
2 parents d9a2be1 + 28bf2bb commit 9ee09d3

5 files changed

Lines changed: 41 additions & 37 deletions

File tree

include/bitcoin/system/wallet/addresses/payment_address.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ class BC_API payment_address
4141
public:
4242
DEFAULT_COPY_MOVE_DESTRUCT(payment_address);
4343

44-
static const uint8_t mainnet_p2kh;
45-
static const uint8_t mainnet_p2sh;
44+
static constexpr uint8_t mainnet_p2kh = 0x00;
45+
static constexpr uint8_t mainnet_p2sh = 0x05;
4646

47-
static const uint8_t testnet_p2kh;
48-
static const uint8_t testnet_p2sh;
47+
static constexpr uint8_t testnet_p2kh = 0x6f;
48+
static constexpr uint8_t testnet_p2sh = 0xc4;
4949

5050
typedef std_vector<payment_address> list;
5151
typedef std::shared_ptr<payment_address> ptr;
@@ -90,7 +90,12 @@ class BC_API payment_address
9090
/// Properties.
9191
uint8_t prefix() const NOEXCEPT;
9292
short_hash hash() const NOEXCEPT;
93-
chain::script output_script() const NOEXCEPT;
93+
94+
/// Script is extracted to p2pk or p2sh by matching its prefix to the
95+
/// args provided here. So for testnet and other alts these are required.
96+
chain::script output_script(
97+
uint8_t p2kh_prefix=mainnet_p2kh,
98+
uint8_t p2sh_prefix=mainnet_p2sh) const NOEXCEPT;
9499

95100
/// Methods.
96101
const payment& to_payment() const NOEXCEPT;

include/bitcoin/system/wallet/neutrino.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,17 @@ BC_API bool match_filter(const block_filter& filter,
5353
BC_API bool match_filter(const block_filter& filter,
5454
const chain::scripts& scripts) NOEXCEPT;
5555

56+
/// Prefix values indicate how to convert the address if not btc mainnet.
5657
BC_API bool match_filter(const block_filter& filter,
57-
const wallet::payment_address& address) NOEXCEPT;
58+
const wallet::payment_address& address,
59+
uint8_t p2kh_prefix=wallet::payment_address::mainnet_p2kh,
60+
uint8_t p2sh_prefix=wallet::payment_address::mainnet_p2sh) NOEXCEPT;
5861

62+
/// Prefix values indicate how to convert the address if not btc mainnet.
5963
BC_API bool match_filter(const block_filter& filter,
60-
const wallet::payment_address::list& addresses) NOEXCEPT;
64+
const wallet::payment_address::list& addresses,
65+
uint8_t p2kh_prefix=wallet::payment_address::mainnet_p2kh,
66+
uint8_t p2sh_prefix=wallet::payment_address::mainnet_p2sh) NOEXCEPT;
6167

6268
} // namespace neutrino
6369
} // namespace system

src/wallet/addresses/payment_address.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ namespace libbitcoin {
3131
namespace system {
3232
namespace wallet {
3333

34-
const uint8_t payment_address::mainnet_p2kh = 0x00;
35-
const uint8_t payment_address::mainnet_p2sh = 0x05;
36-
37-
const uint8_t payment_address::testnet_p2kh = 0x6f;
38-
const uint8_t payment_address::testnet_p2sh = 0xc4;
39-
4034
payment_address::payment_address() NOEXCEPT
4135
: payment_()
4236
{
@@ -159,19 +153,17 @@ short_hash payment_address::hash() const NOEXCEPT
159153
return payment_.payload();
160154
}
161155

162-
chain::script payment_address::output_script() const NOEXCEPT
156+
chain::script payment_address::output_script(uint8_t p2kh_prefix,
157+
uint8_t p2sh_prefix) const NOEXCEPT
163158
{
164-
switch (prefix())
165-
{
166-
case mainnet_p2kh:
167-
case testnet_p2kh:
168-
return chain::script::to_pay_key_hash_pattern(hash());
159+
const auto byte = prefix();
169160

170-
case mainnet_p2sh:
171-
case testnet_p2sh:
172-
return chain::script::to_pay_script_hash_pattern(hash());
173-
}
161+
if (byte == p2kh_prefix)
162+
return chain::script::to_pay_key_hash_pattern(hash());
174163

164+
if (byte == p2sh_prefix)
165+
return chain::script::to_pay_script_hash_pattern(hash());
166+
175167
return {};
176168
}
177169

src/wallet/neutrino.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,24 @@ bool match_filter(const block_filter& filter,
147147
}
148148

149149
bool match_filter(const block_filter& filter,
150-
const wallet::payment_address& address) NOEXCEPT
150+
const wallet::payment_address& address, uint8_t p2kh_prefixh,
151+
uint8_t p2sh_prefix) NOEXCEPT
151152
{
152-
return match_filter(filter, address.output_script());
153+
return match_filter(filter, address.output_script(p2kh_prefixh, p2sh_prefix));
153154
}
154155

155156
bool match_filter(const block_filter& filter,
156-
const wallet::payment_address::list& addresses) NOEXCEPT
157+
const wallet::payment_address::list& addresses, uint8_t p2kh_prefixh,
158+
uint8_t p2sh_prefix) NOEXCEPT
157159
{
158160
if (addresses.empty())
159161
return false;
160162

161163
chain::scripts stack(addresses.size());
162-
163164
std::transform(addresses.begin(), addresses.end(), stack.begin(),
164-
[](const wallet::payment_address& address) NOEXCEPT
165+
[=](const wallet::payment_address& address) NOEXCEPT
165166
{
166-
return address.output_script();
167+
return address.output_script(p2kh_prefixh, p2sh_prefix);
167168
});
168169

169170
return match_filter(filter, stack);

test/wallet/neutrino.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3529,7 +3529,7 @@ BOOST_AUTO_TEST_CASE(neutrino__match_filter_1__input_prevout__true)
35293529
const wallet::payment_address address
35303530
{
35313531
base16_array("001fa7459a6cfc64bdc178ba7e7a21603bb2568f"),
3532-
wallet::payment_address::testnet_p2kh
3532+
wallet::payment_address::mainnet_p2kh
35333533
};
35343534

35353535
BOOST_REQUIRE(neutrino::match_filter(filter, address));
@@ -3546,7 +3546,7 @@ BOOST_AUTO_TEST_CASE(neutrino__match_filter_1__unrelated_address__false)
35463546
const wallet::payment_address address
35473547
{
35483548
base16_array("001fa005900cf004b00100ba700021000b00500f"),
3549-
wallet::payment_address::testnet_p2kh
3549+
wallet::payment_address::mainnet_p2kh
35503550
};
35513551

35523552
BOOST_REQUIRE(!neutrino::match_filter(filter, address));
@@ -3560,19 +3560,19 @@ BOOST_AUTO_TEST_CASE(neutrino__match_filter_2__input_prevout__true)
35603560
base16_chunk("0db414c859a07e8205876354a210a75042d0463404913d61a8e068e58a3ae2aa080026")
35613561
};
35623562

3563+
constexpr auto testnet_p2kh = wallet::payment_address::testnet_p2kh;
3564+
constexpr auto testnet_p2sh = wallet::payment_address::testnet_p2kh;
35633565
const wallet::payment_address::list addresses
35643566
{
35653567
{
3566-
base16_array("001fa7459a6cfc64bdc100ba700a21003b005000"),
3567-
wallet::payment_address::testnet_p2kh
3568+
base16_array("001fa7459a6cfc64bdc100ba700a21003b005000"), testnet_p2kh
35683569
},
35693570
{
3570-
base16_array("001fa7459a6cfc64bdc178ba7e7a21603bb2568f"),
3571-
wallet::payment_address::testnet_p2kh
3571+
base16_array("001fa7459a6cfc64bdc178ba7e7a21603bb2568f"), testnet_p2kh
35723572
}
35733573
};
35743574

3575-
BOOST_REQUIRE(neutrino::match_filter(filter, addresses));
3575+
BOOST_REQUIRE(neutrino::match_filter(filter, addresses, testnet_p2kh, testnet_p2sh));
35763576
}
35773577

35783578
BOOST_AUTO_TEST_CASE(neutrino__match_filter_2__unrelated_address__false)
@@ -3587,7 +3587,7 @@ BOOST_AUTO_TEST_CASE(neutrino__match_filter_2__unrelated_address__false)
35873587
{
35883588
{
35893589
base16_array("001fa7459a6cfc64bdc100ba700a21003b005000"),
3590-
wallet::payment_address::testnet_p2kh
3590+
wallet::payment_address::mainnet_p2kh
35913591
}
35923592
};
35933593

0 commit comments

Comments
 (0)