Skip to content

Commit

Permalink
fix: issues with multi_pay_invoice
Browse files Browse the repository at this point in the history
- fix permission check
- do not change tags after decrypting
  • Loading branch information
rolznz committed Jan 22, 2024
1 parent b1a43fe commit 3fad688
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 48 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,15 @@ You can also contribute to our [bounty program](https://github.com/getAlby/light

`pay_invoice`
- ⚠️ amount not supported (for amountless invoices)
- ⚠️ PAYMENT_FAILED error code not supported

`pay_keysend`
- ⚠️ PAYMENT_FAILED error code not supported

`make_invoice`

`lookup_invoice`
- ⚠️ NOT_FOUND error code not supported

`list_transactions`
- ⚠️ from and until in request not supported
Expand All @@ -161,15 +164,18 @@ You can also contribute to our [bounty program](https://github.com/getAlby/light

`pay_invoice`
- ⚠️ amount not supported (for amountless invoices)
- ⚠️ PAYMENT_FAILED error code not supported

`pay_keysend`
- ⚠️ preimage in request not supported
- ⚠️ PAYMENT_FAILED error code not supported

`make_invoice`
- ⚠️ expiry in request not supported

`lookup_invoice`
- ⚠️ fees_paid in response not supported
- ⚠️ NOT_FOUND error code not supported

`list_transactions`
- ⚠️ offset and unpaid in request not supported
Expand Down
7 changes: 3 additions & 4 deletions handle_balance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (svc *Service) HandleGetBalanceEvent(ctx context.Context, request *Nip47Req
Error: &Nip47Error{
Code: code,
Message: message,
}}, ss)
}}, nostr.Tags{}, ss)
}

svc.Logger.WithFields(logrus.Fields{
Expand All @@ -63,7 +63,7 @@ func (svc *Service) HandleGetBalanceEvent(ctx context.Context, request *Nip47Req
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Something went wrong while fetching balance: %s", err.Error()),
},
}, ss)
}, nostr.Tags{}, ss)
}

responsePayload := &Nip47BalanceResponse{
Expand All @@ -84,6 +84,5 @@ func (svc *Service) HandleGetBalanceEvent(ctx context.Context, request *Nip47Req
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_GET_BALANCE_METHOD,
Result: responsePayload,
},
ss)
}, nostr.Tags{}, ss)
}
6 changes: 3 additions & 3 deletions handle_info_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (svc *Service) HandleGetInfoEvent(ctx context.Context, request *Nip47Reques
Error: &Nip47Error{
Code: code,
Message: message,
}}, ss)
}}, nostr.Tags{}, ss)
}

svc.Logger.WithFields(logrus.Fields{
Expand All @@ -59,7 +59,7 @@ func (svc *Service) HandleGetInfoEvent(ctx context.Context, request *Nip47Reques
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Something went wrong while fetching node info: %s", err.Error()),
},
}, ss)
}, nostr.Tags{}, ss)
}

responsePayload := &Nip47GetInfoResponse{
Expand All @@ -77,5 +77,5 @@ func (svc *Service) HandleGetInfoEvent(ctx context.Context, request *Nip47Reques
return svc.createResponse(event, Nip47Response{
ResultType: request.Method,
Result: responsePayload,
}, ss)
}, nostr.Tags{}, ss)
}
7 changes: 3 additions & 4 deletions handle_list_transactions_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (svc *Service) HandleListTransactionsEvent(ctx context.Context, request *Ni
Error: &Nip47Error{
Code: code,
Message: message,
}}, ss)
}}, nostr.Tags{}, ss)
}

svc.Logger.WithFields(logrus.Fields{
Expand All @@ -74,7 +74,7 @@ func (svc *Service) HandleListTransactionsEvent(ctx context.Context, request *Ni
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Something went wrong while fetching transactions: %s", err.Error()),
},
}, ss)
}, nostr.Tags{}, ss)
}

responsePayload := &Nip47ListTransactionsResponse{
Expand All @@ -87,6 +87,5 @@ func (svc *Service) HandleListTransactionsEvent(ctx context.Context, request *Ni
return svc.createResponse(event, Nip47Response{
ResultType: request.Method,
Result: responsePayload,
},
ss)
}, nostr.Tags{}, ss)
}
9 changes: 4 additions & 5 deletions handle_lookup_invoice_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (svc *Service) HandleLookupInvoiceEvent(ctx context.Context, request *Nip47
Error: &Nip47Error{
Code: code,
Message: message,
}}, ss)
}}, nostr.Tags{}, ss)
}

// TODO: move to a shared generic function
Expand Down Expand Up @@ -80,7 +80,7 @@ func (svc *Service) HandleLookupInvoiceEvent(ctx context.Context, request *Nip47
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Failed to decode bolt11 invoice: %s", err.Error()),
},
}, ss)
}, nostr.Tags{}, ss)
}
paymentHash = paymentRequest.PaymentHash
}
Expand All @@ -102,7 +102,7 @@ func (svc *Service) HandleLookupInvoiceEvent(ctx context.Context, request *Nip47
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Something went wrong while looking up invoice: %s", err.Error()),
},
}, ss)
}, nostr.Tags{}, ss)
}

