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/log.h>
11 : :
12 : : namespace wallet {
13 : 2469 : fs::path GetWalletDir()
14 : : {
15 : 2469 : fs::path path;
16 : :
17 [ + - + - : 2469 : if (gArgs.IsArgSet("-walletdir")) {
+ + ]
18 [ + - + - ]: 57 : 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 : : }
24 : : } else {
25 [ + - ]: 4900 : path = gArgs.GetDataDirNet();
26 : : // If a wallets directory exists, use that, otherwise default to GetDataDir
27 [ + - + - : 9800 : if (fs::is_directory(path / "wallets")) {
+ - + + ]
28 [ + - ]: 1818 : path /= "wallets";
29 : : }
30 : : }
31 : :
32 : 2469 : return path;
33 : 0 : }
34 : :
35 : 4202 : WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& addr_type, bool internal)
36 : : {
37 : 4202 : int64_t creation_time = GetTime();
38 : :
39 : 4202 : std::string xpub = EncodeExtPubKey(master_key);
40 : :
41 : : // Build descriptor string
42 [ + - ]: 4202 : std::string desc_prefix;
43 [ + - ]: 4202 : std::string desc_suffix = "/*)";
44 [ + + + + : 4202 : switch (addr_type) {
- - ]
45 : 1048 : case OutputType::LEGACY: {
46 [ + - ]: 2096 : desc_prefix = "pkh(" + xpub + "/44h";
47 : 1048 : break;
48 : : }
49 : 1044 : case OutputType::P2SH_SEGWIT: {
50 [ + - ]: 2088 : desc_prefix = "sh(wpkh(" + xpub + "/49h";
51 [ + - ]: 1044 : desc_suffix += ")";
52 : : break;
53 : : }
54 : 1048 : case OutputType::BECH32: {
55 [ + - ]: 2096 : desc_prefix = "wpkh(" + xpub + "/84h";
56 : 1048 : break;
57 : : }
58 : 1062 : case OutputType::BECH32M: {
59 [ + - ]: 2124 : desc_prefix = "tr(" + xpub + "/86h";
60 : 1062 : break;
61 : : }
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 : : }
67 : : } // no default case, so the compiler can warn about missing cases
68 [ - + ]: 4202 : assert(!desc_prefix.empty());
69 : :
70 : : // Mainnet derives at 0', testnet and regtest derive at 1'
71 [ + - + + ]: 4202 : if (Params().IsTestChain()) {
72 [ + - ]: 3866 : desc_prefix += "/1h";
73 : : } else {
74 [ + - ]: 336 : desc_prefix += "/0h";
75 : : }
76 : :
77 [ + + + - ]: 10505 : std::string internal_path = internal ? "/1" : "/0";
78 [ + - + - : 12606 : std::string desc_str = desc_prefix + "/0h" + internal_path + desc_suffix;
+ - ]
79 : :
80 : : // Make the descriptor
81 : 8404 : FlatSigningProvider keys;
82 [ - + ]: 8404 : std::string error;
83 [ - + + - ]: 8404 : std::vector<std::unique_ptr<Descriptor>> desc = Parse(desc_str, keys, error, false);
84 [ + - + - : 4202 : WalletDescriptor w_desc(std::move(desc.at(0)), creation_time, 0, 0, 0);
+ - ]
85 : 4202 : return w_desc;
86 : 4202 : }
87 : :
88 : 28 : void WalletDescriptor::UpdateFrom(const WalletDescriptor& other)
89 : : {
90 [ + - ]: 28 : if (!IsCanonicallyEquivalent(other)) {
91 : : return;
92 : : }
93 : 28 : range_start = other.range_start;
94 : 28 : next_index = other.next_index;
95 : 28 : range_end = other.range_end;
96 : 28 : creation_time = other.creation_time;
97 : 28 : cache = other.cache;
98 : : }
99 : :
100 : 9702 : uint256 WalletDescriptor::GetCanonicalHash() const
101 : : {
102 [ + + ]: 9702 : if (!m_canonical_hash) {
103 : 3085 : m_canonical_hash.emplace();
104 : 3085 : std::string canonical = descriptor->ToCanonicalString();
105 [ + - + - : 6170 : CSHA256().Write((unsigned char*)canonical.data(), canonical.size()).Finalize(m_canonical_hash->begin());
+ - ]
106 : 3085 : }
107 : 9702 : return *m_canonical_hash;
108 : : }
109 : :
110 : 4851 : bool WalletDescriptor::IsCanonicallyEquivalent(const WalletDescriptor& other) const
111 : : {
112 [ + + ]: 4851 : return GetCanonicalHash() == other.GetCanonicalHash();
113 : : }
114 : :
115 : : } // namespace wallet
|