Skip to content

Commit 39f1003

Browse files
committed
rfqmsg+tapchannelmsg: add new custom channel data fields to parser
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.
1 parent ff548c0 commit 39f1003

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

rfqmsg/custom_channel_data.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,17 @@ type JsonCloseOutput struct {
5656
AssetInternalKey string `json:"asset_internal_key"`
5757
ScriptKeys map[string]string `json:"script_keys"`
5858
}
59+
60+
// JsonHtlcBalance is a struct that represents the balance of a single asset
61+
// HTLC.
62+
type JsonHtlcBalance struct {
63+
AssetID string `json:"asset_id"`
64+
Amount uint64 `json:"amount"`
65+
}
66+
67+
// JsonHtlc is a struct that represents the asset information that can be
68+
// transferred via an HTLC.
69+
type JsonHtlc struct {
70+
Balances []*JsonHtlcBalance `json:"balances"`
71+
RfqID string `json:"rfq_id"`
72+
}

rfqmsg/records.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package rfqmsg
22

33
import (
44
"bytes"
5+
"encoding/hex"
6+
"encoding/json"
57
"errors"
68
"fmt"
79
"io"
@@ -137,6 +139,31 @@ func (h *Htlc) Bytes() []byte {
137139
return buf.Bytes()
138140
}
139141

142+
// ToCustomRecords converts the Htlc record to a map of custom records.
143+
func (h *Htlc) ToCustomRecords() (lnwire.CustomRecords, error) {
144+
return tlv.RecordsToMap(h.Records())
145+
}
146+
147+
// AsJson returns the Htlc record as a JSON blob.
148+
func (h *Htlc) AsJson() ([]byte, error) {
149+
j := &JsonHtlc{
150+
Balances: make([]*JsonHtlcBalance, len(h.Balances())),
151+
}
152+
153+
h.RfqID.ValOpt().WhenSome(func(id ID) {
154+
j.RfqID = hex.EncodeToString(id[:])
155+
})
156+
157+
for idx, balance := range h.Balances() {
158+
j.Balances[idx] = &JsonHtlcBalance{
159+
AssetID: hex.EncodeToString(balance.AssetID.Val[:]),
160+
Amount: balance.Amount.Val,
161+
}
162+
}
163+
164+
return json.Marshal(j)
165+
}
166+
140167
// DecodeHtlc deserializes a Htlc from the given blob.
141168
func DecodeHtlc(blob tlv.Blob) (*Htlc, error) {
142169
var h Htlc

tapchannelmsg/custom_channel_data.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func ParseCustomChannelData(msg proto.Message) error {
356356
continue
357357
}
358358

359-
if rpcChannel.CustomChannelData == nil {
359+
if len(rpcChannel.CustomChannelData) == 0 {
360360
continue
361361
}
362362

@@ -398,6 +398,46 @@ func ParseCustomChannelData(msg proto.Message) error {
398398
"custom close data: %w", err)
399399
}
400400
}
401+
402+
case *lnrpc.Route:
403+
if len(m.CustomChannelData) == 0 {
404+
return nil
405+
}
406+
407+
parsedHtlc, err := rfqmsg.DecodeHtlc(m.CustomChannelData)
408+
if err != nil {
409+
return fmt.Errorf("error parsing custom "+
410+
"channel data: %w", err)
411+
}
412+
413+
m.CustomChannelData, err = parsedHtlc.AsJson()
414+
if err != nil {
415+
return fmt.Errorf("error converting custom "+
416+
"channel data to JSON: %w", err)
417+
}
418+
419+
case *lnrpc.Invoice:
420+
for idx := range m.Htlcs {
421+
htlc := m.Htlcs[idx]
422+
423+
if len(htlc.CustomChannelData) == 0 {
424+
continue
425+
}
426+
427+
parsedHtlc, err := rfqmsg.DecodeHtlc(
428+
htlc.CustomChannelData,
429+
)
430+
if err != nil {
431+
return fmt.Errorf("error parsing custom "+
432+
"channel data: %w", err)
433+
}
434+
435+
htlc.CustomChannelData, err = parsedHtlc.AsJson()
436+
if err != nil {
437+
return fmt.Errorf("error converting custom "+
438+
"channel data to JSON: %w", err)
439+
}
440+
}
401441
}
402442

403443
return nil

0 commit comments

Comments
 (0)