responsePayload := &Nip47LookupInvoiceResponse{
Expand All @@ -114,6 +114,5 @@ func (svc *Service) HandleLookupInvoiceEvent(ctx context.Context, request *Nip47
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_LOOKUP_INVOICE_METHOD,
Result: responsePayload,
},
ss)
}, nostr.Tags{}, ss)
}
9 changes: 4 additions & 5 deletions handle_make_invoice_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Re
Error: &Nip47Error{
Code: code,
Message: message,
}}, ss)
}}, nostr.Tags{}, ss)
}

// TODO: move to a shared generic function
Expand Down Expand Up @@ -68,7 +68,7 @@ func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Re
Code: NIP_47_OTHER,
Message: "Only one of description, description_hash can be provided",
},
}, ss)
}, nostr.Tags{}, ss)
}

svc.Logger.WithFields(logrus.Fields{
Expand Down Expand Up @@ -100,7 +100,7 @@ func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Re
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Something went wrong while making invoice: %s", err.Error()),
},
}, ss)
}, nostr.Tags{}, ss)
}

responsePayload := &Nip47MakeInvoiceResponse{
Expand All @@ -112,6 +112,5 @@ func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Re
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_MAKE_INVOICE_METHOD,
Result: responsePayload,
},
ss)
}, nostr.Tags{}, ss)
}
27 changes: 12 additions & 15 deletions handle_multi_pay_invoice_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ 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
dTag := []string{"d", invoiceInfo.Id}
resp, err := svc.createResponse(event, Nip47Response{
ResultType: NIP_47_MULTI_PAY_INVOICE_METHOD,
Error: &Nip47Error{
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Failed to decode bolt11 invoice: %s", err.Error()),
},
}, ss)
}, nostr.Tags{dTag}, ss)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
Expand All @@ -77,20 +79,18 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
}).Errorf("Failed to process event: %v", err)
return
}
// TODO: Decide what to do if id is empty
dTag := []string{"a", fmt.Sprintf("%d:%s:%s", NIP_47_RESPONSE_KIND, event.PubKey, invoiceInfo.Id)}
resp.Tags = append(resp.Tags, dTag)

svc.PublishEvent(ctx, sub, event, resp)
return
}

id := invoiceInfo.Id
if id == "" {
id = paymentRequest.PaymentHash
invoiceDTagValue := invoiceInfo.Id
if invoiceDTagValue == "" {
invoiceDTagValue = paymentRequest.PaymentHash
}
dTag := []string{"a", fmt.Sprintf("%d:%s:%s", NIP_47_RESPONSE_KIND, event.PubKey, id)}
dTag := []string{"d", invoiceDTagValue}

hasPermission, code, message := svc.hasPermission(&app, event, request.Method, paymentRequest.MSatoshi)
hasPermission, code, message := svc.hasPermission(&app, event, NIP_47_PAY_INVOICE_METHOD, paymentRequest.MSatoshi)

if !hasPermission {
svc.Logger.WithFields(logrus.Fields{
Expand All @@ -105,7 +105,7 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
Code: code,
Message: message,
},
}, ss)
}, nostr.Tags{dTag}, ss)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
Expand All @@ -115,7 +115,6 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
}).Errorf("Failed to process event: %v", err)
return
}
resp.Tags = append(resp.Tags, dTag)
svc.PublishEvent(ctx, sub, event, resp)
return
}
Expand Down Expand Up @@ -157,7 +156,7 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Something went wrong while paying invoice: %s", err.Error()),
},
}, ss)
}, nostr.Tags{dTag}, ss)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
Expand All @@ -167,7 +166,6 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
}).Errorf("Failed to process event: %v", err)
return
}
resp.Tags = append(resp.Tags, dTag)
svc.PublishEvent(ctx, sub, event, resp)
return
}
Expand All @@ -181,7 +179,7 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
Result: Nip47PayResponse{
Preimage: preimage,
},
}, ss)
}, nostr.Tags{dTag}, ss)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
Expand All @@ -191,7 +189,6 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
}).Errorf("Failed to process event: %v", err)
return
}
resp.Tags = append(resp.Tags, dTag)
svc.PublishEvent(ctx, sub, event, resp)
}(invoiceInfo)
}
Expand Down
6 changes: 3 additions & 3 deletions handle_pay_keysend_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (svc *Service) HandlePayKeysendEvent(ctx context.Context, request *Nip47Req
Error: &Nip47Error{
Code: code,
Message: message,
}}, ss)
}}, nostr.Tags{}, ss)
}

