github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/protocol/bc/tx.go (about)

     1  package bc
     2  
     3  import (
     4  	"github.com/bytom/bytom/crypto/sha3pool"
     5  	"github.com/bytom/bytom/errors"
     6  )
     7  
     8  // Tx is a wrapper for the entries-based representation of a transaction.
     9  type Tx struct {
    10  	*TxHeader
    11  	ID       Hash
    12  	Entries  map[Hash]Entry
    13  	InputIDs []Hash // 1:1 correspondence with TxData.Inputs
    14  
    15  	SpentOutputIDs []Hash
    16  	GasInputIDs    []Hash
    17  }
    18  
    19  // SigHash ...
    20  func (tx *Tx) SigHash(n uint32) (hash Hash) {
    21  	hasher := sha3pool.Get256()
    22  	defer sha3pool.Put256(hasher)
    23  
    24  	tx.InputIDs[n].WriteTo(hasher)
    25  	tx.ID.WriteTo(hasher)
    26  	hash.ReadFrom(hasher)
    27  	return hash
    28  }
    29  
    30  // Convenience routines for accessing entries of specific types by ID.
    31  var (
    32  	ErrEntryType    = errors.New("invalid entry type")
    33  	ErrMissingEntry = errors.New("missing entry")
    34  )
    35  
    36  // Output try to get the output entry by given hash
    37  func (tx *Tx) Output(id Hash) (*Output, error) {
    38  	e, ok := tx.Entries[id]
    39  	if !ok || e == nil {
    40  		return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
    41  	}
    42  	o, ok := e.(*Output)
    43  	if !ok {
    44  		return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
    45  	}
    46  	return o, nil
    47  }
    48  
    49  // Spend try to get the spend entry by given hash
    50  func (tx *Tx) Spend(id Hash) (*Spend, error) {
    51  	e, ok := tx.Entries[id]
    52  	if !ok || e == nil {
    53  		return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
    54  	}
    55  	sp, ok := e.(*Spend)
    56  	if !ok {
    57  		return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
    58  	}
    59  	return sp, nil
    60  }
    61  
    62  // Issuance try to get the issuance entry by given hash
    63  func (tx *Tx) Issuance(id Hash) (*Issuance, error) {
    64  	e, ok := tx.Entries[id]
    65  	if !ok || e == nil {
    66  		return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
    67  	}
    68  	iss, ok := e.(*Issuance)
    69  	if !ok {
    70  		return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
    71  	}
    72  	return iss, nil
    73  }