multi: add blinded path TLVs to onion payload / hops

This commit adds the encrypted_data, blinding_point and total_amt_msat
tlvs to the known set of even tlvs for the onion payload. These TLVs
are added in two places (the onion payload and hop struct) because
lnd uses the same set of TLV types for both structs (and they
inherently represent the same thing).

Note: in some places, unit tests intentionally mimic the style
of older tests, so as to be more consistently readable.
This commit is contained in:
Carla Kirk-Cohen
2022-10-26 10:57:37 -04:00
committed by Olaoluwa Osuntokun
parent 539a275faa
commit fee0e05708
7 changed files with 392 additions and 23 deletions

View File

@@ -2,15 +2,23 @@ package hop_test
import (
"bytes"
"encoding/hex"
"reflect"
"testing"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record"
"github.com/stretchr/testify/require"
)
var (
//nolint:lll
testPrivKeyBytes, _ = hex.DecodeString("e126f68f7eafcc8b74f54d269fe206be715000f94dac067d1c04a8ca3b2db734")
_, testPubKey = btcec.PrivKeyFromBytes(testPrivKeyBytes)
)
const testUnknownRequiredType = 0x80
type decodePayloadTest struct {
@@ -20,7 +28,10 @@ type decodePayloadTest struct {
expCustomRecords map[uint64][]byte
shouldHaveMPP bool
shouldHaveAMP bool
shouldHaveEncData bool
shouldHaveBlinding bool
shouldHaveMetadata bool
shouldHaveTotalAmt bool
}
var decodePayloadTests = []decodePayloadTest{
@@ -217,6 +228,33 @@ var decodePayloadTests = []decodePayloadTest{
FinalHop: false,
},
},
{
name: "intermediate hop with encrypted data",
payload: []byte{
// amount
0x02, 0x00,
// cltv
0x04, 0x00,
// encrypted data
0x0a, 0x03, 0x03, 0x02, 0x01,
},
shouldHaveEncData: true,
},
{
name: "intermediate hop with blinding point",
payload: append([]byte{
// amount
0x02, 0x00,
// cltv
0x04, 0x00,
// blinding point (type / length)
0x0c, 0x21,
},
// blinding point (value)
testPubKey.SerializeCompressed()...,
),
shouldHaveBlinding: true,
},
{
name: "final hop with mpp",
payload: []byte{
@@ -271,6 +309,18 @@ var decodePayloadTests = []decodePayloadTest{
},
shouldHaveMetadata: true,
},
{
name: "final hop with total amount",
payload: []byte{
// amount
0x02, 0x00,
// cltv
0x04, 0x00,
// total amount
0x12, 0x01, 0x01,
},
shouldHaveTotalAmt: true,
},
}
// TestDecodeHopPayloadRecordValidation asserts that parsing the payloads in the
@@ -306,6 +356,7 @@ func testDecodeHopPayloadValidation(t *testing.T, test decodePayloadTest) {
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
}
testEncData = []byte{3, 2, 1}
testMetadata = []byte{1, 2, 3}
testChildIndex = uint32(9)
)
@@ -354,6 +405,29 @@ func testDecodeHopPayloadValidation(t *testing.T, test decodePayloadTest) {
t.Fatalf("unexpected metadata")
}
if test.shouldHaveEncData {
require.NotNil(t, p.EncryptedData(),
"payment should have encrypted data")
require.Equal(t, testEncData, p.EncryptedData())
} else {
require.Nil(t, p.EncryptedData())
}
if test.shouldHaveBlinding {
require.NotNil(t, p.BlindingPoint())
require.Equal(t, testPubKey, p.BlindingPoint())
} else {
require.Nil(t, p.BlindingPoint())
}
if test.shouldHaveTotalAmt {
require.NotZero(t, p.TotalAmtMsat())
} else {
require.Zero(t, p.TotalAmtMsat())
}
// Convert expected nil map to empty map, because we always expect an
// initiated map from the payload.
expCustomRecords := make(record.CustomSet)