Skip to content

Commit

Permalink
build+lncli: default to hex encoding for byte slices
Browse files Browse the repository at this point in the history
This commit swaps out golang/protobuf/jsonpb for a custom variant that
by default prints byte slices as hex, which is more useful for our
setting. Some existing wrapper structs are removed as they can now be
printed directly with the new jsonpb.

!!! NOTE !!!

This commit introduces a breaking change to lncli listinvoices since
payment hashes and preimages will now be printed in hex instead of
base64.
  • Loading branch information
cfromknecht committed Oct 31, 2019
1 parent 64e5d06 commit ce03d1e
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 139 deletions.
2 changes: 1 addition & 1 deletion cmd/lncli/cmd_build_route.go
Expand Up @@ -88,7 +88,7 @@ func buildRoute(ctx *cli.Context) error {
return err
}

printJSON(route)
printRespJSON(route)

return nil
}
27 changes: 1 addition & 26 deletions cmd/lncli/cmd_query_mission_control.go
Expand Up @@ -4,7 +4,6 @@ package main

import (
"context"
"encoding/hex"

"github.com/lightningnetwork/lnd/lnrpc/routerrpc"

Expand All @@ -31,31 +30,7 @@ func queryMissionControl(ctx *cli.Context) error {
return err
}

type displayPairHistory struct {
NodeFrom, NodeTo string
LastAttemptSuccessful bool
Timestamp int64
MinPenalizeAmtSat int64
}

displayResp := struct {
Pairs []displayPairHistory
}{}

for _, n := range snapshot.Pairs {
displayResp.Pairs = append(
displayResp.Pairs,
displayPairHistory{
NodeFrom: hex.EncodeToString(n.NodeFrom),
NodeTo: hex.EncodeToString(n.NodeTo),
LastAttemptSuccessful: n.LastAttemptSuccessful,
Timestamp: n.Timestamp,
MinPenalizeAmtSat: n.MinPenalizeAmtSat,
},
)
}

printJSON(displayResp)
printRespJSON(snapshot)

return nil
}
48 changes: 8 additions & 40 deletions cmd/lncli/commands.go
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -20,8 +19,9 @@ import (

"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/lightninglabs/protobuf-hex-display/json"
"github.com/lightninglabs/protobuf-hex-display/jsonpb"
"github.com/lightninglabs/protobuf-hex-display/proto"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/walletunlocker"
Expand Down Expand Up @@ -2321,15 +2321,7 @@ func sendPaymentRequest(ctx *cli.Context, req *lnrpc.SendRequest) error {

paymentStream.CloseSend()

printJSON(struct {
E string `json:"payment_error"`
P string `json:"payment_preimage"`
R *lnrpc.Route `json:"payment_route"`
}{
E: resp.PaymentError,
P: hex.EncodeToString(resp.PaymentPreimage),
R: resp.PaymentRoute,
})
printRespJSON(resp)

// If we get a payment error back, we pass an error
// up to main which eventually calls fatal() and returns
Expand Down Expand Up @@ -2531,15 +2523,7 @@ func sendToRouteRequest(ctx *cli.Context, req *lnrpc.SendToRouteRequest) error {
return err
}

printJSON(struct {
E string `json:"payment_error"`
P string `json:"payment_preimage"`
R *lnrpc.Route `json:"payment_route"`
}{
E: resp.PaymentError,
P: hex.EncodeToString(resp.PaymentPreimage),
R: resp.PaymentRoute,
})
printRespJSON(resp)

return nil
}
Expand Down Expand Up @@ -2667,15 +2651,7 @@ func addInvoice(ctx *cli.Context) error {
return err
}

printJSON(struct {
RHash string `json:"r_hash"`
PayReq string `json:"pay_req"`
AddIndex uint64 `json:"add_index"`
}{
RHash: hex.EncodeToString(resp.RHash),
PayReq: resp.PaymentRequest,
AddIndex: resp.AddIndex,
})
printRespJSON(resp)

return nil
}
Expand Down Expand Up @@ -3795,16 +3771,8 @@ func exportChanBackup(ctx *cli.Context) error {
}.String())
}

printJSON(struct {
ChanPoints []string `json:"chan_points"`
MultiChanBackup string `json:"multi_chan_backup"`
}{
ChanPoints: chanPoints,
MultiChanBackup: hex.EncodeToString(
chanBackup.MultiChanBackup.MultiChanBackup,
),
},
)
printRespJSON(chanBackup)

return nil
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/lncli/invoicesrpc_active.go
Expand Up @@ -81,7 +81,7 @@ func settleInvoice(ctx *cli.Context) error {
return err
}

printJSON(resp)
printRespJSON(resp)

