/
transaction.go
304 lines (261 loc) · 7.34 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package types
import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"github.com/mixbee/mixbee-crypto/keypair"
"github.com/mixbee/mixbee/common"
"github.com/mixbee/mixbee/common/constants"
"github.com/mixbee/mixbee/common/serialization"
"github.com/mixbee/mixbee/core/payload"
"github.com/mixbee/mixbee/core/program"
"github.com/mixbee/mixbee/errors"
)
type Transaction struct {
Version byte
TxType TransactionType
Nonce uint32
GasPrice uint64
GasLimit uint64
Payer common.Address
Payload Payload
//Attributes []*TxAttribute
attributes byte //this must be 0 now, Attribute Array length use VarUint encoding, so byte is enough for extension
Sigs []*Sig
hash *common.Uint256
}
type Sig struct {
SigData [][]byte
PubKeys []keypair.PublicKey
M uint16
}
func (self *Sig) Serialize(w io.Writer) error {
invocationScript := program.ProgramFromParams(self.SigData)
var verificationScript []byte
if len(self.PubKeys) == 0 {
return errors.NewErr("no pubkeys in sig")
} else if len(self.PubKeys) == 1 {
verificationScript = program.ProgramFromPubKey(self.PubKeys[0])
} else {
script, err := program.ProgramFromMultiPubKey(self.PubKeys, int(self.M))
if err != nil {
return err
}
verificationScript = script
}
err := serialization.WriteVarBytes(w, invocationScript)
if err != nil {
return err
}
err = serialization.WriteVarBytes(w, verificationScript)
if err != nil {
return err
}
return nil
}
func (self *Sig) Deserialize(r io.Reader) error {
invocationScript, err := serialization.ReadVarBytes(r)
if err != nil {
return err
}
verificationScript, err := serialization.ReadVarBytes(r)
if err != nil {
return err
}
sigs, err := program.GetParamInfo(invocationScript)
if err != nil {
return err
}
info, err := program.GetProgramInfo(verificationScript)
if err != nil {
return err
}
self.SigData = sigs
self.M = info.M
self.PubKeys = info.PubKeys
return nil
}
func (self *Transaction) GetSignatureAddresses() []common.Address {
address := make([]common.Address, 0, len(self.Sigs))
for _, sig := range self.Sigs {
m := int(sig.M)
n := len(sig.PubKeys)
if n == 1 {
address = append(address, AddressFromPubKey(sig.PubKeys[0]))
} else {
addr, err := AddressFromMultiPubKeys(sig.PubKeys, m)
if err != nil {
return nil
}
address = append(address, addr)
}
}
return address
}
type TransactionType byte
const (
Bookkeeper TransactionType = 0x02
Deploy TransactionType = 0xd0
Invoke TransactionType = 0xd1
)
// Payload define the func for loading the payload data
// base on payload type which have different struture
type Payload interface {
//Serialize payload data
Serialize(w io.Writer) error
Deserialize(r io.Reader) error
}
// Serialize the Transaction
func (tx *Transaction) Serialize(w io.Writer) error {
err := tx.SerializeUnsigned(w)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[Serialize], Transaction txSerializeUnsigned Serialize failed.")
}
err = serialization.WriteVarUint(w, uint64(len(tx.Sigs)))
if err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[Serialize], Transaction serialize tx sigs length failed.")
}
for _, sig := range tx.Sigs {
err = sig.Serialize(w)
if err != nil {
return err
}
}
return nil
}
//Serialize the Transaction data without contracts
func (tx *Transaction) SerializeUnsigned(w io.Writer) error {
//txType
if _, err := w.Write([]byte{byte(tx.Version), byte(tx.TxType)}); err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[SerializeUnsigned], Transaction version failed.")
}
if err := serialization.WriteUint32(w, tx.Nonce); err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[SerializeUnsigned], Transaction nonce failed.")
}
if err := serialization.WriteUint64(w, tx.GasPrice); err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[SerializeUnsigned], Transaction gasPrice failed.")
}
if err := serialization.WriteUint64(w, tx.GasLimit); err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[SerializeUnsigned], Transaction gasLimit failed.")
}
if err := tx.Payer.Serialize(w); err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[SerializeUnsigned], Transaction payer failed.")
}
//Payload
if tx.Payload == nil {
return errors.NewErr("Transaction Payload is nil.")
}
if err := tx.Payload.Serialize(w); err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[SerializeUnsigned], Transaction payload failed.")
}
err := serialization.WriteVarUint(w, uint64(tx.attributes))
if err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[SerializeUnsigned], Transaction item txAttribute length serialization failed.")
}
return nil
}
// deserialize the Transaction
func (tx *Transaction) Deserialize(r io.Reader) error {
// tx deserialize
err := tx.DeserializeUnsigned(r)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[Deserialize], Transaction deserializeUnsigned error.")
}
// tx sigs
length, err := serialization.ReadVarUint(r, 0)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[Deserialize], Transaction sigs length deserialize error.")
}
if length > constants.TX_MAX_SIG_SIZE {
return fmt.Errorf("transaction signature number %d execced %d", length, constants.TX_MAX_SIG_SIZE)
}
for i := 0; i < int(length); i++ {
sig := new(Sig)
err := sig.Deserialize(r)
if err != nil {
return errors.NewErr("deserialize transaction failed")
}
tx.Sigs = append(tx.Sigs, sig)
}
return nil
}
func (tx *Transaction) DeserializeUnsigned(r io.Reader) error {
var versiontype [2]byte
_, err := io.ReadFull(r, versiontype[:])
if err != nil {
return err
}
nonce, err := serialization.ReadUint32(r)
if err != nil {
return err
}
gasPrice, err := serialization.ReadUint64(r)
if err != nil {
return err
}
gasLimit, err := serialization.ReadUint64(r)
if err != nil {
return err
}
tx.Version = versiontype[0]
tx.TxType = TransactionType(versiontype[1])
tx.Nonce = nonce
tx.GasPrice = gasPrice
tx.GasLimit = gasLimit
if err := tx.Payer.Deserialize(r); err != nil {
return err
}
switch tx.TxType {
case Invoke:
tx.Payload = new(payload.InvokeCode)
case Deploy:
tx.Payload = new(payload.DeployCode)
default:
return errors.NewErr(fmt.Sprintf("unsupported tx type %v", tx.Type()))
}
err = tx.Payload.Deserialize(r)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[DeserializeUnsigned], Transaction payload parse error.")
}
//attributes
length, err := serialization.ReadVarUint(r, 0)
if err != nil {
return err
}
if length != 0 {
return fmt.Errorf("transaction attribute must be 0, got %d", length)
}
tx.attributes = 0
return nil
}
func (tx *Transaction) GetMessage() []byte {
buf := new(bytes.Buffer)
tx.SerializeUnsigned(buf)
return buf.Bytes()
}
func (tx *Transaction) ToArray() []byte {
b := new(bytes.Buffer)
tx.Serialize(b)
return b.Bytes()
}
func (tx *Transaction) Hash() common.Uint256 {
if tx.hash == nil {
buf := bytes.Buffer{}
tx.SerializeUnsigned(&buf)
temp := sha256.Sum256(buf.Bytes())
f := common.Uint256(sha256.Sum256(temp[:]))
tx.hash = &f
}
return *tx.hash
}
func (tx *Transaction) SetHash(hash common.Uint256) {
tx.hash = &hash
}
func (tx *Transaction) Type() common.InventoryType {
return common.TRANSACTION
}
func (tx *Transaction) Verify() error {
panic("unimplemented ")
return nil
}