-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
87 lines (78 loc) · 2.68 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"encoding/hex"
"errors"
"fmt"
"strconv"
"strings"
"github.com/brsuite/brond/chaincfg/chainhash"
"github.com/brsuite/broln/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 {
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) (*lnrpc.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 &lnrpc.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 lnrpc.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 *lnrpc.Utxo) *Utxo {
return &Utxo{
Type: utxo.AddressType,
Address: utxo.Address,
AmountSat: utxo.AmountSat,
PkScript: utxo.PkScript,
OutPoint: NewOutPointFromProto(utxo.Outpoint),
Confirmations: utxo.Confirmations,
}
}
// FailedUpdate displays information about a failed update, including its
// address, reason and update error.
type FailedUpdate struct {
OutPoint OutPoint `json:"outpoint"`
Reason string `json:"reason"`
UpdateError string `json:"update_error"`
}
// NewFailedUpdateFromProto creates a display from the FailedUpdate
// proto. This filters out the raw txid bytes from the provided outpoint,
// which will otherwise be printed in base64.
func NewFailedUpdateFromProto(update *lnrpc.FailedUpdate) *FailedUpdate {
return &FailedUpdate{
OutPoint: NewOutPointFromProto(update.Outpoint),
Reason: update.Reason.String(),
UpdateError: update.UpdateError,
}
}