Skip to content

Commit 050f04e

Browse files
committed
fixup! sphinx_test: onion message packet creation
1 parent e56b670 commit 050f04e

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

sphinx_test.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/rand"
66
"encoding/hex"
77
"encoding/json"
8+
"errors"
89
"fmt"
910
"io"
1011
"os"
@@ -40,6 +41,14 @@ var (
4041
testLegacyRouteNumHops = 20
4142
)
4243

44+
// Static errors used in tests.
45+
var (
46+
// ErrInsufficientHops is returned when there are not enough hops to
47+
// create a route.
48+
ErrInsufficientHops = errors.New("at least 2 hops are required to " +
49+
"create an onion message route")
50+
)
51+
4352
// encodeTLVRecord encodes a TLV record with the given type and value.
4453
func encodeTLVRecord(recordType uint64, value []byte) []byte {
4554
var buf bytes.Buffer
@@ -58,19 +67,20 @@ func encodeTLVRecord(recordType uint64, value []byte) []byte {
5867

5968
// writeVarInt writes a variable-length integer to the buffer.
6069
func writeVarInt(buf *bytes.Buffer, n uint64) {
61-
if n < 0xfd {
70+
switch {
71+
case n < 0xfd:
6272
buf.WriteByte(byte(n))
63-
} else if n <= 0xffff {
73+
case n <= 0xffff:
6474
buf.WriteByte(0xfd)
6575
buf.WriteByte(byte(n))
6676
buf.WriteByte(byte(n >> 8))
67-
} else if n <= 0xffffffff {
77+
case n <= 0xffffffff:
6878
buf.WriteByte(0xfe)
6979
buf.WriteByte(byte(n))
7080
buf.WriteByte(byte(n >> 8))
7181
buf.WriteByte(byte(n >> 16))
7282
buf.WriteByte(byte(n >> 24))
73-
} else {
83+
default:
7484
buf.WriteByte(0xff)
7585
buf.WriteByte(byte(n))
7686
buf.WriteByte(byte(n >> 8))
@@ -385,19 +395,19 @@ func TestTLVPayloadMessagePacket(t *testing.T) {
385395
jsonBytes, err := os.ReadFile(testOnionMessageFileName)
386396
require.NoError(t, err)
387397

388-
// Once we have the raw file, we'll unpack it into our jsonTestCase
389-
// struct defined above.
398+
// Once we have the raw file, we'll unpack it into our
399+
// onionMessageJsonTestCase struct defined in path_test.go.
390400
testCase := &onionMessageJsonTestCase{}
391401
require.NoError(t, json.Unmarshal(jsonBytes, testCase))
392402

393403
// Next, we'll populate a new OnionHop using the information included
394404
// in this test case.
395405
var route PaymentPath
396406
for i, hop := range testCase.Route.Hops {
397-
pubKeyBytes, err := hex.DecodeString(hop.BlindedNodeID)
407+
blindedPKbytes, err := hex.DecodeString(hop.BlindedNodeID)
398408
require.NoError(t, err)
399409

400-
pubKey, err := btcec.ParsePubKey(pubKeyBytes)
410+
blindedPubKey, err := btcec.ParsePubKey(blindedPKbytes)
401411
require.NoError(t, err)
402412

403413
encryptedRecipientData, err := hex.DecodeString(
@@ -418,8 +428,9 @@ func TestTLVPayloadMessagePacket(t *testing.T) {
418428
b.Write(encodeTLVRecord(4, encryptedRecipientData))
419429

420430
route[i] = OnionHop{
421-
NodePub: *pubKey,
431+
NodePub: *blindedPubKey,
422432
HopPayload: HopPayload{
433+
// Onion messages always use TLV payloads.
423434
Type: PayloadTLV,
424435
Payload: b.Bytes(),
425436
},
@@ -1162,7 +1173,7 @@ const (
11621173
testTLVFileName = "testdata/onion-test.json"
11631174

11641175
// testOnionMessageFileName is the name of the onion message test file.
1165-
testOnionMessageFileName = "testdata/blinded-onion-message-onion-test.json"
1176+
testOnionMessageFileName = "testdata/blinded-onion-message-onion-test.json" //nolint:lll
11661177
)
11671178

11681179
type jsonHop struct {

0 commit comments

Comments
 (0)