Bitcoin Core  21.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 <string>
17 
18 static const std::string OUTPUT_TYPE_STRING_LEGACY = "legacy";
19 static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT = "p2sh-segwit";
20 static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32";
21 
22 bool ParseOutputType(const std::string& type, OutputType& output_type)
23 {
24  if (type == OUTPUT_TYPE_STRING_LEGACY) {
25  output_type = OutputType::LEGACY;
26  return true;
27  } else if (type == OUTPUT_TYPE_STRING_P2SH_SEGWIT) {
28  output_type = OutputType::P2SH_SEGWIT;
29  return true;
30  } else if (type == OUTPUT_TYPE_STRING_BECH32) {
31  output_type = OutputType::BECH32;
32  return true;
33  }
34  return false;
35 }
36 
37 const std::string& FormatOutputType(OutputType type)
38 {
39  switch (type) {
43  } // no default case, so the compiler can warn about missing cases
44  assert(false);
45 }
46 
48 {
49  switch (type) {
50  case OutputType::LEGACY: return PKHash(key);
52  case OutputType::BECH32: {
53  if (!key.IsCompressed()) return PKHash(key);
54  CTxDestination witdest = WitnessV0KeyHash(key);
55  CScript witprog = GetScriptForDestination(witdest);
56  if (type == OutputType::P2SH_SEGWIT) {
57  return ScriptHash(witprog);
58  } else {
59  return witdest;
60  }
61  }
62  } // no default case, so the compiler can warn about missing cases
63  assert(false);
64 }
65 
66 std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key)
67 {
68  PKHash keyid(key);
69  CTxDestination p2pkh{keyid};
70  if (key.IsCompressed()) {
71  CTxDestination segwit = WitnessV0KeyHash(keyid);
73  return Vector(std::move(p2pkh), std::move(p2sh), std::move(segwit));
74  } else {
75  return Vector(std::move(p2pkh));
76  }
77 }
78 
80 {
81  // Add script to keystore
82  keystore.AddCScript(script);
83  // Note that scripts over 520 bytes are not yet supported.
84  switch (type) {
85  case OutputType::LEGACY:
86  return ScriptHash(script);
88  case OutputType::BECH32: {
89  CTxDestination witdest = WitnessV0ScriptHash(script);
90  CScript witprog = GetScriptForDestination(witdest);
91  // Check if the resulting program is solvable (i.e. doesn't use an uncompressed key)
92  if (!IsSolvable(keystore, witprog)) return ScriptHash(script);
93  // Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours.
94  keystore.AddCScript(witprog);
95  if (type == OutputType::BECH32) {
96  return witdest;
97  } else {
98  return ScriptHash(witprog);
99  }
100  }
101  } // no default case, so the compiler can warn about missing cases
102  assert(false);
103 }
assert(!tx.IsCoinBase())
virtual bool AddCScript(const CScript &redeemScript)
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:212
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:66
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
Definition: vector.h:20
static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT
Definition: outputtype.cpp:19
An encapsulated public key.
Definition: pubkey.h:31
Fillable signing provider that keeps keys in an address->secret map.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:300
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:79
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Definition: sign.cpp:437
const std::string & FormatOutputType(OutputType type)
Definition: outputtype.cpp:37
static const std::string OUTPUT_TYPE_STRING_LEGACY
Definition: outputtype.cpp:18
static const std::string OUTPUT_TYPE_STRING_BECH32
Definition: outputtype.cpp:20
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
CTxDestination GetDestinationForKey(const CPubKey &key, OutputType type)
Get a destination of the requested type (if possible) to the specified key.
Definition: outputtype.cpp:47
bool ParseOutputType(const std::string &type, OutputType &output_type)
Definition: outputtype.cpp:22
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:193