forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
68 lines (61 loc) · 2 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"encoding/hex"
"errors"
"fmt"
"github.com/lightningnetwork/lnd/lnrpc/api"
"strconv"
"strings"
"github.com/btcsuite/btcd/chaincfg/chainhash"
)
// 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 *api.OutPoint) OutPoint {
var hash chainhash.Hash
copy(hash[:], op.TxidBytes)
return OutPoint(fmt.Sprintf("%v:%d", hash, op.OutputIndex))
}
// NewProtoOutPoint parses an OutPoint into its corresponding lnrpc.OutPoint
// type.
func NewProtoOutPoint(op string) (*api.OutPoint, error) {
parts := strings.Split(op, ":")
if len(parts) != 2 {
return nil, errors.New("outpoint should be of the form txid:index")
}
txid := parts[0]
if hex.DecodedLen(len(txid)) != chainhash.HashSize {
return nil, fmt.Errorf("invalid hex-encoded txid %v", txid)
}
outputIndex, err := strconv.Atoi(parts[1])
if err != nil {
return nil, fmt.Errorf("invalid output index: %v", err)
}
return &api.OutPoint{
TxidStr: txid,
OutputIndex: uint32(outputIndex),
}, nil
}
// Utxo displays information about an unspent output, including its address,
// amount, pkscript, and confirmations.
type Utxo struct {
Type api.AddressType `json:"address_type"`
Address string `json:"address"`
AmountSat int64 `json:"amount_sat"`
PkScript string `json:"pk_script"`
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 *api.Utxo) *Utxo {
return &Utxo{
Type: utxo.Type,
Address: utxo.Address,
AmountSat: utxo.AmountSat,
PkScript: utxo.PkScript,
OutPoint: NewOutPointFromProto(utxo.Outpoint),
Confirmations: utxo.Confirmations,
}
}