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"`
31 func (a *controlReceiverAction) Build(ctx context.Context, b *TemplateBuilder) error {
33 if a.Receiver == nil {
34 missing = append(missing, "receiver")
36 if len(a.Receiver.ControlProgram) == 0 {
37 missing = append(missing, "receiver.control_program")
40 if a.AssetId.IsZero() {
41 missing = append(missing, "asset_id")
44 return MissingFieldsError(missing...)
47 out := legacy.NewTxOutput(*a.AssetId, a.Amount, a.Receiver.ControlProgram)
48 return b.AddOutput(out)
51 // DecodeControlAddressAction convert input data to action struct
52 func DecodeControlAddressAction(data []byte) (Action, error) {
53 a := new(controlAddressAction)
54 err := stdjson.Unmarshal(data, a)
58 type controlAddressAction struct {
60 Address string `json:"address"`
63 func (a *controlAddressAction) Build(ctx context.Context, b *TemplateBuilder) error {
66 missing = append(missing, "address")
68 if a.AssetId.IsZero() {
69 missing = append(missing, "asset_id")
72 return MissingFieldsError(missing...)
75 address, err := common.DecodeAddress(a.Address, &consensus.MainNetParams)
79 redeemContract := address.ScriptAddress()
82 switch address.(type) {
83 case *common.AddressWitnessPubKeyHash:
84 program, err = vmutil.P2WPKHProgram(redeemContract)
85 case *common.AddressWitnessScriptHash:
86 program, err = vmutil.P2WSHProgram(redeemContract)
88 return errors.New("unsupport address type")
94 out := legacy.NewTxOutput(*a.AssetId, a.Amount, program)
95 return b.AddOutput(out)
98 // DecodeControlProgramAction convert input data to action struct
99 func DecodeControlProgramAction(data []byte) (Action, error) {
100 a := new(controlProgramAction)
101 err := stdjson.Unmarshal(data, a)
105 type controlProgramAction struct {
107 Program json.HexBytes `json:"control_program"`
110 func (a *controlProgramAction) Build(ctx context.Context, b *TemplateBuilder) error {
112 if len(a.Program) == 0 {
113 missing = append(missing, "control_program")
115 if a.AssetId.IsZero() {
116 missing = append(missing, "asset_id")
118 if len(missing) > 0 {
119 return MissingFieldsError(missing...)
122 out := legacy.NewTxOutput(*a.AssetId, a.Amount, a.Program)
123 return b.AddOutput(out)
126 // DecodeRetireAction convert input data to action struct
127 func DecodeRetireAction(data []byte) (Action, error) {
128 a := new(retireAction)
129 err := stdjson.Unmarshal(data, a)
133 type retireAction struct {
137 func (a *retireAction) Build(ctx context.Context, b *TemplateBuilder) error {
139 if a.AssetId.IsZero() {
140 missing = append(missing, "asset_id")
143 missing = append(missing, "amount")
145 if len(missing) > 0 {
146 return MissingFieldsError(missing...)
149 out := legacy.NewTxOutput(*a.AssetId, a.Amount, retirementProgram)
150 return b.AddOutput(out)