payment := Payment{App: app, NostrEvent: nostrEvent, Amount: uint(payParams.Amount / 1000)}
Expand Down Expand Up @@ -81,7 +81,7 @@ func (svc *Service) HandlePayKeysendEvent(ctx context.Context, request *Nip47Req
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Something went wrong while paying invoice: %s", err.Error()),
},
}, ss)
}, nostr.Tags{}, ss)
}
payment.Preimage = &preimage
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
Expand All @@ -92,5 +92,5 @@ func (svc *Service) HandlePayKeysendEvent(ctx context.Context, request *Nip47Req
Result: Nip47PayResponse{
Preimage: preimage,
},
}, ss)
}, nostr.Tags{}, ss)
}
8 changes: 4 additions & 4 deletions handle_payment_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (svc *Service) HandlePayInvoiceEvent(ctx context.Context, request *Nip47Req
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Failed to decode bolt11 invoice: %s", err.Error()),
},
}, ss)
}, nostr.Tags{}, ss)
}

hasPermission, code, message := svc.hasPermission(&app, event, request.Method, paymentRequest.MSatoshi)
Expand All @@ -70,7 +70,7 @@ func (svc *Service) HandlePayInvoiceEvent(ctx context.Context, request *Nip47Req
Error: &Nip47Error{
Code: code,
Message: message,
}}, ss)
}}, nostr.Tags{}, ss)
}

payment := Payment{App: app, NostrEvent: nostrEvent, PaymentRequest: bolt11, Amount: uint(paymentRequest.MSatoshi / 1000)}
Expand Down Expand Up @@ -102,7 +102,7 @@ func (svc *Service) HandlePayInvoiceEvent(ctx context.Context, request *Nip47Req
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Something went wrong while paying invoice: %s", err.Error()),
},
}, ss)
}, nostr.Tags{}, ss)
}
payment.Preimage = &preimage
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
Expand All @@ -113,5 +113,5 @@ func (svc *Service) HandlePayInvoiceEvent(ctx context.Context, request *Nip47Req
Result: Nip47PayResponse{
Preimage: preimage,
},
}, ss)
}, nostr.Tags{}, ss)
}
2 changes: 1 addition & 1 deletion models.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
NIP_47_ERROR_EXPIRED = "EXPIRED"
NIP_47_ERROR_RESTRICTED = "RESTRICTED"
NIP_47_OTHER = "OTHER"
NIP_47_CAPABILITIES = "pay_invoice,pay_keysend,get_balance,get_info,make_invoice,lookup_invoice,list_transactions"
NIP_47_CAPABILITIES = "pay_invoice,pay_keysend,get_balance,get_info,make_invoice,lookup_invoice,list_transactions,multi_pay_invoice"
)

const (
Expand Down
12 changes: 8 additions & 4 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (svc *Service) handleAndPublishEvent(ctx context.Context, sub *nostr.Subscr
Code: NIP_47_ERROR_UNAUTHORIZED,
Message: "The public key does not have a wallet connected.",
},
}, ss)
}, nostr.Tags{}, ss)
svc.PublishEvent(ctx, sub, event, resp)
return
}
Expand Down Expand Up @@ -238,7 +238,7 @@ func (svc *Service) handleAndPublishEvent(ctx context.Context, sub *nostr.Subscr
Error: &Nip47Error{
Code: NIP_47_ERROR_NOT_IMPLEMENTED,
Message: fmt.Sprintf("Unknown method: %s", nip47Request.Method),
}}, ss)
}}, nostr.Tags{}, ss)
}

if err != nil {
Expand All @@ -252,7 +252,7 @@ func (svc *Service) handleAndPublishEvent(ctx context.Context, sub *nostr.Subscr
}
}

func (svc *Service) createResponse(initialEvent *nostr.Event, content interface{}, ss []byte) (result *nostr.Event, err error) {
func (svc *Service) createResponse(initialEvent *nostr.Event, content interface{}, tags nostr.Tags, ss []byte) (result *nostr.Event, err error) {
payloadBytes, err := json.Marshal(content)
if err != nil {
return nil, err
Expand All @@ -261,11 +261,15 @@ func (svc *Service) createResponse(initialEvent *nostr.Event, content interface{
if err != nil {
return nil, err
}

allTags := nostr.Tags{[]string{"p", initialEvent.PubKey}, []string{"e", initialEvent.ID}}
allTags = append(allTags, tags...)

resp := &nostr.Event{
PubKey: svc.cfg.IdentityPubkey,
CreatedAt: nostr.Now(),
Kind: NIP_47_RESPONSE_KIND,
Tags: nostr.Tags{[]string{"p", initialEvent.PubKey}, []string{"e", initialEvent.ID}},
Tags: allTags,
Content: msg,
}
err = resp.Sign(svc.cfg.NostrSecretKey)
Expand Down

0 comments on commit 3fad688

Please sign in to comment.