OSDN Git Service

add votetx output action (#79)
authorwz <mars@bytom.io>
Tue, 21 May 2019 07:17:38 +0000 (15:17 +0800)
committerPaladz <yzhu101@uottawa.ca>
Tue, 21 May 2019 07:17:38 +0000 (15:17 +0800)
api/transact.go
blockchain/txbuilder/actions.go

index 4a6c17c..be9714d 100644 (file)
@@ -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,
        }
index 8da65a7..d5baf78 100644 (file)
@@ -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"
+}