rpcserver+cmd/lncli: implement DecodePayReq

This commit implements the newly added RPC to decode payment requests
passed over the command line or directly via gRPC.

With this tool, users can now examine payment requests they see in the
wild for diagnostic or debugging purposes.
This commit is contained in:
Olaoluwa Osuntokun 2017-01-17 13:39:30 -08:00
parent faae0b3bf2
commit 9662887d2f
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
4 changed files with 56 additions and 0 deletions

View File

@ -531,6 +531,8 @@ func listChannels(ctx *cli.Context) error {
return err
}
// TODO(roasbeef): defer close the client for the all
printRespJson(resp)
return nil
@ -955,3 +957,33 @@ func debugLevel(ctx *cli.Context) error {
printRespJson(resp)
return nil
}
var DecodePayReq = cli.Command{
Name: "decodepayreq",
Usage: "decodepayreq --pay_req=[encoded_pay_req]",
Description: "Decode the passed payment request revealing the destination, payment hash and value of the payment request",
Flags: []cli.Flag{
cli.StringFlag{
Name: "pay_req",
Usage: "the zpay32 encoded payment request",
},
},
Action: decodePayReq,
}
func decodePayReq(ctx *cli.Context) error {
ctxb := context.Background()
client := getClient(ctx)
req := &lnrpc.PayReqString{
PayReq: ctx.String("pay_req"),
}
resp, err := client.DecodePayReq(ctxb, req)
if err != nil {
return err
}
printRespJson(resp)
return nil
}

View File

@ -71,6 +71,7 @@ func main() {
QueryRouteCommand,
GetNetworkInfoCommand,
DebugLevel,
DecodePayReq,
}
if err := app.Run(os.Args); err != nil {

View File

@ -1020,6 +1020,7 @@ func testInvoiceSubscriptions(net *networkHarness, t *harnessTest) {
// With the assertion above set up, send a payment from Alice to Bob
// which should finalize and settle the invoice.
time.Sleep(time.Millisecond * 500)
sendStream, err := net.Alice.SendPayment(ctxb)
if err != nil {
t.Fatalf("unable to create alice payment stream: %v", err)

View File

@ -1792,3 +1792,25 @@ func (r *rpcServer) DebugLevel(ctx context.Context,
return &lnrpc.DebugLevelResponse{}, nil
}
// DecodePayReq takes an encoded payment request string and attempts to decode
// it, returning a full description of the conditions encoded within the
// payment request.
func (r *rpcServer) DecodePayReq(ctx context.Context,
req *lnrpc.PayReqString) (*lnrpc.PayReq, error) {
// Fist we'll attempt to decode the payment request string, if the
// request is invalid or the checksum doesn't match, then we'll exit
// here with an error.
payReq, err := zpay32.Decode(req.PayReq)
if err != nil {
return nil, err
}
dest := payReq.Destination.SerializeCompressed()
return &lnrpc.PayReq{
Destination: hex.EncodeToString(dest),
PaymentHash: hex.EncodeToString(payReq.PaymentHash[:]),
NumSatoshis: int64(payReq.Amount),
}, nil
}