5 stdjson "encoding/json"
8 "github.com/bytom/common"
9 "github.com/bytom/consensus"
10 "github.com/bytom/encoding/json"
11 "github.com/bytom/protocol/bc"
12 "github.com/bytom/protocol/bc/legacy"
13 "github.com/bytom/protocol/vm"
14 "github.com/bytom/protocol/vm/vmutil"
17 var retirementProgram = []byte{byte(vm.OP_FAIL)}
19 // DecodeControlReceiverAction convert input data to action struct
20 func DecodeControlReceiverAction(data []byte) (Action, error) {
21 a := new(controlReceiverAction)
22 err := stdjson.Unmarshal(data, a)
26 type controlReceiverAction struct {
28 Receiver *Receiver `json:"receiver"`
29 ReferenceData json.Map `json:"reference_data"`
32 func (a *controlReceiverAction) Build(ctx context.Context, b *TemplateBuilder) error {
34 if a.Receiver == nil {
35 missing = append(missing, "receiver")
37 if len(a.Receiver.ControlProgram) == 0 {
38 missing = append(missing, "receiver.control_program")
41 if a.AssetId.IsZero() {
42 missing = append(missing, "asset_id")
45 return MissingFieldsError(missing...)
48 out := legacy.NewTxOutput(*a.AssetId, a.Amount, a.Receiver.ControlProgram, a.ReferenceData)
49 return b.AddOutput(out)
52 // DecodeControlAddressAction convert input data to action struct
53 func DecodeControlAddressAction(data []byte) (Action, error) {
54 a := new(controlAddressAction)
55 err := stdjson.Unmarshal(data, a)
59 type controlAddressAction struct {
61 Address string `json:"address"`
62 ReferenceData json.Map `json:"reference_data"`
65 func (a *controlAddressAction) Build(ctx context.Context, b *TemplateBuilder) error {
68 missing = append(missing, "address")
70 if a.AssetId.IsZero() {
71 missing = append(missing, "asset_id")
74 return MissingFieldsError(missing...)
77 address, err := common.DecodeAddress(a.Address, &consensus.MainNetParams)
81 redeemContract := address.ScriptAddress()
84 switch address.(type) {
85 case *common.AddressWitnessPubKeyHash:
86 program, err = vmutil.P2WPKHProgram(redeemContract)
87 case *common.AddressWitnessScriptHash:
88 program, err = vmutil.P2WSHProgram(redeemContract)
90 return errors.New("unsupport address type")
96 out := legacy.NewTxOutput(*a.AssetId, a.Amount, program, a.ReferenceData)
97 return b.AddOutput(out)
100 // DecodeControlProgramAction convert input data to action struct
101 func DecodeControlProgramAction(data []byte) (Action, error) {
102 a := new(controlProgramAction)
103 err := stdjson.Unmarshal(data, a)
107 type controlProgramAction struct {
109 Program json.HexBytes `json:"control_program"`
110 ReferenceData json.Map `json:"reference_data"`
113 func (a *controlProgramAction) Build(ctx context.Context, b *TemplateBuilder) error {
115 if len(a.Program) == 0 {
116 missing = append(missing, "control_program")
118 if a.AssetId.IsZero() {
119 missing = append(missing, "asset_id")
121 if len(missing) > 0 {
122 return MissingFieldsError(missing...)
125 out := legacy.NewTxOutput(*a.AssetId, a.Amount, a.Program, a.ReferenceData)
126 return b.AddOutput(out)
129 // DecodeSetTxRefDataAction convert input data to action struct
130 func DecodeSetTxRefDataAction(data []byte) (Action, error) {
131 a := new(setTxRefDataAction)
132 err := stdjson.Unmarshal(data, a)
136 type setTxRefDataAction struct {
137 Data json.Map `json:"reference_data"`
140 func (a *setTxRefDataAction) Build(ctx context.Context, b *TemplateBuilder) error {
141 if len(a.Data) == 0 {
142 return MissingFieldsError("reference_data")
144 return b.setReferenceData(a.Data)
147 // DecodeRetireAction convert input data to action struct
148 func DecodeRetireAction(data []byte) (Action, error) {
149 a := new(retireAction)
150 err := stdjson.Unmarshal(data, a)
154 type retireAction struct {
156 ReferenceData json.Map `json:"reference_data"`
159 func (a *retireAction) Build(ctx context.Context, b *TemplateBuilder) error {
161 if a.AssetId.IsZero() {
162 missing = append(missing, "asset_id")
165 missing = append(missing, "amount")
167 if len(missing) > 0 {
168 return MissingFieldsError(missing...)
171 out := legacy.NewTxOutput(*a.AssetId, a.Amount, retirementProgram, a.ReferenceData)
172 return b.AddOutput(out)