Branch data Line data Source code
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/check.h>
11 : : #include <util/log.h>
12 : :
13 : : namespace wallet {
14 : 24 : fs::path GetWalletDir()
15 : : {
16 : 24 : fs::path path;
17 : :
18 [ + - + - : 24 : if (gArgs.IsArgSet("-walletdir")) {
- + ]
19 [ # # # # ]: 0 : path = gArgs.GetPathArg("-walletdir");
20 [ # # # # ]: 0 : if (!fs::is_directory(path)) {
21 : : // If the path specified doesn't exist, we return the deliberately
22 : : // invalid empty string.
23 [ # # ]: 0 : path = "";
24 : : }
25 : : } else {
26 [ + - ]: 48 : path = gArgs.GetDataDirNet();
27 : : // If a wallets directory exists, use that, otherwise default to GetDataDir
28 [ + - + - : 96 : if (fs::is_directory(path / "wallets")) {
+ - + + ]
29 [ + - ]: 8 : path /= "wallets";
30 : : }
31 : : }
32 : :
33 : 24 : return path;
34 : 0 : }
35 : :
36 : 392 : WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& addr_type, bool internal)
37 : : {
38 : 392 : int64_t creation_time = GetTime();
39 : :
40 : 392 : std::string xpub = EncodeExtPubKey(master_key);
41 : :
42 : : // Build descriptor string
43 [ + - ]: 392 : std::string desc_prefix;
44 [ + - ]: 392 : std::string desc_suffix = "/*)";
45 [ + + + + : 392 : switch (addr_type) {
- - ]
46 : 98 : case OutputType::LEGACY: {
47 [ + - ]: 196 : desc_prefix = "pkh(" + xpub + "/44h";
48 : 98 : break;
49 : : }
50 : 98 : case OutputType::P2SH_SEGWIT: {
51 [ + - ]: 196 : desc_prefix = "sh(wpkh(" + xpub + "/49h";
52 [ + - ]: 98 : desc_suffix += ')';
53 : : break;
54 : : }
55 : 98 : case OutputType::BECH32: {
56 [ + - ]: 196 : desc_prefix = "wpkh(" + xpub + "/84h";
57 : 98 : break;
58 : : }
59 : 98 : case OutputType::BECH32M: {
60 [ + - ]: 196 : desc_prefix = "tr(" + xpub + "/86h";
61 : 98 : break;
62 : : }
63 : 0 : case OutputType::UNKNOWN: {
64 : : // We should never have a DescriptorScriptPubKeyMan for an UNKNOWN OutputType,
65 : : // so if we get to this point something is wrong
66 : 0 : AssertUnreachable();
67 : : }
68 : : } // no default case, so the compiler can warn about missing cases
69 [ - + ]: 392 : assert(!desc_prefix.empty());
70 : :
71 : : // Mainnet derives at 0', testnet and regtest derive at 1'
72 [ + - + + ]: 392 : if (Params().IsTestChain()) {
73 [ + - ]: 56 : desc_prefix += "/1h";
74 : : } else {
75 [ + - ]: 336 : desc_prefix += "/0h";
76 : : }
77 : :
78 [ + + + - ]: 980 : std::string internal_path = internal ? "/1" : "/0";
79 [ + - + - : 1176 : std::string desc_str = desc_prefix + "/0h" + internal_path + desc_suffix;
+ - ]
80 : :
81 : : // Make the descriptor
82 : 784 : FlatSigningProvider keys;
83 [ - + ]: 784 : std::string error;
84 [ - + + - ]: 784 : std::vector<std::unique_ptr<Descriptor>> desc = Parse(desc_str, keys, error, false);
85 [ + - + - : 392 : WalletDescriptor w_desc(std::move(desc.at(0)), creation_time, 0, 0, 0);
+ - ]
86 : 392 : return w_desc;
87 : 392 : }
88 : :
89 : 9 : void WalletDescriptor::UpdateFrom(const WalletDescriptor& other)
90 : : {
91 [ + - ]: 9 : if (!IsCanonicallyEquivalent(other)) {
92 : : return;
93 : : }
94 : 9 : range_start = other.range_start;
95 : 9 : next_index = other.next_index;
96 : 9 : range_end = other.range_end;
97 : 9 : creation_time = other.creation_time;
98 : 9 : cache = other.cache;
99 : : }
100 : :
101 : 318 : uint256 WalletDescriptor::GetCanonicalHash() const
102 : : {
103 [ + + ]: 318 : if (!m_canonical_hash) {
104 : 141 : m_canonical_hash.emplace();
105 : 141 : std::string canonical = descriptor->ToCanonicalString();
106 [ + - + - : 282 : CSHA256().Write((unsigned char*)canonical.data(), canonical.size()).Finalize(m_canonical_hash->begin());
+ - ]
107 : 141 : }
108 : 318 : return *m_canonical_hash;
109 : : }
110 : :
111 : 159 : bool WalletDescriptor::IsCanonicallyEquivalent(const WalletDescriptor& other) const
112 : : {
113 [ + + ]: 159 : return GetCanonicalHash() == other.GetCanonicalHash();
114 : : }
115 : :
116 : : } // namespace wallet
|