Skip to content

Commit

Permalink
rfqmsg+tapchannelmsg: add new custom channel data fields to parser
Browse files Browse the repository at this point in the history
We now get custom_channel_data fields in the ListInvoices/LookupInvoice
as well as in the ListPayments RPC. We add those fields to our data
parser implementation.
  • Loading branch information
guggero committed Sep 19, 2024
1 parent ab213c7 commit 11d1821
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
14 changes: 14 additions & 0 deletions rfqmsg/custom_channel_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,17 @@ type JsonCloseOutput struct {
AssetInternalKey string `json:"asset_internal_key"`
ScriptKeys map[string]string `json:"script_keys"`
}

// JsonHtlcBalance is a struct that represents the balance of a single asset
// HTLC.
type JsonHtlcBalance struct {
AssetID string `json:"asset_id"`
Amount uint64 `json:"amount"`
}

// JsonHtlc is a struct that represents the asset information that can be
// transferred via an HTLC.
type JsonHtlc struct {
Balances []*JsonHtlcBalance `json:"balances"`
RfqID string `json:"rfq_id"`
}
27 changes: 27 additions & 0 deletions rfqmsg/records.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package rfqmsg

import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -137,6 +139,31 @@ func (h *Htlc) Bytes() []byte {
return buf.Bytes()
}

// ToCustomRecords converts the Htlc record to a map of custom records.
func (h *Htlc) ToCustomRecords() (lnwire.CustomRecords, error) {
return tlv.RecordsToMap(h.Records())
}

// AsJson returns the Htlc record as a JSON blob.
func (h *Htlc) AsJson() ([]byte, error) {
j := &JsonHtlc{
Balances: make([]*JsonHtlcBalance, len(h.Balances())),
}

h.RfqID.ValOpt().WhenSome(func(id ID) {
j.RfqID = hex.EncodeToString(id[:])
})

for idx, balance := range h.Balances() {
j.Balances[idx] = &JsonHtlcBalance{
AssetID: hex.EncodeToString(balance.AssetID.Val[:]),
Amount: balance.Amount.Val,
}
}

return json.Marshal(j)
}

// DecodeHtlc deserializes a Htlc from the given blob.
func DecodeHtlc(blob tlv.Blob) (*Htlc, error) {
var h Htlc
Expand Down
42 changes: 41 additions & 1 deletion tapchannelmsg/custom_channel_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func ParseCustomChannelData(msg proto.Message) error {
continue
}

if rpcChannel.CustomChannelData == nil {
if len(rpcChannel.CustomChannelData) == 0 {
continue
}

Expand Down Expand Up @@ -398,6 +398,46 @@ func ParseCustomChannelData(msg proto.Message) error {
"custom close data: %w", err)
}
}

case *lnrpc.Route:
if len(m.CustomChannelData) == 0 {
return nil
}

parsedHtlc, err := rfqmsg.DecodeHtlc(m.CustomChannelData)
if err != nil {
return fmt.Errorf("error parsing custom "+
"channel data: %w", err)
}

m.CustomChannelData, err = parsedHtlc.AsJson()
if err != nil {
return fmt.Errorf("error converting custom "+
"channel data to JSON: %w", err)
}

case *lnrpc.Invoice:
for idx := range m.Htlcs {
htlc := m.Htlcs[idx]

if len(htlc.CustomChannelData) == 0 {
continue
}

parsedHtlc, err := rfqmsg.DecodeHtlc(
htlc.CustomChannelData,
)
if err != nil {
return fmt.Errorf("error parsing custom "+
"channel data: %w", err)
}

htlc.CustomChannelData, err = parsedHtlc.AsJson()
if err != nil {
return fmt.Errorf("error converting custom "+
"channel data to JSON: %w", err)
}
}
}

return nil
Expand Down

0 comments on commit 11d1821

Please sign in to comment.