Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ if(ENABLE_WALLET)
bitcoin_wallet
bitcoin_common
bitcoin_util
univalue
)
install_binary_component(bitcoin-wallet HAS_MANPAGE)
endif()
Expand Down
6 changes: 6 additions & 0 deletions src/bitcoin-wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ static void SetupWalletToolArgs(ArgsManager& argsman)
argsman.AddArg("-dumpfile=<file name>", "When used with 'dump', writes out the records to this file. When used with 'createfromdump', loads the records into a new wallet.", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::COMMAND_OPTIONS);
argsman.AddArg("-debug=<category>", "Output debugging information (default: 0).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
argsman.AddArg("-pubkey=<key>", "Extended public key (xpub/tpub) for decrypting a backup. Optional when -wallet is provided: candidate keys are then derived from the wallet's own keys at the backup's derivation path hints and the common derivation paths.", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::COMMAND_OPTIONS);
argsman.AddArg("-xpub=<key>", "Extended public key (xpub/tpub) whose derivation path to include in backup header. Common derivation paths, which recovery implementations try automatically, are omitted.", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::COMMAND_OPTIONS);
argsman.AddArg("-compact", "Use compact descriptor backup content for encryptbackup and skip decoy secrets, minimizing the backup size. When importing decrypted compact descriptors, use timestamp \"now\" to avoid an automatic rescan, then call rescanblockchain after import.", ArgsManager::ALLOW_ANY, OptionsCategory::COMMAND_OPTIONS);

argsman.AddCommand("info", "Get wallet info");
argsman.AddCommand("create", "Create a new descriptor wallet file");
argsman.AddCommand("dump", "Print out all of the wallet key-value records", {"-dumpfile"});
argsman.AddCommand("createfromdump", "Create new wallet file from dumped records", {"-dumpfile"});
argsman.AddCommand("encryptbackup", "Create an encrypted backup of wallet descriptors (outputs base64)", {"-xpub", "-compact"});
argsman.AddCommand("decryptbackup", "Decrypt an encrypted backup (reads base64 from stdin, requires -pubkey and/or -wallet; imports descriptors when -wallet is provided)", {"-pubkey"});
argsman.AddCommand("inspectbackup", "Show unencrypted metadata from a backup (reads base64 from stdin)");
}

static std::optional<int> WalletAppInit(ArgsManager& args, int argc, char* argv[])
Expand Down
9 changes: 9 additions & 0 deletions src/dummywallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
#include <util/log.h>
#include <walletinitinterface.h>

#include <memory>
#include <stdexcept>

class ArgsManager;

namespace interfaces {
class Chain;
class Handler;
class Wallet;
class WalletBackup;
class WalletLoader;
}

Expand Down Expand Up @@ -62,4 +66,9 @@ std::unique_ptr<WalletLoader> MakeWalletLoader(Chain& chain, ArgsManager& args)
throw std::logic_error("Wallet function called in non-wallet build.");
}

std::unique_ptr<WalletBackup> MakeWalletBackup()
{
throw std::logic_error("Wallet function called in non-wallet build.");
}

} // namespace interfaces
31 changes: 31 additions & 0 deletions src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class Wallet
//! Back up wallet.
virtual bool backupWallet(const std::string& filename) = 0;

//! Create an encrypted backup of public wallet descriptors.
virtual util::Result<std::string> createEncryptedDescriptorBackup(const std::optional<std::string>& target_xpub, bool compact) = 0;

//! Get wallet name.
virtual std::string getWalletName() = 0;

Expand Down Expand Up @@ -318,6 +321,30 @@ class Wallet
virtual util::Result<std::string> exportWatchOnlyWallet(const fs::path& destination) = 0;
};

//! Metadata for an encrypted descriptor backup.
struct EncryptedDescriptorBackupMetadata
{
int version{0};
//! Number of individual secrets in the backup. May include decoys, so
//! this is an upper bound on the number of recipients, not their count.
size_t individual_secret_count{0};
std::string encryption;
std::vector<std::string> derivation_paths;
};

