4 "github.com/bytom/bytom/crypto/sha3pool"
5 "github.com/bytom/bytom/errors"
8 // Tx is a wrapper for the entries-based representation of a transaction.
12 Entries map[Hash]Entry
13 InputIDs []Hash // 1:1 correspondence with TxData.Inputs
19 func (tx *Tx) SigHash(n uint32) (hash Hash) {
20 hasher := sha3pool.Get256()
21 defer sha3pool.Put256(hasher)
23 tx.InputIDs[n].WriteTo(hasher)
29 // Convenience routines for accessing entries of specific types by ID.
31 ErrEntryType = errors.New("invalid entry type")
32 ErrMissingEntry = errors.New("missing entry")
35 // OriginalOutput try to get the output entry by given hash
36 func (tx *Tx) OriginalOutput(id Hash) (*OriginalOutput, error) {
37 e, ok := tx.Entries[id]
39 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
41 o, ok := e.(*OriginalOutput)
43 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
48 // Spend try to get the spend entry by given hash
49 func (tx *Tx) Spend(id Hash) (*Spend, error) {
50 e, ok := tx.Entries[id]
52 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
56 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
61 // Issuance try to get the issuance entry by given hash
62 func (tx *Tx) Issuance(id Hash) (*Issuance, error) {
63 e, ok := tx.Entries[id]
65 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
67 iss, ok := e.(*Issuance)
69 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
74 // VetoInput try to get the veto entry by given hash
75 func (tx *Tx) VetoInput(id Hash) (*VetoInput, error) {
76 e, ok := tx.Entries[id]
78 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
80 sp, ok := e.(*VetoInput)
82 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
87 // VoteOutput try to get the vote output entry by given hash
88 func (tx *Tx) VoteOutput(id Hash) (*VoteOutput, error) {
89 e, ok := tx.Entries[id]
91 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
93 o, ok := e.(*VoteOutput)
95 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)