Skip to content

Commit 9a44db2

Browse files
committed
Add outputtype module
Moves OutputType into its own module
1 parent 88a15eb commit 9a44db2

File tree

7 files changed

+122
-86
lines changed

7 files changed

+122
-86
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ BITCOIN_CORE_H = \
135135
netbase.h \
136136
netmessagemaker.h \
137137
noui.h \
138+
outputtype.h \
138139
policy/feerate.h \
139140
policy/fees.h \
140141
policy/policy.h \
@@ -265,6 +266,7 @@ libbitcoin_wallet_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
265266
libbitcoin_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
266267
libbitcoin_wallet_a_SOURCES = \
267268
interfaces/wallet.cpp \
269+
outputtype.cpp \
268270
wallet/crypter.cpp \
269271
wallet/db.cpp \
270272
wallet/feebumper.cpp \

src/outputtype.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2017 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#include <outputtype.h>
7+
8+
#include <keystore.h>
9+
#include <pubkey.h>
10+
#include <script/script.h>
11+
#include <script/standard.h>
12+
13+
#include <assert.h>
14+
#include <string>
15+
16+
static const std::string OUTPUT_TYPE_STRING_LEGACY = "legacy";
17+
static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT = "p2sh-segwit";
18+
static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32";
19+
20+
bool ParseOutputType(const std::string& type, OutputType& output_type)
21+
{
22+
if (type == OUTPUT_TYPE_STRING_LEGACY) {
23+
output_type = OutputType::LEGACY;
24+
return true;
25+
} else if (type == OUTPUT_TYPE_STRING_P2SH_SEGWIT) {
26+
output_type = OutputType::P2SH_SEGWIT;
27+
return true;
28+
} else if (type == OUTPUT_TYPE_STRING_BECH32) {
29+
output_type = OutputType::BECH32;
30+
return true;
31+
}
32+
return false;
33+
}
34+
35+
const std::string& FormatOutputType(OutputType type)
36+
{
37+
switch (type) {
38+
case OutputType::LEGACY: return OUTPUT_TYPE_STRING_LEGACY;
39+
case OutputType::P2SH_SEGWIT: return OUTPUT_TYPE_STRING_P2SH_SEGWIT;
40+
case OutputType::BECH32: return OUTPUT_TYPE_STRING_BECH32;
41+
default: assert(false);
42+
}
43+
}
44+
45+
CTxDestination GetDestinationForKey(const CPubKey& key, OutputType type)
46+
{
47+
switch (type) {
48+
case OutputType::LEGACY: return key.GetID();
49+
case OutputType::P2SH_SEGWIT:
50+
case OutputType::BECH32: {
51+
if (!key.IsCompressed()) return key.GetID();
52+
CTxDestination witdest = WitnessV0KeyHash(key.GetID());
53+
CScript witprog = GetScriptForDestination(witdest);
54+
if (type == OutputType::P2SH_SEGWIT) {
55+
return CScriptID(witprog);
56+
} else {
57+
return witdest;
58+
}
59+
}
60+
default: assert(false);
61+
}
62+
}
63+
64+
std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key)
65+
{
66+
CKeyID keyid = key.GetID();
67+
if (key.IsCompressed()) {
68+
CTxDestination segwit = WitnessV0KeyHash(keyid);
69+
CTxDestination p2sh = CScriptID(GetScriptForDestination(segwit));
70+
return std::vector<CTxDestination>{std::move(keyid), std::move(p2sh), std::move(segwit)};
71+
} else {
72+
return std::vector<CTxDestination>{std::move(keyid)};
73+
}
74+
}
75+
76+

src/outputtype.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2017 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef BITCOIN_OUTPUTTYPE_H
7+
#define BITCOIN_OUTPUTTYPE_H
8+
9+
#include <script/standard.h>
10+
11+
#include <string>
12+
#include <vector>
13+
14+
enum class OutputType {
15+
LEGACY,
16+
P2SH_SEGWIT,
17+
BECH32,
18+
19+
/**
20+
* Special output type for change outputs only. Automatically choose type
21+
* based on address type setting and the types other of non-change outputs
22+
* (see -changetype option documentation and implementation in
23+
* CWallet::TransactionChangeType for details).
24+
*/
25+
CHANGE_AUTO,
26+
};
27+
28+
bool ParseOutputType(const std::string& str, OutputType& output_type);
29+
const std::string& FormatOutputType(OutputType type);
30+
31+
/**
32+
* Get a destination of the requested type (if possible) to the specified key.
33+
* The caller must make sure LearnRelatedScripts has been called beforehand.
34+
*/
35+
CTxDestination GetDestinationForKey(const CPubKey& key, OutputType);
36+
37+
/** Get all destinations (potentially) supported by the wallet for the given key. */
38+
std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key);
39+
40+
#endif // BITCOIN_OUTPUTTYPE_H
41+

src/wallet/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <init.h>
88
#include <net.h>
99
#include <scheduler.h>
10+
#include <outputtype.h>
1011
#include <util.h>
1112
#include <utilmoneystr.h>
1213
#include <validation.h>

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <validation.h>
1212
#include <key_io.h>
1313
#include <net.h>
14+
#include <outputtype.h>
1415
#include <policy/feerate.h>
1516
#include <policy/fees.h>
1617
#include <policy/policy.h>