//! Interface for encrypted wallet backup utility operations.
class WalletBackup
{
public:
virtual ~WalletBackup() = default;

//! Decrypt an encrypted descriptor backup with an extended public key.
virtual util::Result<std::vector<uint8_t>> decryptEncryptedDescriptorBackup(const std::string& base64_str, const std::string& pubkey_str) = 0;

//! Get metadata from an encrypted descriptor backup.
virtual util::Result<EncryptedDescriptorBackupMetadata> getEncryptedDescriptorBackupMetadata(const std::string& base64_str) = 0;
};

//! Wallet chain client that in addition to having chain client methods for
//! starting up, shutting down, and registering RPCs, also has additional
//! methods (called by the GUI) to load and create wallets.
Expand Down Expand Up @@ -449,6 +476,10 @@ struct WalletMigrationResult
//! dummywallet.cpp and throws if the wallet component is not compiled.
std::unique_ptr<Wallet> MakeWallet(wallet::WalletContext& context, const std::shared_ptr<wallet::CWallet>& wallet);

//! Return implementation of WalletBackup interface. This function is defined in
//! dummywallet.cpp and throws if the wallet component is not compiled.
std::unique_ptr<WalletBackup> MakeWalletBackup();

//! Return implementation of ChainClient interface for a wallet loader. This
//! function will be undefined in builds where ENABLE_WALLET is false.
std::unique_ptr<WalletLoader> MakeWalletLoader(Chain& chain, ArgsManager& args);
Expand Down
60 changes: 53 additions & 7 deletions src/script/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,21 @@ struct PubkeyProvider
virtual std::optional<CPubKey> GetRootPubKey() const = 0;
/** Return the extended public key for this PubkeyProvider, if it has one. */
virtual std::optional<CExtPubKey> GetRootExtPubKey() const = 0;
/** Whether this PubkeyProvider is a bare xpub with no derivation. */
virtual bool IsBareExtPubKey() const { return false; }
/** Add the extended public key represented by this provider, if any.
*
* @param[out] ext_pubs Any extended public keys
* @param[in] exclude_observable Whether to exclude xpubs that may be observable from spends
*/
virtual void GetExtPubKeys(std::set<CExtPubKey>& ext_pubs, bool exclude_observable) const
{
// A bare xpub has no derivation boundary separating the descriptor key from the
// pubkey revealed by a spend, unlike xpub/path or xpub/* entries.
if (exclude_observable && IsBareExtPubKey()) return;
std::optional<CExtPubKey> ext_pub = GetRootExtPubKey();
if (ext_pub) ext_pubs.insert(*ext_pub);
}

