github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/lib/btc/unspent.go (about)

     1  package btc
     2  
     3  import (
     4  	"encoding/binary"
     5  	"fmt"
     6  )
     7  
     8  type AllUnspentTx []*OneUnspentTx
     9  
    10  // OneUnspentTx is returned by GetUnspentFromPkScr.
    11  type OneUnspentTx struct {
    12  	TxPrevOut
    13  	Value   uint64
    14  	MinedAt uint32
    15  	*BtcAddr
    16  	destAddr string
    17  }
    18  
    19  func (x AllUnspentTx) Len() int {
    20  	return len(x)
    21  }
    22  
    23  func (x AllUnspentTx) Less(i, j int) bool {
    24  	if x[i].MinedAt == x[j].MinedAt {
    25  		if x[i].TxPrevOut.Hash == x[j].TxPrevOut.Hash {
    26  			return x[i].TxPrevOut.Vout < x[j].TxPrevOut.Vout
    27  		}
    28  		return binary.LittleEndian.Uint64(x[i].TxPrevOut.Hash[24:32]) <
    29  			binary.LittleEndian.Uint64(x[j].TxPrevOut.Hash[24:32])
    30  	}
    31  	return x[i].MinedAt < x[j].MinedAt
    32  }
    33  
    34  func (x AllUnspentTx) Swap(i, j int) {
    35  	x[i], x[j] = x[j], x[i]
    36  }
    37  
    38  func (ou *OneUnspentTx) String() (s string) {
    39  	s = fmt.Sprintf("%15.8f  ", float64(ou.Value)/1e8) + ou.TxPrevOut.String()
    40  	if ou.BtcAddr != nil {
    41  		s += " " + ou.DestAddr() + ou.BtcAddr.Label()
    42  	}
    43  	if ou.MinedAt != 0 {
    44  		s += fmt.Sprint("  ", ou.MinedAt)
    45  	}
    46  	return
    47  }
    48  
    49  func (ou *OneUnspentTx) UnspentTextLine() (s string) {
    50  	s = fmt.Sprintf("%s # %.8f BTC @ %s%s, block %d", ou.TxPrevOut.String(),
    51  		float64(ou.Value)/1e8, ou.DestAddr(), ou.BtcAddr.Label(), ou.MinedAt)
    52  	return
    53  }
    54  
    55  func (ou *OneUnspentTx) DestAddr() string {
    56  	if ou.destAddr == "" {
    57  		return ou.BtcAddr.String()
    58  	}
    59  	return ou.destAddr
    60  }