Skip to content
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

lnrpc+peer: custom peer messages #5346

Merged
merged 2 commits into from
Oct 19, 2021
Merged
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
83 changes: 83 additions & 0 deletions cmd/lncli/cmd_custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"encoding/hex"
"fmt"

"github.com/lightningnetwork/lnd/lnrpc"
"github.com/urfave/cli"
)

var sendCustomCommand = cli.Command{
Name: "sendcustom",
Flags: []cli.Flag{
cli.StringFlag{
Name: "peer",
},
cli.Uint64Flag{
Name: "type",
},
cli.StringFlag{
Name: "data",
},
},
Action: actionDecorator(sendCustom),
}

func sendCustom(ctx *cli.Context) error {
ctxc := getContext()
client, cleanUp := getClient(ctx)
defer cleanUp()

peer, err := hex.DecodeString(ctx.String("peer"))
if err != nil {
return err
}

msgType := ctx.Uint64("type")

data, err := hex.DecodeString(ctx.String("data"))
if err != nil {
return err
}

_, err = client.SendCustomMessage(
ctxc,
&lnrpc.SendCustomMessageRequest{
Peer: peer,
Type: uint32(msgType),
Data: data,
},
)

return err
}

var subscribeCustomCommand = cli.Command{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how useful this CLI command will be in practice, but don't see much harm in including it /shruggie

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, its use is doubtful. But because this is generic functionality, there may be an unexpected use for it. In any case, it is useful in development.

Name: "subscribecustom",
Action: actionDecorator(subscribeCustom),
}

func subscribeCustom(ctx *cli.Context) error {
ctxc := getContext()
client, cleanUp := getClient(ctx)
defer cleanUp()

stream, err := client.SubscribeCustomMessages(
ctxc,
&lnrpc.SubscribeCustomMessagesRequest{},
)
if err != nil {
return err
}

for {
msg, err := stream.Recv()
if err != nil {
return err
}

fmt.Printf("Received from peer %x: type=%d, data=%x\n",
msg.Peer, msg.Type, msg.Data)
}
}
2 changes: 2 additions & 0 deletions cmd/lncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ func main() {
profileSubCommand,
getStateCommand,
deletePaymentsCommand,
sendCustomCommand,
subscribeCustomCommand,
}

// Add any extra commands determined by build flags.
Expand Down
19 changes: 18 additions & 1 deletion docs/release-notes/release-notes-0.14.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,23 @@ If you use a strange system or changed group membership of the group running LND
you may want to check your system to see if it introduces additional risk for
you.

## Safety
## Custom peer messages

Lightning nodes have a connection to each of their peers for exchanging
messages. In regular operation, these messages coordinate processes such as
channel opening and payment forwarding.

The lightning spec however also defines a custom range (>= 32768) for
experimental and application-specific peer messages.

With this release, [custom peer message
exchange](https://github.com/lightningnetwork/lnd/pull/5346) is added to open up
a range of new possibilities. Custom peer messages allow the lightning protocol
with its transport mechanisms (including tor) and public key authentication to
be leveraged for application-level communication. Note that peers exchange these
messages directly. There is no routing/path finding involved.

# Safety

* Locally force closed channels are now [kept in the channel.backup file until
their time lock has fully matured](https://github.com/lightningnetwork/lnd/pull/5528).
Expand Down Expand Up @@ -504,6 +520,7 @@ change](https://github.com/lightningnetwork/lnd/pull/5613).
* Hampus Sjöberg
* Harsha Goli
* Jesse de Wit
* Joost Jager
* Martin Habovstiak
* Naveen Srinivasan
* Oliver Gugger
Expand Down
Loading