4 "github.com/bytom/crypto/sha3pool"
5 "github.com/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
15 // IDs of reachable entries of various kinds
20 func (tx *Tx) SigHash(n uint32) (hash Hash) {
21 hasher := sha3pool.Get256()
22 defer sha3pool.Put256(hasher)
24 tx.InputIDs[n].WriteTo(hasher)
30 // Convenience routines for accessing entries of specific types by ID.
33 ErrEntryType = errors.New("invalid entry type")
34 ErrMissingEntry = errors.New("missing entry")
37 func (tx *Tx) Output(id Hash) (*Output, error) {
38 e, ok := tx.Entries[id]
40 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
44 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
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 func (tx *Tx) Issuance(id Hash) (*Issuance, error) {
62 e, ok := tx.Entries[id]
64 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
66 iss, ok := e.(*Issuance)
68 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)
73 func (tx *Tx) Nonce(id Hash) (*Nonce, error) {
74 e, ok := tx.Entries[id]
76 return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes())
78 nonce, ok := e.(*Nonce)
80 return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e)