OSDN Git Service

Add confirm tx
[bytom/vapor.git] / api / dpos_vote.go
index e2379e1..4374112 100644 (file)
@@ -6,10 +6,10 @@ import (
 
        log "github.com/sirupsen/logrus"
        "github.com/vapor/account"
+       "github.com/vapor/blockchain/pseudohsm"
        "github.com/vapor/blockchain/txbuilder"
        "github.com/vapor/config"
        "github.com/vapor/crypto/ed25519/chainkd"
-       "github.com/vapor/errors"
        "github.com/vapor/protocol/bc"
 )
 
@@ -17,7 +17,7 @@ func (a *API) dpos(ctx context.Context, ins struct {
        To     string `json:"to"`
        Fee    uint64 `json:"fee"`
        Stake  uint64 `json:"stake"`
-       TxType uint `json:"tx_type"`
+       TxType uint32 `json:"tx_type"`
 }) Response {
        // 找到utxo
        var assetID bc.AssetID
@@ -28,6 +28,7 @@ func (a *API) dpos(ctx context.Context, ins struct {
                From:     config.CommonConfig.Consensus.Dpos.Coinbase,
                To:       ins.To,
                Fee:      ins.Fee,
+               TxType:   ins.TxType,
        }
        dpos.Amount = ins.Stake
        dpos.AssetId = &assetID
@@ -43,7 +44,7 @@ func (a *API) dpos(ctx context.Context, ins struct {
        }
        var xprv chainkd.XPrv
        xprv.UnmarshalText([]byte(config.CommonConfig.Consensus.Dpos.XPrv))
-       if err := signWithKey(tmpl, xprv); err != nil {
+       if err := pseudohsm.SignWithKey(tmpl, xprv); err != nil {
                return NewErrorResponse(err)
        }
        log.Info("Sign Transaction complete.")
@@ -57,55 +58,4 @@ func (a *API) dpos(ctx context.Context, ins struct {
 
        log.WithField("tx_id", tmpl.Transaction.ID.String()).Info("submit single tx")
        return NewSuccessResponse(&submitTxResp{TxID: &tmpl.Transaction.ID})
-
-       //return NewSuccessResponse(nil)
-}
-
-func signWithKey(tmpl *txbuilder.Template, xprv chainkd.XPrv) error {
-       for i, sigInst := range tmpl.SigningInstructions {
-               for j, wc := range sigInst.WitnessComponents {
-                       switch sw := wc.(type) {
-                       case *txbuilder.SignatureWitness:
-                               err := sw.Sign(tmpl, uint32(i), xprv)
-                               if err != nil {
-                                       return errors.WithDetailf(err, "adding signature(s) to signature witness component %d of input %d", j, i)
-                               }
-                       case *txbuilder.RawTxSigWitness:
-                               err := sw.Sign(tmpl, uint32(i), xprv)
-                               if err != nil {
-                                       return errors.WithDetailf(err, "adding signature(s) to raw-signature witness component %d of input %d", j, i)
-                               }
-                       }
-               }
-       }
-       return materializeWitnessesWithKey(tmpl)
-}
-
-func materializeWitnessesWithKey(txTemplate *txbuilder.Template) error {
-       msg := txTemplate.Transaction
-
-       if msg == nil {
-               return errors.Wrap(txbuilder.ErrMissingRawTx)
-       }
-
-       if len(txTemplate.SigningInstructions) > len(msg.Inputs) {
-               return errors.Wrap(txbuilder.ErrBadInstructionCount)
-       }
-
-       for i, sigInst := range txTemplate.SigningInstructions {
-               if msg.Inputs[sigInst.Position] == nil {
-                       return errors.WithDetailf(txbuilder.ErrBadTxInputIdx, "signing instruction %d references missing tx input %d", i, sigInst.Position)
-               }
-
-               var witness [][]byte
-               for j, wc := range sigInst.WitnessComponents {
-                       err := wc.Materialize(&witness)
-                       if err != nil {
-                               return errors.WithDetailf(err, "error in witness component %d of input %d", j, i)
-                       }
-               }
-               msg.SetInputArguments(sigInst.Position, witness)
-       }
-
-       return nil
 }