forked from eoscanada/eos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
393 lines (318 loc) · 10.1 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package eos
import (
"bytes"
"compress/flate"
"compress/zlib"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"math"
"reflect"
"time"
"io"
"encoding/json"
"io/ioutil"
"github.com/eoscanada/eos-go/ecc"
)
type TransactionHeader struct {
Expiration JSONTime `json:"expiration"`
RefBlockNum uint16 `json:"ref_block_num"`
RefBlockPrefix uint32 `json:"ref_block_prefix"`
MaxNetUsageWords Varuint32 `json:"max_net_usage_words"`
MaxCPUUsageMS uint8 `json:"max_cpu_usage_ms"`
DelaySec Varuint32 `json:"delay_sec"` // number of secs to delay, making it cancellable for that duration
}
type Transaction struct { // WARN: is a `variant` in C++, can be a SignedTransaction or a Transaction.
TransactionHeader
ContextFreeActions []*Action `json:"context_free_actions"`
Actions []*Action `json:"actions"`
Extensions []*Extension `json:"transaction_extensions"`
}
// NewTransaction creates a transaction. Unless you plan on adding HeadBlockID later, to be complete, opts should contain it. Sign
func NewTransaction(actions []*Action, opts *TxOptions) *Transaction {
if opts == nil {
opts = &TxOptions{}
}
tx := &Transaction{Actions: actions}
tx.Fill(opts.HeadBlockID, opts.DelaySecs, opts.MaxNetUsageWords, opts.MaxCPUUsageMS)
return tx
}
func (tx *Transaction) SetExpiration(in time.Duration) {
tx.Expiration = JSONTime{time.Now().UTC().Add(in)}
}
type Extension struct {
Type uint16
Data HexBytes
}
func (e *Extension) MarshalJSON() ([]byte, error) {
pair := make([]interface{}, 2)
pair[0] = e.Type
pair[1] = e.Data
return json.Marshal(&pair)
}
func (e *Extension) UnmarshalJSON(data []byte) error {
var pair []interface{}
err := json.Unmarshal(data, &pair)
if err != nil {
return err
}
typeFloat, ok := pair[0].(float64)
if !ok {
return unmarshalTypeError(pair[0], typeFloat, e, "Type")
}
if typeFloat < 0 || typeFloat > math.MaxUint16 {
return unmarshalTypeError(pair[0], e.Type, e, "Type")
}
e.Type = uint16(typeFloat)
dataHexString, ok := pair[1].(string)
if !ok {
return unmarshalTypeError(pair[1], dataHexString, e, "Data")
}
e.Data, err = hex.DecodeString(dataHexString)
if err != nil {
return unmarshalTypeError(pair[1], e.Data, e, "Data")
}
return nil
}
func unmarshalTypeError(value interface{}, reflectTypeHost interface{}, target interface{}, field string) *json.UnmarshalTypeError {
return &json.UnmarshalTypeError{
Value: fmt.Sprintf("%T", value),
Type: reflect.TypeOf(reflectTypeHost),
Struct: fmt.Sprintf("%T", target),
Field: field,
}
}
// Fill sets the fields on a transaction. If you pass `headBlockID`, then `api` can be nil. If you don't pass `headBlockID`, then the `api` is going to be called to fetch
func (tx *Transaction) Fill(headBlockID Checksum256, delaySecs, maxNetUsageWords uint32, maxCPUUsageMS uint8) {
tx.setRefBlock(headBlockID)
if tx.ContextFreeActions == nil {
tx.ContextFreeActions = make([]*Action, 0, 0)
}
if tx.Extensions == nil {
tx.Extensions = make([]*Extension, 0, 0)
}
tx.MaxNetUsageWords = Varuint32(maxNetUsageWords)
tx.MaxCPUUsageMS = maxCPUUsageMS
tx.DelaySec = Varuint32(delaySecs)
tx.SetExpiration(30 * time.Second)
}
func (tx *Transaction) setRefBlock(blockID []byte) {
if len(blockID) == 0 {
return
}
tx.RefBlockNum = uint16(binary.BigEndian.Uint32(blockID[:4]))
tx.RefBlockPrefix = binary.LittleEndian.Uint32(blockID[8:16])
}
type SignedTransaction struct {
*Transaction
Signatures []ecc.Signature `json:"signatures"`
ContextFreeData []HexBytes `json:"context_free_data"`
packed *PackedTransaction
}
func NewSignedTransaction(tx *Transaction) *SignedTransaction {
return &SignedTransaction{
Transaction: tx,
Signatures: make([]ecc.Signature, 0),
ContextFreeData: make([]HexBytes, 0),
}
}
func (s *SignedTransaction) String() string {
data, err := json.Marshal(s)
if err != nil {
return err.Error()
}
return string(data)
}
func (s *SignedTransaction) SignedByKeys(chainID Checksum256) (out []ecc.PublicKey, err error) {
trx, cfd, err := s.PackedTransactionAndCFD()
if err != nil {
return
}
for _, sig := range s.Signatures {
pubKey, err := sig.PublicKey(SigDigest(chainID, trx, cfd))
if err != nil {
return nil, err
}
out = append(out, pubKey)
}
return
}
func (s *SignedTransaction) PackedTransactionAndCFD() ([]byte, []byte, error) {
rawtrx, err := MarshalBinary(s.Transaction)
if err != nil {
return nil, nil, err
}
rawcfd := []byte{}
if len(s.ContextFreeData) > 0 {
rawcfd, err = MarshalBinary(s.ContextFreeData)
if err != nil {
return nil, nil, err
}
}
return rawtrx, rawcfd, nil
}
func (s *SignedTransaction) Pack(compression CompressionType) (*PackedTransaction, error) {
rawtrx, rawcfd, err := s.PackedTransactionAndCFD()
if err != nil {
return nil, err
}
switch compression {
case CompressionZlib:
var trx bytes.Buffer
var cfd bytes.Buffer
// Compress Trx
writer, _ := zlib.NewWriterLevel(&trx, flate.BestCompression) // can only fail if invalid `level`..
writer.Write(rawtrx) // ignore error, could only bust memory
err = writer.Close()
if err != nil {
return nil, fmt.Errorf("tx writer close %s", err)
}
rawtrx = trx.Bytes()
// Compress ContextFreeData
writer, _ = zlib.NewWriterLevel(&cfd, flate.BestCompression) // can only fail if invalid `level`..
writer.Write(rawcfd) // ignore errors, memory errors only
err = writer.Close()
if err != nil {
return nil, fmt.Errorf("cfd writer close %s", err)
}
rawcfd = cfd.Bytes()
}
packed := &PackedTransaction{
Signatures: s.Signatures,
Compression: compression,
PackedContextFreeData: rawcfd,
PackedTransaction: rawtrx,
wasPackedLocally: true,
}
return packed, nil
}
// PackedTransaction represents a fully packed transaction, with
// signatures, and all. They circulate like that on the P2P net, and
// that's how they are stored.
type PackedTransaction struct {
Signatures []ecc.Signature `json:"signatures"`
Compression CompressionType `json:"compression"` // in C++, it's an enum, not sure how it Binary-marshals..
PackedContextFreeData HexBytes `json:"packed_context_free_data"`
PackedTransaction HexBytes `json:"packed_trx"`
wasPackedLocally bool
}
// ID returns the hash of a transaction.
func (p *PackedTransaction) ID() (Checksum256, error) {
h := sha256.New()
if p.wasPackedLocally {
_, _ = h.Write(p.PackedTransaction)
return h.Sum(nil), nil
}
signed, err := p.UnpackBare()
if err != nil {
return nil, err
}
repacked, err := signed.Pack(CompressionNone)
if err != nil {
return nil, err
}
_, _ = h.Write(repacked.PackedTransaction)
return h.Sum(nil), nil
}
// Unpack decodes the bytestream of the transaction, and attempts to
// decode the registered actions.
func (p *PackedTransaction) Unpack() (signedTx *SignedTransaction, err error) {
return p.unpack(false)
}
// UnpackBare decodes the transcation payload, but doesn't decode the
// nested action data structure. See also `Unpack`.
func (p *PackedTransaction) UnpackBare() (signedTx *SignedTransaction, err error) {
return p.unpack(true)
}
func (p *PackedTransaction) unpack(bare bool) (signedTx *SignedTransaction, err error) {
var txReader io.Reader
txReader = bytes.NewBuffer(p.PackedTransaction)
var freeDataReader io.Reader
freeDataReader = bytes.NewBuffer(p.PackedContextFreeData)
switch p.Compression {
case CompressionZlib:
txReader, err = zlib.NewReader(txReader)
if err != nil {
return nil, fmt.Errorf("new reader for tx, %s", err)
}
if len(p.PackedContextFreeData) > 0 {
freeDataReader, err = zlib.NewReader(freeDataReader)
if err != nil {
return nil, fmt.Errorf("new reader for free data, %s", err)
}
}
}
data, err := ioutil.ReadAll(txReader)
if err != nil {
return nil, fmt.Errorf("unpack read all, %s", err)
}
decoder := NewDecoder(data)
decoder.DecodeActions(!bare)
var tx Transaction
err = decoder.Decode(&tx)
if err != nil {
return nil, fmt.Errorf("unpacking Transaction, %s", err)
}
// TODO: wire that in
//decoder = NewDecoder(freeDataReader)
//var contextFreeData []HexBytes
//err = decoder.Decode(&contextFreeData)
//if err != nil {
// fmt.Println("PackedTransaction@freedata err: ", err)
// return
//}
signedTx = NewSignedTransaction(&tx)
//signedTx.ContextFreeData = contextFreeData
signedTx.Signatures = p.Signatures
signedTx.packed = p
return
}
type DeferredTransaction struct {
*Transaction
SenderID uint32 `json:"sender_id"`
Sender AccountName `json:"sender"`
DelayUntil JSONTime `json:"delay_until"`
}
type ScheduledTransaction struct {
TransactionID Checksum256 `json:"trx_id"`
Sender AccountName `json:"sender"`
SenderID string `json:"sender_id"`
Payer AccountName `json:"payer"`
DelayUntil JSONTime `json:"delay_until"`
Expiration JSONTime `json:"expiration"`
Published JSONTime `json:"published"`
Transaction *Transaction `json:"transaction"`
}
// TxOptions represents options you want to pass to the transaction
// you're sending.
type TxOptions struct {
ChainID Checksum256 // If specified, we won't hit the API to fetch it
HeadBlockID Checksum256 // If provided, don't hit API to fetch it. This allows offline transaction signing.
MaxNetUsageWords uint32
DelaySecs uint32
MaxCPUUsageMS uint8 // If you want to override the CPU usage (in counts of 1024)
//ExtraKCPUUsage uint32 // If you want to *add* some CPU usage to the estimated amount (in counts of 1024)
Compress CompressionType
}
// FillFromChain will load ChainID (for signing transactions) and
// HeadBlockID (to fill transaction with TaPoS data).
func (opts *TxOptions) FillFromChain(api *API) error {
if opts == nil {
return errors.New("TxOptions should not be nil, send an object")
}
if opts.HeadBlockID == nil || opts.ChainID == nil {
info, err := api.cachedGetInfo()
if err != nil {
return err
}
if opts.HeadBlockID == nil {
opts.HeadBlockID = info.HeadBlockID
}
if opts.ChainID == nil {
opts.ChainID = info.ChainID
}
}
return nil
}