Skip to content

Commit

Permalink
chore: add list transactions and lookup invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jan 4, 2024
1 parent f729389 commit ae9acaa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion alby.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func (svc *AlbyOAuthService) ListTransactions(ctx context.Context, senderPubkey
endpoint += "/outgoing"
}

requestUrl := fmt.Sprintf("%s%s?%s", svc.cfg.AlbyAPIURL, endpoint, urlParams.Encode())
requestUrl := fmt.Sprintf("%s%s?%s", svc.cfg.OAuthAPIURL, endpoint, urlParams.Encode())

req, err := http.NewRequest("GET", requestUrl, nil)
if err != nil {
Expand Down
46 changes: 31 additions & 15 deletions strike.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"math"
"net/http"
"strconv"
"time"

"github.com/golang-jwt/jwt"
"github.com/google/uuid"
Expand Down Expand Up @@ -97,11 +98,17 @@ func (*StrikeOAuthService) SendKeysend(ctx context.Context, senderPubkey string,
return "", errors.New("not implemented")
}

func (svc *StrikeOAuthService) LookupInvoice(ctx context.Context, senderPubkey string, paymentHash string) (invoice string, paid bool, err error) {
return "", false, errors.New("not implemented")
func (svc *StrikeOAuthService) ListTransactions(ctx context.Context, senderPubkey string, from, until, limit, offset uint64, unpaid bool, invoiceType string) (transactions []Nip47Transaction, err error) {
// return empty array for now
return transactions, nil
}

func (svc *StrikeOAuthService) MakeInvoice(ctx context.Context, senderPubkey string, amount int64, description string, descriptionHash string, expiry int64) (invoice string, paymentHash string, err error) {
func (svc *StrikeOAuthService) LookupInvoice(ctx context.Context, senderPubkey string, paymentHash string) (transaction *Nip47Transaction, err error) {
// return empty transaction for now
return transaction, nil
}

func (svc *StrikeOAuthService) MakeInvoice(ctx context.Context, senderPubkey string, amount int64, description string, descriptionHash string, expiry int64) (transaction *Nip47Transaction, err error) {
app := App{}
err = svc.db.Preload("User").First(&app, &App{
NostrPubkey: senderPubkey,
Expand All @@ -114,7 +121,7 @@ func (svc *StrikeOAuthService) MakeInvoice(ctx context.Context, senderPubkey str
"descriptionHash": descriptionHash,
"expiry": expiry,
}).Errorf("App not found: %v", err)
return "", "", err
return nil, err
}

correlationId := uuid.New()
Expand All @@ -128,7 +135,7 @@ func (svc *StrikeOAuthService) MakeInvoice(ctx context.Context, senderPubkey str
"descriptionHash": descriptionHash,
"expiry": expiry,
}).Errorf("amount must be 1000 msat or greater")
return "", "", errors.New("amount must be 1000 msat or greater")
return nil, errors.New("amount must be 1000 msat or greater")
}

svc.Logger.WithFields(logrus.Fields{
Expand All @@ -142,7 +149,7 @@ func (svc *StrikeOAuthService) MakeInvoice(ctx context.Context, senderPubkey str
}).Info("Processing make invoice request")
tok, err := svc.FetchUserToken(ctx, app)
if err != nil {
return "", "", err
return nil, err
}
client := svc.oauthConf.Client(ctx, tok)

Expand All @@ -161,7 +168,7 @@ func (svc *StrikeOAuthService) MakeInvoice(ctx context.Context, senderPubkey str
req, err := http.NewRequest("POST", fmt.Sprintf("%s/invoices", svc.cfg.OAuthAPIURL), body)
if err != nil {
svc.Logger.WithError(err).Error("Error creating request /invoices")
return "", "", err
return nil, err
}

req.Header.Set("User-Agent", "NWC")
Expand All @@ -178,7 +185,7 @@ func (svc *StrikeOAuthService) MakeInvoice(ctx context.Context, senderPubkey str
"appId": app.ID,
"userId": app.User.ID,
}).Errorf("Failed to make invoice: %v", err)
return "", "", err
return nil, err
}

if resp.StatusCode >= 300 {
Expand All @@ -194,19 +201,19 @@ func (svc *StrikeOAuthService) MakeInvoice(ctx context.Context, senderPubkey str
"userId": app.User.ID,
"APIHttpStatus": resp.StatusCode,
}).Errorf("Make invoice failed %s", string(errorPayload.Data.Message))
return "", "", errors.New(errorPayload.Data.Message)
return nil, errors.New(errorPayload.Data.Message)
}

responsePayload := &StrikeInvoiceQuoteResponse{}
err = json.NewDecoder(resp.Body).Decode(responsePayload)
if err != nil {
return "", "", err
return nil, err
}

req, err = http.NewRequest("POST", fmt.Sprintf("%s/invoices/%s/quote", svc.cfg.OAuthAPIURL, responsePayload.InvoiceId), nil)
if err != nil {
svc.Logger.WithError(err).Errorf("Error creating request /invoices/%s/quote", responsePayload.InvoiceId)
return "", "", err
return nil, err
}

req.Header.Set("User-Agent", "NWC")
Expand All @@ -223,14 +230,14 @@ func (svc *StrikeOAuthService) MakeInvoice(ctx context.Context, senderPubkey str
"appId": app.ID,
"userId": app.User.ID,
}).Errorf("Failed to make invoice: %v", err)
return "", "", err
return nil, err
}

if resp.StatusCode < 300 {
responsePayload := &StrikeMakeInvoiceResponse{}
err = json.NewDecoder(resp.Body).Decode(responsePayload)
if err != nil {
return "", "", err
return nil, err
}
svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
Expand All @@ -244,7 +251,16 @@ func (svc *StrikeOAuthService) MakeInvoice(ctx context.Context, senderPubkey str
"paymentHash": "paymentHash",
}).Info("Make invoice successful")
// Payment hash is unsupported
return responsePayload.LnInvoice, "paymentHash", nil
expiresAt := time.Unix(responsePayload.Expiry, 0)
return &Nip47Transaction{
Type: "incoming",
Invoice: responsePayload.LnInvoice,
Description: description,
DescriptionHash: descriptionHash,
PaymentHash: "paymentHash",
ExpiresAt: &expiresAt,
Amount: amount,
}, nil
}

errorPayload := &StrikeErrorResponse{}
Expand All @@ -259,7 +275,7 @@ func (svc *StrikeOAuthService) MakeInvoice(ctx context.Context, senderPubkey str
"userId": app.User.ID,
"APIHttpStatus": resp.StatusCode,
}).Errorf("Make invoice failed %s", string(errorPayload.Data.Message))
return "", "", errors.New(errorPayload.Data.Message)
return nil, errors.New(errorPayload.Data.Message)
}

func (svc *StrikeOAuthService) GetBalance(ctx context.Context, senderPubkey string) (balance int64, err error) {
Expand Down

0 comments on commit ae9acaa

Please sign in to comment.