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