Skip to content

Commit d24db47

Browse files
committed
lightningclient: add send custom message api
1 parent 9bf868e commit d24db47

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lightning_client.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ type LightningClient interface {
198198
RegisterRPCMiddleware(ctx context.Context, middlewareName,
199199
customCaveatName string, readOnly bool, timeout time.Duration,
200200
intercept InterceptFunction) (chan error, error)
201+
202+
// SendCustomMessage sends a custom message to a peer.
203+
SendCustomMessage(ctx context.Context, msg CustomMessage) error
201204
}
202205

203206
// Info contains info about the connected lnd node.
@@ -1076,6 +1079,18 @@ type QueryRoutesResponse struct {
10761079
TotalAmtMsat lnwire.MilliSatoshi
10771080
}
10781081

1082+
// CustomMessage describes custom messages exchanged with peers.
1083+
type CustomMessage struct {
1084+
// Peer is the peer that the message was exchanged with.
1085+
Peer route.Vertex
1086+
1087+
// MsgType is the protocol message type number for the custom message.
1088+
MsgType uint32
1089+
1090+
// Data is the data exchanged.
1091+
Data []byte
1092+
}
1093+
10791094
var (
10801095
// ErrNoRouteFound is returned if we can't find a path with the passed
10811096
// parameters.
@@ -3723,3 +3738,22 @@ func (s *lightningClient) RegisterRPCMiddleware(ctx context.Context,
37233738

37243739
return errChan, nil
37253740
}
3741+
3742+
// SendCustomMessage sends a custom message to one of our existing peers. Note
3743+
// that lnd must already be connected to a peer to send it messages.
3744+
func (s *lightningClient) SendCustomMessage(ctx context.Context,
3745+
msg CustomMessage) error {
3746+
3747+
rpcCtx, cancel := context.WithTimeout(ctx, s.timeout)
3748+
defer cancel()
3749+
3750+
rpcCtx = s.adminMac.WithMacaroonAuth(rpcCtx)
3751+
rpcReq := &lnrpc.SendCustomMessageRequest{
3752+
Peer: msg.Peer[:],
3753+
Type: msg.MsgType,
3754+
Data: msg.Data,
3755+
}
3756+
3757+
_, err := s.client.SendCustomMessage(rpcCtx, rpcReq)
3758+
return err
3759+
}

testdata/permissions.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,14 @@
476476
}
477477
]
478478
},
479+
"/lnrpc.Lightning/SendCustomMessage": {
480+
"permissions": [
481+
{
482+
"entity": "peers",
483+
"action": "write"
484+
}
485+
]
486+
},
479487
"/lnrpc.Lightning/SendPayment": {
480488
"permissions": [
481489
{

0 commit comments

Comments
 (0)