forked from onflow/flow-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction.go
641 lines (547 loc) · 19.5 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
* Flow Go SDK
*
* Copyright 2019 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package flow
import (
"bytes"
"errors"
"fmt"
"sort"
"github.com/ethereum/go-ethereum/rlp"
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/flow-go-sdk/crypto"
)
// A Transaction is a full transaction object containing a payload and signatures.
type Transaction struct {
// Script is the UTF-8 encoded Cadence source code that defines the execution logic for this transaction.
Script []byte
// Arguments is a list of Cadence values passed into this transaction.
//
// Each argument is encoded as JSON-CDC bytes.
Arguments [][]byte
// ReferenceBlockID is a reference to the block used to calculate the expiry of this transaction.
//
// A transaction is considered expired if it is submitted to Flow after refBlock + N, where N
// is a constant defined by the network.
//
// For example, if a transaction references a block with height of X and the network limit is 10,
// a block with height X+10 is the last block that is allowed to include this transaction.
ReferenceBlockID Identifier
// GasLimit is the maximum number of computational units that can be used to execute this transaction.
GasLimit uint64
// ProposalKey is the account key used to propose this transaction.
//
// A proposal key references a specific key on an account, along with an up-to-date
// sequence number for that key. This sequence number is used to prevent replay attacks.
//
// You can find more information about sequence numbers here: https://docs.onflow.org/concepts/transaction-signing/#sequence-numbers
ProposalKey ProposalKey
// Payer is the account that pays the fee for this transaction.
//
// You can find more information about the payer role here: https://docs.onflow.org/concepts/transaction-signing/#signer-roles
Payer Address
// Authorizers is a list of the accounts that are authorizing this transaction to
// mutate to their on-chain account state.
//
// You can find more information about the authorizer role here: https://docs.onflow.org/concepts/transaction-signing/#signer-roles
Authorizers []Address
// PayloadSignatures is a list of signatures generated by the proposer and authorizer roles.
//
// A payload signature is generated over the inner portion of the transaction (TransactionDomainTag + payload).
//
// You can find more information about transaction signatures here: https://docs.onflow.org/concepts/transaction-signing/#anatomy-of-a-transaction
PayloadSignatures []TransactionSignature
// EnvelopeSignatures is a list of signatures generated by the payer role.
//
// An envelope signature is generated over the outer portion of the transaction (TransactionDomainTag + payload + payloadSignatures).
//
// You can find more information about transaction signatures here: https://docs.onflow.org/concepts/transaction-signing/#anatomy-of-a-transaction
EnvelopeSignatures []TransactionSignature
}
type payloadCanonicalForm struct {
Script []byte
Arguments [][]byte
ReferenceBlockID []byte
GasLimit uint64
ProposalKeyAddress []byte
ProposalKeyIndex uint64
ProposalKeySequenceNumber uint64
Payer []byte
Authorizers [][]byte
}
type envelopeCanonicalForm struct {
Payload payloadCanonicalForm
PayloadSignatures []transactionSignatureCanonicalForm
}
type transactionCanonicalForm struct {
Payload payloadCanonicalForm
PayloadSignatures []transactionSignatureCanonicalForm
EnvelopeSignatures []transactionSignatureCanonicalForm
}
// DefaultTransactionGasLimit should be high enough for small transactions
const DefaultTransactionGasLimit = 9999
// NewTransaction initializes and returns an empty transaction.
func NewTransaction() *Transaction {
return &Transaction{
GasLimit: DefaultTransactionGasLimit,
}
}
// ID returns the canonical SHA3-256 hash of this transaction.
func (t *Transaction) ID() Identifier {
return HashToID(defaultEntityHasher.ComputeHash(t.Encode()))
}
// SetScript sets the Cadence script for this transaction.
//
// The script is the UTF-8 encoded Cadence source code.
func (t *Transaction) SetScript(script []byte) *Transaction {
t.Script = script
return t
}
// AddArgument adds a Cadence argument to this transaction.
func (t *Transaction) AddArgument(arg cadence.Value) error {
encodedArg, err := jsoncdc.Encode(arg)
if err != nil {
return fmt.Errorf("failed to encode argument: %w", err)
}
t.Arguments = append(t.Arguments, encodedArg)
return nil
}
// AddRawArgument adds a raw JSON-CDC encoded argument to this transaction.
func (t *Transaction) AddRawArgument(arg []byte) *Transaction {
t.Arguments = append(t.Arguments, arg)
return t
}
// Argument returns the decoded argument at the given index.
func (t *Transaction) Argument(i int, options ...jsoncdc.Option) (cadence.Value, error) {
if i < 0 {
return nil, fmt.Errorf("argument index must be positive")
}
if i >= len(t.Arguments) {
return nil, fmt.Errorf("no argument at index %d", i)
}
encodedArg := t.Arguments[i]
arg, err := jsoncdc.Decode(nil, encodedArg, options...)
if err != nil {
return nil, fmt.Errorf("failed to decode argument at index %d: %w", i, err)
}
return arg, nil
}
// SetReferenceBlockID sets the reference block ID for this transaction.
//
// A transaction is considered expired if it is submitted to Flow after refBlock + N, where N
// is a constant defined by the network.
//
// For example, if a transaction references a block with height of X and the network limit is 10,
// a block with height X+10 is the last block that is allowed to include this transaction.
func (t *Transaction) SetReferenceBlockID(blockID Identifier) *Transaction {
t.ReferenceBlockID = blockID
return t
}
// SetGasLimit sets the gas limit for this transaction.
func (t *Transaction) SetGasLimit(limit uint64) *Transaction {
t.GasLimit = limit
return t
}
// SetProposalKey sets the proposal key and sequence number for this transaction.
//
// The first two arguments specify the account key to be used, and the last argument is the sequence
// number being declared.
func (t *Transaction) SetProposalKey(address Address, keyIndex int, sequenceNum uint64) *Transaction {
proposalKey := ProposalKey{
Address: address,
KeyIndex: keyIndex,
SequenceNumber: sequenceNum,
}
t.ProposalKey = proposalKey
t.refreshSignerIndex()
return t
}
// SetPayer sets the payer account for this transaction.
func (t *Transaction) SetPayer(address Address) *Transaction {
t.Payer = address
t.refreshSignerIndex()
return t
}
// AddAuthorizer adds an authorizer account to this transaction.
func (t *Transaction) AddAuthorizer(address Address) *Transaction {
t.Authorizers = append(t.Authorizers, address)
t.refreshSignerIndex()
return t
}
// signerList returns a list of unique accounts required to sign this transaction.
//
// The list is returned in the following order:
// 1. PROPOSER
// 2. PAYER
// 2. AUTHORIZERS (in insertion order)
//
// The only exception to the above ordering is for deduplication; if the same account
// is used in multiple signing roles, only the first occurrence is included in the list.
func (t *Transaction) signerList() []Address {
signers := make([]Address, 0)
seen := make(map[Address]struct{})
var addSigner = func(address Address) {
_, ok := seen[address]
if ok {
return
}
signers = append(signers, address)
seen[address] = struct{}{}
}
if t.ProposalKey.Address != EmptyAddress {
addSigner(t.ProposalKey.Address)
}
if t.Payer != EmptyAddress {
addSigner(t.Payer)
}
for _, authorizer := range t.Authorizers {
addSigner(authorizer)
}
return signers
}
// signerMap returns a mapping from address to signer index.
func (t *Transaction) signerMap() map[Address]int {
signers := make(map[Address]int)
for i, signer := range t.signerList() {
signers[signer] = i
}
return signers
}
func (t *Transaction) refreshSignerIndex() {
signerMap := t.signerMap()
for i, sig := range t.PayloadSignatures {
signerIndex, signerExists := signerMap[sig.Address]
if !signerExists {
signerIndex = -1
}
t.PayloadSignatures[i].SignerIndex = signerIndex
}
for i, sig := range t.EnvelopeSignatures {
signerIndex, signerExists := signerMap[sig.Address]
if !signerExists {
signerIndex = -1
}
t.EnvelopeSignatures[i].SignerIndex = signerIndex
}
}
// SignPayload signs the transaction payload (TransactionDomainTag + payload) with the specified account key.
//
// The resulting signature is combined with the account address and key index before
// being added to the transaction.
//
// This function returns an error if the signature cannot be generated.
func (t *Transaction) SignPayload(address Address, keyIndex int, signer crypto.Signer) error {
message := t.PayloadMessage()
message = append(TransactionDomainTag[:], message...)
sig, err := signer.Sign(message)
if err != nil {
// TODO: wrap error
return err
}
t.AddPayloadSignature(address, keyIndex, sig)
return nil
}
// SignEnvelope signs the full transaction (TransactionDomainTag + payload + payload signatures) with the specified account key.
//
// The resulting signature is combined with the account address and key index before
// being added to the transaction.
//
// This function returns an error if the signature cannot be generated.
func (t *Transaction) SignEnvelope(address Address, keyIndex int, signer crypto.Signer) error {
message := t.EnvelopeMessage()
message = append(TransactionDomainTag[:], message...)
sig, err := signer.Sign(message)
if err != nil {
// TODO: wrap error
return err
}
t.AddEnvelopeSignature(address, keyIndex, sig)
return nil
}
// AddPayloadSignature adds a payload signature to the transaction for the given address and key index.
func (t *Transaction) AddPayloadSignature(address Address, keyIndex int, sig []byte) *Transaction {
s := t.createSignature(address, keyIndex, sig)
t.PayloadSignatures = append(t.PayloadSignatures, s)
sort.Slice(t.PayloadSignatures, compareSignatures(t.PayloadSignatures))
t.refreshSignerIndex()
return t
}
// AddEnvelopeSignature adds an envelope signature to the transaction for the given address and key index.
func (t *Transaction) AddEnvelopeSignature(address Address, keyIndex int, sig []byte) *Transaction {
s := t.createSignature(address, keyIndex, sig)
t.EnvelopeSignatures = append(t.EnvelopeSignatures, s)
sort.Slice(t.EnvelopeSignatures, compareSignatures(t.EnvelopeSignatures))
t.refreshSignerIndex()
return t
}
func (t *Transaction) createSignature(address Address, keyIndex int, sig []byte) TransactionSignature {
signerIndex, signerExists := t.signerMap()[address]
if !signerExists {
signerIndex = -1
}
return TransactionSignature{
Address: address,
SignerIndex: signerIndex,
KeyIndex: keyIndex,
Signature: sig,
}
}
func (t *Transaction) PayloadMessage() []byte {
temp := t.payloadCanonicalForm()
return mustRLPEncode(&temp)
}
func (t *Transaction) payloadCanonicalForm() payloadCanonicalForm {
authorizers := make([][]byte, len(t.Authorizers))
for i, auth := range t.Authorizers {
authorizers[i] = auth.Bytes()
}
// note(sideninja): This is a temporary workaround until cadence defines canonical format addressing the issue https://github.com/onflow/flow-go-sdk/issues/286
for i, arg := range t.Arguments {
if arg[len(arg)-1] == byte(10) { // extra new line character
t.Arguments[i] = arg[:len(arg)-1]
}
}
return payloadCanonicalForm{
Script: t.Script,
Arguments: t.Arguments,
ReferenceBlockID: t.ReferenceBlockID[:],
GasLimit: t.GasLimit,
ProposalKeyAddress: t.ProposalKey.Address.Bytes(),
ProposalKeyIndex: uint64(t.ProposalKey.KeyIndex),
ProposalKeySequenceNumber: t.ProposalKey.SequenceNumber,
Payer: t.Payer.Bytes(),
Authorizers: authorizers,
}
}
// EnvelopeMessage returns the signable message for the transaction envelope.
//
// This message is only signed by the payer account.
func (t *Transaction) EnvelopeMessage() []byte {
temp := t.envelopeCanonicalForm()
return mustRLPEncode(&temp)
}
func (t *Transaction) envelopeCanonicalForm() envelopeCanonicalForm {
return envelopeCanonicalForm{
Payload: t.payloadCanonicalForm(),
PayloadSignatures: signaturesList(t.PayloadSignatures).canonicalForm(),
}
}
// Encode serializes the full transaction data including the payload and all signatures.
func (t *Transaction) Encode() []byte {
temp := struct {
Payload payloadCanonicalForm
PayloadSignatures interface{}
EnvelopeSignatures interface{}
}{
Payload: t.payloadCanonicalForm(),
PayloadSignatures: signaturesList(t.PayloadSignatures).canonicalForm(),
EnvelopeSignatures: signaturesList(t.EnvelopeSignatures).canonicalForm(),
}
return mustRLPEncode(&temp)
}
// DecodeTransaction decodes the input bytes into a Transaction struct
// able to decode outputs from PayloadMessage(), EnvelopeMessage() and Encode()
// functions
func DecodeTransaction(transactionMessage []byte) (*Transaction, error) {
temp, err := decodeTransaction(transactionMessage)
if err != nil {
return nil, err
}
authorizers := make([]Address, len(temp.Payload.Authorizers))
for i, auth := range temp.Payload.Authorizers {
authorizers[i] = BytesToAddress(auth)
}
t := &Transaction{
Script: temp.Payload.Script,
Arguments: temp.Payload.Arguments,
ReferenceBlockID: BytesToID(temp.Payload.ReferenceBlockID),
GasLimit: temp.Payload.GasLimit,
ProposalKey: ProposalKey{
Address: BytesToAddress(temp.Payload.ProposalKeyAddress),
KeyIndex: int(temp.Payload.ProposalKeyIndex),
SequenceNumber: temp.Payload.ProposalKeySequenceNumber,
},
Payer: BytesToAddress(temp.Payload.Payer),
Authorizers: authorizers,
}
signers := t.signerList()
if len(temp.PayloadSignatures) > 0 {
payloadSignatures := make([]TransactionSignature, len(temp.PayloadSignatures))
for i, sig := range temp.PayloadSignatures {
payloadSignatures[i] = transactionSignatureFromCanonicalForm(sig)
payloadSignatures[i].Address = signers[payloadSignatures[i].SignerIndex]
}
t.PayloadSignatures = payloadSignatures
}
if len(temp.EnvelopeSignatures) > 0 {
envelopeSignatures := make([]TransactionSignature, len(temp.EnvelopeSignatures))
for i, sig := range temp.EnvelopeSignatures {
envelopeSignatures[i] = transactionSignatureFromCanonicalForm(sig)
envelopeSignatures[i].Address = signers[envelopeSignatures[i].SignerIndex]
}
t.EnvelopeSignatures = envelopeSignatures
}
if len(t.Arguments) == 0 {
t.Arguments = nil
}
if len(t.Script) == 0 {
t.Script = nil
}
return t, nil
}
func decodeTransaction(transactionMessage []byte) (*transactionCanonicalForm, error) {
s := rlp.NewStream(bytes.NewReader(transactionMessage), 0)
temp := &transactionCanonicalForm{}
kind, _, err := s.Kind()
if err != nil {
return nil, err
}
// First kind should always be a list
if kind != rlp.List {
return nil, errors.New("unexpected rlp decoding type")
}
_, err = s.List()
if err != nil {
return nil, err
}
// Need to look at the type of the first element to determine if how we're going to be decoding
kind, _, err = s.Kind()
if err != nil {
return nil, err
}
// If first kind is not list, safe to assume this is actually just encoded payload, and decrypt as such
if kind != rlp.List {
s.Reset(bytes.NewReader(transactionMessage), 0)
txPayload := payloadCanonicalForm{}
err := s.Decode(&txPayload)
if err != nil {
return nil, err
}
temp.Payload = txPayload
return temp, nil
}
// If we're here, we will assume that we're decoding either a envelopeCanonicalForm
// or a full transactionCanonicalForm
// Decode the payload
txPayload := payloadCanonicalForm{}
err = s.Decode(&txPayload)
if err != nil {
return nil, err
}
temp.Payload = txPayload
// Decode the payload sigs
payloadSigs := []transactionSignatureCanonicalForm{}
err = s.Decode(&payloadSigs)
if err != nil {
return nil, err
}
temp.PayloadSignatures = payloadSigs
// It's possible for the envelope signature to not exist (e.g. envelopeCanonicalForm).
kind, _, err = s.Kind()
if errors.Is(err, rlp.EOL) {
return temp, nil
} else if err != nil {
return nil, err
}
// If we're not at EOL, and no error, finish decoding
envelopeSigs := []transactionSignatureCanonicalForm{}
err = s.Decode(&envelopeSigs)
if err != nil {
return nil, err
}
temp.EnvelopeSignatures = envelopeSigs
return temp, nil
}
// A ProposalKey is the key that specifies the proposal key and sequence number for a transaction.
type ProposalKey struct {
Address Address
KeyIndex int
SequenceNumber uint64
}
// A TransactionSignature is a signature associated with a specific account key.
type TransactionSignature struct {
Address Address
SignerIndex int
KeyIndex int
Signature []byte
}
type transactionSignatureCanonicalForm struct {
SignerIndex uint
KeyIndex uint
Signature []byte
}
func (s TransactionSignature) canonicalForm() transactionSignatureCanonicalForm {
return transactionSignatureCanonicalForm{
SignerIndex: uint(s.SignerIndex), // int is not RLP-serializable
KeyIndex: uint(s.KeyIndex), // int is not RLP-serializable
Signature: s.Signature,
}
}
func transactionSignatureFromCanonicalForm(v transactionSignatureCanonicalForm) TransactionSignature {
return TransactionSignature{
SignerIndex: int(v.SignerIndex),
KeyIndex: int(v.KeyIndex),
Signature: v.Signature,
}
}
func compareSignatures(signatures []TransactionSignature) func(i, j int) bool {
return func(i, j int) bool {
sigA := signatures[i]
sigB := signatures[j]
if sigA.SignerIndex == sigB.SignerIndex {
return sigA.KeyIndex < sigB.KeyIndex
}
return sigA.SignerIndex < sigB.SignerIndex
}
}
type signaturesList []TransactionSignature
func (s signaturesList) canonicalForm() []transactionSignatureCanonicalForm {
signatures := make([]transactionSignatureCanonicalForm, len(s))
for i, signature := range s {
signatures[i] = signature.canonicalForm()
}
return signatures
}
type TransactionResult struct {
Status TransactionStatus
Error error
Events []Event
BlockID Identifier
BlockHeight uint64
TransactionID Identifier
}
// TransactionStatus represents the status of a transaction.
type TransactionStatus int
const (
// TransactionStatusUnknown indicates that the transaction status is not known.
TransactionStatusUnknown TransactionStatus = iota
// TransactionStatusPending is the status of a pending transaction.
TransactionStatusPending
// TransactionStatusFinalized is the status of a finalized transaction.
TransactionStatusFinalized
// TransactionStatusExecuted is the status of an executed transaction.
TransactionStatusExecuted
// TransactionStatusSealed is the status of a sealed transaction.
TransactionStatusSealed
// TransactionStatusExpired is the status of an expired transaction.
TransactionStatusExpired
)
// String returns the string representation of a transaction status.
func (s TransactionStatus) String() string {
return [...]string{"UNKNOWN", "PENDING", "FINALIZED", "EXECUTED", "SEALED", "EXPIRED"}[s]
}