OSDN Git Service

wip: init DecodeCrossOutAction
authorHAOYUatHZ <haoyu@protonmail.com>
Tue, 21 May 2019 01:55:25 +0000 (09:55 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Tue, 21 May 2019 01:55:25 +0000 (09:55 +0800)
api/transact.go
blockchain/txbuilder/actions.go

index 4a6c17c..5c5c74c 100644 (file)
@@ -25,8 +25,9 @@ func (a *API) actionDecoder(action string) (func([]byte) (txbuilder.Action, erro
        decoders := map[string]func([]byte) (txbuilder.Action, error){
                "control_address":              txbuilder.DecodeControlAddressAction,
                "control_program":              txbuilder.DecodeControlProgramAction,
-               "retire":                       txbuilder.DecodeRetireAction,
+               "cross_chain_in":               txbuilder.DecodeCrossInAction,
                "cross_chain_out":              txbuilder.DecodeCrossOutAction,
+               "retire":                       txbuilder.DecodeRetireAction,
                "spend_account":                a.wallet.AccountMgr.DecodeSpendAction,
                "spend_account_unspent_output": a.wallet.AccountMgr.DecodeSpendUTXOAction,
        }
index 8da65a7..6798779 100644 (file)
@@ -102,20 +102,23 @@ func (a *controlProgramAction) ActionType() string {
        return "control_program"
 }
 
-// DecodeRetireAction convert input data to action struct
-func DecodeRetireAction(data []byte) (Action, error) {
-       a := new(retireAction)
+// DecodeCrossInAction convert input data to action struct
+func DecodeCrossInAction(data []byte) (Action, error) {
+       a := new(crossInAction)
        err := stdjson.Unmarshal(data, a)
        return a, err
 }
 
-type retireAction struct {
+type crossInAction struct {
        bc.AssetAmount
-       Arbitrary json.HexBytes `json:"arbitrary"`
+       Address string `json:"address"`
 }
 
-func (a *retireAction) Build(ctx context.Context, b *TemplateBuilder) error {
+func (a *crossInAction) 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")
        }
@@ -126,16 +129,31 @@ func (a *retireAction) Build(ctx context.Context, b *TemplateBuilder) error {
                return MissingFieldsError(missing...)
        }
 
-       program, err := vmutil.RetireProgram(a.Arbitrary)
+       address, err := common.DecodeAddress(a.Address, &consensus.MainNetParams)
        if err != nil {
                return err
        }
-       out := types.NewIntraChainOutput(*a.AssetId, a.Amount, program)
+
+       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.NewCrossChainOutput(*a.AssetId, a.Amount, program)
        return b.AddOutput(out)
 }
 
-func (a *retireAction) ActionType() string {
-       return "retire"
+func (a *crossInAction) ActionType() string {
+       return "cross_chain_in"
 }
 
 // DecodeCrossOutAction convert input data to action struct
@@ -191,3 +209,39 @@ func (a *crossOutAction) Build(ctx context.Context, b *TemplateBuilder) error {
 func (a *crossOutAction) ActionType() string {
        return "cross_chain_out"
 }
+
+// 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.NewIntraChainOutput(*a.AssetId, a.Amount, program)
+       return b.AddOutput(out)
+}
+
+func (a *retireAction) ActionType() string {
+       return "retire"
+}