src/wallet/wallet.cpp

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4453,35 +4453,6 @@ bool CWalletTx::AcceptToMemoryPool(const CAmount& nAbsurdFee, CValidationState&
44534453
return ret;
44544454
}
44554455

4456-
static const std::string OUTPUT_TYPE_STRING_LEGACY = "legacy";
4457-
static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT = "p2sh-segwit";
4458-
static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32";
4459-
4460-
bool ParseOutputType(const std::string& type, OutputType& output_type)
4461-
{
4462-
if (type == OUTPUT_TYPE_STRING_LEGACY) {
4463-
output_type = OutputType::LEGACY;
4464-
return true;
4465-
} else if (type == OUTPUT_TYPE_STRING_P2SH_SEGWIT) {
4466-
output_type = OutputType::P2SH_SEGWIT;
4467-
return true;
4468-
} else if (type == OUTPUT_TYPE_STRING_BECH32) {
4469-
output_type = OutputType::BECH32;
4470-
return true;
4471-
}
4472-
return false;
4473-
}
4474-
4475-
const std::string& FormatOutputType(OutputType type)
4476-
{
4477-
switch (type) {
4478-
case OutputType::LEGACY: return OUTPUT_TYPE_STRING_LEGACY;
4479-
case OutputType::P2SH_SEGWIT: return OUTPUT_TYPE_STRING_P2SH_SEGWIT;
4480-
case OutputType::BECH32: return OUTPUT_TYPE_STRING_BECH32;
4481-
default: assert(false);
4482-
}
4483-
}
4484-
44854456
void CWallet::LearnRelatedScripts(const CPubKey& key, OutputType type)
44864457
{
44874458
if (key.IsCompressed() && (type == OutputType::P2SH_SEGWIT || type == OutputType::BECH32)) {
@@ -4499,37 +4470,6 @@ void CWallet::LearnAllRelatedScripts(const CPubKey& key)
44994470
LearnRelatedScripts(key, OutputType::P2SH_SEGWIT);
45004471
}
45014472

4502-
CTxDestination GetDestinationForKey(const CPubKey& key, OutputType type)
4503-
{
4504-
switch (type) {
4505-
case OutputType::LEGACY: return key.GetID();
4506-
case OutputType::P2SH_SEGWIT:
4507-
case OutputType::BECH32: {
4508-
if (!key.IsCompressed()) return key.GetID();
4509-
CTxDestination witdest = WitnessV0KeyHash(key.GetID());
4510-
CScript witprog = GetScriptForDestination(witdest);
4511-
if (type == OutputType::P2SH_SEGWIT) {
4512-
return CScriptID(witprog);
4513-
} else {
4514-
return witdest;
4515-
}
4516-
}
4517-
default: assert(false);
4518-
}
4519-
}
4520-
4521-
std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key)
4522-
{
4523-
CKeyID keyid = key.GetID();
4524-
if (key.IsCompressed()) {
4525-
CTxDestination segwit = WitnessV0KeyHash(keyid);
4526-
CTxDestination p2sh = CScriptID(GetScriptForDestination(segwit));
4527-
return std::vector<CTxDestination>{std::move(keyid), std::move(p2sh), std::move(segwit)};
4528-
} else {
4529-
return std::vector<CTxDestination>{std::move(keyid)};
4530-
}
4531-
}
4532-
45334473
CTxDestination CWallet::AddAndGetDestinationForScript(const CScript& script, OutputType type)
45344474
{
45354475
// Note that scripts over 520 bytes are not yet supported.

src/wallet/wallet.h

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define BITCOIN_WALLET_WALLET_H
88

99
#include <amount.h>
10+
#include <outputtype.h>
1011
#include <policy/feerate.h>
1112
#include <streams.h>
1213
#include <tinyformat.h>
@@ -93,20 +94,6 @@ enum WalletFeature
9394
FEATURE_LATEST = FEATURE_PRE_SPLIT_KEYPOOL
9495
};
9596

96-
enum class OutputType {
97-
LEGACY,
98-
P2SH_SEGWIT,
99-
BECH32,
100-
101-
/**
102-
* Special output type for change outputs only. Automatically choose type
103-
* based on address type setting and the types other of non-change outputs
104-
* (see -changetype option documentation and implementation in
105-
* CWallet::TransactionChangeType for details).
106-
*/
107-
CHANGE_AUTO,
108-
};
109-
11097
//! Default for -addresstype
11198
constexpr OutputType DEFAULT_ADDRESS_TYPE{OutputType::P2SH_SEGWIT};
11299

@@ -1266,18 +1253,6 @@ class CAccount
12661253
}
12671254
};
12681255

1269-
bool ParseOutputType(const std::string& str, OutputType& output_type);
1270-
const std::string& FormatOutputType(OutputType type);
1271-
1272-
/**
1273-
* Get a destination of the requested type (if possible) to the specified key.
1274-
* The caller must make sure LearnRelatedScripts has been called beforehand.
1275-
*/
1276-
CTxDestination GetDestinationForKey(const CPubKey& key, OutputType);
1277-
1278-
/** Get all destinations (potentially) supported by the wallet for the given key. */
1279-
std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key);
1280-
12811256
/** RAII object to check and reserve a wallet rescan */
12821257
class WalletRescanReserver
12831258
{

0 commit comments

Comments
 (0)