-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
149 lines (125 loc) · 3.78 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//go:generate protoc -I=. -I=$GOPATH/src -I=$GOPATH/src/github.com/Dharitri-org/protobuf/protobuf --gogoslick_out=$GOPATH/src transaction.proto
package transaction
import (
"math/big"
"github.com/Dharitri-org/drtg-core/core"
"github.com/Dharitri-org/drtg-core/core/check"
"github.com/Dharitri-org/drtg-core/data"
)
var _ = data.TransactionHandler(&Transaction{})
// IsInterfaceNil verifies if underlying object is nil
func (tx *Transaction) IsInterfaceNil() bool {
return tx == nil
}
// SetValue sets the value of the transaction
func (tx *Transaction) SetValue(value *big.Int) {
tx.Value = value
}
// SetData sets the data of the transaction
func (tx *Transaction) SetData(data []byte) {
tx.Data = data
}
// SetRcvAddr sets the receiver address of the transaction
func (tx *Transaction) SetRcvAddr(addr []byte) {
tx.RcvAddr = addr
}
// SetSndAddr sets the sender address of the transaction
func (tx *Transaction) SetSndAddr(addr []byte) {
tx.SndAddr = addr
}
// TrimSlicePtr creates a copy of the provided slice without the excess capacity
func TrimSlicePtr(in []*Transaction) []*Transaction {
if len(in) == 0 {
return []*Transaction{}
}
ret := make([]*Transaction, len(in))
copy(ret, in)
return ret
}
// TrimSliceHandler creates a copy of the provided slice without the excess capacity
func TrimSliceHandler(in []data.TransactionHandler) []data.TransactionHandler {
if len(in) == 0 {
return []data.TransactionHandler{}
}
ret := make([]data.TransactionHandler, len(in))
copy(ret, in)
return ret
}
// GetDataForSigning returns the serialized transaction having an empty signature field
func (tx *Transaction) GetDataForSigning(encoder data.Encoder, marshaller data.Marshaller, hasher data.Hasher) ([]byte, error) {
if check.IfNil(encoder) {
return nil, ErrNilEncoder
}
if check.IfNil(marshaller) {
return nil, ErrNilMarshalizer
}
if check.IfNil(hasher) {
return nil, ErrNilHasher
}
receiverAddr, err := encoder.Encode(tx.RcvAddr)
if err != nil {
return nil, err
}
senderAddr, err := encoder.Encode(tx.SndAddr)
if err != nil {
return nil, err
}
ftx := &FrontendTransaction{
Nonce: tx.Nonce,
Value: tx.Value.String(),
Receiver: receiverAddr,
Sender: senderAddr,
GasPrice: tx.GasPrice,
GasLimit: tx.GasLimit,
SenderUsername: tx.SndUserName,
ReceiverUsername: tx.RcvUserName,
Data: tx.Data,
ChainID: string(tx.ChainID),
Version: tx.Version,
Options: tx.Options,
}
if len(tx.GuardianAddr) > 0 {
guardianAddr, errGuardian := encoder.Encode(tx.GuardianAddr)
if errGuardian != nil {
return nil, errGuardian
}
ftx.GuardianAddr = guardianAddr
}
ftxBytes, err := marshaller.Marshal(ftx)
if err != nil {
return nil, err
}
shouldSignOnTxHash := tx.Version > core.InitialVersionOfTransaction && tx.HasOptionHashSignSet()
if !shouldSignOnTxHash {
return ftxBytes, nil
}
ftxHash := hasher.Compute(string(ftxBytes))
return ftxHash, nil
}
// HasOptionGuardianSet returns true if the guarded transaction option is set
func (tx *Transaction) HasOptionGuardianSet() bool {
return tx.Options&MaskGuardedTransaction > 0
}
// HasOptionHashSignSet returns true if the signed with hash option is set
func (tx *Transaction) HasOptionHashSignSet() bool {
return tx.Options&MaskSignedWithHash > 0
}
// CheckIntegrity checks for not nil fields and negative value
func (tx *Transaction) CheckIntegrity() error {
if tx.Signature == nil {
return data.ErrNilSignature
}
if tx.Value == nil {
return data.ErrNilValue
}
if tx.Value.Sign() < 0 {
return data.ErrNegativeValue
}
if len(tx.RcvUserName) > core.MaxUserNameLength {
return data.ErrInvalidUserNameLength
}
if len(tx.SndUserName) > core.MaxUserNameLength {
return data.ErrInvalidUserNameLength
}
return nil
}