Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/outputtype.cpp
Line
Count
Source
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
std::optional<OutputType> ParseOutputType(const std::string& type)
24
0
{
25
0
    if (type == OUTPUT_TYPE_STRING_LEGACY) {
  Branch (25:9): [True: 0, False: 0]
26
0
        return OutputType::LEGACY;
27
0
    } else if (type == OUTPUT_TYPE_STRING_P2SH_SEGWIT) {
  Branch (27:16): [True: 0, False: 0]
28
0
        return OutputType::P2SH_SEGWIT;
29
0
    } else if (type == OUTPUT_TYPE_STRING_BECH32) {
  Branch (29:16): [True: 0, False: 0]
30
0
        return OutputType::BECH32;
31
0
    } else if (type == OUTPUT_TYPE_STRING_BECH32M) {
  Branch (31:16): [True: 0, False: 0]
32
0
        return OutputType::BECH32M;
33
0
    }
34
0
    return std::nullopt;
35
0
}
36
37
const std::string& FormatOutputType(OutputType type)
38
99.8k
{
39
99.8k
    switch (type) {
  Branch (39:13): [True: 0, False: 99.8k]
40
22.1k
    case OutputType::LEGACY: return OUTPUT_TYPE_STRING_LEGACY;
  Branch (40:5): [True: 22.1k, False: 77.6k]
41
22.1k
    case OutputType::P2SH_SEGWIT: return OUTPUT_TYPE_STRING_P2SH_SEGWIT;
  Branch (41:5): [True: 22.1k, False: 77.6k]
42
33.2k
    case OutputType::BECH32: return OUTPUT_TYPE_STRING_BECH32;
  Branch (42:5): [True: 33.2k, False: 66.5k]
43
22.1k
    case OutputType::BECH32M: return OUTPUT_TYPE_STRING_BECH32M;
  Branch (43:5): [True: 22.1k, False: 77.6k]
44
0
    case OutputType::UNKNOWN: return OUTPUT_TYPE_STRING_UNKNOWN;
  Branch (44:5): [True: 0, False: 99.8k]
45
99.8k
    } // no default case, so the compiler can warn about missing cases
46
99.8k
    assert(false);
  Branch (46:5): [Folded - Ignored]
47
0
}
48
49
CTxDestination AddAndGetDestinationForScript(FlatSigningProvider& keystore, const CScript& script, OutputType type)
50
0
{
51
    // Add script to keystore
52
0
    keystore.scripts.emplace(CScriptID(script), script);
53
54
0
    switch (type) {
  Branch (54:13): [True: 0, False: 0]
55
0
    case OutputType::LEGACY:
  Branch (55:5): [True: 0, False: 0]
56
0
        return ScriptHash(script);
57
0
    case OutputType::P2SH_SEGWIT:
  Branch (57:5): [True: 0, False: 0]
58
0
    case OutputType::BECH32: {
  Branch (58:5): [True: 0, False: 0]
59
0
        CTxDestination witdest = WitnessV0ScriptHash(script);
60
0
        CScript witprog = GetScriptForDestination(witdest);
61
        // Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours.
62
0
        keystore.scripts.emplace(CScriptID(witprog), witprog);
63
0
        if (type == OutputType::BECH32) {
  Branch (63:13): [True: 0, False: 0]
64
0
            return witdest;
65
0
        } else {
66
0
            return ScriptHash(witprog);
67
0
        }
68
0
    }
69
0
    case OutputType::BECH32M:
  Branch (69:5): [True: 0, False: 0]
70
0
    case OutputType::UNKNOWN: {} // This function should not be used for BECH32M or UNKNOWN, so let it assert
  Branch (70:5): [True: 0, False: 0]
71
0
    } // no default case, so the compiler can warn about missing cases
72
0
    assert(false);
  Branch (72:5): [Folded - Ignored]
73
0
}
74
75
0
std::optional<OutputType> OutputTypeFromDestination(const CTxDestination& dest) {
76
0
    if (std::holds_alternative<PKHash>(dest) ||
  Branch (76:9): [True: 0, False: 0]
77
0
        std::holds_alternative<ScriptHash>(dest)) {
  Branch (77:9): [True: 0, False: 0]
78
0
        return OutputType::LEGACY;
79
0
    }
80
0
    if (std::holds_alternative<WitnessV0KeyHash>(dest) ||
  Branch (80:9): [True: 0, False: 0]
81
0
        std::holds_alternative<WitnessV0ScriptHash>(dest)) {
  Branch (81:9): [True: 0, False: 0]
82
0
        return OutputType::BECH32;
83
0
    }
84
0
    if (std::holds_alternative<WitnessV1Taproot>(dest) ||
  Branch (84:9): [True: 0, False: 0]
85
0
        std::holds_alternative<WitnessUnknown>(dest)) {
  Branch (85:9): [True: 0, False: 0]
86
0
        return OutputType::BECH32M;
87
0
    }
88
0
    return std::nullopt;
89
0
}