OSDN Git Service

merge with dev
[bytom/bytom.git] / blockchain / txbuilder / actions.go
index c8997b7..996c01e 100644 (file)
@@ -3,15 +3,20 @@ package txbuilder
 import (
        "context"
        stdjson "encoding/json"
-
-       "github.com/blockchain/encoding/json"
-       "github.com/blockchain/protocol/bc"
-       "github.com/blockchain/protocol/bc/legacy"
-       "github.com/blockchain/protocol/vm"
+       "errors"
+
+       "github.com/bytom/common"
+       "github.com/bytom/consensus"
+       "github.com/bytom/encoding/json"
+       "github.com/bytom/protocol/bc"
+       "github.com/bytom/protocol/bc/types"
+       "github.com/bytom/protocol/vm"
+       "github.com/bytom/protocol/vm/vmutil"
 )
 
 var retirementProgram = []byte{byte(vm.OP_FAIL)}
 
+// DecodeControlReceiverAction convert input data to action struct
 func DecodeControlReceiverAction(data []byte) (Action, error) {
        a := new(controlReceiverAction)
        err := stdjson.Unmarshal(data, a)
@@ -20,8 +25,7 @@ func DecodeControlReceiverAction(data []byte) (Action, error) {
 
 type controlReceiverAction struct {
        bc.AssetAmount
-       Receiver      *Receiver `json:"receiver"`
-       ReferenceData json.Map  `json:"reference_data"`
+       Receiver *Receiver `json:"receiver"`
 }
 
 func (a *controlReceiverAction) Build(ctx context.Context, b *TemplateBuilder) error {
@@ -32,9 +36,6 @@ func (a *controlReceiverAction) Build(ctx context.Context, b *TemplateBuilder) e
                if len(a.Receiver.ControlProgram) == 0 {
                        missing = append(missing, "receiver.control_program")
                }
-               if a.Receiver.ExpiresAt.IsZero() {
-                       missing = append(missing, "receiver.expires_at")
-               }
        }
        if a.AssetId.IsZero() {
                missing = append(missing, "asset_id")
@@ -43,27 +44,26 @@ func (a *controlReceiverAction) Build(ctx context.Context, b *TemplateBuilder) e
                return MissingFieldsError(missing...)
        }
 
-       b.RestrictMaxTime(a.Receiver.ExpiresAt)
-       out := legacy.NewTxOutput(*a.AssetId, a.Amount, a.Receiver.ControlProgram, a.ReferenceData)
+       out := types.NewTxOutput(*a.AssetId, a.Amount, a.Receiver.ControlProgram)
        return b.AddOutput(out)
 }
 
-func DecodeControlProgramAction(data []byte) (Action, error) {
-       a := new(controlProgramAction)
+// 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 controlProgramAction struct {
+type controlAddressAction struct {
        bc.AssetAmount
-       Program       json.HexBytes `json:"control_program"`
-       ReferenceData json.Map      `json:"reference_data"`
+       Address string `json:"address"`
 }
 
-func (a *controlProgramAction) Build(ctx context.Context, b *TemplateBuilder) error {
+func (a *controlAddressAction) Build(ctx context.Context, b *TemplateBuilder) error {
        var missing []string
-       if len(a.Program) == 0 {
-               missing = append(missing, "control_program")
+       if a.Address == "" {
+               missing = append(missing, "address")
        }
        if a.AssetId.IsZero() {
                missing = append(missing, "asset_id")
@@ -72,27 +72,58 @@ func (a *controlProgramAction) Build(ctx context.Context, b *TemplateBuilder) er
                return MissingFieldsError(missing...)
        }
 
-       out := legacy.NewTxOutput(*a.AssetId, a.Amount, a.Program, a.ReferenceData)
+       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 DecodeSetTxRefDataAction(data []byte) (Action, error) {
-       a := new(setTxRefDataAction)
+// 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 setTxRefDataAction struct {
-       Data json.Map `json:"reference_data"`
+type controlProgramAction struct {
+       bc.AssetAmount
+       Program json.HexBytes `json:"control_program"`
 }
 
-func (a *setTxRefDataAction) Build(ctx context.Context, b *TemplateBuilder) error {
-       if len(a.Data) == 0 {
-               return MissingFieldsError("reference_data")
+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")
        }
-       return b.setReferenceData(a.Data)
+       if len(missing) > 0 {
+               return MissingFieldsError(missing...)
+       }
+
+       out := types.NewTxOutput(*a.AssetId, a.Amount, a.Program)
+       return b.AddOutput(out)
 }
 
+// DecodeRetireAction convert input data to action struct
 func DecodeRetireAction(data []byte) (Action, error) {
        a := new(retireAction)
        err := stdjson.Unmarshal(data, a)
@@ -101,7 +132,6 @@ func DecodeRetireAction(data []byte) (Action, error) {
 
 type retireAction struct {
        bc.AssetAmount
-       ReferenceData json.Map `json:"reference_data"`
 }
 
 func (a *retireAction) Build(ctx context.Context, b *TemplateBuilder) error {
@@ -116,6 +146,6 @@ func (a *retireAction) Build(ctx context.Context, b *TemplateBuilder) error {
                return MissingFieldsError(missing...)
        }
 
-       out := legacy.NewTxOutput(*a.AssetId, a.Amount, retirementProgram, a.ReferenceData)
+       out := types.NewTxOutput(*a.AssetId, a.Amount, retirementProgram)
        return b.AddOutput(out)
 }