-
Notifications
You must be signed in to change notification settings - Fork 18
/
server.go
141 lines (119 loc) · 3.12 KB
/
server.go
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package notifications
import (
context "context"
"encoding/hex"
"fmt"
"log"
"github.com/breez/lspd/lightning"
lspdrpc "github.com/breez/lspd/rpc"
ecies "github.com/ecies/go/v2"
"google.golang.org/protobuf/proto"
)
var ErrInternal = fmt.Errorf("internal error")
type server struct {
store Store
NotificationsServer
}
func NewNotificationsServer(store Store) NotificationsServer {
return &server{
store: store,
}
}
func (s *server) SubscribeNotifications(
ctx context.Context,
in *EncryptedNotificationRequest,
) (*SubscribeNotificationsReply, error) {
node, _, err := lspdrpc.GetNode(ctx)
if err != nil {
return nil, err
}
data, err := ecies.Decrypt(node.EciesPrivateKey, in.Blob)
if err != nil {
log.Printf(
"failed to register for notifications: ecies.Decrypt error: %v",
err,
)
return nil, fmt.Errorf("ecies.Decrypt(%x) error: %w", in.Blob, err)
}
var request SubscribeNotificationsRequest
err = proto.Unmarshal(data, &request)
if err != nil {
log.Printf(
"failed to register for notifications on url %v: proto.Unmarshal(%x) error: %v",
request.Url,
data,
err,
)
return nil, fmt.Errorf("proto.Unmarshal(%x) error: %w", data, err)
}
pubkey, err := lightning.VerifyMessage([]byte(request.Url), request.Signature)
if err != nil {
log.Printf(
"failed to register %x for notifications on url %s: lightning.VerifyMessage error: %v",
pubkey.SerializeCompressed(),
request.Url,
err,
)
return nil, err
}
pubkeyStr := hex.EncodeToString(pubkey.SerializeCompressed())
err = s.store.Register(ctx, pubkeyStr, request.Url)
if err != nil {
log.Printf(
"failed to register %x for notifications on url %s: %v",
pubkey.SerializeCompressed(),
request.Url,
err,
)
return nil, ErrInternal
}
log.Printf("%s was successfully registered for notifications on url %s", pubkeyStr, request.Url)
return &SubscribeNotificationsReply{}, nil
}
func (s *server) UnsubscribeNotifications(
ctx context.Context,
in *EncryptedNotificationRequest,
) (*UnsubscribeNotificationsReply, error) {
node, _, err := lspdrpc.GetNode(ctx)
if err != nil {
return nil, err
}
data, err := ecies.Decrypt(node.EciesPrivateKey, in.Blob)
if err != nil {
log.Printf(
"failed to unsubscribe: ecies.Decrypt error: %v",
err,
)
return nil, fmt.Errorf("ecies.Decrypt(%x) error: %w", in.Blob, err)
}
var request UnsubscribeNotificationsRequest
err = proto.Unmarshal(data, &request)
if err != nil {
log.Printf(
"failed to unsubscribe: proto.Unmarshal(%x) error: %v",
data,
err,
)
return nil, fmt.Errorf("proto.Unmarshal(%x) error: %w", data, err)
}
pubkey, err := lightning.VerifyMessage([]byte(request.Url), request.Signature)
if err != nil {
log.Printf(
"failed to unsubscribe on url %s: lightning.VerifyMessage error: %v",
request.Url,
err,
)
return nil, err
}
err = s.store.Unsubscribe(ctx, hex.EncodeToString(pubkey.SerializeCompressed()), request.Url)
if err != nil {
log.Printf(
"failed to unsubscribe %x on url %s: %v",
pubkey.SerializeCompressed(),
request.Url,
err,
)
return nil, ErrInternal
}
return &UnsubscribeNotificationsReply{}, nil
}