Skip to content

Commit

Permalink
cmd/lncli: update listchannels output fields
Browse files Browse the repository at this point in the history
Added scid_str as a string representation of chan_id,
replacing chan_id with scid, channel_point with
chan_point, and including chan_id in lncli
listchannels output.

Signed-off-by: Nishant Bansal <[email protected]>
  • Loading branch information
NishantBansal2003 committed Jan 13, 2025
1 parent a0eadfe commit 80b90d7
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions cmd/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/jessevdk/go-flags"
"github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/lightningnetwork/lnd/signal"
Expand Down Expand Up @@ -50,6 +51,14 @@ var (
customDataPattern = regexp.MustCompile(
`"custom_channel_data":\s*"([0-9a-f]+)"`,
)

chanIDPattern = regexp.MustCompile(
`"chan_id":\s*"(\d+)"`,
)

channelPointPattern = regexp.MustCompile(
`"channel_point":\s*"([0-9a-fA-F]+:[0-9]+)"`,
)
)

// replaceCustomData replaces the custom channel data hex string with the
Expand Down Expand Up @@ -86,6 +95,96 @@ func replaceCustomData(jsonBytes []byte) []byte {
return buf.Bytes()
}

// replaceChanID replaces the chan_id with scid and appends the human
// readable string representation of scid.
func replaceChanID(jsonBytes []byte) []byte {
// If there's nothing to replace, return the original JSON.
if !chanIDPattern.Match(jsonBytes) {
return jsonBytes
}

replacedBytes := chanIDPattern.ReplaceAllFunc(
jsonBytes, func(match []byte) []byte {
// Extract the captured scid group from the match.
chanID := chanIDPattern.FindStringSubmatch(
string(match),
)[1]

scid, err := strconv.ParseUint(chanID, 10, 64)
if err != nil {
return match
}

// Format a new JSON field for the scid (chan_id),
// including both its numeric representation and its
// string representation (scid_str).
scidStr := lnwire.NewShortChanIDFromInt(scid).String()
updatedField := fmt.Sprintf(
`"scid": "%d", "scid_str": "%s"`, scid, scidStr,
)

// Replace the entire match with the new structure.
return []byte(updatedField)
},
)

var buf bytes.Buffer
err := json.Indent(&buf, replacedBytes, "", " ")
if err != nil {
// If we can't indent the JSON, it likely means the replacement
// data wasn't correct, so we return the original JSON.
return jsonBytes
}

return buf.Bytes()
}

// replaceChannelPoint replaces the channel_point with chan_point and appends
// the chan_id which is computed using the outpoint of the funding transaction
// (the txid, and output index).
func replaceChannelPoint(jsonBytes []byte) []byte {
// If there's nothing to replace, return the original JSON.
if !channelPointPattern.Match(jsonBytes) {
return jsonBytes
}

replacedBytes := channelPointPattern.ReplaceAllFunc(
jsonBytes, func(match []byte) []byte {
chanPoint := channelPointPattern.FindStringSubmatch(
string(match),
)[1]

chanOutpoint, err := wire.NewOutPointFromString(
chanPoint)
if err != nil {
return match
}

// Format a new JSON field computed from the
// channel_point (chan_id), replacing channel_point
// with chan_point.
chanID := lnwire.NewChanIDFromOutPoint(*chanOutpoint)
updatedField := fmt.Sprintf(
`"chan_point": "%s", "chan_id": "%s"`,
chanPoint, chanID.String(),
)

// Replace the entire match with the new structure.
return []byte(updatedField)
},
)

var buf bytes.Buffer
err := json.Indent(&buf, replacedBytes, "", " ")
if err != nil {
// If we can't indent the JSON, it likely means the replacement
// data wasn't correct, so we return the original JSON.
return jsonBytes
}

return buf.Bytes()
}

func getContext() context.Context {
shutdownInterceptor, err := signal.Intercept()
if err != nil {
Expand Down Expand Up @@ -122,6 +221,10 @@ func printRespJSON(resp proto.Message) {

jsonBytesReplaced := replaceCustomData(jsonBytes)

jsonBytesReplaced = replaceChannelPoint(jsonBytesReplaced)

jsonBytesReplaced = replaceChanID(jsonBytesReplaced)

fmt.Printf("%s\n", jsonBytesReplaced)
}

Expand Down

0 comments on commit 80b90d7

Please sign in to comment.