From eb84d636e79e56dd9dfcb9c80070c6d37ced5a3e Mon Sep 17 00:00:00 2001 From: im-adithya Date: Wed, 7 Aug 2024 00:53:15 +0530 Subject: [PATCH] feat: add boostagram column and use jsonb in metadata --- api/models.go | 51 ++++++----- api/transactions.go | 35 +++---- ...2408061737_add_boostagrams_and_use_json.go | 32 +++++++ db/migrations/migrate.go | 1 + db/models.go | 9 +- frontend/src/components/TransactionItem.tsx | 31 +------ frontend/src/types.ts | 4 +- go.mod | 6 +- go.sum | 33 ++++++- lnclient/models.go | 7 +- nip47/models/transactions.go | 4 +- transactions/transactions_service.go | 91 +++++++++++++++---- 12 files changed, 205 insertions(+), 99 deletions(-) create mode 100644 db/migrations/202408061737_add_boostagrams_and_use_json.go diff --git a/api/models.go b/api/models.go index e8239e63..0b226a39 100644 --- a/api/models.go +++ b/api/models.go @@ -192,27 +192,36 @@ type ListTransactionsResponse = []Transaction // TODO: camelCase type Transaction struct { - Type string `json:"type"` - Invoice string `json:"invoice"` - Description string `json:"description"` - DescriptionHash string `json:"description_hash"` - Preimage *string `json:"preimage"` - PaymentHash string `json:"payment_hash"` - Amount uint64 `json:"amount"` - FeesPaid uint64 `json:"fees_paid"` - CreatedAt string `json:"created_at"` - SettledAt *string `json:"settled_at"` - AppId *uint `json:"app_id"` - Metadata Metadata `json:"metadata,omitempty"` -} - -type Metadata struct { - TlvRecords []TLVRecord `json:"tlv_records,omitempty"` -} - -type TLVRecord struct { - Type uint64 `json:"type"` - Value string `json:"value"` + Type string `json:"type"` + Invoice string `json:"invoice"` + Description string `json:"description"` + DescriptionHash string `json:"description_hash"` + Preimage *string `json:"preimage"` + PaymentHash string `json:"payment_hash"` + Amount uint64 `json:"amount"` + FeesPaid uint64 `json:"fees_paid"` + CreatedAt string `json:"created_at"` + SettledAt *string `json:"settled_at"` + AppId *uint `json:"app_id"` + Metadata *lnclient.Metadata `json:"metadata,omitempty"` + Boostagram *Boostagram `json:"boostagram,omitempty"` +} + +type Boostagram struct { + AppName string `json:"app_name"` + Name string `json:"name"` + Podcast string `json:"podcast"` + URL string `json:"url"` + Episode string `json:"episode,omitempty"` + FeedID string `json:"feedID,omitempty"` + ItemID string `json:"itemID,omitempty"` + Timestamp string `json:"ts,omitempty"` + Message string `json:"message,omitempty"` + SenderID string `json:"sender_id"` + SenderName string `json:"sender_name"` + Time string `json:"time"` + Action string `json:"action"` + ValueMsatTotal int `json:"value_msat_total"` } // debug api diff --git a/api/transactions.go b/api/transactions.go index 00d2010f..c95da6bf 100644 --- a/api/transactions.go +++ b/api/transactions.go @@ -2,11 +2,11 @@ package api import ( "context" - "encoding/hex" "encoding/json" "errors" "time" + "github.com/getAlby/hub/lnclient" "github.com/getAlby/hub/logger" "github.com/getAlby/hub/transactions" "github.com/sirupsen/logrus" @@ -74,33 +74,25 @@ func toApiTransaction(transaction *transactions.Transaction) *Transaction { preimage = transaction.Preimage } - var metadata Metadata - if transaction.Metadata != "" { - jsonErr := json.Unmarshal([]byte(transaction.Metadata), &metadata) + var metadata *lnclient.Metadata + if transaction.Metadata != nil { + jsonErr := json.Unmarshal(transaction.Metadata, &metadata) if jsonErr != nil { logger.Logger.WithError(jsonErr).WithFields(logrus.Fields{ "id": transaction.ID, "metadata": transaction.Metadata, }).Error("Failed to deserialize transaction metadata") } + } - for i, record := range metadata.TlvRecords { - // TODO: skip other un-encoded tlv values - // tlv record of type 5482373484 is preimage - if record.Type == 5482373484 { - continue - } - - bytes, err := hex.DecodeString(record.Value) - if err != nil { - logger.Logger.WithError(err).WithFields(logrus.Fields{ - "id": transaction.ID, - "type": record.Type, - "value": record.Value, - }).Error("Failed to decode hex value in tlv record") - continue - } - metadata.TlvRecords[i].Value = string(bytes) + var boostagram *Boostagram + if transaction.Boostagram != nil { + jsonErr := json.Unmarshal(transaction.Boostagram, &boostagram) + if jsonErr != nil { + logger.Logger.WithError(jsonErr).WithFields(logrus.Fields{ + "id": transaction.ID, + "boostagram": transaction.Boostagram, + }).Error("Failed to deserialize transaction boostagram info") } } @@ -117,5 +109,6 @@ func toApiTransaction(transaction *transactions.Transaction) *Transaction { CreatedAt: createdAt, SettledAt: settledAt, Metadata: metadata, + Boostagram: boostagram, } } diff --git a/db/migrations/202408061737_add_boostagrams_and_use_json.go b/db/migrations/202408061737_add_boostagrams_and_use_json.go new file mode 100644 index 00000000..08b0b683 --- /dev/null +++ b/db/migrations/202408061737_add_boostagrams_and_use_json.go @@ -0,0 +1,32 @@ +package migrations + +import ( + _ "embed" + + "github.com/go-gormigrate/gormigrate/v2" + "gorm.io/gorm" +) + +// This migration adds boostagram column to transactions +var _202408061737_add_boostagrams_and_use_json = &gormigrate.Migration{ + ID: "202408061737_add_boostagrams_and_use_json", + Migrate: func(tx *gorm.DB) error { + if err := tx.Exec("ALTER TABLE transactions ADD COLUMN boostagram JSONB").Error; err != nil { + return err + } + + if err := tx.Exec(` + ALTER TABLE transactions ADD COLUMN metadata_temp JSONB; + UPDATE transactions SET metadata_temp = json(metadata); + ALTER TABLE transactions DROP COLUMN metadata; + ALTER TABLE transactions RENAME COLUMN metadata_temp TO metadata; + `).Error; err != nil { + return err + } + + return nil + }, + Rollback: func(tx *gorm.DB) error { + return nil + }, +} diff --git a/db/migrations/migrate.go b/db/migrations/migrate.go index 104ddf24..7ef37acf 100644 --- a/db/migrations/migrate.go +++ b/db/migrations/migrate.go @@ -19,6 +19,7 @@ func Migrate(gormDB *gorm.DB) error { _202407151352_autoincrement, _202407201604_transactions_indexes, _202407262257_remove_invalid_scopes, + _202408061737_add_boostagrams_and_use_json, }) return m.Migrate() diff --git a/db/models.go b/db/models.go index f3419b8a..2ea1cf40 100644 --- a/db/models.go +++ b/db/models.go @@ -1,6 +1,10 @@ package db -import "time" +import ( + "time" + + "gorm.io/datatypes" +) type UserConfig struct { ID uint @@ -75,8 +79,9 @@ type Transaction struct { ExpiresAt *time.Time UpdatedAt time.Time SettledAt *time.Time - Metadata string + Metadata datatypes.JSON SelfPayment bool + Boostagram datatypes.JSON } type DBService interface { diff --git a/frontend/src/components/TransactionItem.tsx b/frontend/src/components/TransactionItem.tsx index aef96423..18ce65a3 100644 --- a/frontend/src/components/TransactionItem.tsx +++ b/frontend/src/components/TransactionItem.tsx @@ -25,7 +25,7 @@ import { toast } from "src/components/ui/use-toast"; import { useApps } from "src/hooks/useApps"; import { copyToClipboard } from "src/lib/clipboard"; import { cn } from "src/lib/utils"; -import { Boostagram, Transaction } from "src/types"; +import { Transaction } from "src/types"; dayjs.extend(utc); dayjs.extend(timezone); @@ -46,28 +46,6 @@ function TransactionItem({ tx }: Props) { toast({ title: "Copied to clipboard." }); }; - const getDescription = () => { - // Check for Boostagram message first - for (const record of tx.metadata?.tlv_records || []) { - if (record.type === 7629169) { - const boost = JSON.parse(record.value) as Boostagram; - if (boost.message) { - return boost.message; - } - } - } - - // Check for Whatsat message - for (const record of tx.metadata?.tlv_records || []) { - if (record.type === 34349334 && record.value) { - return record.value; - } - } - - // Default to transaction - return tx.description || ""; - }; - return (

