Skip to content

Commit

Permalink
lnd: add aux data parser
Browse files Browse the repository at this point in the history
This commit adds an optional data parser that can inspect and in-place
format custom data of certain RPC messages.
  • Loading branch information
guggero committed May 17, 2024
1 parent 13718bb commit 5a834c1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 4 additions & 0 deletions config_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ type AuxComponents struct {
// AuxSigner is an optional signer that can be used to sign auxiliary
// leaves for certain custom channel types.
AuxSigner fn.Option[lnwallet.AuxSigner]

// AuxDataParser is an optional data parser that can be used to parse
// auxiliary data for certain custom channel types.
AuxDataParser fn.Option[AuxDataParser]
}

// DefaultWalletImpl is the default implementation of our normal, btcwallet
Expand Down
39 changes: 37 additions & 2 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/lightningnetwork/lnd/contractcourt"
"github.com/lightningnetwork/lnd/discovery"
"github.com/lightningnetwork/lnd/feature"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/funding"
"github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
Expand Down Expand Up @@ -82,6 +83,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"gopkg.in/macaroon-bakery.v2/bakery"
)

Expand Down Expand Up @@ -567,6 +569,17 @@ func MainRPCServerPermissions() map[string][]bakery.Op {
}
}

// AuxDataParser is an interface that is used to parse auxiliary custom data
// within RPC messages. This is used to transform binary blobs to human-readable
// JSON representations.
type AuxDataParser interface {
// InlineParseCustomData replaces any custom data binary blob in the
// given RPC message with its corresponding JSON formatted data. This
// transforms the binary (likely TLV encoded) data to a human-readable
// JSON representation (still as byte slice).
InlineParseCustomData(msg proto.Message) error
}

// rpcServer is a gRPC, RPC front end to the lnd daemon.
// TODO(roasbeef): pagination support for the list-style calls
type rpcServer struct {
Expand Down Expand Up @@ -3544,7 +3557,7 @@ func (r *rpcServer) ChannelBalance(ctx context.Context,
unsettledRemoteBalance, pendingOpenLocalBalance,
pendingOpenRemoteBalance)

return &lnrpc.ChannelBalanceResponse{
resp := &lnrpc.ChannelBalanceResponse{
LocalBalance: &lnrpc.Amount{
Sat: uint64(localBalance.ToSatoshis()),
Msat: uint64(localBalance),
Expand Down Expand Up @@ -3574,7 +3587,19 @@ func (r *rpcServer) ChannelBalance(ctx context.Context,
// Deprecated fields.
Balance: int64(localBalance.ToSatoshis()),
PendingOpenBalance: int64(pendingOpenLocalBalance.ToSatoshis()),
}, nil
}

err = fn.MapOptionZ(
r.server.implCfg.AuxDataParser,
func(parser AuxDataParser) error {
return parser.InlineParseCustomData(resp)
},
)
if err != nil {
return nil, fmt.Errorf("error parsing custom data: %w", err)
}

return resp, nil
}

type (
Expand Down Expand Up @@ -4330,6 +4355,16 @@ func (r *rpcServer) ListChannels(ctx context.Context,
resp.Channels = append(resp.Channels, channel)
}

err = fn.MapOptionZ(
r.server.implCfg.AuxDataParser,
func(parser AuxDataParser) error {
return parser.InlineParseCustomData(resp)
},
)
if err != nil {
return nil, fmt.Errorf("error parsing custom data: %w", err)
}

return resp, nil
}

Expand Down

0 comments on commit 5a834c1

Please sign in to comment.