/** Make a deep copy of this PubkeyProvider */
virtual std::unique_ptr<PubkeyProvider> Clone() const = 0;
Expand Down Expand Up @@ -379,6 +394,10 @@ class OriginPubkeyProvider final : public PubkeyProvider
{
return m_provider->GetRootExtPubKey();
}
bool IsBareExtPubKey() const override
{
return m_provider->IsBareExtPubKey();
}
std::unique_ptr<PubkeyProvider> Clone() const override
{
return std::make_unique<OriginPubkeyProvider>(m_expr_index, m_origin, m_provider->Clone());
Expand Down Expand Up @@ -689,6 +708,7 @@ class BIP32PubkeyProvider final : public PubkeyProvider
{
return m_root_extkey;
}
bool IsBareExtPubKey() const override { return m_path.empty() && !IsRange(); }
std::unique_ptr<PubkeyProvider> Clone() const override
{
return std::make_unique<BIP32PubkeyProvider>(m_expr_index, m_root_extkey, m_path, m_derive, m_hardened);
Expand Down Expand Up @@ -910,6 +930,18 @@ class MuSigPubkeyProvider final : public PubkeyProvider
{
return std::nullopt;
}
void GetExtPubKeys(std::set<CExtPubKey>& ext_pubs, bool exclude_observable) const override
{
if (!exclude_observable) {
PubkeyProvider::GetExtPubKeys(ext_pubs, exclude_observable);
return;
}
// Recurse into participants for backup key discovery: the aggregate key
// revealed by a spend does not reveal the participant xpubs.
for (const auto& prov : m_participants) {
prov->GetExtPubKeys(ext_pubs, /*exclude_observable=*/false);
}
}

std::unique_ptr<PubkeyProvider> Clone() const override
{
Expand Down Expand Up @@ -1232,24 +1264,38 @@ class DescriptorImpl : public Descriptor
std::optional<int64_t> MaxSatisfactionElems() const override { return {}; }

// NOLINTNEXTLINE(misc-no-recursion)
void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs) const override
void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs, bool only_observable) const override
{
auto insert_pubs = [&pubkeys, &ext_pubs](const std::unique_ptr<PubkeyProvider>& p) {
auto insert_pubs = [&pubkeys, &ext_pubs, only_observable](const std::unique_ptr<PubkeyProvider>& p) {
std::optional<CPubKey> pub = p->GetRootPubKey();
if (pub) pubkeys.insert(*pub);
std::optional<CExtPubKey> ext_pub = p->GetRootExtPubKey();
if (ext_pub) ext_pubs.insert(*ext_pub);
if (ext_pub && (!only_observable || p->IsBareExtPubKey())) ext_pubs.insert(*ext_pub);
};
for (const auto& p : m_pubkey_args) {
if (MuSigPubkeyProvider* musig = dynamic_cast<MuSigPubkeyProvider*>(p.get())) {
for (const auto& mp : musig->m_participants) {
insert_pubs(mp);
// Spending reveals only the aggregate key, so MuSig participants are never observable.
if (!only_observable) {
if (MuSigPubkeyProvider* musig = dynamic_cast<MuSigPubkeyProvider*>(p.get())) {
for (const auto& mp : musig->m_participants) {
insert_pubs(mp);
}
}
}
insert_pubs(p);
}
for (const auto& arg : m_subdescriptor_args) {
arg->GetPubKeys(pubkeys, ext_pubs);
arg->GetPubKeys(pubkeys, ext_pubs, only_observable);
}
}

// NOLINTNEXTLINE(misc-no-recursion)
void GetExtPubKeys(std::set<CExtPubKey>& ext_pubs, bool exclude_observable) const override
{
for (const auto& p : m_pubkey_args) {
p->GetExtPubKeys(ext_pubs, exclude_observable);
}
for (const auto& arg : m_subdescriptor_args) {
arg->GetExtPubKeys(ext_pubs, exclude_observable);
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/script/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,16 @@ struct Descriptor {
*
* @param[out] pubkeys Any public keys
* @param[out] ext_pubs Any extended public keys
* @param[in] only_observable Only return literal pubkeys and bare xpubs outside MuSig aggregates
*/
virtual void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs) const = 0;
virtual void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs, bool only_observable = false) const = 0;

/** Return all extended public keys for this descriptor, including any from subdescriptors.
*
* @param[out] ext_pubs Any extended public keys
* @param[in] exclude_observable Whether to exclude xpubs that may be observable from spends
*/
virtual void GetExtPubKeys(std::set<CExtPubKey>& ext_pubs, bool exclude_observable = false) const = 0;

/** Whether this descriptor produces any scripts with the Expand functions */
virtual bool HasScripts() const = 0;
Expand Down
11 changes: 11 additions & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ add_executable(test_bitcoin
util_not_null_tests.cpp
util_expected_tests.cpp
util_string_tests.cpp
utf8_tests.cpp
util_tests.cpp
util_threadnames_tests.cpp
util_trace_tests.cpp
Expand All @@ -148,6 +149,15 @@ include(TargetDataSources)
target_json_data_sources(test_bitcoin
data/base58_encode_decode.json
data/bip341_wallet_vectors.json
data/bip138_recipient_keys.json
data/bip138_encryption_secret.json
data/bip138_derivation_path.json
data/bip138_individual_secrets.json
data/bip138_content_type.json
data/bip138_payload.json
data/bip138_encrypted_backup.json
data/bip138_chacha20poly1305_encryption.json
data/bip138_bip380_descriptor_backup.json
data/bip352_send_and_receive_vectors.json
data/blockfilters.json
data/key_io_invalid.json
Expand All @@ -160,6 +170,7 @@ target_json_data_sources(test_bitcoin
)
target_raw_data_sources(test_bitcoin NAMESPACE test::data
data/asmap.raw
data/bip138_bip380_descriptor_backup.txt
)

add_windows_application_manifest(test_bitcoin)
Expand Down
45 changes: 45 additions & 0 deletions src/test/data/bip138_bip380_descriptor_backup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[
{
"description": "Descriptor with change descriptor",
"valid": true,
"document": {
"version": 1,
"descriptor_sets": [
{
"archived": false,
"birth_time": 1710000000,
"range": [0, 999],
"descriptor": "wpkh([d34db33f/84h/1h/0h]tpubDC5FSnBiZDMmhiuCmWAYsLwgLYrrT9rAqvTySfuCCrgsWz8wxMXUS9Tb9iVMvcRbvFcAHGkMD5Kx8koh4GquNGNTfohfk7pgjhaPCdXpoba/0/*)",
"change_descriptor": "wpkh([d34db33f/84h/1h/0h]tpubDC5FSnBiZDMmhiuCmWAYsLwgLYrrT9rAqvTySfuCCrgsWz8wxMXUS9Tb9iVMvcRbvFcAHGkMD5Kx8koh4GquNGNTfohfk7pgjhaPCdXpoba/1/*)"
}
]
}
},
{
"description": "BIP389 multipath descriptor",
"valid": true,
"document": {
"version": 1,
"descriptor_sets": [
{
"archived": false,
"birth_time": 1710000000,
"range": [0, 999],
"descriptor": "wpkh([d34db33f/84h/1h/0h]tpubDC5FSnBiZDMmhiuCmWAYsLwgLYrrT9rAqvTySfuCCrgsWz8wxMXUS9Tb9iVMvcRbvFcAHGkMD5Kx8koh4GquNGNTfohfk7pgjhaPCdXpoba/<0;1>/*)"
}
]
}
},
{
"description": "Receive-only descriptor set without optional metadata",
"valid": true,
"document": {
"version": 1,
"descriptor_sets": [
{
"descriptor": "wpkh([d34db33f/84h/1h/0h]tpubDC5FSnBiZDMmhiuCmWAYsLwgLYrrT9rAqvTySfuCCrgsWz8wxMXUS9Tb9iVMvcRbvFcAHGkMD5Kx8koh4GquNGNTfohfk7pgjhaPCdXpoba/0/*)"
}
]
}
}
]
2 changes: 2 additions & 0 deletions src/test/data/bip138_bip380_descriptor_backup.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# BIP380 descriptor backup text vector
wpkh([d34db33f/84h/1h/0h]tpubDC5FSnBiZDMmhiuCmWAYsLwgLYrrT9rAqvTySfuCCrgsWz8wxMXUS9Tb9iVMvcRbvFcAHGkMD5Kx8koh4GquNGNTfohfk7pgjhaPCdXpoba/<0;1>/*)
37 changes: 37 additions & 0 deletions src/test/data/bip138_chacha20poly1305_encryption.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[
{
"description": "Basic encryption with short plaintext",
"nonce": "000102030405060708090a0b",
"plaintext": "48656c6c6f",
"secret": "0000000000000000000000000000000000000000000000000000000000000000",
"ciphertext": "7df9cb9a0ac5851cc054b14d05f781127b2b0d31fa"
},
{
"description": "Empty plaintext should fail",
"nonce": "000102030405060708090a0b",
"plaintext": "",
"secret": "0000000000000000000000000000000000000000000000000000000000000000",
"ciphertext": null
},
{
"description": "Encryption with zeroed nonce should fail",
"nonce": "000000000000000000000000",
"plaintext": "00000000000000000000000000000000",
"secret": "0000000000000000000000000000000000000000000000000000000000000000",
"ciphertext": null
},
{
"description": "Encryption with all FFs",
"nonce": "ffffffffffffffffffffffff",
"plaintext": "ffffffffffffffffffffffffffffffff",
"secret": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"ciphertext": "22d8bfc313e141bd692c82cec7785b29a0e5d5014c0d3b2d3a8b00548cd8d221"
},
{
"description": "Longer plaintext",
"nonce": "0f1e2d3c4b5a69788796a5b4",
"plaintext": "546869732069732061206c6f6e67657220706c61696e746578742074686174207368756c6420626520656e637279707465642070726f7065726c792e",
"secret": "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
"ciphertext": "2af094190e2b43a02b8dd6bfc25a8833f84f81bc6f690d75f0214e466ef392616c4644bee65118c444a926e1e365b3a7ba0509a5636524eb4e5722f5926938f32e4df79237acb6dd4a6ccbc4"
}
]
Loading
Loading