Skip to content

Commit

Permalink
spscriptpubkeyman: Check that desc allows labels before creating labe…
Browse files Browse the repository at this point in the history
…l destinations
  • Loading branch information
Eunovo committed Oct 4, 2024
1 parent 12ec48a commit f3e3357
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/wallet/scriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2841,18 +2841,15 @@ SilentPaymentDescriptorScriptPubKeyMan::SilentPaymentDescriptorScriptPubKeyMan(W
throw std::runtime_error(std::string(__func__) + ": descriptor is not a Silent Payment Descriptor");
}

// Populate m_map_label_tweaks if descriptor is Silent Payments
if (descriptor.descriptor->GetOutputType() == OutputType::SILENT_PAYMENT) {
auto sppubkey = GetSpPubKeyFrom(descriptor.descriptor);
if (!sppubkey.has_value()) {
throw std::runtime_error(std::string(__func__) + ": descriptor expansion failed");
}
auto change_label_data = BIP352::CreateLabelTweak(sppubkey->scanKey, 0);
m_map_label_tweaks.insert(change_label_data);
for (int i = 1; i < descriptor.next_index; i++) {
// Add the other generated labelled destinations
m_map_label_tweaks.insert(BIP352::CreateLabelTweak(sppubkey->scanKey, i));
}
auto sppubkey = GetSpPubKeyFrom(descriptor.descriptor);
if (!sppubkey.has_value()) {
throw std::runtime_error(std::string(__func__) + ": descriptor expansion failed");
}
auto change_label_data = BIP352::CreateLabelTweak(sppubkey->scanKey, 0);
m_map_label_tweaks.insert(change_label_data);
for (int i = 1; i < descriptor.next_index; i++) {
// Add the other generated labelled destinations
m_map_label_tweaks.insert(BIP352::CreateLabelTweak(sppubkey->scanKey, i));
}
}

Expand Down Expand Up @@ -2897,6 +2894,15 @@ V0SilentPaymentDestination SilentPaymentDescriptorScriptPubKeyMan::GetLabelledDe
util::Result<CTxDestination> SilentPaymentDescriptorScriptPubKeyMan::GetNewLabelledDestination(uint64_t& index)
{
LOCK(cs_desc_man);

auto sppubkey = GetSpPubKeyFrom(m_wallet_descriptor.descriptor);
if (!sppubkey.has_value()) {
throw std::runtime_error(std::string(__func__) + ": descriptor expansion failed");
}
if (!sppubkey->AllowLabels()) {
return util::Error{ .message=Untranslated("Failed to create new label destination. Labels not allowed") };
}

auto dest = GetLabelledDestination(m_wallet_descriptor.next_index);
index = m_wallet_descriptor.next_index; // Return the index for this destination
m_wallet_descriptor.next_index++;
Expand Down
27 changes: 27 additions & 0 deletions test/functional/wallet_silentpayments_receiving.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

from test_framework.descriptors import descsum_create
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_approx,
Expand Down Expand Up @@ -45,6 +46,32 @@ def test_labels(self):
assert_equal(wallet_txs_by_label[0]["label"], "test")
assert_equal(wallet.getreceivedbylabel("test"), 10)

self.log.info("Check that a silent payments wallet allows labels only when the SP desc allows it")
allow_labels_desc = descsum_create("sp(sprtprv1qqqqqqqqq850xtnj8hk0gpg6a7kgutyne8zmy9p38qtumvq6zj2tj97ggd4n2q8g7vh8y00v7sz34mav3ckf8jw9kg2rzwqhekcp59y5hytussmtx5yvyrl8)")
disallow_labels_desc = descsum_create("sp(sprtprv1qqqqqqqqqr50xtnj8hk0gpg6a7kgutyne8zmy9p38qtumvq6zj2tj97ggd4n2q8g7vh8y00v7sz34mav3ckf8jw9kg2rzwqhekcp59y5hytussmtx5vlynje)")

self.nodes[0].createwallet(wallet_name="allow_labels", blank=True, silent_payment=True)
allow_labels_wallet = self.nodes[0].get_wallet_rpc("allow_labels")
assert allow_labels_wallet.importdescriptors([{
"desc": allow_labels_desc,
"active": True,
"next_index": 0,
"timestamp": "now"
}])[0]["success"]
allow_labels_wallet.getnewaddress(address_type="silent-payments", label="test")
assert_equal(allow_labels_wallet.listlabels(), ["test"])

self.nodes[0].createwallet(wallet_name="disallow_labels", blank=True, silent_payment=True)
disallow_labels_wallet = self.nodes[0].get_wallet_rpc("disallow_labels")
assert disallow_labels_wallet.importdescriptors([{
"desc": disallow_labels_desc,
"active": True,
"next_index": 0,
"timestamp": "now"
}])[0]["success"]
assert_raises_rpc_error(-12, "Failed to create new label destination. Labels not allowed", disallow_labels_wallet.getnewaddress, address_type="silent-payments", label="test")
assert_equal(disallow_labels_wallet.listlabels(), [])

def test_encrypt_and_decrypt(self):
self.log.info("Check that a silent payments wallet can be encrypted and decrypted")
self.log.info("Create encrypted wallet")
Expand Down

0 comments on commit f3e3357

Please sign in to comment.