Bitcoin Core  22.99.0
P2P Digital Currency
outputtype.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 <pubkey.h>
9 #include <script/script.h>
10 #include <script/sign.h>
11 #include <script/signingprovider.h>
12 #include <script/standard.h>
13 #include <util/vector.h>
14 
15 #include <assert.h>
16 #include <optional>
17 #include <string>
18 
19 static const std::string OUTPUT_TYPE_STRING_LEGACY = "legacy";
20 static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT = "p2sh-segwit";
21 static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32";
22 static const std::string OUTPUT_TYPE_STRING_BECH32M = "bech32m";
23 
24 std::optional<OutputType> ParseOutputType(const std::string& type)
25 {
26  if (type == OUTPUT_TYPE_STRING_LEGACY) {
27  return OutputType::LEGACY;
28  } else if (type == OUTPUT_TYPE_STRING_P2SH_SEGWIT) {
30  } else if (type == OUTPUT_TYPE_STRING_BECH32) {
31  return OutputType::BECH32;
32  } else if (type == OUTPUT_TYPE_STRING_BECH32M) {
33  return OutputType::BECH32M;
34  }
35  return std::nullopt;
36 }
37 
38 const std::string& FormatOutputType(OutputType type)
39 {
40  switch (type) {
45  } // no default case, so the compiler can warn about missing cases
46  assert(false);
47 }
48 
50 {
51  switch (type) {
52  case OutputType::LEGACY: return PKHash(key);
54  case OutputType::BECH32: {
55  if (!key.IsCompressed()) return PKHash(key);
56  CTxDestination witdest = WitnessV0KeyHash(key);
57  CScript witprog = GetScriptForDestination(witdest);
58  if (type == OutputType::P2SH_SEGWIT) {
59  return ScriptHash(witprog);
60  } else {
61  return witdest;
62  }
63  }
64  case OutputType::BECH32M: {} // This function should never be used with BECH32M, so let it assert
65  } // no default case, so the compiler can warn about missing cases
66  assert(false);
67 }
68 
69 std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key)
70 {
71  PKHash keyid(key);
72  CTxDestination p2pkh{keyid};
73  if (key.IsCompressed()) {
74  CTxDestination segwit = WitnessV0KeyHash(keyid);
76  return Vector(std::move(p2pkh), std::move(p2sh), std::move(segwit));
77  } else {
78  return Vector(std::move(p2pkh));
79  }
80 }
81 
83 {
84  // Add script to keystore
85  keystore.AddCScript(script);
86  // Note that scripts over 520 bytes are not yet supported.
87  switch (type) {
88  case OutputType::LEGACY:
89  return ScriptHash(script);
91  case OutputType::BECH32: {
92  CTxDestination witdest = WitnessV0ScriptHash(script);
93  CScript witprog = GetScriptForDestination(witdest);
94  // Check if the resulting program is solvable (i.e. doesn't use an uncompressed key)
95  if (!IsSolvable(keystore, witprog)) return ScriptHash(script);
96  // Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours.
97  keystore.AddCScript(witprog);
98  if (type == OutputType::BECH32) {
99  return witdest;
100  } else {
101  return ScriptHash(witprog);
102  }
103  }
104  case OutputType::BECH32M: {} // This function should not be used for BECH32M, so let it assert
105  } // no default case, so the compiler can warn about missing cases
106  assert(false);
107 }
108 
109 std::optional<OutputType> OutputTypeFromDestination(const CTxDestination& dest) {
110  if (std::holds_alternative<PKHash>(dest) ||
111  std::holds_alternative<ScriptHash>(dest)) {
112  return OutputType::LEGACY;
113  }
114  if (std::holds_alternative<WitnessV0KeyHash>(dest) ||
115  std::holds_alternative<WitnessV0ScriptHash>(dest)) {
116  return OutputType::BECH32;
117  }
118  if (std::holds_alternative<WitnessV1Taproot>(dest) ||
119  std::holds_alternative<WitnessUnknown>(dest)) {
120  return OutputType::BECH32M;
121  }
122  return std::nullopt;
123 }
OutputType
OutputType
Definition: outputtype.h:18
assert
assert(!tx.IsCoinBase())
FormatOutputType
const std::string & FormatOutputType(OutputType type)
Definition: outputtype.cpp:38
OutputType::LEGACY
@ LEGACY
FillableSigningProvider
Fillable signing provider that keeps keys in an address->secret map.
Definition: signingprovider.h:91
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:310
AddAndGetDestinationForScript
CTxDestination AddAndGetDestinationForScript(FillableSigningProvider &keystore, const CScript &script, OutputType type)
Get a destination of the requested type (if possible) to the specified script.
Definition: outputtype.cpp:82
outputtype.h
OUTPUT_TYPE_STRING_LEGACY
static const std::string OUTPUT_TYPE_STRING_LEGACY
Definition: outputtype.cpp:19
GetDestinationForKey
CTxDestination GetDestinationForKey(const CPubKey &key, OutputType type)
Get a destination of the requested type (if possible) to the specified key.
Definition: outputtype.cpp:49
pubkey.h
OUTPUT_TYPE_STRING_BECH32
static const std::string OUTPUT_TYPE_STRING_BECH32
Definition: outputtype.cpp:21
WitnessV0KeyHash
Definition: standard.h:109
OutputType::BECH32M
@ BECH32M
signingprovider.h
CTxDestination
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
OutputTypeFromDestination
std::optional< OutputType > OutputTypeFromDestination(const CTxDestination &dest)
Get the OutputType for a CTxDestination.
Definition: outputtype.cpp:109
OUTPUT_TYPE_STRING_BECH32M
static const std::string OUTPUT_TYPE_STRING_BECH32M
Definition: outputtype.cpp:22
sign.h
standard.h
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
script.h
CPubKey::IsCompressed
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:192
OutputType::P2SH_SEGWIT
@ P2SH_SEGWIT
OutputType::BECH32
@ BECH32
PKHash
Definition: standard.h:79
CPubKey
An encapsulated public key.
Definition: pubkey.h:32
IsSolvable
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Definition: sign.cpp:582
Vector
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
Definition: vector.h:20
vector.h
FillableSigningProvider::AddCScript
virtual bool AddCScript(const CScript &redeemScript)
Definition: signingprovider.cpp:153
WitnessV0ScriptHash
Definition: standard.h:102
GetAllDestinationsForKey
std::vector< CTxDestination > GetAllDestinationsForKey(const CPubKey &key)
Get all destinations (potentially) supported by the wallet for the given key.
Definition: outputtype.cpp:69
OUTPUT_TYPE_STRING_P2SH_SEGWIT
static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT
Definition: outputtype.cpp:20
ScriptHash
Definition: standard.h:89
ParseOutputType
std::optional< OutputType > ParseOutputType(const std::string &type)
Definition: outputtype.cpp:24