zpay32: add functional opt to error out on unknown feature bit

This commit adds two functional options to the zpay32.Decode function.
`WithKnownFeatureBits` allows the caller to overwrite the default set of
known feature bits used by the function.
`WithErrorOnUnknownFeatureBit` allows the caller to instruct the
function to error out if the invoice that is decoded contaijns unknown
feature bits. We then use this new error-out option from the
`rpcServer`'s `extractPaymentIntent` method.
This commit is contained in:
Elle Mouton committed 2024-08-07 17:03:12 +02:00
1 parent e4619afc08
commit 48a9a8d20e
3 files changed
+133 -4

No files matched your search

+68 -1
View File
@@ -191,6 +191,7 @@ func TestDecodeEncode(t *testing.T) {
encodedInvoice string
valid bool
decodedInvoice func() *Invoice
decodeOpts []DecodeOption
skipEncoding bool
beforeEncoding func(*Invoice)
}{
@@ -758,6 +759,70 @@ func TestDecodeEncode(t *testing.T) {
i.Destination = nil
},
},
{
// Invoice with unknown feature bits but since the
// WithErrorOnUnknownFeatureBit option is not provided,
// it is not expected to error out.
encodedInvoice: "lnbc25m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdq5vdhkven9v5sxyetpdeessp5zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zygs9q4psqqqqqqqqqqqqqqqpqsqq40wa3khl49yue3zsgm26jrepqr2eghqlx86rttutve3ugd05em86nsefzh4pfurpd9ek9w2vp95zxqnfe2u7ckudyahsa52q66tgzcp6t2dyk",
valid: true,
skipEncoding: true,
decodedInvoice: func() *Invoice {
return &Invoice{
Net: &chaincfg.MainNetParams,
MilliSat: &testMillisat25mBTC,
Timestamp: time.Unix(1496314658, 0),
PaymentHash: &testPaymentHash,
PaymentAddr: &specPaymentAddr,
Description: &testCoffeeBeans,
Destination: testPubKey,
Features: lnwire.NewFeatureVector(
lnwire.NewRawFeatureVector(
9, 15, 99, 100,
),
lnwire.Features,
),
}
},
decodeOpts: []DecodeOption{
WithKnownFeatureBits(map[lnwire.FeatureBit]string{
9: "9",
15: "15",
99: "99",
}),
},
},
{
// Invoice with unknown feature bits with option set to
// error out on unknown feature bit.
encodedInvoice: "lnbc25m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdq5vdhkven9v5sxyetpdeessp5zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zygs9q4psqqqqqqqqqqqqqqqpqsqq40wa3khl49yue3zsgm26jrepqr2eghqlx86rttutve3ugd05em86nsefzh4pfurpd9ek9w2vp95zxqnfe2u7ckudyahsa52q66tgzcp6t2dyk",
valid: false,
skipEncoding: true,
decodedInvoice: func() *Invoice {
return &Invoice{
Net: &chaincfg.MainNetParams,
MilliSat: &testMillisat25mBTC,
Timestamp: time.Unix(1496314658, 0),
PaymentHash: &testPaymentHash,
PaymentAddr: &specPaymentAddr,
Description: &testCoffeeBeans,
Destination: testPubKey,
Features: lnwire.NewFeatureVector(
lnwire.NewRawFeatureVector(
9, 15, 99, 100,
),
lnwire.Features,
),
}
},
decodeOpts: []DecodeOption{
WithKnownFeatureBits(map[lnwire.FeatureBit]string{
9: "9",
15: "15",
99: "99",
}),
WithErrorOnUnknownFeatureBit(),
},
},
}
for i, test := range tests {
@@ -773,7 +838,9 @@ func TestDecodeEncode(t *testing.T) {
net = decodedInvoice.Net
}
invoice, err := Decode(test.encodedInvoice, net)
invoice, err := Decode(
test.encodedInvoice, net, test.decodeOpts...,
)
if !test.valid {
require.Error(t, err)
} else {