Skip to content

Commit 255c9b4

Browse files
committed
sphinx_test: onion message packet creation
TestTLVPayloadMessagePacket creates a onion message with payload and the blinded route from the test vector. It then checks if the onion packet we create is equal to the one provided in the test vector.
1 parent 90ace25 commit 255c9b4

File tree

4 files changed

+121
-5
lines changed

4 files changed

+121
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor/
22
.idea
3+
.aider*

go.mod

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,30 @@ module github.com/lightningnetwork/lightning-onion
22

33
require (
44
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da
5-
github.com/btcsuite/btcd v0.22.0-beta.0.20220207191057-4dc4ff7963b4
6-
github.com/btcsuite/btcd/btcec/v2 v2.1.0
5+
github.com/btcsuite/btcd v0.24.1-0.20240301210420-1a2b599bf1af
6+
github.com/btcsuite/btcd/btcec/v2 v2.3.2
77
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
88
github.com/davecgh/go-spew v1.1.1
99
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1
10-
github.com/stretchr/testify v1.8.2
10+
github.com/stretchr/testify v1.8.4
1111
github.com/urfave/cli v1.22.5
12-
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
12+
golang.org/x/crypto v0.16.0
13+
)
14+
15+
require (
16+
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
17+
github.com/lightningnetwork/lnd/fn/v2 v2.0.2 // indirect
18+
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b // indirect
19+
golang.org/x/sync v0.7.0 // indirect
1320
)
1421

1522
require (
1623
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
24+
github.com/lightningnetwork/lnd/tlv v1.3.2
1725
github.com/pmezard/go-difflib v1.0.0 // indirect
1826
github.com/russross/blackfriday/v2 v2.0.1 // indirect
1927
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
20-
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed // indirect
28+
golang.org/x/sys v0.15.0 // indirect
2129
gopkg.in/yaml.v3 v3.0.1 // indirect
2230
)
2331

go.sum

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da h1:KjTM2ks9d14ZYCvmH
33
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da/go.mod h1:eHEWzANqSiWQsof+nXEI9bUVUyV6F53Fp89EuCh2EAA=
44
github.com/btcsuite/btcd v0.22.0-beta.0.20220207191057-4dc4ff7963b4 h1:CEGr/598C/0LZQUoioaT6sdGGcJgu4+ck0PDeJ/QkKs=
55
github.com/btcsuite/btcd v0.22.0-beta.0.20220207191057-4dc4ff7963b4/go.mod h1:7alexyj/lHlOtr2PJK7L/+HDJZpcGDn/pAU98r7DY08=
6+
github.com/btcsuite/btcd v0.24.1-0.20240301210420-1a2b599bf1af h1:F60A3wst4/fy9Yr1Vn8MYmFlfn7DNLxp8o8UTvhqgBE=
7+
github.com/btcsuite/btcd v0.24.1-0.20240301210420-1a2b599bf1af/go.mod h1:5C8ChTkl5ejr3WHj8tkQSCmydiMEPB0ZhQhehpq7Dgg=
68
github.com/btcsuite/btcd/btcec/v2 v2.1.0 h1:Whmbo9yShKKG+WrUfYGFfgj77vYBiwhwBSJnM66TMKI=
79
github.com/btcsuite/btcd/btcec/v2 v2.1.0/go.mod h1:2VzYrv4Gm4apmbVVsSq5bqf1Ec8v56E48Vt0Y/umPgA=
10+
github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U=
11+
github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
12+
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 h1:59Kx4K6lzOW5w6nFlA0v5+lk/6sjybR934QNHSJZPTQ=
13+
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
814
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f h1:bAs4lUbRJpnnkd9VhRV3jjAVU7DJVjMaK+IsvSeZvFo=
915
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
1016
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
@@ -15,6 +21,10 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
1521
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
1622
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc=
1723
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
24+
github.com/lightningnetwork/lnd/fn/v2 v2.0.2 h1:M7o2lYrh/zCp+lntPB3WP/rWTu5U+4ssyHW+kqNJ0fs=
25+
github.com/lightningnetwork/lnd/fn/v2 v2.0.2/go.mod h1:TOzwrhjB/Azw1V7aa8t21ufcQmdsQOQMDtxVOQWNl8s=
26+
github.com/lightningnetwork/lnd/tlv v1.3.2 h1:MO4FCk7F4k5xPMqVZF6Nb/kOpxlwPrUQpYjmyKny5s0=
27+
github.com/lightningnetwork/lnd/tlv v1.3.2/go.mod h1:pJuiBj1ecr1WWLOtcZ+2+hu9Ey25aJWFIsjmAoPPnmc=
1828
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1929
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2030
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
@@ -28,16 +38,26 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
2838
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
2939
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
3040
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
41+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
42+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
3143
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
3244
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
3345
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3446
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
3547
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
48+
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
49+
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
50+
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b h1:kLiC65FbiHWFAOu+lxwNPujcsl8VYyTYYEZnsOO1WK4=
51+
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
3652
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
53+
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
54+
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
3755
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3856
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3957
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed h1:J22ig1FUekjjkmZUM7pTKixYm8DvrYsvrBZdunYeIuQ=
4058
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
59+
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
60+
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
4161
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
4262
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
4363
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

