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

Added custom_records to metadata field of list_transactions #251

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion alby.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,19 @@ 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]interface{}{}
if invoice.Metadata != nil {
metadata["alby"] = invoice.Metadata
}
if len(customRecords) != 0 {
metadata["custom_records"] = customRecords
}

return &Nip47Transaction{
Type: invoice.Type,
Invoice: invoice.PaymentRequest,
Expand All @@ -733,6 +746,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