From 058859b8f2ea68caaa2e5d4fbd3d3db57dae9802 Mon Sep 17 00:00:00 2001 From: Santiago Martin Date: Mon, 21 Apr 2025 13:32:24 -0700 Subject: [PATCH] logging --- examples/remote-signing-server/server.go | 14 ++++++++++++++ objects/webhook_event_type.go | 6 ++++++ services/lightspark_client.go | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/examples/remote-signing-server/server.go b/examples/remote-signing-server/server.go index 3892982..9428fe6 100644 --- a/examples/remote-signing-server/server.go +++ b/examples/remote-signing-server/server.go @@ -12,6 +12,8 @@ import ( "github.com/lightsparkdev/go-sdk/webhooks" ) +const hardcodedPreimage = "00000000000000000000000000000000000000000000000000000000deadbeef" + /** * This is a simple Gin server (https://gin-gonic.com) that implements a simple remote-signer using * the Lightspark SDK. @@ -101,6 +103,18 @@ func main() { log.Printf("Webhook complete") } + c.Status(http.StatusNoContent) + } + case objects.WebhookEventTypeHoldInvoiceAccepted: + resp, err := lsClient.ReleasePaymentPreimage(event.EntityId, hardcodedPreimage) + if err != nil { + log.Printf("ERROR: Unable to handle remote signing webhook: %s", err) + c.AbortWithStatus(http.StatusInternalServerError) + return + } + if resp != nil { + c.JSON(http.StatusOK, resp.Invoice) + } else { c.Status(http.StatusNoContent) } default: diff --git a/objects/webhook_event_type.go b/objects/webhook_event_type.go index 3367ef6..9d0f04f 100644 --- a/objects/webhook_event_type.go +++ b/objects/webhook_event_type.go @@ -40,6 +40,8 @@ const ( WebhookEventTypeHighBalance WebhookEventTypeChannelOpeningFees + + WebhookEventTypeHoldInvoiceAccepted ) func (a *WebhookEventType) UnmarshalJSON(b []byte) error { @@ -80,6 +82,8 @@ func (a *WebhookEventType) UnmarshalJSON(b []byte) error { *a = WebhookEventTypeHighBalance case "CHANNEL_OPENING_FEES": *a = WebhookEventTypeChannelOpeningFees + case "HOLD_INVOICE_ACCEPTED": + *a = WebhookEventTypeHoldInvoiceAccepted } return nil @@ -120,6 +124,8 @@ func (a WebhookEventType) StringValue() string { s = "HIGH_BALANCE" case WebhookEventTypeChannelOpeningFees: s = "CHANNEL_OPENING_FEES" + case WebhookEventTypeHoldInvoiceAccepted: + s = "HOLD_INVOICE_ACCEPTED" } return s diff --git a/services/lightspark_client.go b/services/lightspark_client.go index 711fcaf..b6aef0a 100644 --- a/services/lightspark_client.go +++ b/services/lightspark_client.go @@ -815,6 +815,25 @@ func (client *LightsparkClient) PayOffer(nodeId string, encodedOffer string, tim return &payment, nil } +func (client *LightsparkClient) ReleasePaymentPreimage(invoiceId string, paymentPreimage string) (*objects.ReleasePaymentPreimageOutput, error) { + variables := map[string]interface{}{ + "invoice_id": invoiceId, + "payment_preimage": paymentPreimage, + } + + response, err := client.ExecuteGraphql(scripts.RELEASE_PAYMENT_PREIMAGE_MUTATION, variables, nil) + if err != nil { + return nil, err + } + outputJson, err := json.Marshal(response["release_payment_preimage"].(map[string]interface{})) + if err != nil { + return nil, errors.New("error parsing response") + } + var output objects.ReleasePaymentPreimageOutput + json.Unmarshal(outputJson, &output) + return &output, nil +} + // RequestWithdrawal withdraws funds from the account and sends it to the requested // bitcoin address. Depending on the chosen mode, it will first take the funds from // the wallet, and if applicable, close channels appropriately to recover enough