Bitcoin Core  22.99.0
P2P Digital Currency
wallet.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019 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 <test/util/wallet.h>
6 
7 #include <key_io.h>
8 #include <outputtype.h>
9 #include <script/standard.h>
10 #ifdef ENABLE_WALLET
11 #include <util/translation.h>
12 #include <wallet/wallet.h>
13 #endif
14 
15 const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
16 
17 #ifdef ENABLE_WALLET
18 std::string getnewaddress(CWallet& w)
19 {
20  constexpr auto output_type = OutputType::BECH32;
21  CTxDestination dest;
23  if (!w.GetNewDestination(output_type, "", dest, error)) assert(false);
24 
25  return EncodeDestination(dest);
26 }
27 
28 void importaddress(CWallet& wallet, const std::string& address)
29 {
30  auto spk_man = wallet.GetLegacyScriptPubKeyMan();
31  LOCK2(wallet.cs_wallet, spk_man->cs_KeyStore);
32  const auto dest = DecodeDestination(address);
34  const auto script = GetScriptForDestination(dest);
35  wallet.MarkDirty();
36  assert(!spk_man->HaveWatchOnly(script));
37  if (!spk_man->AddWatchOnly(script, 0 /* nCreateTime */)) assert(false);
38  wallet.SetAddressBook(dest, /* label */ "", "receive");
39 }
40 #endif // ENABLE_WALLET
LOCK2
#define LOCK2(cs1, cs2)
Definition: sync.h:227
assert
assert(!tx.IsCoinBase())
ADDRESS_BCRT1_UNSPENDABLE
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition: wallet.cpp:15
wallet.h
key_io.h
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:351
bilingual_str
Bilingual messages:
Definition: translation.h:16
CWallet::GetNewDestination
bool GetNewDestination(const OutputType type, const std::string label, CTxDestination &dest, bilingual_str &error)
Definition: wallet.cpp:2135
outputtype.h
getnewaddress
std::string getnewaddress(CWallet &w)
Returns a new address from the wallet.
wallet
Definition: interfaces.cpp:50
CTxDestination
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
IsValidDestination
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:373
standard.h
DecodeDestination
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg)
Definition: key_io.cpp:261
OutputType::BECH32
@ BECH32
importaddress
void importaddress(CWallet &wallet, const std::string &address)
Import the address to the wallet.
translation.h
CWallet
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:228
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
EncodeDestination
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:256
wallet.h