sphinx_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/btcsuite/btcd/btcec/v2"
1414
"github.com/davecgh/go-spew/spew"
15+
"github.com/lightningnetwork/lnd/tlv"
1516
"github.com/stretchr/testify/require"
1617
)
1718

@@ -162,6 +163,89 @@ func TestBolt4Packet(t *testing.T) {
162163
}
163164
}
164165

166+
func TestTLVPayloadMessagePacket(t *testing.T) {
167+
t.Parallel()
168+
169+
// First, we'll read out the raw JSON file at the target location.
170+
jsonBytes, err := os.ReadFile(testOnionMessageFileName)
171+
require.NoError(t, err)
172+
173+
// Once we have the raw file, we'll unpack it into our jsonTestCase
174+
// struct defined above.
175+
testCase := &onionMessageJsonTestCase{}
176+
require.NoError(t, json.Unmarshal(jsonBytes, testCase))
177+
178+
// Next, we'll populate a new OnionHop using the information included
179+
// in this test case.
180+
var route PaymentPath
181+
for i, hop := range testCase.Route.Hops {
182+
pubKeyBytes, err := hex.DecodeString(hop.BlindedNodeID)
183+
require.NoError(t, err)
184+
185+
pubKey, err := btcec.ParsePubKey(pubKeyBytes)
186+
require.NoError(t, err)
187+
188+
EncryptedRecipientData, err := hex.DecodeString(
189+
hop.EncryptedRecipientData,
190+
)
191+
require.NoError(t, err)
192+
193+
// Manually encode our onion payload
194+
records := []tlv.Record{}
195+
196+
if i == len(testCase.Route.Hops)-1 {
197+
helloBytes := []byte("hello")
198+
records = append(records, tlv.MakePrimitiveRecord(
199+
1, &helloBytes,
200+
))
201+
}
202+
203+
records = append(records, tlv.MakePrimitiveRecord(
204+
4, &EncryptedRecipientData,
205+
))
206+
207+
stream, err := tlv.NewStream(records...)
208+
require.NoError(t, err, "new stream")
209+
210+
b := new(bytes.Buffer)
211+
require.NoError(t, stream.Encode(b), "encode")
212+
213+
route[i] = OnionHop{
214+
NodePub: *pubKey,
215+
HopPayload: HopPayload{
216+
Type: PayloadTLV,
217+
Payload: b.Bytes(),
218+
},
219+
}
220+
}
221+
222+
finalPacket, err := hex.DecodeString(
223+
testCase.OnionMessage.OnionMessagePacket,
224+
)
225+
require.NoError(t, err)
226+
227+
sessionKeyBytes, err := hex.DecodeString(testCase.Generate.SessionKey)
228+
229+
require.NoError(t, err)
230+
231+
// With all the required data assembled, we'll craft a new packet.
232+
sessionKey, _ := btcec.PrivKeyFromBytes(sessionKeyBytes)
233+
234+
pkt, err := NewOnionPacket(
235+
&route, sessionKey, nil, DeterministicPacketFiller,
236+
)
237+
require.NoError(t, err)
238+
239+
var b bytes.Buffer
240+
require.NoError(t, pkt.Encode(&b))
241+
242+
// Finally, we expect that our packet matches the packet included in
243+
// the spec's test vectors.
244+
require.Equalf(t, b.Bytes(), finalPacket, "final packet does not "+
245+
"match expected BOLT 4 packet, want: %s, got %s",
246+
hex.EncodeToString(finalPacket), hex.EncodeToString(b.Bytes()))
247+
}
248+
165249
func TestSphinxCorrectness(t *testing.T) {
166250
nodes, _, hopDatas, fwdMsg, err := newTestRoute(testLegacyRouteNumHops)
167251
if err != nil {
@@ -755,6 +839,9 @@ const (
755839

756840
// testTLVFileName is the name of the tlv-payload-only onion test file.
757841
testTLVFileName = "testdata/onion-test.json"
842+
843+
// testOnionMessageFileName is the name of the onion message test file.
844+
testOnionMessageFileName = "testdata/blinded-onion-message-onion-test.json"
758845
)
759846

760847
type jsonHop struct {

0 commit comments

Comments
 (0)