-
Notifications
You must be signed in to change notification settings - Fork 328
/
signedaction.go
489 lines (465 loc) · 12.2 KB
/
signedaction.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
// Copyright (c) 2019 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package action
import (
"encoding/hex"
"math/big"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/iotex-address/address"
"github.com/pkg/errors"
)
// vars
var (
ValidSig, _ = hex.DecodeString("15e73ad521ec9e06600c59e49b127c9dee114ad64fb2fcbe5e0d9f4c8d2b766e73d708cca1dc050dd27b20f2ee607f30428bf035f45d4da8ec2fb04a90c2c30901")
)
type SignedActionOption func(*EnvelopeBuilder)
func WithChainID(chainID uint32) SignedActionOption {
return func(b *EnvelopeBuilder) {
b.SetChainID(chainID)
}
}
// SignedTransfer return a signed transfer
func SignedTransfer(recipientAddr string, senderPriKey crypto.PrivateKey, nonce uint64, amount *big.Int, payload []byte, gasLimit uint64, gasPrice *big.Int, options ...SignedActionOption) (*SealedEnvelope, error) {
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(NewTransfer(amount, recipientAddr, payload))
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, senderPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign transfer %v", elp)
}
return selp, nil
}
// SignedExecution return a signed execution
func SignedExecution(contractAddr string, executorPriKey crypto.PrivateKey, nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, options ...SignedActionOption) (*SealedEnvelope, error) {
execution := NewExecution(contractAddr, amount, data)
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(execution)
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, executorPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign execution %v", elp)
}
return selp, nil
}
// SignedCandidateRegister returns a signed candidate register
func SignedCandidateRegister(
nonce uint64,
name, operatorAddrStr, rewardAddrStr, ownerAddrStr, amountStr string,
duration uint32,
autoStake bool,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
registererPriKey crypto.PrivateKey,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
cr, err := NewCandidateRegister(name, operatorAddrStr, rewardAddrStr, ownerAddrStr, amountStr,
duration, autoStake, payload)
if err != nil {
return nil, err
}
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(cr)
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, registererPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign candidate register %v", elp)
}
return selp, nil
}
// SignedCandidateUpdate returns a signed candidate update
func SignedCandidateUpdate(
nonce uint64,
name, operatorAddrStr, rewardAddrStr string,
gasLimit uint64,
gasPrice *big.Int,
registererPriKey crypto.PrivateKey,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
cu, err := NewCandidateUpdate(name, operatorAddrStr, rewardAddrStr)
if err != nil {
return nil, err
}
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(cu)
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, registererPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign candidate update %v", elp)
}
return selp, nil
}
// SignedCandidateActivate returns a signed candidate selfstake
func SignedCandidateActivate(
nonce uint64,
bucketIndex uint64,
gasLimit uint64,
gasPrice *big.Int,
registererPriKey crypto.PrivateKey,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
cu := NewCandidateActivate(bucketIndex)
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(cu)
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, registererPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign candidate selfstake %v", elp)
}
return selp, nil
}
// SignedCandidateEndorsementLegacy returns a signed candidate endorsement
func SignedCandidateEndorsementLegacy(
nonce uint64,
bucketIndex uint64,
endorse bool,
gasLimit uint64,
gasPrice *big.Int,
registererPriKey crypto.PrivateKey,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
cu := NewCandidateEndorsementLegacy(bucketIndex, endorse)
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(cu)
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, registererPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign candidate endorsement %v", elp)
}
return selp, nil
}
// SignedCandidateEndorsement returns a signed candidate endorsement
func SignedCandidateEndorsement(
nonce uint64,
bucketIndex uint64,
op CandidateEndorsementOp,
gasLimit uint64,
gasPrice *big.Int,
registererPriKey crypto.PrivateKey,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
cu, err := NewCandidateEndorsement(bucketIndex, op)
if err != nil {
return nil, err
}
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(cu)
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, registererPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign candidate endorsement %v", elp)
}
return selp, nil
}
// SignedCreateStake returns a signed create stake
func SignedCreateStake(nonce uint64,
candidateName, amount string,
duration uint32,
autoStake bool,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
stakerPriKey crypto.PrivateKey,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
cs, err := NewCreateStake(candidateName, amount, duration, autoStake, payload)
if err != nil {
return nil, err
}
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(cs)
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, stakerPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign create stake %v", elp)
}
return selp, nil
}
// SignedReclaimStake returns a signed unstake or withdraw stake
func SignedReclaimStake(
withdraw bool,
nonce uint64,
bucketIndex uint64,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
reclaimerPriKey crypto.PrivateKey,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit)
for _, opt := range options {
opt(bd)
}
var elp Envelope
// unstake
if !withdraw {
us := NewUnstake(bucketIndex, payload)
elp = bd.SetAction(us).Build()
} else {
w := NewWithdrawStake(bucketIndex, payload)
elp = bd.SetAction(w).Build()
}
selp, err := Sign(elp, reclaimerPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign reclaim stake %v", elp)
}
return selp, nil
}
// SignedChangeCandidate returns a signed change candidate
func SignedChangeCandidate(
nonce uint64,
candName string,
bucketIndex uint64,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
stakerPriKey crypto.PrivateKey,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
cc := NewChangeCandidate(candName, bucketIndex, payload)
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(cc)
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, stakerPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign change candidate %v", elp)
}
return selp, nil
}
// SignedTransferStake returns a signed transfer stake
func SignedTransferStake(
nonce uint64,
voterAddress string,
bucketIndex uint64,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
stakerPriKey crypto.PrivateKey,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
ts, err := NewTransferStake(voterAddress, bucketIndex, payload)
if err != nil {
return nil, err
}
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(ts)
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, stakerPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign transfer stake %v", elp)
}
return selp, nil
}
// SignedDepositToStake returns a signed deposit to stake
func SignedDepositToStake(
nonce uint64,
index uint64,
amount string,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
depositorPriKey crypto.PrivateKey,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
ds, err := NewDepositToStake(index, amount, payload)
if err != nil {
return nil, err
}
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(ds)
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, depositorPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign deposit to stake %v", elp)
}
return selp, nil
}
// SignedRestake returns a signed restake
func SignedRestake(
nonce uint64,
index uint64,
duration uint32,
autoStake bool,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
restakerPriKey crypto.PrivateKey,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
rs := NewRestake(index, duration, autoStake, payload)
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(rs)
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, restakerPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign restake %v", elp)
}
return selp, nil
}
// SignedCandidateTransferOwnership returns a signed candidate transfer ownership
func SignedCandidateTransferOwnership(
nonce uint64,
ownerAddrStr string,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
senderPriKey crypto.PrivateKey,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
cto, err := NewCandidateTransferOwnership(ownerAddrStr, payload)
if err != nil {
return nil, err
}
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(cto)
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, senderPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign candidate transfer ownership %v", elp)
}
return selp, nil
}
func SignedMigrateStake(
nonce uint64,
bucketIndex uint64,
gasLimit uint64,
gasPrice *big.Int,
senderPriKey crypto.PrivateKey,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
cto := NewMigrateStake(bucketIndex)
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(cto)
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, senderPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign candidate transfer ownership %v", elp)
}
return selp, nil
}
func SignedClaimRewardLegacy(
nonce uint64,
gasLimit uint64,
gasPrice *big.Int,
senderPriKey crypto.PrivateKey,
amount *big.Int,
payload []byte,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
return SignedClaimReward(nonce, gasLimit, gasPrice, senderPriKey, amount, payload, nil, options...)
}
func SignedClaimReward(
nonce uint64,
gasLimit uint64,
gasPrice *big.Int,
senderPriKey crypto.PrivateKey,
amount *big.Int,
payload []byte,
address address.Address,
options ...SignedActionOption,
) (*SealedEnvelope, error) {
act := NewClaimFromRewardingFund(amount, address, payload)
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(act)
for _, opt := range options {
opt(bd)
}
elp := bd.Build()
selp, err := Sign(elp, senderPriKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign candidate transfer ownership %v", elp)
}
return selp, nil
}