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: transaction metadata (zaps, payerdata, comment) #678

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (api *api) GetApp(dbApp *db.App) *App {
maxAmount := uint64(paySpecificPermission.MaxAmountSat)
budgetUsage = queries.GetBudgetUsageSat(api.db, &paySpecificPermission)

var metadata Metadata
var metadata AppMetadata
if dbApp.Metadata != nil {
jsonErr := json.Unmarshal(dbApp.Metadata, &metadata)
if jsonErr != nil {
Expand Down Expand Up @@ -334,7 +334,7 @@ func (api *api) ListApps() ([]App, error) {
apiApp.LastEventAt = &lastEvent.CreatedAt
}

var metadata Metadata
var metadata AppMetadata
if dbApp.Metadata != nil {
jsonErr := json.Unmarshal(dbApp.Metadata, &metadata)
if jsonErr != nil {
Expand Down
136 changes: 103 additions & 33 deletions api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"context"
"encoding/json"
"io"
"time"

Expand Down Expand Up @@ -56,46 +57,48 @@ type API interface {
}

type App struct {
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
NostrPubkey string `json:"nostrPubkey"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
LastEventAt *time.Time `json:"lastEventAt"`
ExpiresAt *time.Time `json:"expiresAt"`
Scopes []string `json:"scopes"`
MaxAmountSat uint64 `json:"maxAmount"`
BudgetUsage uint64 `json:"budgetUsage"`
BudgetRenewal string `json:"budgetRenewal"`
Isolated bool `json:"isolated"`
Balance uint64 `json:"balance"`
Metadata Metadata `json:"metadata,omitempty"`
}
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
NostrPubkey string `json:"nostrPubkey"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
LastEventAt *time.Time `json:"lastEventAt"`
ExpiresAt *time.Time `json:"expiresAt"`
Scopes []string `json:"scopes"`
MaxAmountSat uint64 `json:"maxAmount"`
BudgetUsage uint64 `json:"budgetUsage"`
BudgetRenewal string `json:"budgetRenewal"`
Isolated bool `json:"isolated"`
Balance uint64 `json:"balance"`
Metadata AppMetadata `json:"metadata,omitempty"`
}

type AppMetadata = map[string]interface{}

type ListAppsResponse struct {
Apps []App `json:"apps"`
}

type UpdateAppRequest struct {
Name string `json:"name"`
MaxAmountSat uint64 `json:"maxAmount"`
BudgetRenewal string `json:"budgetRenewal"`
ExpiresAt string `json:"expiresAt"`
Scopes []string `json:"scopes"`
Metadata Metadata `json:"metadata,omitempty"`
Name string `json:"name"`
MaxAmountSat uint64 `json:"maxAmount"`
BudgetRenewal string `json:"budgetRenewal"`
ExpiresAt string `json:"expiresAt"`
Scopes []string `json:"scopes"`
Metadata AppMetadata `json:"metadata,omitempty"`
}

type CreateAppRequest struct {
Name string `json:"name"`
Pubkey string `json:"pubkey"`
MaxAmountSat uint64 `json:"maxAmount"`
BudgetRenewal string `json:"budgetRenewal"`
ExpiresAt string `json:"expiresAt"`
Scopes []string `json:"scopes"`
ReturnTo string `json:"returnTo"`
Isolated bool `json:"isolated"`
Metadata Metadata `json:"metadata,omitempty"`
Name string `json:"name"`
Pubkey string `json:"pubkey"`
MaxAmountSat uint64 `json:"maxAmount"`
BudgetRenewal string `json:"budgetRenewal"`
ExpiresAt string `json:"expiresAt"`
Scopes []string `json:"scopes"`
ReturnTo string `json:"returnTo"`
Isolated bool `json:"isolated"`
Metadata AppMetadata `json:"metadata,omitempty"`
}

type StartRequest struct {
Expand Down Expand Up @@ -218,12 +221,79 @@ type Transaction struct {
CreatedAt string `json:"createdAt"`
SettledAt *string `json:"settledAt"`
AppId *uint `json:"appId"`
Metadata Metadata `json:"metadata,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
Boostagram *Boostagram `json:"boostagram,omitempty"`
}

type Metadata = map[string]interface{}
type Metadata struct {
im-adithya marked this conversation as resolved.
Show resolved Hide resolved
Comment string `json:"comment,omitempty"`
Nostr *NostrEvent `json:"nostr,omitempty"`
PayerData *PayerData `json:"payerData,omitempty"`
}

func (m *Metadata) UnmarshalJSON(data []byte) error {
var t struct {
Comment string `json:"comment"`
Nostr *NostrEvent `json:"nostr"`
PayerData *PayerData `json:"payer_data"`
}

if err := json.Unmarshal(data, &t); err != nil {
return err
}

m.Comment = t.Comment
m.Nostr = t.Nostr
m.PayerData = t.PayerData

return nil
}

type NostrEvent struct {
Content string `json:"content"`
CreatedAt int64 `json:"createdAt"`
ID string `json:"id"`
Kind int `json:"kind"`
PubKey string `json:"pubkey"`
Sig string `json:"sig"`
Tags [][]string `json:"tags"`
}

func (ne *NostrEvent) UnmarshalJSON(data []byte) error {
var t struct {
Content string `json:"content"`
CreatedAt int64 `json:"created_at"`
ID string `json:"id"`
Kind int `json:"kind"`
PubKey string `json:"pubkey"`
Sig string `json:"sig"`
Tags [][]string `json:"tags"`
}

if err := json.Unmarshal(data, &t); err != nil {
return err
}

ne.Content = t.Content
ne.CreatedAt = t.CreatedAt
ne.ID = t.ID
ne.Kind = t.Kind
ne.PubKey = t.PubKey
ne.Sig = t.Sig
ne.Tags = t.Tags

return nil
}

// unmarshal method is only needed for structs with snake_case fields
type PayerData struct {
im-adithya marked this conversation as resolved.
Show resolved Hide resolved
Email string `json:"email,omitempty"`
Name string `json:"name,omitempty"`
Pubkey string `json:"pubkey,omitempty"`
}

// we did not add the unmarshal method here as Boostagram
// struct is anyways being used in transaction service
type Boostagram struct {
AppName string `json:"appName"`
Name string `json:"name"`
Expand Down
25 changes: 3 additions & 22 deletions api/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ func toApiTransaction(transaction *transactions.Transaction) *Transaction {
preimage = transaction.Preimage
}

var metadata Metadata
var metadata *Metadata
if transaction.Metadata != nil {
jsonErr := json.Unmarshal(transaction.Metadata, &metadata)
if jsonErr != nil {
logger.Logger.WithError(jsonErr).WithFields(logrus.Fields{
"payment_hash": transaction.PaymentHash,
"metadata": transaction.Metadata,
}).Error("Failed to deserialize transaction metadata")
}).Error("Failed to deserialize transaction metadata info")
}
}

Expand All @@ -94,7 +94,7 @@ func toApiTransaction(transaction *transactions.Transaction) *Transaction {
"boostagram": transaction.Boostagram,
}).Error("Failed to deserialize transaction boostagram info")
}
boostagram = toApiBoostagram(&txBoostagram)
boostagram = (*Boostagram)(&txBoostagram)
}

return &Transaction{
Expand All @@ -113,22 +113,3 @@ func toApiTransaction(transaction *transactions.Transaction) *Transaction {
Boostagram: boostagram,
}
}

func toApiBoostagram(boostagram *transactions.Boostagram) *Boostagram {
return &Boostagram{
AppName: boostagram.AppName,
Name: boostagram.Name,
Podcast: boostagram.Podcast,
URL: boostagram.URL,
Episode: boostagram.Episode,
FeedId: boostagram.FeedId,
ItemId: boostagram.ItemId,
Timestamp: boostagram.Timestamp,
Message: boostagram.Message,
SenderId: boostagram.SenderId,
SenderName: boostagram.SenderName,
Time: boostagram.Time,
Action: boostagram.Action,
ValueMsatTotal: boostagram.ValueMsatTotal,
}
}
24 changes: 23 additions & 1 deletion frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,32 @@ export type Transaction = {
feesPaid: number;
createdAt: string;
settledAt: string | undefined;
metadata?: Record<string, unknown>;
metadata?: Metadata;
boostagram?: Boostagram;
};

export type Metadata = {
comment?: string;
nostr?: NostrEvent;
payerData?: PayerData;
};

export type NostrEvent = {
content: string;
createdAt: number;
id: string;
kind: number;
pubkey: string;
sig: string;
tags: string[][];
};

export type PayerData = {
email?: string;
name?: string;
pubkey?: string;
};

export type Boostagram = {
appName: string;
name: string;
Expand Down
23 changes: 23 additions & 0 deletions transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ type Boostagram struct {
ValueMsatTotal int64 `json:"value_msat_total"`
}

type Metadata struct {
Comment string `json:"comment"`
Nostr *NostrEvent `json:"nostr"`
PayerData *PayerData `json:"payer_data"`
AdditionalFields map[string]interface{} `json:"-"`
}

type NostrEvent struct {
Content string `json:"content"`
CreatedAt int64 `json:"created_at"`
ID string `json:"id"`
Kind int `json:"kind"`
PubKey string `json:"pubkey"`
Sig string `json:"sig"`
Tags [][]string `json:"tags"`
}

type PayerData struct {
Email string `json:"email"`
Name string `json:"name"`
Pubkey string `json:"pubkey"`
}

type notFoundError struct {
}

Expand Down
Loading