From fa830ceeae6526e95a19fce142fc05d9d1ad6199 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Sun, 25 Feb 2024 16:03:25 +0100 Subject: [PATCH] plugin: fix json rpc protocol Signed-off-by: Vincenzo Palazzo --- plugin/plugin.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/plugin/plugin.go b/plugin/plugin.go index edad416..5c7e51b 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -178,13 +178,13 @@ func (instance *Plugin[T]) Log(level string, message string) { } // Configuring a plugin with the default rpc methods Core Lightning needs to work. -func (instance *Plugin[T]) configurePlugin() { - instance.RegisterRPCMethod("getmanifest", "", "", GetManifest[T]) - instance.RegisterRPCMethod("init", "", "", InitCall[T]) +func (self *Plugin[T]) configurePlugin() { + self.RegisterRPCMethod("getmanifest", "", "", GetManifest[T]) + self.RegisterRPCMethod("init", "", "", InitCall[T]) } -func (instance *Plugin[T]) Start() { - instance.configurePlugin() +func (self *Plugin[T]) Start() { + self.configurePlugin() reader := bufio.NewReader(os.Stdin) writer := bufio.NewWriter(os.Stdout) for { @@ -207,29 +207,29 @@ func (instance *Plugin[T]) Start() { } var request Request - if err := instance.encoder.DecodeFromBytes(rawRequest, &request); err != nil { + if err := self.encoder.DecodeFromBytes(rawRequest, &request); err != nil { panic(fmt.Sprintf("Error parsing request: %s input %s", err, string(rawRequest))) } if request.Id != nil { - result, err := instance.callRPCMethod(request.Method, request.GetParams()) + result, err := self.callRPCMethod(request.Method, request.GetParams()) var response Response if err != nil { - instance.Log("broken", fmt.Sprintf("plugin generate an error: %s", err)) - response = Response{Id: request.Id, Error: map[string]any{"message": err.Error(), "code": -2}, Result: nil} + self.Log("broken", fmt.Sprintf("plugin generate an error: %s", err)) + response = Response{Id: request.Id, Jsonrpc: "2.0", Error: map[string]any{"message": err.Error(), "code": -2}, Result: nil} } else { - response = Response{Id: request.Id, Error: nil, Result: result} + response = Response{Id: request.Id, Jsonrpc: "2.0", Error: nil, Result: result} } - responseStr, err := instance.encoder.EncodeToByte(response) + responseStr, err := self.encoder.EncodeToByte(response) if err != nil { - instance.Log("broken", fmt.Sprintf("Error marshalling response: %s", err)) + self.Log("broken", fmt.Sprintf("Error marshalling response: %s", err)) panic(err) } if _, err := writer.Write(responseStr); err != nil { - instance.tracer.Infof("%s", err) + self.tracer.Infof("%s", err) } writer.Flush() } else { - instance.handleNotification(request.Method, request.GetParams()) + self.handleNotification(request.Method, request.GetParams()) } } }