Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/wallet/walletutil.cpp
Line
Count
Source
1
// Copyright (c) 2017-2022 The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <wallet/walletutil.h>
6
7
#include <chainparams.h>
8
#include <common/args.h>
9
#include <key_io.h>
10
#include <logging.h>
11
12
namespace wallet {
13
fs::path GetWalletDir()
14
33.2k
{
15
33.2k
    fs::path path;
16
17
33.2k
    if (gArgs.IsArgSet("-walletdir")) {
  Branch (17:9): [True: 0, False: 33.2k]
18
0
        path = gArgs.GetPathArg("-walletdir");
19
0
        if (!fs::is_directory(path)) {
  Branch (19:13): [True: 0, False: 0]
20
            // If the path specified doesn't exist, we return the deliberately
21
            // invalid empty string.
22
0
            path = "";
23
0
        }
24
33.2k
    } else {
25
33.2k
        path = gArgs.GetDataDirNet();
26
        // If a wallets directory exists, use that, otherwise default to GetDataDir
27
33.2k
        if (fs::is_directory(path / "wallets")) {
  Branch (27:13): [True: 33.2k, False: 0]
28
33.2k
            path /= "wallets";
29
33.2k
        }
30
33.2k
    }
31
32
33.2k
    return path;
33
33.2k
}
34
35
bool IsFeatureSupported(int wallet_version, int feature_version)
36
0
{
37
0
    return wallet_version >= feature_version;
38
0
}
39
40
WalletFeature GetClosestWalletFeature(int version)
41
0
{
42
0
    static constexpr std::array wallet_features{FEATURE_LATEST, FEATURE_PRE_SPLIT_KEYPOOL, FEATURE_NO_DEFAULT_KEY, FEATURE_HD_SPLIT, FEATURE_HD, FEATURE_COMPRPUBKEY, FEATURE_WALLETCRYPT, FEATURE_BASE};
43
0
    for (const WalletFeature& wf : wallet_features) {
  Branch (43:34): [True: 0, False: 0]
44
0
        if (version >= wf) return wf;
  Branch (44:13): [True: 0, False: 0]
45
0
    }
46
0
    return static_cast<WalletFeature>(0);
47
0
}
48
49
WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& addr_type, bool internal)
50
88.7k
{
51
88.7k
    int64_t creation_time = GetTime();
52
53
88.7k
    std::string xpub = EncodeExtPubKey(master_key);
54
55
    // Build descriptor string
56
88.7k
    std::string desc_prefix;
57
88.7k
    std::string desc_suffix = "/*)";
58
88.7k
    switch (addr_type) {
  Branch (58:13): [True: 0, False: 88.7k]
59
22.1k
    case OutputType::LEGACY: {
  Branch (59:5): [True: 22.1k, False: 66.5k]
60
22.1k
        desc_prefix = "pkh(" + xpub + "/44h";
61
22.1k
        break;
62
0
    }
63
22.1k
    case OutputType::P2SH_SEGWIT: {
  Branch (63:5): [True: 22.1k, False: 66.5k]
64
22.1k
        desc_prefix = "sh(wpkh(" + xpub + "/49h";
65
22.1k
        desc_suffix += ")";
66
22.1k
        break;
67
0
    }
68
22.1k
    case OutputType::BECH32: {
  Branch (68:5): [True: 22.1k, False: 66.5k]
69
22.1k
        desc_prefix = "wpkh(" + xpub + "/84h";
70
22.1k
        break;
71
0
    }
72
22.1k
    case OutputType::BECH32M: {
  Branch (72:5): [True: 22.1k, False: 66.5k]
73
22.1k
        desc_prefix = "tr(" + xpub + "/86h";
74
22.1k
        break;
75
0
    }
76
0
    case OutputType::UNKNOWN: {
  Branch (76:5): [True: 0, False: 88.7k]
77
        // We should never have a DescriptorScriptPubKeyMan for an UNKNOWN OutputType,
78
        // so if we get to this point something is wrong
79
0
        assert(false);
  Branch (79:9): [Folded - Ignored]
80
0
    }
81
88.7k
    } // no default case, so the compiler can warn about missing cases
82
88.7k
    assert(!desc_prefix.empty());
  Branch (82:5): [True: 88.7k, False: 0]
83
84
    // Mainnet derives at 0', testnet and regtest derive at 1'
85
88.7k
    if (Params().IsTestChain()) {
  Branch (85:9): [True: 88.7k, False: 0]
86
88.7k
        desc_prefix += "/1h";
87
88.7k
    } else {
88
0
        desc_prefix += "/0h";
89
0
    }
90
91
88.7k
    std::string internal_path = internal ? "/1" : "/0";
  Branch (91:33): [True: 44.3k, False: 44.3k]
92
88.7k
    std::string desc_str = desc_prefix + "/0h" + internal_path + desc_suffix;
93
94
    // Make the descriptor
95
88.7k
    FlatSigningProvider keys;
96
88.7k
    std::string error;
97
88.7k
    std::vector<std::unique_ptr<Descriptor>> desc = Parse(desc_str, keys, error, false);
98
88.7k
    WalletDescriptor w_desc(std::move(desc.at(0)), creation_time, 0, 0, 0);
99
88.7k
    return w_desc;
100
88.7k
}
101
102
} // namespace wallet