-
Notifications
You must be signed in to change notification settings - Fork 178
/
transaction.go
132 lines (111 loc) · 3.14 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
package request
import (
"fmt"
"io"
"github.com/onflow/flow-go/engine/access/rest/models"
"github.com/onflow/flow-go/engine/access/rest/util"
"github.com/onflow/flow-go/engine/common/rpc/convert"
"github.com/onflow/flow-go/model/flow"
)
const maxAuthorizers = 100
const maxAllowedScriptArguments = 100
type Transaction flow.TransactionBody
func (t *Transaction) Parse(raw io.Reader, chain flow.Chain) error {
var tx models.TransactionsBody
err := parseBody(raw, &tx)
if err != nil {
return err
}
if tx.ProposalKey == nil {
return fmt.Errorf("proposal key not provided")
}
if tx.Script == "" {
return fmt.Errorf("script not provided")
}
if tx.Payer == "" {
return fmt.Errorf("payer not provided")
}
if len(tx.Authorizers) > maxAuthorizers {
return fmt.Errorf("too many authorizers. Maximum authorizers allowed: %d", maxAuthorizers)
}
if len(tx.Arguments) > maxAllowedScriptArguments {
return fmt.Errorf("too many arguments. Maximum arguments allowed: %d", maxAllowedScriptArguments)
}
if tx.ReferenceBlockId == "" {
return fmt.Errorf("reference block not provided")
}
if len(tx.EnvelopeSignatures) == 0 {
return fmt.Errorf("envelope signatures not provided")
}
var args Arguments
err = args.Parse(tx.Arguments)
if err != nil {
return err
}
var payer Address
err = payer.Parse(tx.Payer)
if err != nil {
return fmt.Errorf("invalid payer: %w", err)
}
auths := make([]flow.Address, len(tx.Authorizers))
for i, auth := range tx.Authorizers {
var a Address
err := a.Parse(auth)
if err != nil {
return err
}
auths[i] = a.Flow()
}
var proposal ProposalKey
err = proposal.Parse(*tx.ProposalKey)
if err != nil {
return err
}
var payloadSigs TransactionSignatures
err = payloadSigs.Parse(tx.PayloadSignatures)
if err != nil {
return err
}
var envelopeSigs TransactionSignatures
err = envelopeSigs.Parse(tx.EnvelopeSignatures)
if err != nil {
return err
}
// script comes in as a base64 encoded string, decode base64 back to a string here
script, err := util.FromBase64(tx.Script)
if err != nil {
return fmt.Errorf("invalid transaction script encoding")
}
var blockID ID
err = blockID.Parse(tx.ReferenceBlockId)
if err != nil {
return fmt.Errorf("invalid reference block ID: %w", err)
}
gasLimit, err := util.ToUint64(tx.GasLimit)
if err != nil {
return fmt.Errorf("invalid gas limit: %w", err)
}
flowTransaction := flow.TransactionBody{
ReferenceBlockID: blockID.Flow(),
Script: script,
Arguments: args.Flow(),
GasLimit: gasLimit,
ProposalKey: proposal.Flow(),
Payer: payer.Flow(),
Authorizers: auths,
PayloadSignatures: payloadSigs.Flow(),
EnvelopeSignatures: envelopeSigs.Flow(),
}
// we use the gRPC method of converting the incoming transaction to a Flow transaction since
// it sets the signer_index appropriately.
entityTransaction := convert.TransactionToMessage(flowTransaction)
flowTx, err := convert.MessageToTransaction(entityTransaction, chain)
if err != nil {
return err
}
*t = Transaction(flowTx)
return nil
}
func (t Transaction) Flow() flow.TransactionBody {
return flow.TransactionBody(t)
}