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