Skip to content

Commit

Permalink
Merge pull request #2477 from cfromknecht/utxo-lnrpc
Browse files Browse the repository at this point in the history
lnrpc: replace ChanPoint w/ OutPoint in ListUnspent resp
  • Loading branch information
halseth committed Feb 4, 2019
2 parents 5d7bb74 + 4c63262 commit 5d33d72
Show file tree
Hide file tree
Showing 7 changed files with 710 additions and 571 deletions.
19 changes: 17 additions & 2 deletions cmd/lncli/commands.go
Expand Up @@ -340,11 +340,26 @@ func listUnspent(ctx *cli.Context) error {
MinConfs: int32(minConfirms),
MaxConfs: int32(maxConfirms),
}
jsonResponse, err := client.ListUnspent(ctxb, req)
resp, err := client.ListUnspent(ctxb, req)
if err != nil {
return err
}
printRespJSON(jsonResponse)

// Parse the response into the final json object that will be printed
// to stdout. At the moment, this filters out the raw txid bytes from
// each utxo's outpoint and only prints the txid string.
var listUnspentResp = struct {
Utxos []*Utxo `json:"utxos"`
}{
Utxos: make([]*Utxo, 0, len(resp.Utxos)),
}
for _, protoUtxo := range resp.Utxos {
utxo := NewUtxoFromProto(protoUtxo)
listUnspentResp.Utxos = append(listUnspentResp.Utxos, utxo)
}

printJSON(listUnspentResp)

return nil
}

Expand Down
40 changes: 40 additions & 0 deletions cmd/lncli/types.go
@@ -0,0 +1,40 @@
package main

import (
"fmt"

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

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

// NewOutPointFromProto formats the lnrpc.OutPoint into an OutPoint for display.
func NewOutPointFromProto(op *lnrpc.OutPoint) OutPoint {
return OutPoint(fmt.Sprintf("%s:%d", op.TxidStr, op.OutputIndex))
}

// Utxo displays information about an unspent output, including its address,
// amount, pkscript, and confirmations.
type Utxo struct {
Type lnrpc.AddressType `json:"address_type"`
Address string `json:"address"`
AmountSat int64 `json:"amount_sat"`
ScriptPubkey string `json:"script_pubkey"`
OutPoint OutPoint `json:"outpoint"`
Confirmations int64 `json:"confirmations"`
}

// NewUtxoFromProto creates a display Utxo from the Utxo proto. This filters out
// the raw txid bytes from the provided outpoint, which will otherwise be
// printed in base64.
func NewUtxoFromProto(utxo *lnrpc.Utxo) *Utxo {
return &Utxo{
Type: utxo.Type,
Address: utxo.Address,
AmountSat: utxo.AmountSat,
ScriptPubkey: utxo.ScriptPubkey,
OutPoint: NewOutPointFromProto(utxo.Outpoint),
Confirmations: utxo.Confirmations,
}
}
2 changes: 1 addition & 1 deletion lnrpc/gen_protos.sh
Expand Up @@ -27,7 +27,7 @@ do
DIRECTORY=$(dirname ${file})
echo "Generating protos from ${file}, into ${DIRECTORY}"

protoc -I/usr/local/include -I.\
protoc -I/usr/local/include -I. \
-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--go_out=plugins=grpc,paths=source_relative:. \
${file}
Expand Down

0 comments on commit 5d33d72

Please sign in to comment.