Coverage Report

Created: 2026-09-14 20:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/wallet/walletutil.cpp
Line
Count
Source
1
// Copyright (c) 2017-present 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 <util/log.h>
11
12
namespace wallet {
13
fs::path GetWalletDir()
14
2.36k
{
15
2.36k
    fs::path path;
16
17
2.36k
    if (gArgs.IsArgSet("-walletdir")) {
18
19
        path = gArgs.GetPathArg("-walletdir");
19
19
        if (!fs::is_directory(path)) {
20
            // If the path specified doesn't exist, we return the deliberately
21
            // invalid empty string.
22
0
            path = "";
23
0
        }
24
2.34k
    } else {
25
2.34k
        path = gArgs.GetDataDirNet();
26
        // If a wallets directory exists, use that, otherwise default to GetDataDir
27
2.34k
        if (fs::is_directory(path / "wallets")) {
28
1.72k
            path /= "wallets";
29
1.72k
        }
30
2.34k
    }
31
32
2.36k
    return path;
33
2.36k
}
34
35
WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& addr_type, bool internal)
36
4.04k
{
37
4.04k
    int64_t creation_time = GetTime();
38
39
4.04k
    std::string xpub = EncodeExtPubKey(master_key);
40
41
    // Build descriptor string
42
4.04k
    std::string desc_prefix;
43
4.04k
    std::string desc_suffix = "/*)";
44
4.04k
    switch (addr_type) {
45
1.00k
    case OutputType::LEGACY: {
46
1.00k
        desc_prefix = "pkh(" + xpub + "/44h";
47
1.00k
        break;
48
0
    }
49
1.00k
    case OutputType::P2SH_SEGWIT: {
50
1.00k
        desc_prefix = "sh(wpkh(" + xpub + "/49h";
51
1.00k
        desc_suffix += ")";
52
1.00k
        break;
53
0
    }
54
1.00k
    case OutputType::BECH32: {
55
1.00k
        desc_prefix = "wpkh(" + xpub + "/84h";
56
1.00k
        break;
57
0
    }
58
1.02k
    case OutputType::BECH32M: {
59
1.02k
        desc_prefix = "tr(" + xpub + "/86h";
60
1.02k
        break;
61
0
    }
62
0
    case OutputType::UNKNOWN: {
63
        // We should never have a DescriptorScriptPubKeyMan for an UNKNOWN OutputType,
64
        // so if we get to this point something is wrong
65
0
        assert(false);
66
0
    }
67
4.04k
    } // no default case, so the compiler can warn about missing cases
68
4.04k
    assert(!desc_prefix.empty());
69
70
    // Mainnet derives at 0', testnet and regtest derive at 1'
71
4.04k
    if (Params().IsTestChain()) {
72
3.80k
        desc_prefix += "/1h";
73
3.80k
    } else {
74
240
        desc_prefix += "/0h";
75
240
    }
76
77
4.04k
    std::string internal_path = internal ? "/1" : "/0";
78
4.04k
    std::string desc_str = desc_prefix + "/0h" + internal_path + desc_suffix;
79
80
    // Make the descriptor
81
4.04k
    FlatSigningProvider keys;
82
4.04k
    std::string error;
83
4.04k
    std::vector<std::unique_ptr<Descriptor>> desc = Parse(desc_str, keys, error, false);
84
4.04k
    WalletDescriptor w_desc(std::move(desc.at(0)), creation_time, 0, 0, 0);
85
4.04k
    return w_desc;
86
4.04k
}
87
88
void WalletDescriptor::UpdateFrom(const WalletDescriptor& other)
89
21
{
90
21
    if (descriptor->ToCanonicalString() != other.descriptor->ToCanonicalString()) {
91
0
        return;
92
0
    }
93
21
    range_start = other.range_start;
94
21
    next_index = other.next_index;
95
21
    range_end = other.range_end;
96
21
    creation_time = other.creation_time;
97
21
    cache = other.cache;
98
21
}
99
100
} // namespace wallet