From: HAOYUatHZ Date: Tue, 21 May 2019 02:36:44 +0000 (+0800) Subject: refactor: move DecodeCrossInAction to AccountMgr X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=a4b83d98235f6fc8c05a3a773d7d8fd5cb724708;p=bytom%2Fvapor.git refactor: move DecodeCrossInAction to AccountMgr --- diff --git a/account/builder.go b/account/builder.go index d8160568..6126da34 100644 --- a/account/builder.go +++ b/account/builder.go @@ -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} diff --git a/api/transact.go b/api/transact.go index 5c5c74c1..d12dcdd1 100644 --- a/api/transact.go +++ b/api/transact.go @@ -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, } diff --git a/blockchain/txbuilder/actions.go b/blockchain/txbuilder/actions.go index 67987795..fb8a9718 100644 --- a/blockchain/txbuilder/actions.go +++ b/blockchain/txbuilder/actions.go @@ -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)