Skip to content

Add Support for P2TR Fallback Addresses in BOLT-11 #9975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions docs/release-notes/release-notes-0.20.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@

# New Features

- Added support for [P2TR Fallback Addresses](
https://github.com/lightningnetwork/lnd/pull/9975) in BOLT-11 invoices.

## Functional Enhancements

## RPC Additions
Expand Down
9 changes: 9 additions & 0 deletions zpay32/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,15 @@ func parseFallbackAddr(data []byte, net *chaincfg.Params) (btcutil.Address, erro
len(witness))
}

if err != nil {
return nil, err
}
case 1:
witness, err := bech32.ConvertBits(data[1:], 5, 8, false)
if err != nil {
return nil, err
}
addr, err = btcutil.NewAddressTaproot(witness, net)
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions zpay32/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ func writeTaggedFields(bufferBase32 *bytes.Buffer, invoice *Invoice) error {
version = addr.WitnessVersion()
case *btcutil.AddressWitnessScriptHash:
version = addr.WitnessVersion()
case *btcutil.AddressTaproot:
version = addr.WitnessVersion()
default:
return fmt.Errorf("unknown fallback address type")
}
Expand Down
11 changes: 11 additions & 0 deletions zpay32/invoice_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,11 @@ func TestParseFallbackAddr(t *testing.T) {
testAddrMainnetP2WSHData, _ := bech32.ConvertBits(testAddrMainnetP2WSH.ScriptAddress(), 8, 5, true)
testAddrMainnetP2WSHDataWithVersion := append([]byte{0}, testAddrMainnetP2WSHData...)

testAddrMainnetP2TRData, _ := bech32.ConvertBits(
testAddrMainnetP2TR.ScriptAddress(), 8, 5, true)
testAddrMainnetP2TRDataWithVersion := append(
[]byte{1}, testAddrMainnetP2TRData...)

tests := []struct {
data []byte
net *chaincfg.Params
Expand Down Expand Up @@ -639,6 +644,12 @@ func TestParseFallbackAddr(t *testing.T) {
valid: true,
result: testAddrMainnetP2WSH,
},
{
data: testAddrMainnetP2TRDataWithVersion,
net: &chaincfg.MainNetParams,
valid: true,
result: testAddrMainnetP2TR,
},
}

for i, test := range tests {
Expand Down
44 changes: 42 additions & 2 deletions zpay32/invoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ var (
testAddrMainnetP2SH, _ = btcutil.DecodeAddress("3EktnHQD7RiAE6uzMj2ZifT9YgRrkSgzQX", &chaincfg.MainNetParams)
testAddrMainnetP2WPKH, _ = btcutil.DecodeAddress("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", &chaincfg.MainNetParams)
testAddrMainnetP2WSH, _ = btcutil.DecodeAddress("bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", &chaincfg.MainNetParams)
testAddrMainnetP2TR, _ = btcutil.DecodeAddress("bc1pptdvg0d2nj99568"+
"qn6ssdy4cygnwuxgw2ukmnwgwz7jpqjz2kszse2s3lm",
&chaincfg.MainNetParams)

testHopHintPubkeyBytes1, _ = hex.DecodeString("029e03a901b85534ff1e92c43c74431f7ce72046060fcf7a95c37e148f78c77255")
testHopHintPubkey1, _ = btcec.ParsePubKey(testHopHintPubkeyBytes1)
Expand Down Expand Up @@ -298,8 +301,14 @@ func TestDecodeEncode(t *testing.T) {
},
{
// Ignore unknown witness version in fallback address.
encodedInvoice: "lnbc20m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqhp58yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqsfpppw508d6qejxtdg4y5r3zarvary0c5xw7k8txqv6x0a75xuzp0zsdzk5hq6tmfgweltvs6jk5nhtyd9uqksvr48zga9mw08667w8264gkspluu66jhtcmct36nx363km6cquhhv2cpc6q43r",
valid: true,
encodedInvoice: "lnbc20m1pvjluezpp5qqqsyqcyq5rqwzqfqq" +
"qsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqhp58yjmdan" +
"79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrq" +
"sfp4z6yn92zrp97a6q5hhh8swys7uf4hm9tr8a0xylnk" +
"26fvkg3jx0sdsxvma0zvf2h0pycyyzdrmjncq6lzrfuw" +
"xfhv6gzz4q5303n3up6as4ghe5qthg7x20z7vae8w5rq" +
"u6de3g4jl7kvuap3qedprqsqqmgqqm6s8sl",
valid: true,
decodedInvoice: func() *Invoice {
return &Invoice{
Net: &chaincfg.MainNetParams,
Expand Down Expand Up @@ -648,6 +657,37 @@ func TestDecodeEncode(t *testing.T) {
i.Destination = nil
},
},
{
// On mainnet, with fallback (p2tr) address bc1pptdvg0d2
// nj99568qn6ssdy4cygnwuxgw2ukmnwgwz7jpqjz2kszse2s3lm
encodedInvoice: "lnbc20m1pvjluezpp5qqqsyqcyq5rqwzqfqq" +
"qsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqhp58yjmdan" +
"79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrq" +
"sfp4pptdvg0d2nj99568qn6ssdy4cygnwuxgw2ukmnwg" +
"wz7jpqjz2kszs9zs3tmcpgulwc0ruwc2cm97udy6sdfe" +
"nwvha8qlkfwx49sgk40kze4kwsh706rae3uc30ltpwpw" +
"mjyhc3uan4ljz56wksg5gsnhrrhcqsrq93d",

valid: true,
decodedInvoice: func() *Invoice {
return &Invoice{
Net: &chaincfg.MainNetParams,
MilliSat: &testMillisat20mBTC,
Timestamp: time.Unix(1496314658, 0),
PaymentHash: &testPaymentHash,
DescriptionHash: &testDescriptionHash,
Destination: testPubKey,
FallbackAddr: testAddrMainnetP2TR,
Features: emptyFeatures,
}
},
beforeEncoding: func(i *Invoice) {
// Since this destination pubkey was recovered
// from the signature, we must set it nil before
// encoding to get back the same invoice string.
i.Destination = nil
},
},
{
// Send 2500uBTC for a cup of coffee with a custom CLTV
// expiry value.
Expand Down
Loading