diff --git a/handle_balance_request.go b/handle_balance_request.go index 2e68e22b..552051ea 100644 --- a/handle_balance_request.go +++ b/handle_balance_request.go @@ -14,7 +14,7 @@ const ( func (svc *Service) HandleGetBalanceEvent(ctx context.Context, request *Nip47Request, event *nostr.Event, app App, ss []byte) (result *nostr.Event, err error) { - nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"} + nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content} err = svc.db.Create(&nostrEvent).Error if err != nil { svc.Logger.WithFields(logrus.Fields{ @@ -55,7 +55,6 @@ func (svc *Service) HandleGetBalanceEvent(ctx context.Context, request *Nip47Req "eventKind": event.Kind, "appId": app.ID, }).Infof("Failed to fetch balance: %v", err) - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR svc.db.Save(&nostrEvent) return svc.createResponse(event, Nip47Response{ ResultType: NIP_47_GET_BALANCE_METHOD, @@ -79,7 +78,6 @@ func (svc *Service) HandleGetBalanceEvent(ctx context.Context, request *Nip47Req responsePayload.BudgetRenewal = appPermission.BudgetRenewal } - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED svc.db.Save(&nostrEvent) return svc.createResponse(event, Nip47Response{ ResultType: NIP_47_GET_BALANCE_METHOD, diff --git a/handle_info_request.go b/handle_info_request.go index 041be66b..52f719e0 100644 --- a/handle_info_request.go +++ b/handle_info_request.go @@ -10,7 +10,7 @@ import ( func (svc *Service) HandleGetInfoEvent(ctx context.Context, request *Nip47Request, event *nostr.Event, app App, ss []byte) (result *nostr.Event, err error) { - nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"} + nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content} err = svc.db.Create(&nostrEvent).Error if err != nil { svc.Logger.WithFields(logrus.Fields{ @@ -51,7 +51,6 @@ func (svc *Service) HandleGetInfoEvent(ctx context.Context, request *Nip47Reques "eventKind": event.Kind, "appId": app.ID, }).Infof("Failed to fetch node info: %v", err) - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR svc.db.Save(&nostrEvent) return svc.createResponse(event, Nip47Response{ ResultType: request.Method, @@ -72,7 +71,6 @@ func (svc *Service) HandleGetInfoEvent(ctx context.Context, request *Nip47Reques Methods: svc.GetMethods(&app), } - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED svc.db.Save(&nostrEvent) return svc.createResponse(event, Nip47Response{ ResultType: request.Method, diff --git a/handle_list_transactions_request.go b/handle_list_transactions_request.go index acc9ec72..d908ef8c 100644 --- a/handle_list_transactions_request.go +++ b/handle_list_transactions_request.go @@ -11,7 +11,7 @@ import ( func (svc *Service) HandleListTransactionsEvent(ctx context.Context, request *Nip47Request, event *nostr.Event, app App, ss []byte) (result *nostr.Event, err error) { - nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"} + nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content} err = svc.db.Create(&nostrEvent).Error if err != nil { svc.Logger.WithFields(logrus.Fields{ @@ -66,7 +66,6 @@ func (svc *Service) HandleListTransactionsEvent(ctx context.Context, request *Ni "eventKind": event.Kind, "appId": app.ID, }).Infof("Failed to fetch transactions: %v", err) - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR svc.db.Save(&nostrEvent) return svc.createResponse(event, Nip47Response{ ResultType: request.Method, @@ -82,7 +81,6 @@ func (svc *Service) HandleListTransactionsEvent(ctx context.Context, request *Ni } // fmt.Println(responsePayload) - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED svc.db.Save(&nostrEvent) return svc.createResponse(event, Nip47Response{ ResultType: request.Method, diff --git a/handle_lookup_invoice_request.go b/handle_lookup_invoice_request.go index de1d979d..a0b21ea4 100644 --- a/handle_lookup_invoice_request.go +++ b/handle_lookup_invoice_request.go @@ -13,7 +13,7 @@ import ( func (svc *Service) HandleLookupInvoiceEvent(ctx context.Context, request *Nip47Request, event *nostr.Event, app App, ss []byte) (result *nostr.Event, err error) { // TODO: move to a shared function - nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"} + nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content} err = svc.db.Create(&nostrEvent).Error if err != nil { svc.Logger.WithFields(logrus.Fields{ @@ -94,7 +94,6 @@ func (svc *Service) HandleLookupInvoiceEvent(ctx context.Context, request *Nip47 "invoice": lookupInvoiceParams.Invoice, "paymentHash": lookupInvoiceParams.PaymentHash, }).Infof("Failed to lookup invoice: %v", err) - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR svc.db.Save(&nostrEvent) return svc.createResponse(event, Nip47Response{ ResultType: NIP_47_LOOKUP_INVOICE_METHOD, @@ -109,7 +108,6 @@ func (svc *Service) HandleLookupInvoiceEvent(ctx context.Context, request *Nip47 Nip47Transaction: *transaction, } - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED svc.db.Save(&nostrEvent) return svc.createResponse(event, Nip47Response{ ResultType: NIP_47_LOOKUP_INVOICE_METHOD, diff --git a/handle_make_invoice_request.go b/handle_make_invoice_request.go index 952aeda7..64a3dff9 100644 --- a/handle_make_invoice_request.go +++ b/handle_make_invoice_request.go @@ -14,7 +14,7 @@ import ( func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Request, event *nostr.Event, app App, ss []byte) (result *nostr.Event, err error) { // TODO: move to a shared function - nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"} + nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content} err = svc.db.Create(&nostrEvent).Error if err != nil { svc.Logger.WithFields(logrus.Fields{ @@ -92,7 +92,6 @@ func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Re "descriptionHash": makeInvoiceParams.DescriptionHash, "expiry": makeInvoiceParams.Expiry, }).Infof("Failed to make invoice: %v", err) - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR svc.db.Save(&nostrEvent) return svc.createResponse(event, Nip47Response{ ResultType: NIP_47_MAKE_INVOICE_METHOD, @@ -107,7 +106,6 @@ func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Re Nip47Transaction: *transaction, } - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED svc.db.Save(&nostrEvent) return svc.createResponse(event, Nip47Response{ ResultType: NIP_47_MAKE_INVOICE_METHOD, diff --git a/handle_multi_pay_invoice_request.go b/handle_multi_pay_invoice_request.go index 865af37a..713a8315 100644 --- a/handle_multi_pay_invoice_request.go +++ b/handle_multi_pay_invoice_request.go @@ -14,7 +14,7 @@ import ( func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.Subscription, request *Nip47Request, event *nostr.Event, app App, ss []byte) { - nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"} + nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content} err := svc.db.Create(&nostrEvent).Error if err != nil { svc.Logger.WithFields(logrus.Fields{ @@ -61,7 +61,9 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S "bolt11": bolt11, }).Errorf("Failed to decode bolt11 invoice: %v", err) - // TODO: Decide what to do if id is empty + // NOTE: if Id is empty, and empty d tag will be returned because + // the invoice cannot be parsed to return the payment hash. + // Currently clients must ensure they pass valid BOLT 11 invoices to this method. dTag := []string{"d", invoiceInfo.Id} resp, err := svc.createResponse(event, Nip47Response{ ResultType: NIP_47_MULTI_PAY_INVOICE_METHOD, @@ -146,8 +148,6 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S "appId": app.ID, "bolt11": bolt11, }).Infof("Failed to send payment: %v", err) - // TODO: What to do here? - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR svc.db.Save(&nostrEvent) resp, err := svc.createResponse(event, Nip47Response{ @@ -166,12 +166,11 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S }).Errorf("Failed to process event: %v", err) return } + svc.PublishEvent(ctx, sub, event, resp) return } payment.Preimage = &preimage - // TODO: What to do here? - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED svc.db.Save(&nostrEvent) svc.db.Save(&payment) resp, err := svc.createResponse(event, Nip47Response{ diff --git a/handle_pay_keysend_request.go b/handle_pay_keysend_request.go index 7ebf7493..a5067807 100644 --- a/handle_pay_keysend_request.go +++ b/handle_pay_keysend_request.go @@ -11,7 +11,7 @@ import ( func (svc *Service) HandlePayKeysendEvent(ctx context.Context, request *Nip47Request, event *nostr.Event, app App, ss []byte) (result *nostr.Event, err error) { - nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"} + nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content} err = svc.db.Create(&nostrEvent).Error if err != nil { svc.Logger.WithFields(logrus.Fields{ @@ -73,7 +73,6 @@ func (svc *Service) HandlePayKeysendEvent(ctx context.Context, request *Nip47Req "appId": app.ID, "senderPubkey": payParams.Pubkey, }).Infof("Failed to send payment: %v", err) - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR svc.db.Save(&nostrEvent) return svc.createResponse(event, Nip47Response{ ResultType: request.Method, @@ -84,7 +83,6 @@ func (svc *Service) HandlePayKeysendEvent(ctx context.Context, request *Nip47Req }, nostr.Tags{}, ss) } payment.Preimage = &preimage - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED svc.db.Save(&nostrEvent) svc.db.Save(&payment) return svc.createResponse(event, Nip47Response{ diff --git a/handle_payment_request.go b/handle_payment_request.go index 12550d2f..f9287c2a 100644 --- a/handle_payment_request.go +++ b/handle_payment_request.go @@ -13,7 +13,7 @@ import ( func (svc *Service) HandlePayInvoiceEvent(ctx context.Context, request *Nip47Request, event *nostr.Event, app App, ss []byte) (result *nostr.Event, err error) { - nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"} + nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content} err = svc.db.Create(&nostrEvent).Error if err != nil { svc.Logger.WithFields(logrus.Fields{ @@ -94,7 +94,6 @@ func (svc *Service) HandlePayInvoiceEvent(ctx context.Context, request *Nip47Req "appId": app.ID, "bolt11": bolt11, }).Infof("Failed to send payment: %v", err) - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR svc.db.Save(&nostrEvent) return svc.createResponse(event, Nip47Response{ ResultType: NIP_47_PAY_INVOICE_METHOD, @@ -105,7 +104,6 @@ func (svc *Service) HandlePayInvoiceEvent(ctx context.Context, request *Nip47Req }, nostr.Tags{}, ss) } payment.Preimage = &preimage - nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED svc.db.Save(&nostrEvent) svc.db.Save(&payment) return svc.createResponse(event, Nip47Response{ diff --git a/models.go b/models.go index 11e82165..d40c8233 100644 --- a/models.go +++ b/models.go @@ -105,10 +105,8 @@ type NostrEvent struct { AppId uint `validate:"required"` App App NostrId string `validate:"required"` - ReplyId string Content string - State string - RepliedAt time.Time + RepliedAt time.Time // for multi_* events this will be the last reply CreatedAt time.Time UpdatedAt time.Time } diff --git a/service.go b/service.go index 03b8888e..8dff0170 100644 --- a/service.go +++ b/service.go @@ -23,15 +23,6 @@ type Service struct { Logger *logrus.Logger } -/*var supportedMethods = map[string]bool{ - NIP_47_PAY_INVOICE_METHOD: true, - NIP_47_GET_BALANCE_METHOD: true, - NIP_47_GET_INFO_METHOD: true, - NIP_47_MAKE_INVOICE_METHOD: true, - NIP_47_LOOKUP_INVOICE_METHOD: true, - NIP_47_LIST_TRANSACTIONS_METHOD: true, -}*/ - func (svc *Service) GetUser(c echo.Context) (user *User, err error) { sess, _ := session.Get(CookieName, c) userID := sess.Values["user_id"] @@ -61,7 +52,7 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti svc.Logger.Info("Received EOS") for event := range sub.Events { - go svc.handleAndPublishEvent(ctx, sub, event) + go svc.HandleEvent(ctx, sub, event) } svc.Logger.Info("Subscription ended") }() @@ -101,10 +92,8 @@ func (svc *Service) PublishEvent(ctx context.Context, sub *nostr.Subscription, e }).Error(result.Error) return } - nostrEvent.ReplyId = resp.ID if status == nostr.PublishStatusSucceeded { - nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_CONFIRMED nostrEvent.RepliedAt = time.Now() svc.db.Save(&nostrEvent) svc.Logger.WithFields(logrus.Fields{ @@ -115,7 +104,6 @@ func (svc *Service) PublishEvent(ctx context.Context, sub *nostr.Subscription, e "appId": nostrEvent.AppId, }).Info("Published reply") } else if status == nostr.PublishStatusFailed { - nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_FAILED svc.db.Save(&nostrEvent) svc.Logger.WithFields(logrus.Fields{ "nostrEventId": nostrEvent.ID, @@ -125,7 +113,6 @@ func (svc *Service) PublishEvent(ctx context.Context, sub *nostr.Subscription, e "appId": nostrEvent.AppId, }).Info("Failed to publish reply") } else { - nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_UNCONFIRMED svc.db.Save(&nostrEvent) svc.Logger.WithFields(logrus.Fields{ "nostrEventId": nostrEvent.ID, @@ -137,7 +124,7 @@ func (svc *Service) PublishEvent(ctx context.Context, sub *nostr.Subscription, e } } -func (svc *Service) handleAndPublishEvent(ctx context.Context, sub *nostr.Subscription, event *nostr.Event) { +func (svc *Service) HandleEvent(ctx context.Context, sub *nostr.Subscription, event *nostr.Event) { var resp *nostr.Event svc.Logger.WithFields(logrus.Fields{ "eventId": event.ID,