BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
outputtype.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 
46 {
47  switch (type) {
48  case OutputType::LEGACY: return key.GetID();
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);
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 
77 {
78  // Add script to keystore
79  keystore.AddCScript(script);
80  // Note that scripts over 520 bytes are not yet supported.
81  switch (type) {
82  case OutputType::LEGACY:
83  return CScriptID(script);
85  case OutputType::BECH32: {
86  CTxDestination witdest = WitnessV0ScriptHash(script);
87  CScript witprog = GetScriptForDestination(witdest);
88  // Check if the resulting program is solvable (i.e. doesn't use an uncompressed key)
89  if (!IsSolvable(keystore, witprog)) return CScriptID(script);
90  // Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours.
91  keystore.AddCScript(witprog);
92  if (type == OutputType::BECH32) {
93  return witdest;
94  } else {
95  return CScriptID(witprog);
96  }
97  }
98  default: assert(false);
99  }
100 }
101 
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:155
OutputType
Definition: outputtype.h:15
virtual bool AddCScript(const CScript &redeemScript)=0
Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki.
std::vector< CTxDestination > GetAllDestinationsForKey(const CPubKey &key)
Get all destinations (potentially) supported by the wallet for the given key.
Definition: outputtype.cpp:64
boost::variant< CNoDestination, CKeyID, CScriptID, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:123
CTxDestination AddAndGetDestinationForScript(CKeyStore &keystore, const CScript &script, OutputType type)
Get a destination of the requested type (if possible) to the specified script.
Definition: outputtype.cpp:76
An encapsulated public key.
Definition: pubkey.h:30
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:288
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Definition: sign.cpp:497
const std::string & FormatOutputType(OutputType type)
Definition: outputtype.cpp:35
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:384
A virtual base class for key stores.
Definition: keystore.h:19
A reference to a CKey: the Hash360 of its serialized public key.
Definition: pubkey.h:20
CTxDestination GetDestinationForKey(const CPubKey &key, OutputType type)
Get a destination of the requested type (if possible) to the specified key.
Definition: outputtype.cpp:45
A reference to a CScript: the Hash360 of its serialization (see script.h)
Definition: standard.h:22
bool ParseOutputType(const std::string &type, OutputType &output_type)
Definition: outputtype.cpp:20
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:180