Skip to content

Commit

Permalink
feat: remove webhook subcription by url (#194)
Browse files Browse the repository at this point in the history
* feat: added webhook unsubscription
* test: adding unsubscribe tests
  • Loading branch information
hydra-yse authored Feb 29, 2024
1 parent ed6fd76 commit 11f35cb
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 21 deletions.
32 changes: 32 additions & 0 deletions itest/lspd_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,38 @@ func SubscribeNotifications(l LspNode, b BreezClient, url string, continueOnErro
return err
}

func UnsubscribeNotifications(l LspNode, b BreezClient, url string, continueOnError bool) error {
msg := append(lightning.SignedMsgPrefix, []byte(url)...)
first := sha256.Sum256([]byte(msg))
second := sha256.Sum256(first[:])
sig, err := ecdsa.SignCompact(b.Node().PrivateKey(), second[:], true)
assert.NoError(b.Harness().T, err)
request := notifications.UnsubscribeNotificationsRequest{
Url: url,
Signature: zbase32.EncodeToString(sig),
}
serialized, err := proto.Marshal(&request)
lntest.CheckError(l.Harness().T, err)

encrypted, err := ecies.Encrypt(l.EciesPublicKey(), serialized)
lntest.CheckError(l.Harness().T, err)

ctx := metadata.AppendToOutgoingContext(l.Harness().Ctx, "authorization", "Bearer hello")
log.Printf("Removing notification subscription")
_, err = l.NotificationsRpc().UnsubscribeNotifications(
ctx,
&notifications.EncryptedNotificationRequest{
Blob: encrypted,
},
)

if !continueOnError {
lntest.CheckError(l.Harness().T, err)
}

return nil
}

type FeeParamSetting struct {
Validity time.Duration
MinMsat uint64
Expand Down
4 changes: 4 additions & 0 deletions itest/lspd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ var allTestCases = []*testCase{
name: "testOfflineNotificationZeroConfChannel",
test: testOfflineNotificationZeroConfChannel,
},
{
name: "testNotificationsUnsubscribe",
test: testNotificationsUnsubscribe,
},
{
name: "testLsps0GetProtocolVersions",
test: testLsps0GetProtocolVersions,
Expand Down
40 changes: 40 additions & 0 deletions itest/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,43 @@ func testOfflineNotificationZeroConfChannel(p *testParams) {
actualheight := p.Miner().GetBlockHeight()
assert.Equal(p.t, expectedheight, actualheight)
}

func testNotificationsUnsubscribe(p *testParams) {
url := "http://127.0.0.1/api/v1/notify"
pubkey := hex.EncodeToString(p.BreezClient().Node().PrivateKey().PubKey().SerializeCompressed())

log.Printf("Client pubkey: %s", pubkey)

count_subscriptions := func() (uint64, error) {
row := p.lsp.PostgresBackend().Pool().QueryRow(
p.h.Ctx,
`SELECT COUNT(*)
FROM public.notification_subscriptions
WHERE pubkey = $1 AND url = $2`,
pubkey,
url,
)

var count uint64
if err := row.Scan(&count); err != nil {
p.h.T.Fatalf("Failed to query subscriptions: %v", err)
return 0, err
}

return count, nil
}

SubscribeNotifications(p.lsp, p.BreezClient(), url, false)

// Verify that we have subscribed to the webhook
count, err := count_subscriptions()
assert.Nil(p.h.T, err)
assert.Equal(p.h.T, count, 1)

UnsubscribeNotifications(p.lsp, p.BreezClient(), url, false)

// Verify that we have unsubscribed from the webhook
count, err = count_subscriptions()
assert.Nil(p.h.T, err)
assert.Equal(p.h.T, count, 0)
}
174 changes: 155 additions & 19 deletions notifications/notifications.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions notifications/notifications.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package notifications;
service Notifications {
rpc SubscribeNotifications(EncryptedNotificationRequest)
returns (SubscribeNotificationsReply) {}

rpc UnsubscribeNotifications(EncryptedNotificationRequest)
returns (UnsubscribeNotificationsReply) {}
}

message EncryptedNotificationRequest {
Expand All @@ -18,5 +21,11 @@ message SubscribeNotificationsRequest {
string signature = 2;
}

message SubscribeNotificationsReply {
}
message SubscribeNotificationsReply {}

message UnsubscribeNotificationsRequest {
string url = 1;
string signature = 2;
}

message UnsubscribeNotificationsReply {}
Loading

0 comments on commit 11f35cb

Please sign in to comment.