Skip to content

Commit

Permalink
cln_plugin: custommsg support
Browse files Browse the repository at this point in the history
  • Loading branch information
JssDWt committed Aug 11, 2023
1 parent 0df936d commit abef3b9
Show file tree
Hide file tree
Showing 6 changed files with 454 additions and 42 deletions.
5 changes: 5 additions & 0 deletions cln_plugin/cln_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,8 @@ type LogNotification struct {
Level string `json:"level"`
Message string `json:"message"`
}

type CustomMessageRequest struct {
PeerId string `json:"peer_id"`
Payload string `json:"payload"`
}
47 changes: 46 additions & 1 deletion cln_plugin/cln_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,30 @@ func (c *ClnPlugin) htlcListenServer() {
}
}

// Listens to responses to custommsg requests from the grpc server.
func (c *ClnPlugin) custommsgListenServer() {
for {
select {
case <-c.done:
return
default:
id, result := c.server.ReceiveCustomMessageResponse()

// The server may return nil if it is stopped.
if result == nil {
continue
}

serid, _ := json.Marshal(&id)
c.sendToCln(&Response{
Id: serid,
JsonRpc: SpecVersion,
Result: result,
})
}
}
}

// processes a single message from cln. Sends the message to the appropriate
// handler.
func (c *ClnPlugin) processMsg(msg []byte) {
Expand Down Expand Up @@ -277,6 +301,7 @@ func (c *ClnPlugin) handleGetManifest(request *Request) {
},
Dynamic: true,
Hooks: []Hook{
{Name: "custommsg"},
{Name: "htlc_accepted"},
{Name: "openchannel"},
{Name: "openchannel2"},
Expand Down Expand Up @@ -406,8 +431,9 @@ func (c *ClnPlugin) handleInit(request *Request) {
return
}

// Listen for htlc responses from the grpc server.
// Listen for htlc and custommsg responses from the grpc server.
go c.htlcListenServer()
go c.custommsgListenServer()

// Let cln know the plugin is initialized.
c.sendToCln(&Response{
Expand Down Expand Up @@ -466,6 +492,25 @@ func (c *ClnPlugin) handleSetChannelAcceptScript(request *Request) {
})
}

func (c *ClnPlugin) handleCustomMsg(request *Request) {
var custommsg CustomMessageRequest
err := json.Unmarshal(request.Params, &custommsg)
if err != nil {
c.sendError(
request.Id,
ParseError,
fmt.Sprintf(
"Failed to unmarshal custommsg params:%s [%s]",
err.Error(),
request.Params,
),
)
return
}

c.server.SendCustomMessage(idToString(request.Id), &custommsg)
}

func unmarshalOpenChannel(request *Request) (r json.RawMessage, err error) {
switch request.Method {
case "openchannel":
Expand Down
172 changes: 151 additions & 21 deletions cln_plugin/proto/cln_plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions cln_plugin/proto/cln_plugin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ option go_package="github.com/breez/lspd/cln_plugin/proto";

service ClnPlugin {
rpc HtlcStream(stream HtlcResolution) returns (stream HtlcAccepted);
rpc CustomMsgStream(CustomMessageRequest) returns (stream CustomMessage);
}

message HtlcAccepted {
Expand Down Expand Up @@ -54,3 +55,9 @@ message HtlcFail {
message HtlcResolve {
string payment_key = 1;
}

message CustomMessageRequest {}
message CustomMessage {
string peer_id = 1;
string payload = 2;
}
Loading

0 comments on commit abef3b9

Please sign in to comment.