4 "github.com/vapor/crypto/sha3pool"
5 "github.com/vapor/errors"
6 "github.com/vapor/protocol/bc"
9 // Tx is a wrapper for the entries-based representation of a transaction.
13 Entries map[bc.Hash]bc.Entry
14 InputIDs []bc.Hash // 1:1 correspondence with TxData.Inputs
16 SpentOutputIDs []bc.Hash
21 func (tx *Tx) SigHash(n uint32) (hash bc.Hash) {
22 hasher := sha3pool.Get256()
23 defer sha3pool.Put256(hasher)
25 tx.InputIDs[n].WriteTo(hasher)
31 // Convenience routines for accessing entries of specific types by ID.
33 ErrEntryType = errors.New("invalid entry type")
34 ErrMissingEntry = errors.New("missing entry")
37 // Output try to get the output entry by given hash
38 func (tx *Tx) Output(id bc.Hash) (*bc.Output, error) {
39 e, ok := tx.Entries[id]
41 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
43 o, ok := e.(*bc.Output)
45 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
50 // Spend try to get the spend entry by given hash
51 func (tx *Tx) Spend(id bc.Hash) (*bc.Spend, error) {
52 e, ok := tx.Entries[id]
54 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
56 sp, ok := e.(*bc.Spend)
58 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
63 // Issuance try to get the issuance entry by given hash
64 func (tx *Tx) Issuance(id bc.Hash) (*bc.Issuance, error) {
65 e, ok := tx.Entries[id]
67 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
69 iss, ok := e.(*bc.Issuance)
71 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)