Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
wallet
walletutil.cpp
Go to the documentation of this file.
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
{
15
fs::path path;
16
17
if
(
gArgs
.
IsArgSet
(
"-walletdir"
)) {
18
path =
gArgs
.
GetPathArg
(
"-walletdir"
);
19
if
(!fs::is_directory(path)) {
20
// If the path specified doesn't exist, we return the deliberately
21
// invalid empty string.
22
path =
""
;
23
}
24
}
else
{
25
path =
gArgs
.
GetDataDirNet
();
26
// If a wallets directory exists, use that, otherwise default to GetDataDir
27
if
(fs::is_directory(path /
"wallets"
)) {
28
path /=
"wallets"
;
29
}
30
}
31
32
return
path;
33
}
34
35
WalletDescriptor
GenerateWalletDescriptor
(
const
CExtPubKey
& master_key,
const
OutputType
& addr_type,
bool
internal)
36
{
37
int64_t creation_time =
GetTime
();
38
39
std::string xpub =
EncodeExtPubKey
(master_key);
40
41
// Build descriptor string
42
std::string desc_prefix;
43
std::string desc_suffix =
"/*)"
;
44
switch
(addr_type) {
45
case
OutputType::LEGACY
: {
46
desc_prefix =
"pkh("
+ xpub +
"/44h"
;
47
break
;
48
}
49
case
OutputType::P2SH_SEGWIT
: {
50
desc_prefix =
"sh(wpkh("
+ xpub +
"/49h"
;
51
desc_suffix +=
")"
;
52
break
;
53
}
54
case
OutputType::BECH32
: {
55
desc_prefix =
"wpkh("
+ xpub +
"/84h"
;
56
break
;
57
}
58
case
OutputType::BECH32M
: {
59
desc_prefix =
"tr("
+ xpub +
"/86h"
;
60
break
;
61
}
62
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
assert
(
false
);
66
}
67
}
// no default case, so the compiler can warn about missing cases
68
assert
(!desc_prefix.empty());
69
70
// Mainnet derives at 0', testnet and regtest derive at 1'
71
if
(
Params
().IsTestChain()) {
72
desc_prefix +=
"/1h"
;
73
}
else
{
74
desc_prefix +=
"/0h"
;
75
}
76
77
std::string internal_path = internal ?
"/1"
:
"/0"
;
78
std::string desc_str = desc_prefix +
"/0h"
+ internal_path + desc_suffix;
79
80
// Make the descriptor
81
FlatSigningProvider
keys
;
82
std::string error;
83
std::vector<std::unique_ptr<Descriptor>> desc =
Parse
(desc_str,
keys
, error,
false
);
84
WalletDescriptor
w_desc(std::move(desc.at(0)), creation_time, 0, 0, 0);
85
return
w_desc;
86
}
87
88
void
WalletDescriptor::UpdateFrom
(
const
WalletDescriptor
& other)
89
{
90
if
(!
IsCanonicallyEquivalent
(other)) {
91
return
;
92
}
93
range_start
= other.
range_start
;
94
next_index
= other.
next_index
;
95
range_end
= other.
range_end
;
96
creation_time
= other.
creation_time
;
97
cache
= other.
cache
;
98
}
99
100
uint256
WalletDescriptor::GetCanonicalHash
()
const
101
{
102
if
(!
m_canonical_hash
) {
103
m_canonical_hash
.emplace();
104
std::string canonical =
descriptor
->ToCanonicalString();
105
CSHA256
().Write((
unsigned
char
*)canonical.data(), canonical.size()).Finalize(
m_canonical_hash
->begin());
106
}
107
return
*
m_canonical_hash
;
108
}
109
110
bool
WalletDescriptor::IsCanonicallyEquivalent
(
const
WalletDescriptor
& other)
const
111
{
112
return
GetCanonicalHash
() == other.
GetCanonicalHash
();
113
}
114
115
}
// namespace wallet
gArgs
ArgsManager gArgs
Definition
args.cpp:38
args.h
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition
chainparams.cpp:128
ArgsManager::GetPathArg
fs::path GetPathArg(std::string arg, const fs::path &default_value={}) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return path argument or default value.
Definition
args.cpp:292
ArgsManager::IsArgSet
bool IsArgSet(const std::string &strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return true if the given argument has been manually set.
Definition
args.cpp:431
ArgsManager::GetDataDirNet
fs::path GetDataDirNet() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get data directory path with appended network identifier.
Definition
args.cpp:328
CSHA256::CSHA256
CSHA256()
Definition
sha256.cpp:694
uint256
256-bit opaque blob.
Definition
uint256.h:196
wallet::WalletDescriptor
Descriptor with some wallet metadata.
Definition
walletutil.h:64
wallet::WalletDescriptor::GetCanonicalHash
uint256 GetCanonicalHash() const
Definition
walletutil.cpp:100
wallet::WalletDescriptor::m_canonical_hash
std::optional< uint256 > m_canonical_hash
Definition
walletutil.h:70
wallet::WalletDescriptor::next_index
int32_t next_index
Definition
walletutil.h:67
wallet::WalletDescriptor::WalletDescriptor
WalletDescriptor()=delete
wallet::WalletDescriptor::IsCanonicallyEquivalent
bool IsCanonicallyEquivalent(const WalletDescriptor &other) const
Definition
walletutil.cpp:110
wallet::WalletDescriptor::range_start
int32_t range_start
Definition
walletutil.h:66
wallet::WalletDescriptor::range_end
int32_t range_end
Definition
walletutil.h:68
wallet::WalletDescriptor::descriptor
const std::shared_ptr< const Descriptor > descriptor
Definition
walletutil.h:75
wallet::WalletDescriptor::creation_time
uint64_t creation_time
Definition
walletutil.h:76
wallet::WalletDescriptor::UpdateFrom
void UpdateFrom(const WalletDescriptor &other)
Replaces all metadata (range, start, end, creation time), and cache from another WalletDescriptor if ...
Definition
walletutil.cpp:88
wallet::WalletDescriptor::cache
DescriptorCache cache
Definition
walletutil.h:77
EncodeExtPubKey
std::string EncodeExtPubKey(const CExtPubKey &key)
Definition
key_io.cpp:258
key_io.h
log.h
wallet
Definition
coin_selection.cpp:31
wallet::GetWalletDir
fs::path GetWalletDir()
Get the path of the wallet directory.
Definition
walletutil.cpp:13
wallet::GenerateWalletDescriptor
WalletDescriptor GenerateWalletDescriptor(const CExtPubKey &master_key, const OutputType &addr_type, bool internal)
Definition
walletutil.cpp:35
OutputType
OutputType
Definition
outputtype.h:18
OutputType::LEGACY
@ LEGACY
Definition
outputtype.h:19
OutputType::UNKNOWN
@ UNKNOWN
Definition
outputtype.h:23
OutputType::BECH32
@ BECH32
Definition
outputtype.h:21
OutputType::P2SH_SEGWIT
@ P2SH_SEGWIT
Definition
outputtype.h:20
OutputType::BECH32M
@ BECH32M
Definition
outputtype.h:22
Parse
std::vector< std::unique_ptr< Descriptor > > Parse(std::string_view descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum)
Parse a descriptor string.
Definition
descriptor.cpp:2994
CExtPubKey
Definition
pubkey.h:342
FlatSigningProvider
Definition
signingprovider.h:235
keys
std::vector< uint16_t > keys
Definition
dbwrapper.cpp:376
GetTime
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition
time.cpp:88
assert
assert(!tx.IsCoinBase())
walletutil.h
Generated on
for Bitcoin Core by
1.18.0