return nil
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func cancelInvoice(ctx *cli.Context) error {
return err
}

printJSON(resp)
printRespJSON(resp)

return nil
}
Expand Down
11 changes: 1 addition & 10 deletions cmd/lncli/watchtower_active.go
Expand Up @@ -4,7 +4,6 @@ package main

import (
"context"
"encoding/hex"

"github.com/lightningnetwork/lnd/lnrpc/watchtowerrpc"
"github.com/urfave/cli"
Expand Down Expand Up @@ -51,15 +50,7 @@ func towerInfo(ctx *cli.Context) error {
return err
}

printJSON(struct {
Pubkey string `json:"pubkey"`
Listeners []string `json:"listeners"`
URIs []string `json:"uris"`
}{
Pubkey: hex.EncodeToString(resp.Pubkey),
Listeners: resp.Listeners,
URIs: resp.Uris,
})
printRespJSON(resp)

return nil
}
12 changes: 2 additions & 10 deletions cmd/lncli/wtclient.go
Expand Up @@ -170,16 +170,8 @@ func listTowers(ctx *cli.Context) error {
return err
}

var listTowersResp = struct {
Towers []*Tower `json:"towers"`
}{
Towers: make([]*Tower, len(resp.Towers)),
}
for i, tower := range resp.Towers {
listTowersResp.Towers[i] = NewTowerFromProto(tower)
}
printRespJSON(resp)

printJSON(listTowersResp)
return nil
}

Expand Down Expand Up @@ -224,7 +216,7 @@ func getTower(ctx *cli.Context) error {
return err
}

printJSON(NewTowerFromProto(resp))
printRespJSON(resp)
return nil
}

Expand Down
50 changes: 0 additions & 50 deletions cmd/lncli/wtclient_types.go

This file was deleted.

3 changes: 3 additions & 0 deletions go.mod
Expand Up @@ -34,6 +34,7 @@ require (
github.com/juju/version v0.0.0-20180108022336-b64dbd566305 // indirect
github.com/kkdai/bstream v0.0.0-20181106074824-b3251f7901ec
github.com/lightninglabs/neutrino v0.10.0
github.com/lightninglabs/protobuf-hex-display v1.3.2
github.com/lightningnetwork/lightning-onion v0.0.0-20190909101754-850081b08b6a
github.com/lightningnetwork/lnd/queue v1.0.1
github.com/lightningnetwork/lnd/ticker v1.0.0
Expand All @@ -60,4 +61,6 @@ replace github.com/lightningnetwork/lnd/queue => ./queue

replace git.schwanenlied.me/yawning/bsaes.git => github.com/Yawning/bsaes v0.0.0-20180720073208-c0276d75487e

replace github.com/lightninglabs/protobuf-hex-display => github.com/lightninglabs/protobuf-hex-display v1.3.3-0.20191031062740-ab4db36fadc8

go 1.12
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -132,6 +132,8 @@ github.com/lightninglabs/gozmq v0.0.0-20190710231225-cea2a031735d h1:tt8hwvxl6fk
github.com/lightninglabs/gozmq v0.0.0-20190710231225-cea2a031735d/go.mod h1:vxmQPeIQxPf6Jf9rM8R+B4rKBqLA2AjttNxkFBL2Plk=
github.com/lightninglabs/neutrino v0.10.0 h1:yWVy2cOCCXbKFdpYCE9vD1fWRJDd9FtGXhUws4l9RkU=
github.com/lightninglabs/neutrino v0.10.0/go.mod h1:C3KhCMk1Mcx3j8v0qRVWM1Ow6rIJSvSPnUAq00ZNAfk=
github.com/lightninglabs/protobuf-hex-display v1.3.3-0.20191031062740-ab4db36fadc8 h1:W4PZ1lzAXDxxe4daAyfx8rrWajUFDAbXaUfP8cGxhjg=
github.com/lightninglabs/protobuf-hex-display v1.3.3-0.20191031062740-ab4db36fadc8/go.mod h1:KDb67YMzoh4eudnzClmvs2FbiLG9vxISmLApUkCa4uI=
github.com/lightningnetwork/lightning-onion v0.0.0-20190909101754-850081b08b6a h1:GoWPN4i4jTKRxhVNh9a2vvBBO1Y2seiJB+SopUYoKyo=
github.com/lightningnetwork/lightning-onion v0.0.0-20190909101754-850081b08b6a/go.mod h1:rigfi6Af/KqsF7Za0hOgcyq2PNH4AN70AaMRxcJkff4=
github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796 h1:sjOGyegMIhvgfq5oaue6Td+hxZuf3tDC8lAPrFldqFw=
Expand Down

0 comments on commit ce03d1e

Please sign in to comment.