4 "github.com/vapor/crypto/sha3pool"
5 "github.com/vapor/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
16 MainchainOutputIDs []Hash
21 func (tx *Tx) SigHash(n uint32) (hash 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 // IntraChainOutput try to get the intra-chain output entry by given hash
38 func (tx *Tx) IntraChainOutput(id Hash) (*IntraChainOutput, error) {
39 e, ok := tx.Entries[id]
41 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
43 o, ok := e.(*IntraChainOutput)
45 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
50 // CrossChainOutput try to get the cross-chain output entry by given hash
51 func (tx *Tx) CrossChainOutput(id Hash) (*CrossChainOutput, error) {
52 e, ok := tx.Entries[id]
54 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
56 o, ok := e.(*CrossChainOutput)
58 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
63 // Entry try to get the entry by given hash
64 func (tx *Tx) Entry(id Hash) (Entry, error) {
65 e, ok := tx.Entries[id]
67 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
72 // Spend try to get the spend entry by given hash
73 func (tx *Tx) Spend(id Hash) (*Spend, error) {
74 e, ok := tx.Entries[id]
76 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
80 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
85 // VetoInput try to get the veto entry by given hash
86 func (tx *Tx) VetoInput(id Hash) (*VetoInput, error) {
87 e, ok := tx.Entries[id]
89 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
91 sp, ok := e.(*VetoInput)
93 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
98 // VoteOutput try to get the vote output entry by given hash
99 func (tx *Tx) VoteOutput(id Hash) (*VoteOutput, error) {
100 e, ok := tx.Entries[id]
102 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
104 o, ok := e.(*VoteOutput)
106 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)