-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
225 lines (204 loc) · 6.06 KB
/
tx.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
package tezos
import (
"errors"
"fmt"
"github.com/xiaohuasheng0x1/blockchains/coins/tezos/types"
)
const GasSafetyMargin int64 = 100
var (
// for reveal
defaultRevealLimits = types.Limits{
Fee: 1000,
GasLimit: 1000,
}
// for transfers to tz1/2/3
defaultTransferLimitsEOA = types.Limits{
Fee: 1000,
GasLimit: 1420, // 1820 when source is emptied
}
// for transfers to manager.tz
defaultTransferLimitsKT1 = types.Limits{
Fee: 1000,
GasLimit: 2078,
}
// for delegation
defaultDelegationLimitsEOA = types.Limits{
Fee: 1000,
GasLimit: 1000,
}
// for simulating contract calls and other operations
// used when no explicit costs are set
defaultSimulationLimits = types.Limits{
GasLimit: types.DefaultParams.HardGasLimitPerOperation,
StorageLimit: types.DefaultParams.HardStorageLimitPerOperation,
}
)
func NewTransaction(from, to string, amount int64, opts *CallOptions) (*types.Op, error) {
return NewTransactionByOperation(from, to, amount, types.NewOp(), opts)
}
func NewJakartanetTransaction(from, to string, amount int64, opts *CallOptions) (*types.Op, error) {
return NewTransactionByOperation(from, to, amount, types.NewJakartanetOp(), opts)
}
func InitOperationSourceBranch(from string, op *types.Op, opts *CallOptions) error {
valid, err := ValidAddress(from)
if err != nil || !valid {
return fmt.Errorf("invalid address %q: %v", from, err)
}
sent, err := types.ParseAddress(from)
if err != nil {
return fmt.Errorf("Invalid sender error: %q: %v", from, err)
}
if !sent.IsValid() {
return fmt.Errorf("Invalid sender %q: %v", from, err)
}
op.WithSource(sent)
if opts == nil {
opts = &DefaultOptions
}
if opts.BlockHash.IsValid() {
op.WithBranch(opts.BlockHash)
}
return nil
}
func NewTransactionByOperation(from, to string, amount int64, op *types.Op, opts *CallOptions) (*types.Op, error) {
if op == nil {
op = types.NewOp()
}
if err := InitOperationSourceBranch(from, op, opts); err != nil {
return nil, err
}
recv, err := types.ParseAddress(to)
if err != nil || !recv.IsValid() {
return nil, fmt.Errorf("Invalid receiver %q: %v", to, err)
}
op.WithTransfer(recv, amount)
return op, nil
}
func NewDelegationTransaction(from, to string, opts *CallOptions) (*types.Op, error) {
return NewDelegationTransactionByOperation(from, to, types.NewOp(), opts)
}
func NewJakartanetDelegationTransaction(from, to string, opts *CallOptions) (*types.Op, error) {
return NewDelegationTransactionByOperation(from, to, types.NewJakartanetOp(), opts)
}
func NewDelegationTransactionByOperation(from, to string, op *types.Op, opts *CallOptions) (*types.Op, error) {
if op == nil {
op = types.NewOp()
}
if err := InitOperationSourceBranch(from, op, opts); err != nil {
return nil, err
}
toDelegation, err := types.ParseAddress(to)
if err != nil || !toDelegation.IsValid() {
return nil, fmt.Errorf("Invalid receiver %q: %v", to, err)
}
op.WithDelegation(toDelegation)
return op, nil
}
func NewUnDelegationTransaction(from string, opts *CallOptions) (*types.Op, error) {
return NewUnDelegationTransactionByOperation(from, types.NewOp(), opts)
}
func NewJakartanetUnDelegationTransaction(from string, opts *CallOptions) (*types.Op, error) {
return NewUnDelegationTransactionByOperation(from, types.NewJakartanetOp(), opts)
}
func NewUnDelegationTransactionByOperation(from string, op *types.Op, opts *CallOptions) (*types.Op, error) {
if op == nil {
op = types.NewOp()
}
if err := InitOperationSourceBranch(from, op, opts); err != nil {
return nil, err
}
op.WithUndelegation()
return op, nil
}
func AddTransferOpTransaction(to string, amount int64, op *types.Op) (*types.Op, error) {
if op == nil {
op = types.NewOp()
}
recv, err := types.ParseAddress(to)
if err != nil || !recv.IsValid() {
return nil, fmt.Errorf("Invalid receiver %q: %v", to, err)
}
op.WithTransfer(recv, amount)
return op, nil
}
// CompleteTransaction ensures an operation is compatible with the current source account's
// on-chain state. Sets branch for TTL control, replay counters, and reveals
// the sender's pubkey if not published yet.
func CompleteTransaction(o *types.Op, key types.Key, opts *CallOptions) error {
needBranch := !o.Branch.IsValid()
needCounter := o.NeedCounter()
mayNeedReveal := len(o.Contents) > 0 && o.Contents[0].Kind() != types.OpTypeReveal
if !needBranch && !mayNeedReveal && !needCounter {
return nil
}
// add branch for TTL control
if needBranch {
return errors.New("needBranch")
}
if needCounter || mayNeedReveal {
//add reveal if necessary
if mayNeedReveal && opts.NeedReveal {
reveal := &types.Reveal{
Manager: types.Manager{
Source: key.Address(),
},
PublicKey: key,
}
reveal.WithLimits(defaultRevealLimits)
o.WithContentsFront(reveal)
needCounter = true
}
// add counters
if needCounter {
nextCounter := opts.Counter + 1
for _, op := range o.Contents {
// skip non-manager ops
if op.GetCounter() < 0 {
continue
}
op.WithCounter(nextCounter)
nextCounter++
}
}
}
return nil
}
func BuildTransaction(op *types.Op, fee int64, key types.Key, opts *CallOptions) error {
if err := CompleteTransaction(op, key, opts); err != nil {
return err
}
if opts == nil || !opts.IgnoreLimits {
// use default gas/storage limits, set min fee
for _, oc := range op.Contents {
l := oc.Limits()
if l.GasLimit == 0 {
l.GasLimit = defaultSimulationLimits.GasLimit / int64(len(op.Contents))
}
if l.StorageLimit == 0 {
l.StorageLimit = defaultSimulationLimits.StorageLimit / int64(len(op.Contents))
}
l.Fee = fee
oc.WithLimits(l)
}
}
// check minFee calc against maxFee if set
if opts.MaxFee > 0 {
if l := op.Limits(); l.Fee > opts.MaxFee {
return fmt.Errorf("estimated cost %d > max %d", l.Fee, opts.MaxFee)
}
}
return nil
}
func SignTransaction(op *types.Op, privateKey string, opts *CallOptions) ([]byte, error) {
key, err := types.ParsePrivateKey(privateKey)
if err != nil {
return []byte{}, err
}
err = op.Sign(key)
if err != nil {
return nil, err
}
sig := op.Signature
op.WithSignature(sig)
return op.Bytes(), nil
}