github.com/decred/dcrlnd@v0.7.6/cmd/dcrlncli/types.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/hex"
     5  	"errors"
     6  	"fmt"
     7  	"strconv"
     8  	"strings"
     9  
    10  	"github.com/decred/dcrd/chaincfg/chainhash"
    11  	"github.com/decred/dcrlnd/lnrpc"
    12  )
    13  
    14  // OutPoint displays an outpoint string in the form "<txid>:<output-index>".
    15  type OutPoint string
    16  
    17  // NewOutPointFromProto formats the lnrpc.OutPoint into an OutPoint for display.
    18  func NewOutPointFromProto(op *lnrpc.OutPoint) OutPoint {
    19  	var hash chainhash.Hash
    20  	copy(hash[:], op.TxidBytes)
    21  	return OutPoint(fmt.Sprintf("%v:%d", hash, op.OutputIndex))
    22  }
    23  
    24  // NewProtoOutPoint parses an OutPoint into its corresponding lnrpc.OutPoint
    25  // type.
    26  func NewProtoOutPoint(op string) (*lnrpc.OutPoint, error) {
    27  	parts := strings.Split(op, ":")
    28  	if len(parts) != 2 {
    29  		return nil, errors.New("outpoint should be of the form txid:index")
    30  	}
    31  	txid := parts[0]
    32  	if hex.DecodedLen(len(txid)) != chainhash.HashSize {
    33  		return nil, fmt.Errorf("invalid hex-encoded txid %v", txid)
    34  	}
    35  	outputIndex, err := strconv.Atoi(parts[1])
    36  	if err != nil {
    37  		return nil, fmt.Errorf("invalid output index: %v", err)
    38  	}
    39  	return &lnrpc.OutPoint{
    40  		TxidStr:     txid,
    41  		OutputIndex: uint32(outputIndex),
    42  	}, nil
    43  }
    44  
    45  // Utxo displays information about an unspent output, including its address,
    46  // amount, pkscript, and confirmations.
    47  type Utxo struct {
    48  	Type          lnrpc.AddressType `json:"address_type"`
    49  	Address       string            `json:"address"`
    50  	AmountAtoms   int64             `json:"amount_atoms"`
    51  	PkScript      string            `json:"pk_script"`
    52  	OutPoint      OutPoint          `json:"outpoint"`
    53  	Confirmations int64             `json:"confirmations"`
    54  }
    55  
    56  // NewUtxoFromProto creates a display Utxo from the Utxo proto. This filters out
    57  // the raw txid bytes from the provided outpoint, which will otherwise be
    58  // printed in base64.
    59  func NewUtxoFromProto(utxo *lnrpc.Utxo) *Utxo {
    60  	return &Utxo{
    61  		Type:          utxo.AddressType,
    62  		Address:       utxo.Address,
    63  		AmountAtoms:   utxo.AmountAtoms,
    64  		PkScript:      utxo.PkScript,
    65  		OutPoint:      NewOutPointFromProto(utxo.Outpoint),
    66  		Confirmations: utxo.Confirmations,
    67  	}
    68  }
    69  
    70  // FailedUpdate displays information about a failed update, including its
    71  // address, reason and update error.
    72  type FailedUpdate struct {
    73  	OutPoint    OutPoint `json:"outpoint"`
    74  	Reason      string   `json:"reason"`
    75  	UpdateError string   `json:"update_error"`
    76  }
    77  
    78  // NewFailedUpdateFromProto creates a display from the FailedUpdate
    79  // proto. This filters out the raw txid bytes from the provided outpoint,
    80  // which will otherwise be printed in base64.
    81  func NewFailedUpdateFromProto(update *lnrpc.FailedUpdate) *FailedUpdate {
    82  	return &FailedUpdate{
    83  		OutPoint:    NewOutPointFromProto(update.Outpoint),
    84  		Reason:      update.Reason.String(),
    85  		UpdateError: update.UpdateError,
    86  	}
    87  }