To string `json:"to"`
Fee uint64 `json:"fee"`
UseUnconfirmed bool `json:"use_unconfirmed"`
+ TxType uint32 `json:"tx_type"`
+ Height uint64 `json:"height"`
}
func (a *DopsAction) Build(ctx context.Context, b *txbuilder.TemplateBuilder) error {
missing = append(missing, "to")
}
+ if types.TxType(a.TxType) < types.Binary || types.TxType(a.TxType) > types.ConfirmTx {
+ return errors.New("tx type of dpos is error")
+ }
+
+ txType := types.TxType(a.TxType)
+
if len(missing) > 0 {
return txbuilder.MissingFieldsError(missing...)
}
// Cancel the reservation if the build gets rolled back.
b.OnRollback(func() { a.Accounts.utxoKeeper.Cancel(res.id) })
for _, r := range res.utxos {
- txInput, sigInst, err := DposTx(a.From, a.To, a.Amount, r)
+ txInput, sigInst, err := DposTx(a.From, a.To, a.Amount, r, txType, a.Height)
if err != nil {
return errors.Wrap(err, "creating inputs")
}
}
// DposInputs convert an utxo to the txinput
-func DposTx(from, to string, stake uint64, u *UTXO) (*types.TxInput, *txbuilder.SigningInstruction, error) {
- txInput := types.NewDpos(nil, from, to, u.SourceID, u.AssetID, stake, u.Amount, u.SourcePos, u.ControlProgram, types.Delegate)
+func DposTx(from, to string, stake uint64, u *UTXO, txType types.TxType, h uint64) (*types.TxInput, *txbuilder.SigningInstruction, error) {
+ txInput := types.NewDpos(nil, from, to, u.SourceID, u.AssetID, stake, u.Amount, u.SourcePos, u.ControlProgram, txType, h)
sigInst := &txbuilder.SigningInstruction{}
var xpubs []chainkd.XPub
var xprv chainkd.XPrv
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"
)
To string `json:"to"`
Fee uint64 `json:"fee"`
Stake uint64 `json:"stake"`
- TxType uint8 `json:"tx_type"`
+ TxType uint32 `json:"tx_type"`
}) Response {
// 找到utxo
var assetID bc.AssetID
From: config.CommonConfig.Consensus.Dpos.Coinbase,
To: ins.To,
Fee: ins.Fee,
+ TxType: ins.TxType,
}
dpos.Amount = ins.Stake
dpos.AssetId = &assetID
}
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.")
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
}
--- /dev/null
+package pseudohsm
+
+import (
+ "github.com/vapor/blockchain/txbuilder"
+ "github.com/vapor/crypto/ed25519/chainkd"
+ "github.com/vapor/errors"
+)
+
+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
+}
"sync"
"time"
+ "github.com/vapor/blockchain/pseudohsm"
+
+ "github.com/vapor/blockchain/txbuilder"
"github.com/vapor/config"
log "github.com/sirupsen/logrus"
}
func (m *Miner) sendConfirmTx(height uint64) error {
+ // 找到utxo
+ var assetID bc.AssetID
+ assetID.UnmarshalText([]byte("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"))
+ // 生成dpos交易
+ dpos := account.DopsAction{
+ Accounts: m.accountManager,
+ From: config.CommonConfig.Consensus.Dpos.Coinbase,
+ Fee: 100000000,
+ TxType: 6,
+ Height: height,
+ }
+ dpos.AssetId = &assetID
+ builder := txbuilder.NewBuilder(time.Now())
+ if err := dpos.Build(nil, builder); err != nil {
+ return err
+ }
+ // 签名
+ tmpl, _, err := builder.Build()
+ if err != nil {
+ return err
+ }
+
+ var xprv chainkd.XPrv
+ xprv.UnmarshalText([]byte(config.CommonConfig.Consensus.Dpos.XPrv))
+ if err := pseudohsm.SignWithKey(tmpl, xprv); err != nil {
+ return err
+ }
+
+ if err := txbuilder.FinalizeTx(nil, m.chain, tmpl.Transaction); err != nil {
+ return err
+ }
+
return nil
}
package types
import (
- "github.com/vapor/protocol/bc"
-)
+ "fmt"
-/*
-const (
- LoginCandidate DposType = iota
- LogoutCandidate
- Delegate
- UnDelegate
+ "github.com/vapor/protocol/bc"
)
-*/
type DposTx struct {
SpendCommitmentSuffix []byte
To string
Amount uint64
Stake uint64
- PaymentAmount uint64
Arguments [][]byte
Info string
SpendCommitment
}
-func NewDpos(arguments [][]byte, from, to string, sourceID bc.Hash, assetID bc.AssetID, stake, amount, sourcePos uint64, controlProgram []byte, t TxType) *TxInput {
+func NewDpos(arguments [][]byte, from, to string, sourceID bc.Hash, assetID bc.AssetID, stake, amount, sourcePos uint64, controlProgram []byte, t TxType, height uint64) *TxInput {
var vote string
switch t {
case LoginCandidate:
case Delegate:
vote = "vapor:1:event:vote"
case UnDelegate:
+ case ConfirmTx:
+ vote = fmt.Sprintf("vapor:1:event:confirm:%d", height)
}
sc := SpendCommitment{
AssetAmount: bc.AssetAmount{
LogoutCandidate
Delegate
UnDelegate
+ ConfirmTx
)
// SpendInput satisfies the TypedInput interface and represents a spend transaction.
if inp.Stake, err = blockchain.ReadVarint63(r); err != nil {
return err
}
- if inp.PaymentAmount, err = blockchain.ReadVarint63(r); err != nil {
- return err
- }
if inp.Arguments, err = blockchain.ReadVarstrList(r); err != nil {
return err
}
if _, err := blockchain.WriteVarint63(w, inp.Stake); err != nil {
return err
}
- if _, err := blockchain.WriteVarint63(w, inp.PaymentAmount); err != nil {
- return err
- }
if _, err := blockchain.WriteVarstrList(w, inp.Arguments); err != nil {
return err
}