forked from piotrnar/gocoin
/
unspent.go
46 lines (39 loc) · 925 Bytes
/
unspent.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
package btc
import (
"fmt"
"encoding/binary"
)
type AllUnspentTx []*OneUnspentTx
// Returned by GetUnspentFromPkScr
type OneUnspentTx struct {
TxPrevOut
Value uint64
MinedAt uint32
*BtcAddr
}
func (x AllUnspentTx) Len() int {
return len(x)
}
func (x AllUnspentTx) Less(i, j int) bool {
if x[i].MinedAt == x[j].MinedAt {
if x[i].TxPrevOut.Hash==x[j].TxPrevOut.Hash {
return x[i].TxPrevOut.Vout < x[j].TxPrevOut.Vout
}
return binary.LittleEndian.Uint64(x[i].TxPrevOut.Hash[24:32]) <
binary.LittleEndian.Uint64(x[j].TxPrevOut.Hash[24:32])
}
return x[i].MinedAt < x[j].MinedAt
}
func (x AllUnspentTx) Swap(i, j int) {
x[i], x[j] = x[j], x[i]
}
func (ou *OneUnspentTx) String() (s string) {
s = fmt.Sprintf("%15.8f ", float64(ou.Value)/1e8) + ou.TxPrevOut.String()
if ou.BtcAddr!=nil {
s += " " + ou.BtcAddr.StringLab()
}
if ou.MinedAt != 0 {
s += fmt.Sprint(" ", ou.MinedAt)
}
return
}