Skip to content

Commit 1ccb526

Browse files
authored
Merge pull request #9868 from gijswijs/onion-messaging
Basic structures for onion messages into LND
2 parents 8eb29f1 + 0cbf2ad commit 1ccb526

21 files changed

+5888
-4560
lines changed

docs/release-notes/release-notes-0.21.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
# Bug Fixes
2323

2424
# New Features
25+
26+
- Basic Support for [onion messaging forwarding](https://github.com/lightningnetwork/lnd/pull/9868)
27+
consisting of a new message type, `OnionMessage`. This includes the message's
28+
definition, comprising a path key and an onion blob, along with the necessary
29+
serialization and deserialization logic for peer-to-peer communication.
30+
2531
## Functional Enhancements
2632

2733
## RPC Additions

itest/list_on_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,10 @@ var allTestCases = []*lntest.TestCase{
531531
Name: "custom message",
532532
TestFunc: testCustomMessage,
533533
},
534+
{
535+
Name: "onion message",
536+
TestFunc: testOnionMessage,
537+
},
534538
{
535539
Name: "sign verify message with addr",
536540
TestFunc: testSignVerifyMessageWithAddr,

itest/lnd_onion_message_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package itest
2+
3+
import (
4+
"time"
5+
6+
"github.com/btcsuite/btcd/btcec/v2"
7+
"github.com/lightningnetwork/lnd/lnrpc"
8+
"github.com/lightningnetwork/lnd/lntest"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
// testOnionMessage tests sending and receiving of the onion message type.
13+
func testOnionMessage(ht *lntest.HarnessTest) {
14+
alice := ht.NewNode("Alice", nil)
15+
bob := ht.NewNode("Bob", nil)
16+
17+
// Subscribe Alice to onion messages before we send any, so that we
18+
// don't miss any.
19+
msgClient, cancel := alice.RPC.SubscribeOnionMessages()
20+
defer cancel()
21+
22+
// Create a channel to receive onion messages on.
23+
messages := make(chan *lnrpc.OnionMessage)
24+
go func() {
25+
for {
26+
// If we fail to receive, just exit. The test should
27+
// fail elsewhere if it doesn't get a message that it
28+
// was expecting.
29+
msg, err := msgClient.Recv()
30+
if err != nil {
31+
return
32+
}
33+
34+
// Deliver the message into our channel or exit if the
35+
// test is shutting down.
36+
select {
37+
case messages <- msg:
38+
case <-ht.Context().Done():
39+
return
40+
}
41+
}
42+
}()
43+
44+
// Connect alice and bob so that they can exchange messages.
45+
ht.EnsureConnected(alice, bob)
46+
47+
// Create a random onion message.
48+
randomPriv, err := btcec.NewPrivateKey()
49+
require.NoError(ht.T, err)
50+
randomPub := randomPriv.PubKey()
51+
msgPathKey := randomPub.SerializeCompressed()
52+
// Create a random payload. The content doesn't matter for this and
53+
// doesn't need to be encrypted. It's also of arbitrary length, so it
54+
// doesn't follow the BOLT 4 spec for onion message payload length of
55+
// either 1300 or 32768 bytes. Here we just use a few bytes to keep it
56+
// simple.
57+
msgOnion := []byte{1, 2, 3}
58+
59+
// Send it from Bob to Alice.
60+
bobMsg := &lnrpc.SendOnionMessageRequest{
61+
Peer: alice.PubKey[:],
62+
PathKey: msgPathKey,
63+
Onion: msgOnion,
64+
}
65+
bob.RPC.SendOnionMessage(bobMsg)
66+
67+
// Wait for Alice to receive the message.
68+
select {
69+
case msg := <-messages:
70+
// Check our type and data and (sanity) check the peer we got
71+
// it from.
72+
require.Equal(ht, msgOnion, msg.Onion, "msg data wrong")
73+
require.Equal(ht, msgPathKey, msg.PathKey, "msg "+
74+
"path key wrong")
75+
require.Equal(ht, bob.PubKey[:], msg.Peer, "msg peer wrong")
76+
77+
case <-time.After(lntest.DefaultTimeout):
78+
ht.Fatalf("alice did not receive onion message: %v", bobMsg)
79+
}
80+
}

0 commit comments

Comments
 (0)