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
ParseOutputType(const std::string & type,OutputType & output_type)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
FormatOutputType(OutputType type)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
GetDestinationForKey(const CPubKey & key,OutputType type)45 CTxDestination GetDestinationForKey(const CPubKey& key, OutputType type)
46 {
47 switch (type) {
48 case OutputType::LEGACY: return key.GetID();
49 case OutputType::P2SH_SEGWIT:
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
GetAllDestinationsForKey(const CPubKey & key)64 std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key)
65 {
66 CKeyID keyid = key.GetID();
67 if (key.IsCompressed()) {
68 CTxDestination segwit = WitnessV0KeyHash(keyid);
69 CTxDestination p2sh = CScriptID(GetScriptForDestination(segwit));
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
AddAndGetDestinationForScript(CKeyStore & keystore,const CScript & script,OutputType type)76 CTxDestination AddAndGetDestinationForScript(CKeyStore& keystore, const CScript& script, OutputType type)
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);
84 case OutputType::P2SH_SEGWIT:
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
102