- {getDescription()} + {tx.description}

@@ -240,12 +218,9 @@ function TransactionItem({ tx }: Props) {

); - } else if (record.type === 7629169) { - // Podcasting info - const boost = JSON.parse(record.value) as Boostagram; - return ; } })} + {tx.boostagram && }

Preimage

diff --git a/frontend/src/types.ts b/frontend/src/types.ts index 771183ae..8d649719 100644 --- a/frontend/src/types.ts +++ b/frontend/src/types.ts @@ -378,9 +378,10 @@ export type Transaction = { fees_paid: number; created_at: string; settled_at: string | undefined; - metadata: { + metadata?: { tlv_records: TLVRecord[]; }; + boostagram?: Boostagram; }; export type TLVRecord = { @@ -397,6 +398,7 @@ export type Boostagram = { podcast: string; url: string; episode?: string; + feedID?: string; itemID?: string; ts?: string; message?: string; diff --git a/go.mod b/go.mod index 4a5e4d68..b2fd0d7b 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( ) require ( + filippo.io/edwards25519 v1.1.0 // indirect github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect github.com/BurntSushi/toml v1.2.1 // indirect github.com/DataDog/datadog-go/v5 v5.3.0 // indirect @@ -71,6 +72,7 @@ require ( github.com/go-logr/stdr v1.2.2 // indirect github.com/go-macaroon-bakery/macaroonpb v1.0.0 // indirect github.com/go-ole/go-ole v1.2.6 // indirect + github.com/go-sql-driver/mysql v1.8.1 // indirect github.com/gobwas/httphead v0.1.0 // indirect github.com/gobwas/pool v0.2.1 // indirect github.com/gobwas/ws v1.2.1 // indirect @@ -101,7 +103,7 @@ require ( github.com/jackc/pgio v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgproto3/v2 v2.3.3 // indirect - github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect github.com/jackc/pgtype v1.14.0 // indirect github.com/jackc/pgx/v4 v4.18.3 // indirect github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect @@ -213,6 +215,7 @@ require ( gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + gorm.io/driver/mysql v1.5.6 // indirect modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect modernc.org/libc v1.49.3 // indirect modernc.org/mathutil v1.6.0 // indirect @@ -235,6 +238,7 @@ require ( github.com/lightningnetwork/lnd v0.17.4-beta github.com/sirupsen/logrus v1.9.3 golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect + gorm.io/datatypes v1.2.1 ) // See https://github.com/lightningnetwork/lnd/blob/v0.17.4-beta/go.mod#L12C58-L12C70 diff --git a/go.sum b/go.sum index 3393f62b..01ac61a6 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= +filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0= github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= @@ -212,8 +214,9 @@ github.com/go-macaroon-bakery/macaroonpb v1.0.0 h1:It9exBaRMZ9iix1iJ6gwzfwsDE6Ex github.com/go-macaroon-bakery/macaroonpb v1.0.0/go.mod h1:UzrGOcbiwTXISFP2XDLDPjfhMINZa+fX/7A2lMd31zc= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= -github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= -github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= +github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= @@ -236,6 +239,10 @@ github.com/golang-jwt/jwt/v4 v4.4.3 h1:Hxl6lhQFj4AnOX6MLrsCb/+7tCj7DxP7VA+2rDIq5 github.com/golang-jwt/jwt/v4 v4.4.3/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= github.com/golang-migrate/migrate/v4 v4.17.0 h1:rd40H3QXU0AA4IoLllFcEAEo9dYKRHYND2gB4p7xcaU= github.com/golang-migrate/migrate/v4 v4.17.0/go.mod h1:+Cp2mtLP4/aXDTKb9wmXYitdrNx2HGs45rbWAo6OsKM= +github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA= +github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= +github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A= +github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= @@ -334,8 +341,8 @@ github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwX github.com/jackc/pgproto3/v2 v2.3.3 h1:1HLSx5H+tXR9pW3in3zaztoEwQYRC9SQaYUHjTSUOag= github.com/jackc/pgproto3/v2 v2.3.3/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= -github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= -github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg= github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc= github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw= @@ -348,9 +355,14 @@ github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQ github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs= github.com/jackc/pgx/v4 v4.18.3 h1:dE2/TrEsGX3RBprb3qryqSV9Y60iZN1C6i8IrmW9/BA= github.com/jackc/pgx/v4 v4.18.3/go.mod h1:Ey4Oru5tH5sB6tV7hDmfWFahwF15Eb7DNXlRKx2CkVw= +github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw= +github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A= github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle v1.3.0 h1:eHK/5clGOatcjX3oWGBO/MpxpbHzSwud5EWTSCI+MX0= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e h1:Q3+PugElBCf4PFpxhErSzU3/PY5sFL5Z6rfv4AbGAck= github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e/go.mod h1:alcuEEnZsY1WQsagKhZDsoPCRoOijYqhZvPwLG0kzVs= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= @@ -479,6 +491,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/microsoft/go-mssqldb v1.0.0 h1:k2p2uuG8T5T/7Hp7/e3vMGTnnR0sU4h8d1CcC71iLHU= +github.com/microsoft/go-mssqldb v1.0.0/go.mod h1:+4wZTUnz/SV6nffv+RRRB/ss8jPng5Sho2SmM1l2ts4= github.com/miekg/dns v1.1.58 h1:ca2Hdkz+cDg/7eNF6V56jjzuZ4aCAE+DbVkILdQWG/4= github.com/miekg/dns v1.1.58/go.mod h1:Ypv+3b/KadlvW9vJfXOTf300O4UqaHFzFCuHz+rPkBY= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= @@ -938,6 +952,17 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/datatypes v1.2.1 h1:r+g0bk4LPCW2v4+Ls7aeNgGme7JYdNDQ2VtvlNUfBh0= +gorm.io/datatypes v1.2.1/go.mod h1:hYK6OTb/1x+m96PgoZZq10UXJ6RvEBb9kRDQ2yyhzGs= +gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8= +gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM= +gorm.io/driver/postgres v1.5.0 h1:u2FXTy14l45qc3UeCJ7QaAXZmZfDDv0YrthvmRq1l0U= +gorm.io/driver/postgres v1.5.0/go.mod h1:FUZXzO+5Uqg5zzwzv4KK49R8lvGIyscBOqYrtI1Ce9A= +gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU= +gorm.io/driver/sqlite v1.4.3/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI= +gorm.io/driver/sqlserver v1.4.2 h1:nMtEeKqv2R/vv9FoHUFWfXfP6SskAgRar0TPlZV1stk= +gorm.io/driver/sqlserver v1.4.2/go.mod h1:XHwBuB4Tlh7DqO0x7Ema8dmyWsQW7wi38VQOAFkrbXY= +gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg= gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= diff --git a/lnclient/models.go b/lnclient/models.go index 7607a08e..61bd90af 100644 --- a/lnclient/models.go +++ b/lnclient/models.go @@ -7,11 +7,12 @@ import ( // TODO: remove JSON tags from these models (LNClient models should not be exposed directly) type TLVRecord struct { - Type uint64 `json:"type"` - // hex-encoded value + Type uint64 `json:"type"` Value string `json:"value"` } +type Metadata = map[string]interface{} + type NodeInfo struct { Alias string Color string @@ -34,7 +35,7 @@ type Transaction struct { CreatedAt int64 ExpiresAt *int64 SettledAt *int64 - Metadata interface{} + Metadata Metadata } type NodeConnectionInfo struct { diff --git a/nip47/models/transactions.go b/nip47/models/transactions.go index 2771b18b..8b895d10 100644 --- a/nip47/models/transactions.go +++ b/nip47/models/transactions.go @@ -24,8 +24,8 @@ func ToNip47Transaction(transaction *transactions.Transaction) *Transaction { } var metadata interface{} - if transaction.Metadata != "" { - jsonErr := json.Unmarshal([]byte(transaction.Metadata), &metadata) + if transaction.Metadata != nil { + jsonErr := json.Unmarshal(transaction.Metadata, &metadata) if jsonErr != nil { logger.Logger.WithError(jsonErr).WithFields(logrus.Fields{ "id": transaction.ID, diff --git a/transactions/transactions_service.go b/transactions/transactions_service.go index 5fbcccda..7f257917 100644 --- a/transactions/transactions_service.go +++ b/transactions/transactions_service.go @@ -21,6 +21,7 @@ import ( "github.com/getAlby/hub/logger" decodepay "github.com/nbd-wtf/ln-decodepay" "github.com/sirupsen/logrus" + "gorm.io/datatypes" "gorm.io/gorm" ) @@ -78,10 +79,12 @@ func NewTransactionsService(db *gorm.DB) *transactionsService { } } +// TODO: fix metadata type func (svc *transactionsService) MakeInvoice(ctx context.Context, amount int64, description string, descriptionHash string, expiry int64, metadata interface{}, lnClient lnclient.LNClient, appId *uint, requestEventId *uint) (*Transaction, error) { - var encodedMetadata string + var metadataBytes []byte if metadata != nil { - metadataBytes, err := json.Marshal(metadata) + var err error + metadataBytes, err = json.Marshal(metadata) if err != nil { logger.Logger.WithError(err).Error("Failed to serialize metadata") return nil, err @@ -89,7 +92,6 @@ func (svc *transactionsService) MakeInvoice(ctx context.Context, amount int64, d if len(metadataBytes) > constants.INVOICE_METADATA_MAX_LENGTH { return nil, fmt.Errorf("encoded invoice metadata provided is too large. Limit: %d Received: %d", constants.INVOICE_METADATA_MAX_LENGTH, len(metadataBytes)) } - encodedMetadata = string(metadataBytes) } lnClientTransaction, err := lnClient.MakeInvoice(ctx, amount, description, descriptionHash, expiry) @@ -121,7 +123,7 @@ func (svc *transactionsService) MakeInvoice(ctx context.Context, amount int64, d PaymentHash: lnClientTransaction.PaymentHash, ExpiresAt: expiresAt, Preimage: preimage, - Metadata: encodedMetadata, + Metadata: datatypes.JSON(metadataBytes), } err = svc.db.Create(&dbTransaction).Error if err != nil { @@ -263,13 +265,17 @@ func (svc *transactionsService) SendKeysend(ctx context.Context, amount uint64, metadata := map[string]interface{}{} metadata["destination"] = destination - metadata["tlv_records"] = customRecords - metadataBytes, err := json.Marshal(metadata) + // we copy records because parseMetadata decodes record values + copiedRecords := append([]lnclient.TLVRecord(nil), customRecords...) + + metadata["tlv_records"] = copiedRecords + metadataBytes, err := svc.parseMetadata(metadata) if err != nil { - logger.Logger.WithError(err).Error("Failed to marshal metadata") + logger.Logger.WithError(err).Error("Failed to serialize transaction metadata") return nil, err } + boostagramBytes := svc.getBoostagramFromCustomRecords(copiedRecords) var dbTransaction db.Transaction @@ -286,7 +292,8 @@ func (svc *transactionsService) SendKeysend(ctx context.Context, amount uint64, State: constants.TRANSACTION_STATE_PENDING, FeeReserveMsat: svc.calculateFeeReserveMsat(uint64(amount)), AmountMsat: amount, - Metadata: string(metadataBytes), + Metadata: datatypes.JSON(metadataBytes), + Boostagram: datatypes.JSON(boostagramBytes), PaymentHash: paymentHash, Preimage: &preimage, } @@ -533,14 +540,19 @@ func (svc *transactionsService) ConsumeEvent(ctx context.Context, event *events. if result.RowsAffected == 0 { // Note: brand new payments cannot be associated with an app - var metadata string + var metadataBytes []byte + var boostagramBytes []byte if lnClientTransaction.Metadata != nil { - metadataBytes, err := json.Marshal(lnClientTransaction.Metadata) + var err error + metadataBytes, err = svc.parseMetadata(lnClientTransaction.Metadata) if err != nil { logger.Logger.WithError(err).Error("Failed to serialize transaction metadata") return err } - metadata = string(metadataBytes) + + var customRecords []lnclient.TLVRecord + customRecords, _ = lnClientTransaction.Metadata["tlv_records"].([]lnclient.TLVRecord) + boostagramBytes = svc.getBoostagramFromCustomRecords(customRecords) } var expiresAt *time.Time if lnClientTransaction.ExpiresAt != nil { @@ -555,7 +567,8 @@ func (svc *transactionsService) ConsumeEvent(ctx context.Context, event *events. Description: lnClientTransaction.Description, DescriptionHash: lnClientTransaction.DescriptionHash, ExpiresAt: expiresAt, - Metadata: metadata, + Metadata: datatypes.JSON(metadataBytes), + Boostagram: datatypes.JSON(boostagramBytes), } err := tx.Create(&dbTransaction).Error if err != nil { @@ -607,14 +620,19 @@ func (svc *transactionsService) ConsumeEvent(ctx context.Context, event *events. if result.RowsAffected == 0 { // Note: brand new payments cannot be associated with an app - var metadata string + var metadataBytes []byte + var boostagramBytes []byte if lnClientTransaction.Metadata != nil { - metadataBytes, err := json.Marshal(lnClientTransaction.Metadata) + var err error + metadataBytes, err = svc.parseMetadata(lnClientTransaction.Metadata) if err != nil { logger.Logger.WithError(err).Error("Failed to serialize transaction metadata") return err } - metadata = string(metadataBytes) + + var customRecords []lnclient.TLVRecord + customRecords, _ = lnClientTransaction.Metadata["tlv_records"].([]lnclient.TLVRecord) + boostagramBytes = svc.getBoostagramFromCustomRecords(customRecords) } var expiresAt *time.Time if lnClientTransaction.ExpiresAt != nil { @@ -629,7 +647,8 @@ func (svc *transactionsService) ConsumeEvent(ctx context.Context, event *events. Description: lnClientTransaction.Description, DescriptionHash: lnClientTransaction.DescriptionHash, ExpiresAt: expiresAt, - Metadata: metadata, + Metadata: datatypes.JSON(metadataBytes), + Boostagram: datatypes.JSON(boostagramBytes), } err := tx.Create(&dbTransaction).Error if err != nil { @@ -784,3 +803,43 @@ func makePreimageHex() ([]byte, error) { } return bytes, nil } + +func (svc *transactionsService) parseMetadata(metadata lnclient.Metadata) ([]byte, error) { + var tlvRecords []lnclient.TLVRecord + tlvRecords, _ = metadata["tlv_records"].([]lnclient.TLVRecord) + + for i, record := range tlvRecords { + // TODO: skip other un-encoded tlv values + // tlv record of type 5482373484 is preimage + if record.Type == 5482373484 { + continue + } + + bytes, err := hex.DecodeString(record.Value) + if err != nil { + logger.Logger.WithError(err).WithFields(logrus.Fields{ + "type": record.Type, + "value": record.Value, + }).Error("Failed to decode hex value in tlv record") + continue + } + tlvRecords[i].Value = string(bytes) + } + + metadataBytes, err := json.Marshal(metadata) + if err != nil { + return nil, err + } + + return metadataBytes, nil +} + +func (svc *transactionsService) getBoostagramFromCustomRecords(customRecords []lnclient.TLVRecord) []byte { + for _, record := range customRecords { + if record.Type == 7629169 { + return []byte(record.Value) + } + } + + return nil +}