Skip to content

Commit

Permalink
lncli: format htlc hops as hex
Browse files Browse the repository at this point in the history
  • Loading branch information
cfromknecht committed Sep 16, 2019
1 parent a1c8d14 commit 0369eb7
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cmd/lncli/commands.go
Expand Up @@ -2844,7 +2844,9 @@ func listPayments(ctx *cli.Context) error {
return err
}

printRespJSON(payments)
resp := NewListPaymentsResponseFromProto(payments)
printJSON(resp)

return nil
}

Expand Down
142 changes: 142 additions & 0 deletions cmd/lncli/types.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"strconv"
Expand All @@ -11,6 +12,26 @@ import (
"github.com/lightningnetwork/lnd/lnrpc"
)

// HexBytes is a byte string that gets marshaled as a hex string.
type HexBytes []byte

// MarshalJSON encodes the byte slice as a hex string.
func (b *HexBytes) MarshalJSON(m *json.Marshaler) ([]byte, error) {
h := hex.EncodeToString(*b)
return []byte(h), nil
}

// UnmarshalJSON decodes a hex string into the byte slice.
func (b *HexBytes) UnmarshalJSON(u *json.Unmarshaler, p []byte) error {
bb, err := hex.DecodeString(string(p))
if err != nil {
return err
}
*b = bb

return nil
}

// OutPoint displays an outpoint string in the form "<txid>:<output-index>".
type OutPoint string

Expand Down Expand Up @@ -66,3 +87,124 @@ func NewUtxoFromProto(utxo *lnrpc.Utxo) *Utxo {
Confirmations: utxo.Confirmations,
}
}

// ListPaymentsResponse contains a list of payments made by this node.
type ListPaymentsResponse struct {
// Payments is the list of payments made.
Payments []Payment `json:"payments"`
}

// Payment encapsulates all information about a particular payment including all
// HTLCs made in attempt to pay the recipient.
type Payment struct {
// PaymentHash is the payment hash that the payment is made to.
PaymentHash string `json:"payment_hash"`

// Value is the amount paid to the recipient. Deprecated, use ValueSat
// or Value Msat.
Value int64 `json:"value"` // Deprecated: Do not use.

// CreationDate is the time at which this record was created.
CreationDate int64 `json:"creation_date"`

// Path is the path taken. Depcreated, use Path on individual Htlcs.
Path []string `json:"path"` // Deprecated: Do not use.

// Fee is the total fees paid. Deprecated, use FeeSat or FeeMsat.
Fee int64 `json:"fee"` // Deprecated: Do not use.

// PaymentPreimage is the preimage learnt event of a successful payment.
PaymentPreimage string `json:"payment_preimage"`

// ValueSat is the amount delivered to the recipient in satoshis.
ValueSat int64 `json:"value_sat"`

// ValueMsat is the amount delivered to the recipient in milli-satoshis.
ValueMsat int64 `json:"value_msat`

// PaymentRequest is the optional payment request that was paid.
PaymentRequest string `json:"payment_request"`

// Status is the status of the overall payment.
Status lnrpc.Payment_PaymentStatus `json:"status"`

// FeeSat is the total fees paid in satoshis.
FeeSat int64 `json:"fee_sat"`

// FeeMsat is the total fees paid in milli-satoshis.
FeeMsat int64 `json:"fee_msat`

// Htlcs the list of HTLCs attempted to complete the payment.
Htlcs []HTLCAttempt `json:"htlcs"`
}

// HTLCAttempt presents info about a particular HTLC we sent out.
type HTLCAttempt struct {
// Status is the status of the HTLC.
Status lnrpc.HTLCAttempt_HTLCStatus `json:"status"`

// Path is the path taken by this HTLC.
Path []HexBytes `json:"path"`

// AttemptTime is the time at which this HTLC was attempted.
AttemptTime int64 `json:"attempt_time"`

// ResolveTime is the time at which this HTLC was settled or failed.
// This value will not be set if the HTLC is still IN_FLIGHT.
ResolveTime int64 `json:"resolve_time"`
}

// NewListPaymentsResponseFromProto converts an rpc ListPaymentsResponse into an
// lncli struct.
func NewListPaymentsResponseFromProto(
resp *lnrpc.ListPaymentsResponse) ListPaymentsResponse {

payments := make([]Payment, 0, len(resp.Payments))
for _, payment := range resp.Payments {
payments = append(payments, NewPaymentFromProto(payment))
}

return ListPaymentsResponse{
Payments: payments,
}
}

// NewPaymentFromProto converts the rpc Payment into one an lncli struct.
func NewPaymentFromProto(payment *lnrpc.Payment) Payment {
htlcs := make([]HTLCAttempt, 0, len(payment.Htlcs))
for _, htlc := range payment.Htlcs {
htlcs = append(htlcs, NewHTLCAttemptFromProto(htlc))
}

return Payment{
PaymentHash: payment.PaymentHash,
Value: payment.Value,
CreationDate: payment.CreationDate,
Path: payment.Path,
Fee: payment.Fee,
PaymentPreimage: payment.PaymentPreimage,
ValueSat: payment.ValueSat,
ValueMsat: payment.ValueMsat,
PaymentRequest: payment.PaymentRequest,
Status: payment.Status,
FeeSat: payment.FeeSat,
FeeMsat: payment.FeeMsat,
Htlcs: htlcs,
}
}

// NewHTLCAttemptFromProto converts the rpc HTLCAttempt into one an lncli
// struct. This allows the HTLCAttempt to print its routes as hex strings.
func NewHTLCAttemptFromProto(attempt *lnrpc.HTLCAttempt) HTLCAttempt {
path := make([]HexBytes, 0, len(attempt.Path))
for _, hop := range attempt.Path {
path = append(path, hop)
}

return HTLCAttempt{
Status: attempt.Status,
Path: path,
AttemptTime: attempt.AttemptTime,
ResolveTime: attempt.ResolveTime,
}
}

0 comments on commit 0369eb7

Please sign in to comment.