mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-01 10:11:11 +02:00
multi: update PaymentAddr to use fn.Option
Since it is allowed to not be set and so can lead to nil deref panics if it is a pointer.
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/btcsuite/btcd/btcutil/bech32"
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/lightningnetwork/lnd/fn"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
)
|
||||
|
||||
@@ -278,13 +279,19 @@ func parseTaggedFields(invoice *Invoice, fields []byte, net *chaincfg.Params) er
|
||||
invoice.PaymentHash, err = parse32Bytes(base32Data)
|
||||
|
||||
case fieldTypeS:
|
||||
if invoice.PaymentAddr != nil {
|
||||
if invoice.PaymentAddr.IsSome() {
|
||||
// We skip the field if we have already seen a
|
||||
// supported one.
|
||||
continue
|
||||
}
|
||||
|
||||
invoice.PaymentAddr, err = parse32Bytes(base32Data)
|
||||
addr, err := parse32Bytes(base32Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if addr != nil {
|
||||
invoice.PaymentAddr = fn.Some(*addr)
|
||||
}
|
||||
|
||||
case fieldTypeD:
|
||||
if invoice.Description != nil {
|
||||
|
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/btcsuite/btcd/btcutil/bech32"
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
"github.com/lightningnetwork/lnd/fn"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
)
|
||||
|
||||
@@ -301,14 +302,14 @@ func writeTaggedFields(bufferBase32 *bytes.Buffer, invoice *Invoice) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if invoice.PaymentAddr != nil {
|
||||
err := writeBytes32(
|
||||
bufferBase32, fieldTypeS, *invoice.PaymentAddr,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err := fn.MapOptionZ(invoice.PaymentAddr, func(addr [32]byte) error {
|
||||
return writeBytes32(bufferBase32, fieldTypeS, addr)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if invoice.Features.SerializeSize32() > 0 {
|
||||
var b bytes.Buffer
|
||||
err := invoice.Features.RawFeatureVector.EncodeBase32(&b)
|
||||
|
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
"github.com/btcsuite/btcd/btcutil"
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/lightningnetwork/lnd/fn"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
)
|
||||
|
||||
@@ -136,7 +137,7 @@ type Invoice struct {
|
||||
|
||||
// PaymentAddr is the payment address to be used by payments to prevent
|
||||
// probing of the destination.
|
||||
PaymentAddr *[32]byte
|
||||
PaymentAddr fn.Option[[32]byte]
|
||||
|
||||
// Destination is the public key of the target node. This will always
|
||||
// be set after decoding, and can optionally be set before encoding to
|
||||
@@ -301,7 +302,7 @@ func Features(features *lnwire.FeatureVector) func(*Invoice) {
|
||||
// the desired payment address that is advertised on the invoice.
|
||||
func PaymentAddr(addr [32]byte) func(*Invoice) {
|
||||
return func(i *Invoice) {
|
||||
i.PaymentAddr = &addr
|
||||
i.PaymentAddr = fn.Some(addr)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||
sphinx "github.com/lightningnetwork/lightning-onion"
|
||||
"github.com/lightningnetwork/lnd/fn"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -562,7 +563,7 @@ func TestDecodeEncode(t *testing.T) {
|
||||
MilliSat: &testMillisat25mBTC,
|
||||
Timestamp: time.Unix(1496314658, 0),
|
||||
PaymentHash: &testPaymentHash,
|
||||
PaymentAddr: &specPaymentAddr,
|
||||
PaymentAddr: fn.Some(specPaymentAddr),
|
||||
Description: &testCoffeeBeans,
|
||||
Destination: testPubKey,
|
||||
Features: lnwire.NewFeatureVector(
|
||||
@@ -589,7 +590,7 @@ func TestDecodeEncode(t *testing.T) {
|
||||
MilliSat: &testMillisat25mBTC,
|
||||
Timestamp: time.Unix(1496314658, 0),
|
||||
PaymentHash: &testPaymentHash,
|
||||
PaymentAddr: &specPaymentAddr,
|
||||
PaymentAddr: fn.Some(specPaymentAddr),
|
||||
Description: &testCoffeeBeans,
|
||||
Destination: testPubKey,
|
||||
Features: lnwire.NewFeatureVector(
|
||||
@@ -718,7 +719,7 @@ func TestDecodeEncode(t *testing.T) {
|
||||
PaymentHash: &testPaymentHash,
|
||||
Description: &testPaymentMetadata,
|
||||
Destination: testPubKey,
|
||||
PaymentAddr: &specPaymentAddr,
|
||||
PaymentAddr: fn.Some(specPaymentAddr),
|
||||
Features: lnwire.NewFeatureVector(
|
||||
lnwire.NewRawFeatureVector(8, 14, 48),
|
||||
lnwire.Features,
|
||||
@@ -772,7 +773,7 @@ func TestDecodeEncode(t *testing.T) {
|
||||
MilliSat: &testMillisat25mBTC,
|
||||
Timestamp: time.Unix(1496314658, 0),
|
||||
PaymentHash: &testPaymentHash,
|
||||
PaymentAddr: &specPaymentAddr,
|
||||
PaymentAddr: fn.Some(specPaymentAddr),
|
||||
Description: &testCoffeeBeans,
|
||||
Destination: testPubKey,
|
||||
Features: lnwire.NewFeatureVector(
|
||||
@@ -803,7 +804,7 @@ func TestDecodeEncode(t *testing.T) {
|
||||
MilliSat: &testMillisat25mBTC,
|
||||
Timestamp: time.Unix(1496314658, 0),
|
||||
PaymentHash: &testPaymentHash,
|
||||
PaymentAddr: &specPaymentAddr,
|
||||
PaymentAddr: fn.Some(specPaymentAddr),
|
||||
Description: &testCoffeeBeans,
|
||||
Destination: testPubKey,
|
||||
Features: lnwire.NewFeatureVector(
|
||||
|
Reference in New Issue
Block a user