LCOV - code coverage report
Current view: top level - src - outputtype.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 86.7 % 45 39
Test Date: 2025-06-01 06:26:32 Functions: 100.0 % 4 4
Branches: 75.6 % 41 31

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present 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                 :             : 
      13                 :             : #include <cassert>
      14                 :             : #include <optional>
      15                 :             : #include <string>
      16                 :             : 
      17                 :             : static const std::string OUTPUT_TYPE_STRING_LEGACY = "legacy";
      18                 :             : static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT = "p2sh-segwit";
      19                 :             : static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32";
      20                 :             : static const std::string OUTPUT_TYPE_STRING_BECH32M = "bech32m";
      21                 :             : static const std::string OUTPUT_TYPE_STRING_UNKNOWN = "unknown";
      22                 :             : 
      23                 :        5421 : std::optional<OutputType> ParseOutputType(const std::string& type)
      24                 :             : {
      25         [ +  + ]:        5421 :     if (type == OUTPUT_TYPE_STRING_LEGACY) {
      26                 :        3193 :         return OutputType::LEGACY;
      27         [ +  + ]:        2228 :     } else if (type == OUTPUT_TYPE_STRING_P2SH_SEGWIT) {
      28                 :         749 :         return OutputType::P2SH_SEGWIT;
      29         [ +  + ]:        1479 :     } else if (type == OUTPUT_TYPE_STRING_BECH32) {
      30                 :        1020 :         return OutputType::BECH32;
      31         [ +  + ]:         459 :     } else if (type == OUTPUT_TYPE_STRING_BECH32M) {
      32                 :         453 :         return OutputType::BECH32M;
      33                 :             :     }
      34                 :           6 :     return std::nullopt;
      35                 :             : }
      36                 :             : 
      37                 :        6937 : const std::string& FormatOutputType(OutputType type)
      38                 :             : {
      39   [ +  +  +  -  :        6937 :     switch (type) {
                   -  + ]
      40                 :             :     case OutputType::LEGACY: return OUTPUT_TYPE_STRING_LEGACY;
      41                 :        1256 :     case OutputType::P2SH_SEGWIT: return OUTPUT_TYPE_STRING_P2SH_SEGWIT;
      42                 :        3055 :     case OutputType::BECH32: return OUTPUT_TYPE_STRING_BECH32;
      43                 :        1369 :     case OutputType::BECH32M: return OUTPUT_TYPE_STRING_BECH32M;
      44                 :           0 :     case OutputType::UNKNOWN: return OUTPUT_TYPE_STRING_UNKNOWN;
      45                 :             :     } // no default case, so the compiler can warn about missing cases
      46                 :           0 :     assert(false);
      47                 :             : }
      48                 :             : 
      49                 :          79 : CTxDestination AddAndGetDestinationForScript(FlatSigningProvider& keystore, const CScript& script, OutputType type)
      50                 :             : {
      51                 :             :     // Add script to keystore
      52                 :          79 :     keystore.scripts.emplace(CScriptID(script), script);
      53                 :             : 
      54      [ +  +  - ]:          79 :     switch (type) {
      55                 :          29 :     case OutputType::LEGACY:
      56                 :          29 :         return ScriptHash(script);
      57                 :          50 :     case OutputType::P2SH_SEGWIT:
      58                 :          50 :     case OutputType::BECH32: {
      59         [ +  - ]:          50 :         CTxDestination witdest = WitnessV0ScriptHash(script);
      60         [ +  - ]:          50 :         CScript witprog = GetScriptForDestination(witdest);
      61                 :             :         // Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours.
      62   [ +  -  +  - ]:          50 :         keystore.scripts.emplace(CScriptID(witprog), witprog);
      63         [ +  + ]:          50 :         if (type == OutputType::BECH32) {
      64                 :          34 :             return witdest;
      65                 :             :         } else {
      66         [ +  - ]:          16 :             return ScriptHash(witprog);
      67                 :             :         }
      68                 :          50 :     }
      69                 :           0 :     case OutputType::BECH32M:
      70                 :           0 :     case OutputType::UNKNOWN: {} // This function should not be used for BECH32M or UNKNOWN, so let it assert
      71                 :             :     } // no default case, so the compiler can warn about missing cases
      72                 :           0 :     assert(false);
      73                 :             : }
      74                 :             : 
      75                 :         156 : std::optional<OutputType> OutputTypeFromDestination(const CTxDestination& dest) {
      76         [ +  - ]:         156 :     if (std::holds_alternative<PKHash>(dest) ||
      77         [ +  + ]:         156 :         std::holds_alternative<ScriptHash>(dest)) {
      78                 :          20 :         return OutputType::LEGACY;
      79                 :             :     }
      80         [ +  + ]:         136 :     if (std::holds_alternative<WitnessV0KeyHash>(dest) ||
      81         [ +  + ]:          12 :         std::holds_alternative<WitnessV0ScriptHash>(dest)) {
      82                 :         126 :         return OutputType::BECH32;
      83                 :             :     }
      84         [ +  + ]:          10 :     if (std::holds_alternative<WitnessV1Taproot>(dest) ||
      85         [ -  + ]:           6 :         std::holds_alternative<WitnessUnknown>(dest)) {
      86                 :          10 :         return OutputType::BECH32M;
      87                 :             :     }
      88                 :           0 :     return std::nullopt;
      89                 :             : }
        

Generated by: LCOV version 2.0-1