Skip to content

Commit f3e3357

Browse files
committed
spscriptpubkeyman: Check that desc allows labels before creating label destinations
1 parent 12ec48a commit f3e3357

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

src/wallet/scriptpubkeyman.cpp

+18-12
Original file line numberDiff line numberDiff line change
@@ -2841,18 +2841,15 @@ SilentPaymentDescriptorScriptPubKeyMan::SilentPaymentDescriptorScriptPubKeyMan(W
28412841
throw std::runtime_error(std::string(__func__) + ": descriptor is not a Silent Payment Descriptor");
28422842
}
28432843

2844-
// Populate m_map_label_tweaks if descriptor is Silent Payments
2845-
if (descriptor.descriptor->GetOutputType() == OutputType::SILENT_PAYMENT) {
2846-
auto sppubkey = GetSpPubKeyFrom(descriptor.descriptor);
2847-
if (!sppubkey.has_value()) {
2848-
throw std::runtime_error(std::string(__func__) + ": descriptor expansion failed");
2849-
}
2850-
auto change_label_data = BIP352::CreateLabelTweak(sppubkey->scanKey, 0);
2851-
m_map_label_tweaks.insert(change_label_data);
2852-
for (int i = 1; i < descriptor.next_index; i++) {
2853-
// Add the other generated labelled destinations
2854-
m_map_label_tweaks.insert(BIP352::CreateLabelTweak(sppubkey->scanKey, i));
2855-
}
2844+
auto sppubkey = GetSpPubKeyFrom(descriptor.descriptor);
2845+
if (!sppubkey.has_value()) {
2846+
throw std::runtime_error(std::string(__func__) + ": descriptor expansion failed");
2847+
}
2848+
auto change_label_data = BIP352::CreateLabelTweak(sppubkey->scanKey, 0);
2849+
m_map_label_tweaks.insert(change_label_data);
2850+
for (int i = 1; i < descriptor.next_index; i++) {
2851+
// Add the other generated labelled destinations
2852+
m_map_label_tweaks.insert(BIP352::CreateLabelTweak(sppubkey->scanKey, i));
28562853
}
28572854
}
28582855

@@ -2897,6 +2894,15 @@ V0SilentPaymentDestination SilentPaymentDescriptorScriptPubKeyMan::GetLabelledDe
28972894
util::Result<CTxDestination> SilentPaymentDescriptorScriptPubKeyMan::GetNewLabelledDestination(uint64_t& index)
28982895
{
28992896
LOCK(cs_desc_man);
2897+
2898+
auto sppubkey = GetSpPubKeyFrom(m_wallet_descriptor.descriptor);
2899+
if (!sppubkey.has_value()) {
2900+
throw std::runtime_error(std::string(__func__) + ": descriptor expansion failed");
2901+
}
2902+
if (!sppubkey->AllowLabels()) {
2903+
return util::Error{ .message=Untranslated("Failed to create new label destination. Labels not allowed") };
2904+
}
2905+
29002906
auto dest = GetLabelledDestination(m_wallet_descriptor.next_index);
29012907
index = m_wallet_descriptor.next_index; // Return the index for this destination
29022908
m_wallet_descriptor.next_index++;

test/functional/wallet_silentpayments_receiving.py

+27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22

3+
from test_framework.descriptors import descsum_create
34
from test_framework.test_framework import BitcoinTestFramework
45
from test_framework.util import (
56
assert_approx,
@@ -45,6 +46,32 @@ def test_labels(self):
4546
assert_equal(wallet_txs_by_label[0]["label"], "test")
4647
assert_equal(wallet.getreceivedbylabel("test"), 10)
4748

49+
self.log.info("Check that a silent payments wallet allows labels only when the SP desc allows it")
50+
allow_labels_desc = descsum_create("sp(sprtprv1qqqqqqqqq850xtnj8hk0gpg6a7kgutyne8zmy9p38qtumvq6zj2tj97ggd4n2q8g7vh8y00v7sz34mav3ckf8jw9kg2rzwqhekcp59y5hytussmtx5yvyrl8)")
51+
disallow_labels_desc = descsum_create("sp(sprtprv1qqqqqqqqqr50xtnj8hk0gpg6a7kgutyne8zmy9p38qtumvq6zj2tj97ggd4n2q8g7vh8y00v7sz34mav3ckf8jw9kg2rzwqhekcp59y5hytussmtx5vlynje)")
52+
53+
self.nodes[0].createwallet(wallet_name="allow_labels", blank=True, silent_payment=True)
54+
allow_labels_wallet = self.nodes[0].get_wallet_rpc("allow_labels")
55+
assert allow_labels_wallet.importdescriptors([{
56+
"desc": allow_labels_desc,
57+
"active": True,
58+
"next_index": 0,
59+
"timestamp": "now"
60+
}])[0]["success"]
61+
allow_labels_wallet.getnewaddress(address_type="silent-payments", label="test")
62+
assert_equal(allow_labels_wallet.listlabels(), ["test"])
63+
64+
self.nodes[0].createwallet(wallet_name="disallow_labels", blank=True, silent_payment=True)
65+
disallow_labels_wallet = self.nodes[0].get_wallet_rpc("disallow_labels")
66+
assert disallow_labels_wallet.importdescriptors([{
67+
"desc": disallow_labels_desc,
68+
"active": True,
69+
"next_index": 0,
70+
"timestamp": "now"
71+
}])[0]["success"]
72+
assert_raises_rpc_error(-12, "Failed to create new label destination. Labels not allowed", disallow_labels_wallet.getnewaddress, address_type="silent-payments", label="test")
73+
assert_equal(disallow_labels_wallet.listlabels(), [])
74+
4875
def test_encrypt_and_decrypt(self):
4976
self.log.info("Check that a silent payments wallet can be encrypted and decrypted")
5077
self.log.info("Create encrypted wallet")

0 commit comments

Comments
 (0)