Coverage Report

Created: 2026-04-29 19:21

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 <logging.h>
11
12
namespace wallet {
13
fs::path GetWalletDir()
14
2.18k
{
15
2.18k
    fs::path path;
16
17
2.18k
    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.16k
    } else {
25
2.16k
        path = gArgs.GetDataDirNet();
26
        // If a wallets directory exists, use that, otherwise default to GetDataDir
27
2.16k
        if (fs::is_directory(path / "wallets")) {
28
1.55k
            path /= "wallets";
29
1.55k
        }
30
2.16k
    }
31
32
2.18k
    return path;
33
2.18k
}
34
35
WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& addr_type, bool internal)
36
3.65k
{
37
3.65k
    int64_t creation_time = GetTime();
38
39
3.65k
    std::string xpub = EncodeExtPubKey(master_key);
40
41
    // Build descriptor string
42
3.65k
    std::string desc_prefix;
43
3.65k
    std::string desc_suffix = "/*)";
44
3.65k
    switch (addr_type) {
45
908
    case OutputType::LEGACY: {
46
908
        desc_prefix = "pkh(" + xpub + "/44h";
47
908
        break;
48
0
    }
49
908
    case OutputType::P2SH_SEGWIT: {
50
908
        desc_prefix = "sh(wpkh(" + xpub + "/49h";
51
908
        desc_suffix += ")";
52
908
        break;
53
0
    }
54
912
    case OutputType::BECH32: {
55
912
        desc_prefix = "wpkh(" + xpub + "/84h";
56
912
        break;
57
0
    }
58
926
    case OutputType::BECH32M: {
59
926
        desc_prefix = "tr(" + xpub + "/86h";
60
926
        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
3.65k
    } // no default case, so the compiler can warn about missing cases
68
3.65k
    assert(!desc_prefix.empty());
69
70
    // Mainnet derives at 0', testnet and regtest derive at 1'
71
3.65k
    if (Params().IsTestChain()) {
72
3.45k
        desc_prefix += "/1h";
73
3.45k
    } else {
74
200
        desc_prefix += "/0h";
75
200
    }
76
77
3.65k
    std::string internal_path = internal ? "/1" : "/0";
78
3.65k
    std::string desc_str = desc_prefix + "/0h" + internal_path + desc_suffix;
79
80
    // Make the descriptor
81
3.65k
    FlatSigningProvider keys;
82
3.65k
    std::string error;
83
3.65k
    std::vector<std::unique_ptr<Descriptor>> desc = Parse(desc_str, keys, error, false);
84
3.65k
    WalletDescriptor w_desc(std::move(desc.at(0)), creation_time, 0, 0, 0);
85
3.65k
    return w_desc;
86
3.65k
}
87
88
} // namespace wallet