LCOV - code coverage report
Current view: top level - src - outputtype.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 53 58 91.4 %
Date: 2020-09-26 01:30:44 Functions: 8 8 100.0 %

          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         650 : static const std::string OUTPUT_TYPE_STRING_LEGACY = "legacy";
      19         650 : static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT = "p2sh-segwit";
      20         650 : static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32";
      21             : 
      22             : const std::array<OutputType, 3> OUTPUT_TYPES = {OutputType::LEGACY, OutputType::P2SH_SEGWIT, OutputType::BECH32};
      23             : 
      24        1679 : bool ParseOutputType(const std::string& type, OutputType& output_type)
      25             : {
      26        1679 :     if (type == OUTPUT_TYPE_STRING_LEGACY) {
      27         388 :         output_type = OutputType::LEGACY;
      28         388 :         return true;
      29        1291 :     } else if (type == OUTPUT_TYPE_STRING_P2SH_SEGWIT) {
      30         370 :         output_type = OutputType::P2SH_SEGWIT;
      31         370 :         return true;
      32         921 :     } else if (type == OUTPUT_TYPE_STRING_BECH32) {
      33         916 :         output_type = OutputType::BECH32;
      34         916 :         return true;
      35             :     }
      36           5 :     return false;
      37        1679 : }
      38             : 
      39        1010 : const std::string& FormatOutputType(OutputType type)
      40             : {
      41        1010 :     switch (type) {
      42           0 :     case OutputType::LEGACY: return OUTPUT_TYPE_STRING_LEGACY;
      43           0 :     case OutputType::P2SH_SEGWIT: return OUTPUT_TYPE_STRING_P2SH_SEGWIT;
      44        1010 :     case OutputType::BECH32: return OUTPUT_TYPE_STRING_BECH32;
      45             :     } // no default case, so the compiler can warn about missing cases
      46           0 :     assert(false);
      47        1010 : }
      48             : 
      49       10822 : CTxDestination GetDestinationForKey(const CPubKey& key, OutputType type)
      50             : {
      51       10822 :     switch (type) {
      52         864 :     case OutputType::LEGACY: return PKHash(key);
      53             :     case OutputType::P2SH_SEGWIT:
      54             :     case OutputType::BECH32: {
      55        9958 :         if (!key.IsCompressed()) return PKHash(key);
      56        9951 :         CTxDestination witdest = WitnessV0KeyHash(key);
      57        9951 :         CScript witprog = GetScriptForDestination(witdest);
      58        9951 :         if (type == OutputType::P2SH_SEGWIT) {
      59         243 :             return ScriptHash(witprog);
      60             :         } else {
      61        9708 :             return witdest;
      62             :         }
      63        9951 :     }
      64             :     } // no default case, so the compiler can warn about missing cases
      65           0 :     assert(false);
      66       10822 : }
      67             : 
      68        1592 : std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key)
      69             : {
      70        1592 :     PKHash keyid(key);
      71        1592 :     CTxDestination p2pkh{keyid};
      72        1592 :     if (key.IsCompressed()) {
      73        1587 :         CTxDestination segwit = WitnessV0KeyHash(keyid);
      74        1587 :         CTxDestination p2sh = ScriptHash(GetScriptForDestination(segwit));
      75        1587 :         return Vector(std::move(p2pkh), std::move(p2sh), std::move(segwit));
      76        1587 :     } else {
      77           5 :         return Vector(std::move(p2pkh));
      78             :     }
      79        1592 : }
      80             : 
      81         210 : CTxDestination AddAndGetDestinationForScript(FillableSigningProvider& keystore, const CScript& script, OutputType type)
      82             : {
      83             :     // Add script to keystore
      84         210 :     keystore.AddCScript(script);
      85             :     // Note that scripts over 520 bytes are not yet supported.
      86         210 :     switch (type) {
      87             :     case OutputType::LEGACY:
      88         127 :         return ScriptHash(script);
      89             :     case OutputType::P2SH_SEGWIT:
      90             :     case OutputType::BECH32: {
      91          83 :         CTxDestination witdest = WitnessV0ScriptHash(script);
      92          83 :         CScript witprog = GetScriptForDestination(witdest);
      93             :         // Check if the resulting program is solvable (i.e. doesn't use an uncompressed key)
      94          83 :         if (!IsSolvable(keystore, witprog)) return ScriptHash(script);
      95             :         // Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours.
      96          83 :         keystore.AddCScript(witprog);
      97          83 :         if (type == OutputType::BECH32) {
      98          42 :             return witdest;
      99             :         } else {
     100          41 :             return ScriptHash(witprog);
     101             :         }
     102          83 :     }
     103             :     } // no default case, so the compiler can warn about missing cases
     104           0 :     assert(false);
     105         210 : }

Generated by: LCOV version 1.15