-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathserver.go
More file actions
126 lines (109 loc) · 3.5 KB
/
server.go
File metadata and controls
126 lines (109 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
package main
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/lightsparkdev/go-sdk/objects"
"github.com/lightsparkdev/go-sdk/remotesigning"
"github.com/lightsparkdev/go-sdk/services"
"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.
*
* By default, this server will run on port 8080. You can make a request to the API through curl
* to make sure the server is working properly (replace ls_test with the username you have
* configured):
*
* curl 127.0.0.1:8080/ping
*
*/
func main() {
config, err := NewConfigFromEnv()
if err != nil {
log.Fatalf("Invalid config: %s", err)
}
lsClient := services.NewLightsparkClient(config.ApiClientId, config.ApiClientSecret, config.ApiEndpoint)
var validator remotesigning.Validator
if config.ValidationEnabled {
validator = remotesigning.NewMultiValidator(
remotesigning.HashValidator{},
remotesigning.NewDestinationValidator(config.MasterSeed, config.L1WalletEnabled))
} else {
validator = remotesigning.PositiveValidator{}
}
engine := gin.Default()
engine.GET("/ping", func(c *gin.Context) {
c.Status(http.StatusNoContent)
})
engine.POST("/ln/webhooks", func(c *gin.Context) {
signature := c.Request.Header.Get(webhooks.SIGNATURE_HEADER)
if signature == "" {
log.Print("ERROR: Signature was not present")
c.AbortWithStatus(http.StatusBadRequest)
return
}
data, err := c.GetRawData()
if err != nil {
log.Printf("ERROR: Couldn't get data: %s", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
event, err := webhooks.VerifyAndParse(data, signature, config.WebhookSecret)
if err != nil {
log.Printf("ERROR: Couldn't parse webhook data: %s", err)
c.AbortWithStatus(http.StatusBadRequest)
return
}
log.Printf("Received %s", event.EventType.StringValue())
switch event.EventType {
case objects.WebhookEventTypeRemoteSigning:
if config.RespondDirectly {
resp, err := remotesigning.GraphQLResponseForRemoteSigningWebhook(
validator, *event, config.MasterSeed)
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.GraphqlResponse().Variables)
} else {
c.Status(http.StatusNoContent)
}
} else {
resp, err := remotesigning.HandleRemoteSigningWebhook(
lsClient, validator, *event, config.MasterSeed)
if err != nil {
log.Printf("ERROR: Unable to handle remote signing webhook: %s", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
if resp != "" {
log.Printf("Webhook complete with response: %s", resp)
} else {
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:
c.Status(http.StatusNoContent)
}
})
engine.Run()
}