/
actions.go
321 lines (275 loc) · 7.93 KB
/
actions.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
package txbuilder
import (
"context"
stdjson "encoding/json"
"errors"
"golang.org/x/crypto/sha3"
"github.com/bytom/vapor/common"
cfg "github.com/bytom/vapor/config"
"github.com/bytom/vapor/consensus"
"github.com/bytom/vapor/encoding/json"
"github.com/bytom/vapor/protocol/bc"
"github.com/bytom/vapor/protocol/bc/types"
"github.com/bytom/vapor/protocol/vm/vmutil"
)
// DecodeControlAddressAction convert input data to action struct
func DecodeControlAddressAction(data []byte) (Action, error) {
a := new(controlAddressAction)
err := stdjson.Unmarshal(data, a)
return a, err
}
type controlAddressAction struct {
bc.AssetAmount
Address string `json:"address"`
}
func (a *controlAddressAction) Build(ctx context.Context, b *TemplateBuilder) error {
var missing []string
if a.Address == "" {
missing = append(missing, "address")
}
if a.AssetId.IsZero() {
missing = append(missing, "asset_id")
}
if a.Amount == 0 {
missing = append(missing, "amount")
}
if len(missing) > 0 {
return MissingFieldsError(missing...)
}
address, err := common.DecodeAddress(a.Address, &consensus.ActiveNetParams)
if err != nil {
return err
}
redeemContract := address.ScriptAddress()
program := []byte{}
switch address.(type) {
case *common.AddressWitnessPubKeyHash:
program, err = vmutil.P2WPKHProgram(redeemContract)
case *common.AddressWitnessScriptHash:
program, err = vmutil.P2WSHProgram(redeemContract)
default:
return errors.New("unsupport address type")
}
if err != nil {
return err
}
out := types.NewIntraChainOutput(*a.AssetId, a.Amount, program)
return b.AddOutput(out)
}
func (a *controlAddressAction) ActionType() string {
return "control_address"
}
// DecodeControlProgramAction convert input data to action struct
func DecodeControlProgramAction(data []byte) (Action, error) {
a := new(controlProgramAction)
err := stdjson.Unmarshal(data, a)
return a, err
}
type controlProgramAction struct {
bc.AssetAmount
Program json.HexBytes `json:"control_program"`
}
func (a *controlProgramAction) Build(ctx context.Context, b *TemplateBuilder) error {
var missing []string
if len(a.Program) == 0 {
missing = append(missing, "control_program")
}
if a.AssetId.IsZero() {
missing = append(missing, "asset_id")
}
if a.Amount == 0 {
missing = append(missing, "amount")
}
if len(missing) > 0 {
return MissingFieldsError(missing...)
}
out := types.NewIntraChainOutput(*a.AssetId, a.Amount, a.Program)
return b.AddOutput(out)
}
func (a *controlProgramAction) ActionType() string {
return "control_program"
}
// DecodeRetireAction convert input data to action struct
func DecodeRetireAction(data []byte) (Action, error) {
a := new(retireAction)
err := stdjson.Unmarshal(data, a)
return a, err
}
type retireAction struct {
bc.AssetAmount
Arbitrary json.HexBytes `json:"arbitrary"`
}
func (a *retireAction) Build(ctx context.Context, b *TemplateBuilder) error {
var missing []string
if a.AssetId.IsZero() {
missing = append(missing, "asset_id")
}
if a.Amount == 0 {
missing = append(missing, "amount")
}
if len(missing) > 0 {
return MissingFieldsError(missing...)
}
program, err := vmutil.RetireProgram(a.Arbitrary)
if err != nil {
return err
}
out := types.NewIntraChainOutput(*a.AssetId, a.Amount, program)
return b.AddOutput(out)
}
func (a *retireAction) ActionType() string {
return "retire"
}
// DecodeCrossOutAction convert input data to action struct
func DecodeCrossOutAction(data []byte) (Action, error) {
a := new(crossOutAction)
err := stdjson.Unmarshal(data, a)
return a, err
}
type crossOutAction struct {
bc.AssetAmount
Address string `json:"address"`
Program json.HexBytes `json:"control_program"`
}
func (a *crossOutAction) Build(ctx context.Context, b *TemplateBuilder) error {
var missing []string
if a.Address == "" && len(a.Program) == 0 {
missing = append(missing, "address or program")
}
if a.AssetId.IsZero() {
missing = append(missing, "asset_id")
}
if a.Amount == 0 {
missing = append(missing, "amount")
}
if len(missing) > 0 {
return MissingFieldsError(missing...)
}
program := a.Program
if a.Address != "" {
address, err := common.DecodeAddress(a.Address, consensus.BytomMainNetParams(&consensus.ActiveNetParams))
if err != nil {
return err
}
redeemContract := address.ScriptAddress()
switch address.(type) {
case *common.AddressWitnessPubKeyHash:
program, err = vmutil.P2WPKHProgram(redeemContract)
case *common.AddressWitnessScriptHash:
program, err = vmutil.P2WSHProgram(redeemContract)
default:
return errors.New("unsupport address type")
}
if err != nil {
return err
}
}
out := types.NewCrossChainOutput(*a.AssetId, a.Amount, program)
return b.AddOutput(out)
}
func (a *crossOutAction) ActionType() string {
return "cross_chain_out"
}
// DecodeVoteOutputAction convert input data to action struct
func DecodeVoteOutputAction(data []byte) (Action, error) {
a := new(voteOutputAction)
err := stdjson.Unmarshal(data, a)
return a, err
}
type voteOutputAction struct {
bc.AssetAmount
Address string `json:"address"`
Vote json.HexBytes `json:"vote"`
}
func (a *voteOutputAction) Build(ctx context.Context, b *TemplateBuilder) error {
var missing []string
if a.Address == "" {
missing = append(missing, "address")
}
if a.AssetId.IsZero() {
missing = append(missing, "asset_id")
}
if a.Amount == 0 {
missing = append(missing, "amount")
}
if len(a.Vote) == 0 {
missing = append(missing, "vote")
}
if len(missing) > 0 {
return MissingFieldsError(missing...)
}
address, err := common.DecodeAddress(a.Address, &consensus.ActiveNetParams)
if err != nil {
return err
}
redeemContract := address.ScriptAddress()
program := []byte{}
switch address.(type) {
case *common.AddressWitnessPubKeyHash:
program, err = vmutil.P2WPKHProgram(redeemContract)
case *common.AddressWitnessScriptHash:
program, err = vmutil.P2WSHProgram(redeemContract)
default:
return errors.New("unsupport address type")
}
if err != nil {
return err
}
out := types.NewVoteOutput(*a.AssetId, a.Amount, program, a.Vote)
return b.AddOutput(out)
}
func (a *voteOutputAction) ActionType() string {
return "vote_output"
}
// DecodeCrossInAction convert input data to action struct
func DecodeCrossInAction(data []byte) (Action, error) {
a := new(crossInAction)
err := stdjson.Unmarshal(data, a)
return a, err
}
type crossInAction struct {
bc.AssetAmount
SourceID bc.Hash `json:"source_id"`
SourcePos uint64 `json:"source_pos"`
VMVersion uint64 `json:"vm_version"`
RawDefinitionByte json.HexBytes `json:"raw_definition_byte"`
IssuanceProgram json.HexBytes `json:"issuance_program"`
}
func (c *crossInAction) Build(ctx context.Context, builder *TemplateBuilder) error {
var missing []string
if c.SourceID.IsZero() {
missing = append(missing, "source_id")
}
if c.AssetId.IsZero() {
missing = append(missing, "asset_id")
}
if c.Amount == 0 {
missing = append(missing, "amount")
}
if len(missing) > 0 {
return MissingFieldsError(missing...)
}
if err := c.checkAssetID(); err != nil {
return err
}
// arguments will be set when materializeWitnesses
txin := types.NewCrossChainInput(nil, c.SourceID, *c.AssetId, c.Amount, c.SourcePos, c.VMVersion, c.RawDefinitionByte, c.IssuanceProgram)
tplIn := &SigningInstruction{}
fed := cfg.CommonConfig.Federation
if !common.IsOpenFederationIssueAsset(c.RawDefinitionByte) {
tplIn.AddRawWitnessKeys(fed.Xpubs, cfg.FedAddressPath, fed.Quorum)
tplIn.AddDataWitness(cfg.FederationPMultiSigScript(cfg.CommonConfig))
}
return builder.AddInput(txin, tplIn)
}
func (c *crossInAction) ActionType() string {
return "cross_chain_in"
}
func (c *crossInAction) checkAssetID() error {
defHash := bc.NewHash(sha3.Sum256(c.RawDefinitionByte))
assetID := bc.ComputeAssetID(c.IssuanceProgram, c.VMVersion, &defHash)
if *c.AssetId != *consensus.BTMAssetID && assetID != *c.AssetAmount.AssetId {
return errors.New("incorrect asset_idincorrect asset_id")
}
return nil
}