Skip to content

Commit

Permalink
chore: refactor nip47 subscription handler
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed May 28, 2024
1 parent 6bd57e0 commit 18ec7d9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
52 changes: 38 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ Notifies about new events matching the filter provided via webhooks.

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

### Subscribe to NIP-47 Events
### Subscribe to NWC Events

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

<details>
<summary>
Expand All @@ -197,22 +197,46 @@ Notifies about new NIP-47 response events which are requested by the pubkey to t

#### 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 | optional | string | Public key of the wallet service |
> | pubkey | optional | string | Public key of the user |
| 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
> {
> "subscription_id": "f370d1fc-x0x0-x0x0-x0x0-8f68fa12f32c",
> "webhookUrl": "https://your.webhook.url"
> }
>```
```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>

------------------------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion internal/nostr/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type ResponseEvent struct {

type ErrorResponse struct {
Message string `json:"message"`
Error string `json:"error"`
}

type InfoRequest struct {
Expand All @@ -157,7 +158,7 @@ type NIP47SubscriptionRequest struct {
RelayUrl string `json:"relayUrl"`
WebhookUrl string `json:"webhookUrl"`
WalletPubkey string `json:"walletPubkey"`
Pubkey string `json:"pubkey"`
ConnPubkey string `json:"connectionPubkey"`
}

type SubscriptionRequest struct {
Expand Down
36 changes: 23 additions & 13 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,32 @@ func (svc *Service) NIP47Handler(c echo.Context) error {

func (svc *Service) NIP47SubscriptionHandler(c echo.Context) error {
var requestData NIP47SubscriptionRequest

// send in a pubkey and authenticate by signing
if err := c.Bind(&requestData); err != nil {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: fmt.Sprintf("error decoding subscription request: %s", err.Error()),
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",
})
}

Expand All @@ -299,27 +314,22 @@ func (svc *Service) NIP47SubscriptionHandler(c echo.Context) error {

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

subscription.Tags = tags

svc.db.Create(&subscription)

errorChan := make(chan error, 1)
ctx, cancel := context.WithCancel(svc.Ctx)
svc.mu.Lock()
svc.subscriptions[subscription.ID] = cancel
svc.mu.Unlock()
go svc.handleSubscription(ctx, &subscription, errorChan)
err := svc.db.Create(&subscription).Error

err := <-errorChan
if err != nil {
svc.stopSubscription(&subscription)
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: fmt.Sprintf("error setting up subscription %s",err.Error()),
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,
Expand Down

0 comments on commit 18ec7d9

Please sign in to comment.