Branch data Line data Source code
# 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 : 2359 : { # 24 [ + + ]: 2359 : if (type == OUTPUT_TYPE_STRING_LEGACY) { # 25 : 471 : output_type = OutputType::LEGACY; # 26 : 471 : return true; # 27 [ + + ]: 1888 : } else if (type == OUTPUT_TYPE_STRING_P2SH_SEGWIT) { # 28 : 465 : output_type = OutputType::P2SH_SEGWIT; # 29 : 465 : return true; # 30 [ + + ]: 1423 : } else if (type == OUTPUT_TYPE_STRING_BECH32) { # 31 : 1413 : output_type = OutputType::BECH32; # 32 : 1413 : return true; # 33 : 1413 : } # 34 : 10 : return false; # 35 : 10 : } # 36 : : # 37 : : const std::string& FormatOutputType(OutputType type) # 38 : 1536 : { # 39 [ - + ]: 1536 : switch (type) { # 40 [ - + ]: 0 : case OutputType::LEGACY: return OUTPUT_TYPE_STRING_LEGACY; # 41 [ - + ]: 0 : case OutputType::P2SH_SEGWIT: return OUTPUT_TYPE_STRING_P2SH_SEGWIT; # 42 [ + - ]: 1536 : case OutputType::BECH32: return OUTPUT_TYPE_STRING_BECH32; # 43 : 0 : } // no default case, so the compiler can warn about missing cases # 44 : 0 : assert(false); # 45 : 0 : } # 46 : : # 47 : : CTxDestination GetDestinationForKey(const CPubKey& key, OutputType type) # 48 : 13239 : { # 49 [ - + ]: 13239 : switch (type) { # 50 [ + + ]: 895 : case OutputType::LEGACY: return PKHash(key); # 51 [ + + ]: 261 : case OutputType::P2SH_SEGWIT: # 52 [ + + ]: 12344 : case OutputType::BECH32: { # 53 [ + + ]: 12344 : if (!key.IsCompressed()) return PKHash(key); # 54 : 12340 : CTxDestination witdest = WitnessV0KeyHash(key); # 55 : 12340 : CScript witprog = GetScriptForDestination(witdest); # 56 [ + + ]: 12340 : if (type == OutputType::P2SH_SEGWIT) { # 57 : 261 : return ScriptHash(witprog); # 58 : 12079 : } else { # 59 : 12079 : return witdest; # 60 : 12079 : } # 61 : 0 : } # 62 : 0 : } // no default case, so the compiler can warn about missing cases # 63 : 0 : assert(false); # 64 : 0 : } # 65 : : # 66 : : std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key) # 67 : 1491 : { # 68 : 1491 : PKHash keyid(key); # 69 : 1491 : CTxDestination p2pkh{keyid}; # 70 [ + + ]: 1491 : if (key.IsCompressed()) { # 71 : 1486 : CTxDestination segwit = WitnessV0KeyHash(keyid); # 72 : 1486 : CTxDestination p2sh = ScriptHash(GetScriptForDestination(segwit)); # 73 : 1486 : return Vector(std::move(p2pkh), std::move(p2sh), std::move(segwit)); # 74 : 1486 : } else { # 75 : 5 : return Vector(std::move(p2pkh)); # 76 : 5 : } # 77 : 1491 : } # 78 : : # 79 : : CTxDestination AddAndGetDestinationForScript(FillableSigningProvider& keystore, const CScript& script, OutputType type) # 80 : 216 : { # 81 : : // Add script to keystore # 82 : 216 : keystore.AddCScript(script); # 83 : : // Note that scripts over 520 bytes are not yet supported. # 84 [ - + ]: 216 : switch (type) { # 85 [ + + ]: 133 : case OutputType::LEGACY: # 86 : 133 : return ScriptHash(script); # 87 [ + + ]: 43 : case OutputType::P2SH_SEGWIT: # 88 [ + + ]: 83 : case OutputType::BECH32: { # 89 : 83 : CTxDestination witdest = WitnessV0ScriptHash(script); # 90 : 83 : CScript witprog = GetScriptForDestination(witdest); # 91 : : // Check if the resulting program is solvable (i.e. doesn't use an uncompressed key) # 92 [ - + ]: 83 : if (!IsSolvable(keystore, witprog)) return ScriptHash(script); # 93 : : // Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours. # 94 : 83 : keystore.AddCScript(witprog); # 95 [ + + ]: 83 : if (type == OutputType::BECH32) { # 96 : 40 : return witdest; # 97 : 43 : } else { # 98 : 43 : return ScriptHash(witprog); # 99 : 43 : } # 100 : 0 : } # 101 : 0 : } // no default case, so the compiler can warn about missing cases # 102 : 0 : assert(false); # 103 : 0 : }