This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 360
/
actions.go
139 lines (120 loc) · 3.23 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
package txbuilder
import (
"context"
stdjson "encoding/json"
"errors"
"github.com/bytom/bytom/common"
"github.com/bytom/bytom/consensus"
"github.com/bytom/bytom/encoding/json"
"github.com/bytom/bytom/protocol/bc"
"github.com/bytom/bytom/protocol/bc/types"
"github.com/bytom/bytom/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.NewTxOutput(*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.NewTxOutput(*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.NewTxOutput(*a.AssetId, a.Amount, program)
return b.AddOutput(out)
}
func (a *retireAction) ActionType() string {
return "retire"
}