OSDN Git Service

refactor: move DecodeCrossInAction to AccountMgr
authorHAOYUatHZ <haoyu@protonmail.com>
Tue, 21 May 2019 02:36:44 +0000 (10:36 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Tue, 21 May 2019 02:36:44 +0000 (10:36 +0800)
account/builder.go
api/transact.go
blockchain/txbuilder/actions.go

index d816056..6126da3 100644 (file)
@@ -22,6 +22,62 @@ var (
        chainTxMergeGas = uint64(10000000)
 )
 
+// DecodeCrossInAction convert input data to action struct
+func (m *Manager) DecodeCrossInAction(data []byte) (Action, error) {
+       a := new(crossInAction)
+       err := stdjson.Unmarshal(data, a)
+       return a, err
+}
+
+type crossInAction struct {
+       bc.AssetAmount
+       // Address string `json:"address"`
+}
+
+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")
+       }
+       if a.Amount == 0 {
+               missing = append(missing, "amount")
+       }
+       if len(missing) > 0 {
+               return MissingFieldsError(missing...)
+       }
+
+       // address, err := common.DecodeAddress(a.Address, &consensus.MainNetParams)
+       // 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.NewCrossChainOutput(*a.AssetId, a.Amount, program)
+       // in :=  types.NewCrossChainInput(arguments [][]byte, sourceID bc.Hash, assetID bc.AssetID, amount, sourcePos uint64, controlProgram, assetDefinition []byte)
+       in := types.NewCrossChainInput(nil, bc.Hash{}, bc.AssetID{}, 0, 0, nil, nil)
+       return b.AddInput(in)
+}
+
+func (a *crossInAction) ActionType() string {
+       return "cross_chain_in"
+}
+
 //DecodeSpendAction unmarshal JSON-encoded data of spend action
 func (m *Manager) DecodeSpendAction(data []byte) (txbuilder.Action, error) {
        a := &spendAction{accounts: m}
index 5c5c74c..d12dcdd 100644 (file)
@@ -25,9 +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,
-               "cross_chain_in":               txbuilder.DecodeCrossInAction,
                "cross_chain_out":              txbuilder.DecodeCrossOutAction,
                "retire":                       txbuilder.DecodeRetireAction,
+               "cross_chain_in":               a.wallet.AccountMgr.DecodeCrossInAction,
                "spend_account":                a.wallet.AccountMgr.DecodeSpendAction,
                "spend_account_unspent_output": a.wallet.AccountMgr.DecodeSpendUTXOAction,
        }
index 6798779..fb8a971 100644 (file)
@@ -102,60 +102,6 @@ func (a *controlProgramAction) ActionType() string {
        return "control_program"
 }
 
-// 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 crossInAction struct {
-       bc.AssetAmount
-       Address string `json:"address"`
-}
-
-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")
-       }
-       if a.Amount == 0 {
-               missing = append(missing, "amount")
-       }
-       if len(missing) > 0 {
-               return MissingFieldsError(missing...)
-       }
-
-       address, err := common.DecodeAddress(a.Address, &consensus.MainNetParams)
-       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.NewCrossChainOutput(*a.AssetId, a.Amount, program)
-       return b.AddOutput(out)
-}
-
-func (a *crossInAction) ActionType() string {
-       return "cross_chain_in"
-}
-
 // DecodeCrossOutAction convert input data to action struct
 func DecodeCrossOutAction(data []byte) (Action, error) {
        a := new(crossOutAction)