-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.go
243 lines (210 loc) · 5.8 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
package txbuilder
import (
"context"
stdjson "encoding/json"
"errors"
"github.com/janotchain/janeta/common"
"github.com/janotchain/janeta/consensus"
"github.com/janotchain/janeta/encoding/json"
"github.com/janotchain/janeta/protocol/bc"
"github.com/janotchain/janeta/protocol/bc/types"
"github.com/janotchain/janeta/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.NewOriginalTxOutput(*a.AssetId, a.Amount, program, nil)
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.NewOriginalTxOutput(*a.AssetId, a.Amount, a.Program, nil)
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.NewOriginalTxOutput(*a.AssetId, a.Amount, program, nil)
return b.AddOutput(out)
}
func (a *retireAction) ActionType() string {
return "retire"
}
// DecodeRegisterAction convert input data to action struct
func DecodeRegisterAction(data []byte) (Action, error) {
a := new(registerAction)
return a, stdjson.Unmarshal(data, a)
}
type registerAction struct {
bc.AssetAmount
Contract json.HexBytes `json:"contract"`
}
func (a *registerAction) 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(a.Contract) == 0 {
missing = append(missing, "contract")
}
if len(missing) > 0 {
return MissingFieldsError(missing...)
}
if a.AssetId.String() != consensus.JTAAssetID.String() {
return errors.New("register contract action asset must be JTA")
}
if a.Amount < consensus.BCRPRequiredJTAAmount {
return errors.New("less than BCRP required JTA amount")
}
program, err := vmutil.RegisterProgram(a.Contract)
if err != nil {
return err
}
out := types.NewOriginalTxOutput(*a.AssetId, a.Amount, program, [][]byte{})
return b.AddOutput(out)
}
func (a *registerAction) ActionType() string {
return "register_contract"
}
// 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, nil)
return b.AddOutput(out)
}
func (a *voteOutputAction) ActionType() string {
return "vote_output"
}