cln_plugin: custommsg support

This commit is contained in:
Jesse de Wit
2023-08-10 09:36:13 +02:00
parent 5c6f7da265
commit c23dc64df5
6 changed files with 454 additions and 42 deletions

View File

@@ -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) {
@@ -277,6 +301,7 @@ func (c *ClnPlugin) handleGetManifest(request *Request) {
},
Dynamic: true,
Hooks: []Hook{
{Name: "custommsg"},
{Name: "htlc_accepted"},
{Name: "openchannel"},
{Name: "openchannel2"},
@@ -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{
@@ -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":