From d2ec2911b1f5642c1b74b450b6e69e69a4a80fa1 Mon Sep 17 00:00:00 2001 From: wz Date: Tue, 21 May 2019 15:17:38 +0800 Subject: [PATCH] add votetx output action (#79) --- api/transact.go | 1 + blockchain/txbuilder/actions.go | 58 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/api/transact.go b/api/transact.go index 4a6c17c2..be9714d2 100644 --- a/api/transact.go +++ b/api/transact.go @@ -27,6 +27,7 @@ func (a *API) actionDecoder(action string) (func([]byte) (txbuilder.Action, erro "control_program": txbuilder.DecodeControlProgramAction, "retire": txbuilder.DecodeRetireAction, "cross_chain_out": txbuilder.DecodeCrossOutAction, + "vote_output": txbuilder.DecodeVoteOutputAction, "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 8da65a79..d5baf78b 100644 --- a/blockchain/txbuilder/actions.go +++ b/blockchain/txbuilder/actions.go @@ -191,3 +191,61 @@ func (a *crossOutAction) Build(ctx context.Context, b *TemplateBuilder) error { func (a *crossOutAction) ActionType() string { return "cross_chain_out" } + +// DecodeVoteOutputAction convert input data to action struct +func DecodeVoteOutputAction(data []byte) (Action, error) { + a := new(voteOutputAction) + err := stdjson.Unmarshal(data, a) + return a, err +} + +type voteOutputAction struct { + bc.AssetAmount + Address string `json:"address"` + Vote json.HexBytes `json:"vote"` +} + +func (a *voteOutputAction) 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(a.Vote) == 0 { + missing = append(missing, "vote") + } + if len(missing) > 0 { + return MissingFieldsError(missing...) + } + + 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.NewVoteOutput(*a.AssetId, a.Amount, program, a.Vote) + return b.AddOutput(out) +} + +func (a *voteOutputAction) ActionType() string { + return "vote_output" +} -- 2.11.0