Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nip47 notifications endpoint #60

Merged
merged 6 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,61 @@ Notifies about new events matching the filter provided via webhooks.
// Source: https://pkg.go.dev/github.com/nbd-wtf/[email protected]#Filter
```

#### Response

```json
{
"subscription_id": "f370d1fc-x0x0-x0x0-x0x0-8f68fa12f32c",
"webhookUrl": "https://your.webhook.url"
}
```

#### Response to Webhook URL

```json
{
"id": "a16ycf4a01bcxx........xxxxx",
"pubkey": "a16y69effexxxx........xxxxx",
"created_at": 1709033612,
"kind": 23195,
"tags": [
[
"p",
"f490f5xxxxx........xxxxx"
],
[
"e",
"a41aefxxxxx........xxxxx"
]
],
"content": "<encrypted content>",
"sig": "<signature>"
}
```

</details>

------------------------------------------------------------------------------------------

### Subscribe to NWC Events

Notifies about new response events which are requested by the pubkey to the wallet service.

<details>
<summary>
<code>POST</code> <code><b>/nip47/subscriptions</b></code>
</summary>

#### Request Body

| name | type | data type | description |
|-----------|-----------|-------------------------|-----------------------------------------------------------------------|
| relayUrl | optional | string | If no relay is provided, it uses the default relay |
| webhookUrl | required | string | Webhook URL to publish events |
| walletPubkey | required | string | Pubkey of the NWC Wallet Provider |
| connectionPubkey | required | string | Public key of the user (derived from secret in NWC connection string) |


#### Response

```json
Expand Down
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func main() {

e.POST("/nip47/info", svc.InfoHandler)
e.POST("/nip47", svc.NIP47Handler)
e.POST("/nip47/notifications", svc.NIP47NotificationHandler)
e.POST("/publish", svc.PublishHandler)
e.POST("/subscriptions", svc.SubscriptionHandler)
e.DELETE("/subscriptions/:id", svc.StopSubscriptionHandler)
Expand Down
7 changes: 7 additions & 0 deletions internal/nostr/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ type NIP47Request struct {
SignedEvent *nostr.Event `json:"event"`
}

type NIP47NotificationRequest struct {
RelayUrl string `json:"relayUrl"`
WebhookUrl string `json:"webhookUrl"`
WalletPubkey string `json:"walletPubkey"`
ConnPubkey string `json:"connectionPubkey"`
}

type NIP47Response struct {
Event *nostr.Event `json:"event,omitempty"`
State string `json:"state"`
Expand Down
63 changes: 63 additions & 0 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,69 @@ func (svc *Service) NIP47Handler(c echo.Context) error {
})
}

func (svc *Service) NIP47NotificationHandler(c echo.Context) error {
var requestData NIP47NotificationRequest
// send in a pubkey and authenticate by signing
if err := c.Bind(&requestData); err != nil {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: "Error decoding subscription request",
Error: err.Error(),
})
}

if (requestData.WebhookUrl == "") {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: "webhook url is empty",
Error: "no webhook url in request data",
})
}

if (requestData.WalletPubkey == "") {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: "Wallet pubkey is empty",
Error: "no wallet pubkey in request data",
})
}

if (requestData.ConnPubkey == "") {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: "Connection pubkey is empty",
Error: "no connection pubkey in request data",
})
}

subscription := Subscription{
RelayUrl: requestData.RelayUrl,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this optional and we default to our relay?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup it defaults to cfg.DefaultRelayURL ("wss://relay.getalby.com/v1") when not set

WebhookUrl: requestData.WebhookUrl,
Open: true,
Authors: &[]string{requestData.WalletPubkey},
Kinds: &[]int{23196},
}

tags := new(nostr.TagMap)
*tags = make(nostr.TagMap)
(*tags)["p"] = []string{requestData.ConnPubkey}

subscription.Tags = tags


err := svc.db.Create(&subscription).Error

if err != nil {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: "Failed to store subscription",
Error: err.Error(),
})
}

go svc.startSubscription(svc.Ctx, &subscription)

return c.JSON(http.StatusOK, SubscriptionResponse{
SubscriptionId: subscription.Uuid,
WebhookUrl: requestData.WebhookUrl,
})
}

func (svc *Service) SubscriptionHandler(c echo.Context) error {
var requestData SubscriptionRequest
// send in a pubkey and authenticate by signing
Expand Down
Loading