Skip to content

Commit

Permalink
Added custom_records to metadata field of list_transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
ericpp committed Feb 28, 2024
1 parent 7de60c2 commit d5d919a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
12 changes: 11 additions & 1 deletion alby.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,16 @@ func albyInvoiceToTransaction(invoice *AlbyInvoice) *Nip47Transaction {
settledAt = &settledAtUnix
}

var customRecords = map[string]string{}
for key, value := range invoice.CustomRecords {
customRecords[key] = value // already base64 encoded
}

var metadata = map[string]map[string]string{}
if len(customRecords) != 0 {
metadata["custom_records"] = customRecords
}

return &Nip47Transaction{
Type: invoice.Type,
Invoice: invoice.PaymentRequest,
Expand All @@ -733,6 +743,6 @@ func albyInvoiceToTransaction(invoice *AlbyInvoice) *Nip47Transaction {
CreatedAt: invoice.CreatedAt.Unix(),
ExpiresAt: expiresAt,
SettledAt: settledAt,
Metadata: invoice.Metadata,
Metadata: metadata,
}
}
33 changes: 30 additions & 3 deletions lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -121,6 +123,21 @@ func (svc *LNDService) ListTransactions(ctx context.Context, senderPubkey string
settledAt = &settledAtUnix
}

var customRecords = map[string]string{}
for _, htlc := range payment.Htlcs {
for _, hop := range htlc.Route.Hops {
for id, value := range hop.CustomRecords {
key := strconv.FormatUint(id, 10)
customRecords[key] = base64.StdEncoding.EncodeToString(value)
}
}
}

var metadata = map[string]map[string]string{}
if len(customRecords) != 0 {
metadata["custom_records"] = customRecords
}

transaction := Nip47Transaction{
Type: "outgoing",
Invoice: payment.PaymentRequest,
Expand All @@ -133,7 +150,7 @@ func (svc *LNDService) ListTransactions(ctx context.Context, senderPubkey string
DescriptionHash: descriptionHash,
ExpiresAt: expiresAt,
SettledAt: settledAt,
//TODO: Metadata: (e.g. keysend),
Metadata: metadata,
}
transactions = append(transactions, transaction)
}
Expand Down Expand Up @@ -369,7 +386,17 @@ func lndInvoiceToTransaction(invoice *lnrpc.Invoice) *Nip47Transaction {
expiresAtUnix := invoice.CreationDate + invoice.Expiry
expiresAt = &expiresAtUnix
}

var customRecords = map[string]string{}
for _, htlc := range invoice.Htlcs {
for id, value := range htlc.CustomRecords {
key := strconv.FormatUint(id, 10)
customRecords[key] = base64.StdEncoding.EncodeToString(value)
}
}
var metadata = map[string]map[string]string{}
if len(customRecords) != 0 {
metadata["custom_records"] = customRecords
}
return &Nip47Transaction{
Type: "incoming",
Invoice: invoice.PaymentRequest,
Expand All @@ -382,6 +409,6 @@ func lndInvoiceToTransaction(invoice *lnrpc.Invoice) *Nip47Transaction {
CreatedAt: invoice.CreationDate,
SettledAt: settledAt,
ExpiresAt: expiresAt,
// TODO: Metadata (e.g. keysend)
Metadata: metadata,
}
}
2 changes: 1 addition & 1 deletion models.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ type AlbyInvoice struct {
CreatedAt time.Time `json:"created_at"`
// CreationDate uint64 `json:"creation_date"`
Currency string `json:"currency"`
// custom_records
CustomRecords map[string]string `json:"custom_records,omitempty"`
DescriptionHash string `json:"description_hash"`
ExpiresAt *time.Time `json:"expires_at"`
Expiry uint32 `json:"expiry"`
Expand Down

0 comments on commit d5d919a

Please sign in to comment.