-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
rpc_utils.go
90 lines (77 loc) · 2.87 KB
/
rpc_utils.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
88
89
90
package lnrpc
import (
"encoding/hex"
"errors"
"sort"
"github.com/lightningnetwork/lnd/lnwallet"
)
// RPCTransactionDetails returns a set of rpc transaction details.
func RPCTransactionDetails(txns []*lnwallet.TransactionDetail) *TransactionDetails {
txDetails := &TransactionDetails{
Transactions: make([]*Transaction, len(txns)),
}
for i, tx := range txns {
var destAddresses []string
for _, destAddress := range tx.DestAddresses {
destAddresses = append(destAddresses, destAddress.EncodeAddress())
}
// We also get unconfirmed transactions, so BlockHash can be
// nil.
blockHash := ""
if tx.BlockHash != nil {
blockHash = tx.BlockHash.String()
}
txDetails.Transactions[i] = &Transaction{
TxHash: tx.Hash.String(),
Amount: int64(tx.Value),
NumConfirmations: tx.NumConfirmations,
BlockHash: blockHash,
BlockHeight: tx.BlockHeight,
TimeStamp: tx.Timestamp,
TotalFees: tx.TotalFees,
DestAddresses: destAddresses,
RawTxHex: hex.EncodeToString(tx.RawTx),
Label: tx.Label,
}
}
// Sort transactions by number of confirmations rather than height so
// that unconfirmed transactions (height =0; confirmations =-1) will
// follow the most recently set of confirmed transactions. If we sort
// by height, unconfirmed transactions will follow our oldest
// transactions, because they have lower block heights.
sort.Slice(txDetails.Transactions, func(i, j int) bool {
return txDetails.Transactions[i].NumConfirmations <
txDetails.Transactions[j].NumConfirmations
})
return txDetails
}
// ExtractMinConfs extracts the minimum number of confirmations that each
// output used to fund a transaction should satisfy.
func ExtractMinConfs(minConfs int32, spendUnconfirmed bool) (int32, error) {
switch {
// Ensure that the MinConfs parameter is non-negative.
case minConfs < 0:
return 0, errors.New("minimum number of confirmations must " +
"be a non-negative number")
// The transaction should not be funded with unconfirmed outputs
// unless explicitly specified by SpendUnconfirmed. We do this to
// provide sane defaults to the OpenChannel RPC, as otherwise, if the
// MinConfs field isn't explicitly set by the caller, we'll use
// unconfirmed outputs without the caller being aware.
case minConfs == 0 && !spendUnconfirmed:
return 1, nil
// In the event that the caller set MinConfs > 0 and SpendUnconfirmed to
// true, we'll return an error to indicate the conflict.
case minConfs > 0 && spendUnconfirmed:
return 0, errors.New("SpendUnconfirmed set to true with " +
"MinConfs > 0")
// The funding transaction of the new channel to be created can be
// funded with unconfirmed outputs.
case spendUnconfirmed:
return 0, nil
// If none of the above cases matched, we'll return the value set
// explicitly by the caller.
default:
return minConfs, nil
}
}