LCOV - code coverage report
Current view: top level - src - outputtype.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 87.1 % 70 61
Test Date: 2024-07-07 05:05:19 Functions: 100.0 % 6 6
Branches: 72.1 % 68 49

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2022 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 <util/vector.h>
      13                 :             : 
      14                 :             : #include <assert.h>
      15                 :             : #include <optional>
      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                 :             : static const std::string OUTPUT_TYPE_STRING_BECH32M = "bech32m";
      22                 :             : static const std::string OUTPUT_TYPE_STRING_UNKNOWN = "unknown";
      23                 :             : 
      24                 :       11097 : std::optional<OutputType> ParseOutputType(const std::string& type)
      25                 :             : {
      26         [ +  + ]:       11097 :     if (type == OUTPUT_TYPE_STRING_LEGACY) {
      27                 :        6387 :         return OutputType::LEGACY;
      28         [ +  + ]:        4710 :     } else if (type == OUTPUT_TYPE_STRING_P2SH_SEGWIT) {
      29                 :        2477 :         return OutputType::P2SH_SEGWIT;
      30         [ +  + ]:        2233 :     } else if (type == OUTPUT_TYPE_STRING_BECH32) {
      31                 :        1771 :         return OutputType::BECH32;
      32         [ +  + ]:         462 :     } else if (type == OUTPUT_TYPE_STRING_BECH32M) {
      33                 :         452 :         return OutputType::BECH32M;
      34                 :             :     }
      35                 :          10 :     return std::nullopt;
      36                 :             : }
      37                 :             : 
      38                 :        6737 : const std::string& FormatOutputType(OutputType type)
      39                 :             : {
      40   [ +  +  +  -  :        6737 :     switch (type) {
                   -  + ]
      41                 :             :     case OutputType::LEGACY: return OUTPUT_TYPE_STRING_LEGACY;
      42                 :        1184 :     case OutputType::P2SH_SEGWIT: return OUTPUT_TYPE_STRING_P2SH_SEGWIT;
      43                 :        3071 :     case OutputType::BECH32: return OUTPUT_TYPE_STRING_BECH32;
      44                 :        1297 :     case OutputType::BECH32M: return OUTPUT_TYPE_STRING_BECH32M;
      45                 :           0 :     case OutputType::UNKNOWN: return OUTPUT_TYPE_STRING_UNKNOWN;
      46                 :             :     } // no default case, so the compiler can warn about missing cases
      47                 :           0 :     assert(false);
      48                 :             : }
      49                 :             : 
      50                 :       15002 : CTxDestination GetDestinationForKey(const CPubKey& key, OutputType type)
      51                 :             : {
      52      [ +  +  - ]:       15002 :     switch (type) {
      53                 :        4707 :     case OutputType::LEGACY: return PKHash(key);
      54                 :       10295 :     case OutputType::P2SH_SEGWIT:
      55                 :       10295 :     case OutputType::BECH32: {
      56         [ -  + ]:       10295 :         if (!key.IsCompressed()) return PKHash(key);
      57         [ +  - ]:       10295 :         CTxDestination witdest = WitnessV0KeyHash(key);
      58         [ +  - ]:       10295 :         CScript witprog = GetScriptForDestination(witdest);
      59         [ +  + ]:       10295 :         if (type == OutputType::P2SH_SEGWIT) {
      60         [ +  - ]:        2465 :             return ScriptHash(witprog);
      61                 :             :         } else {
      62                 :        7830 :             return witdest;
      63                 :             :         }
      64                 :       10295 :     }
      65                 :           0 :     case OutputType::BECH32M:
      66                 :           0 :     case OutputType::UNKNOWN: {} // This function should never be used with BECH32M or UNKNOWN, so let it assert
      67                 :             :     } // no default case, so the compiler can warn about missing cases
      68                 :           0 :     assert(false);
      69                 :             : }
      70                 :             : 
      71                 :        1646 : std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key)
      72                 :             : {
      73                 :        1646 :     PKHash keyid(key);
      74         [ +  + ]:        1646 :     CTxDestination p2pkh{keyid};
      75         [ +  + ]:        1646 :     if (key.IsCompressed()) {
      76         [ +  - ]:        1638 :         CTxDestination segwit = WitnessV0KeyHash(keyid);
      77   [ +  -  +  - ]:        1638 :         CTxDestination p2sh = ScriptHash(GetScriptForDestination(segwit));
      78         [ +  - ]:        1638 :         return Vector(std::move(p2pkh), std::move(p2sh), std::move(segwit));
      79                 :        1638 :     } else {
      80         [ +  - ]:           8 :         return Vector(std::move(p2pkh));
      81                 :             :     }
      82                 :        1646 : }
      83                 :             : 
      84                 :         205 : CTxDestination AddAndGetDestinationForScript(FlatSigningProvider& keystore, const CScript& script, OutputType type)
      85                 :             : {
      86                 :             :     // Add script to keystore
      87                 :         205 :     keystore.scripts.emplace(CScriptID(script), script);
      88                 :             : 
      89      [ +  +  - ]:         205 :     switch (type) {
      90                 :          93 :     case OutputType::LEGACY:
      91                 :          93 :         return ScriptHash(script);
      92                 :         112 :     case OutputType::P2SH_SEGWIT:
      93                 :         112 :     case OutputType::BECH32: {
      94         [ +  - ]:         112 :         CTxDestination witdest = WitnessV0ScriptHash(script);
      95         [ +  - ]:         112 :         CScript witprog = GetScriptForDestination(witdest);
      96                 :             :         // Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours.
      97   [ +  -  +  - ]:         112 :         keystore.scripts.emplace(CScriptID(witprog), witprog);
      98         [ +  + ]:         112 :         if (type == OutputType::BECH32) {
      99                 :          68 :             return witdest;
     100                 :             :         } else {
     101         [ +  - ]:          44 :             return ScriptHash(witprog);
     102                 :             :         }
     103                 :         112 :     }
     104                 :           0 :     case OutputType::BECH32M:
     105                 :           0 :     case OutputType::UNKNOWN: {} // This function should not be used for BECH32M or UNKNOWN, so let it assert
     106                 :             :     } // no default case, so the compiler can warn about missing cases
     107                 :           0 :     assert(false);
     108                 :             : }
     109                 :             : 
     110                 :         346 : std::optional<OutputType> OutputTypeFromDestination(const CTxDestination& dest) {
     111         [ +  + ]:         346 :     if (std::holds_alternative<PKHash>(dest) ||
     112         [ +  + ]:         302 :         std::holds_alternative<ScriptHash>(dest)) {
     113                 :         129 :         return OutputType::LEGACY;
     114                 :             :     }
     115         [ +  + ]:         217 :     if (std::holds_alternative<WitnessV0KeyHash>(dest) ||
     116         [ +  + ]:          24 :         std::holds_alternative<WitnessV0ScriptHash>(dest)) {
     117                 :         203 :         return OutputType::BECH32;
     118                 :             :     }
     119         [ +  + ]:          14 :     if (std::holds_alternative<WitnessV1Taproot>(dest) ||
     120         [ -  + ]:           6 :         std::holds_alternative<WitnessUnknown>(dest)) {
     121                 :          14 :         return OutputType::BECH32M;
     122                 :             :     }
     123                 :           0 :     return std::nullopt;
     124                 :             : }
        

Generated by: LCOV version 2.0-1