5 stdjson "encoding/json"
8 "github.com/vapor/common"
9 "github.com/vapor/consensus"
10 "github.com/vapor/encoding/json"
11 "github.com/vapor/protocol/bc"
12 "github.com/vapor/protocol/bc/types"
13 "github.com/vapor/protocol/vm/vmutil"
16 // DecodeControlAddressAction convert input data to action struct
17 func DecodeControlAddressAction(data []byte) (Action, error) {
18 a := new(controlAddressAction)
19 err := stdjson.Unmarshal(data, a)
23 type controlAddressAction struct {
25 Address string `json:"address"`
28 func (a *controlAddressAction) Build(ctx context.Context, b *TemplateBuilder) error {
31 missing = append(missing, "address")
33 if a.AssetId.IsZero() {
34 missing = append(missing, "asset_id")
37 missing = append(missing, "amount")
40 return MissingFieldsError(missing...)
43 address, err := common.DecodeAddress(a.Address, &consensus.ActiveNetParams)
47 redeemContract := address.ScriptAddress()
50 switch address.(type) {
51 case *common.AddressWitnessPubKeyHash:
52 program, err = vmutil.P2WPKHProgram(redeemContract)
53 case *common.AddressWitnessScriptHash:
54 program, err = vmutil.P2WSHProgram(redeemContract)
56 return errors.New("unsupport address type")
62 out := types.NewTxOutput(*a.AssetId, a.Amount, program)
63 return b.AddOutput(out)
66 func (a *controlAddressAction) ActionType() string {
67 return "control_address"
70 // DecodeControlProgramAction convert input data to action struct
71 func DecodeControlProgramAction(data []byte) (Action, error) {
72 a := new(controlProgramAction)
73 err := stdjson.Unmarshal(data, a)
77 type controlProgramAction struct {
79 Program json.HexBytes `json:"control_program"`
82 func (a *controlProgramAction) Build(ctx context.Context, b *TemplateBuilder) error {
84 if len(a.Program) == 0 {
85 missing = append(missing, "control_program")
87 if a.AssetId.IsZero() {
88 missing = append(missing, "asset_id")
91 missing = append(missing, "amount")
94 return MissingFieldsError(missing...)
97 out := types.NewTxOutput(*a.AssetId, a.Amount, a.Program)
98 return b.AddOutput(out)
101 func (a *controlProgramAction) ActionType() string {
102 return "control_program"
105 // DecodeRetireAction convert input data to action struct
106 func DecodeRetireAction(data []byte) (Action, error) {
107 a := new(retireAction)
108 err := stdjson.Unmarshal(data, a)
112 type retireAction struct {
114 Arbitrary json.HexBytes `json:"arbitrary"`
117 func (a *retireAction) Build(ctx context.Context, b *TemplateBuilder) error {
119 if a.AssetId.IsZero() {
120 missing = append(missing, "asset_id")
123 missing = append(missing, "amount")
125 if len(missing) > 0 {
126 return MissingFieldsError(missing...)
129 program, err := vmutil.RetireProgram(a.Arbitrary)
133 out := types.NewTxOutput(*a.AssetId, a.Amount, program)
134 return b.AddOutput(out)
137 func (a *retireAction) ActionType() string {