Coverage Report

Created: 2026-05-06 07:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/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(std::string_view type)
24
5.56k
{
25
5.56k
    if (type == OUTPUT_TYPE_STRING_LEGACY) {
26
3.20k
        return OutputType::LEGACY;
27
3.20k
    } else if (type == OUTPUT_TYPE_STRING_P2SH_SEGWIT) {
28
748
        return OutputType::P2SH_SEGWIT;
29
1.61k
    } else if (type == OUTPUT_TYPE_STRING_BECH32) {
30
1.01k
        return OutputType::BECH32;
31
1.01k
    } else if (type == OUTPUT_TYPE_STRING_BECH32M) {
32
592
        return OutputType::BECH32M;
33
592
    }
34
6
    return std::nullopt;
35
5.56k
}
36
37
const std::string& FormatOutputType(OutputType type)
38
93.9k
{
39
93.9k
    switch (type) {
40
22.9k
    case OutputType::LEGACY: return OUTPUT_TYPE_STRING_LEGACY;
41
22.9k
    case OutputType::P2SH_SEGWIT: return OUTPUT_TYPE_STRING_P2SH_SEGWIT;
42
24.8k
    case OutputType::BECH32: return OUTPUT_TYPE_STRING_BECH32;
43
23.1k
    case OutputType::BECH32M: return OUTPUT_TYPE_STRING_BECH32M;
44
0
    case OutputType::UNKNOWN: return OUTPUT_TYPE_STRING_UNKNOWN;
45
93.9k
    } // no default case, so the compiler can warn about missing cases
46
93.9k
    assert(false);
47
0
}
48
49
std::string FormatAllOutputTypes()
50
21.4k
{
51
85.9k
    return util::Join(OUTPUT_TYPES, ", ", [](const auto& i) { return "\"" + FormatOutputType(i) + "\""; });
52
21.4k
}
53
54
CTxDestination AddAndGetDestinationForScript(FlatSigningProvider& keystore, const CScript& script, OutputType type)
55
79
{
56
    // Add script to keystore
57
79
    keystore.scripts.emplace(CScriptID(script), script);
58
59
79
    switch (type) {
60
29
    case OutputType::LEGACY:
61
29
        return ScriptHash(script);
62
16
    case OutputType::P2SH_SEGWIT:
63
50
    case OutputType::BECH32: {
64
50
        CTxDestination witdest = WitnessV0ScriptHash(script);
65
50
        CScript witprog = GetScriptForDestination(witdest);
66
        // Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours.
67
50
        keystore.scripts.emplace(CScriptID(witprog), witprog);
68
50
        if (type == OutputType::BECH32) {
69
34
            return witdest;
70
34
        } else {
71
16
            return ScriptHash(witprog);
72
16
        }
73
50
    }
74
0
    case OutputType::BECH32M:
75
0
    case OutputType::UNKNOWN: {} // This function should not be used for BECH32M or UNKNOWN, so let it assert
76
79
    } // no default case, so the compiler can warn about missing cases
77
79
    assert(false);
78
0
}
79
80
162
std::optional<OutputType> OutputTypeFromDestination(const CTxDestination& dest) {
81
162
    if (std::holds_alternative<PKHash>(dest) ||
82
162
        std::holds_alternative<ScriptHash>(dest)) {
83
20
        return OutputType::LEGACY;
84
20
    }
85
142
    if (std::holds_alternative<WitnessV0KeyHash>(dest) ||
86
142
        std::holds_alternative<WitnessV0ScriptHash>(dest)) {
87
126
        return OutputType::BECH32;
88
126
    }
89
16
    if (std::holds_alternative<WitnessV1Taproot>(dest) ||
90
16
        std::holds_alternative<WitnessUnknown>(dest)) {
91
10
        return OutputType::BECH32M;
92
10
    }
93
6
    return std::nullopt;
94
16
}