OSDN Git Service

feat(version): update version to 1.1.0
[bytom/bytom.git] / test / tx_test_util.go
index 4444a9d..cb6b9ee 100644 (file)
@@ -6,22 +6,21 @@ import (
        "fmt"
        "time"
 
-       "github.com/tendermint/tmlibs/db"
-
-       "github.com/bytom/account"
-       "github.com/bytom/asset"
-       "github.com/bytom/blockchain/pseudohsm"
-       "github.com/bytom/blockchain/signers"
-       "github.com/bytom/blockchain/txbuilder"
-       "github.com/bytom/common"
-       "github.com/bytom/consensus"
-       "github.com/bytom/crypto/ed25519/chainkd"
-       "github.com/bytom/crypto/sha3pool"
-       "github.com/bytom/errors"
-       "github.com/bytom/protocol/bc"
-       "github.com/bytom/protocol/bc/types"
-       "github.com/bytom/protocol/vm"
-       "github.com/bytom/protocol/vm/vmutil"
+       "github.com/bytom/bytom/account"
+       "github.com/bytom/bytom/asset"
+       "github.com/bytom/bytom/blockchain/pseudohsm"
+       "github.com/bytom/bytom/blockchain/signers"
+       "github.com/bytom/bytom/blockchain/txbuilder"
+       "github.com/bytom/bytom/common"
+       "github.com/bytom/bytom/consensus"
+       "github.com/bytom/bytom/crypto/ed25519/chainkd"
+       "github.com/bytom/bytom/crypto/sha3pool"
+       dbm "github.com/bytom/bytom/database/leveldb"
+       "github.com/bytom/bytom/errors"
+       "github.com/bytom/bytom/protocol/bc"
+       "github.com/bytom/bytom/protocol/bc/types"
+       "github.com/bytom/bytom/protocol/vm"
+       "github.com/bytom/bytom/protocol/vm/vmutil"
 )
 
 // TxGenerator used to generate new tx
@@ -48,7 +47,7 @@ func (g *TxGenerator) Reset() {
 }
 
 func (g *TxGenerator) createKey(alias string, auth string) error {
-       _, err := g.Hsm.XCreate(alias, auth)
+       _, _, err := g.Hsm.XCreate(alias, auth, "en")
        return err
 }
 
@@ -71,16 +70,16 @@ func (g *TxGenerator) createAccount(name string, keys []string, quorum int) erro
                }
                xpubs = append(xpubs, *xpub)
        }
-       _, err := g.AccountManager.Create(nil, xpubs, quorum, name, nil)
+       _, err := g.AccountManager.Create(xpubs, quorum, name, signers.BIP0044)
        return err
 }
 
 func (g *TxGenerator) createAsset(accountAlias string, assetAlias string) (*asset.Asset, error) {
-       acc, err := g.AccountManager.FindByAlias(nil, accountAlias)
+       acc, err := g.AccountManager.FindByAlias(accountAlias)
        if err != nil {
                return nil, err
        }
-       return g.Assets.Define(acc.XPubs, len(acc.XPubs), nil, assetAlias, nil)
+       return g.Assets.Define(acc.XPubs, len(acc.XPubs), nil, 0, assetAlias, nil)
 }
 
 func (g *TxGenerator) mockUtxo(accountAlias, assetAlias string, amount uint64) (*account.UTXO, error) {
@@ -104,6 +103,7 @@ func (g *TxGenerator) mockUtxo(accountAlias, assetAlias string, amount uint64) (
                AccountID:           ctrlProg.AccountID,
                Address:             ctrlProg.Address,
                ValidHeight:         0,
+               Change:              ctrlProg.Change,
        }
        return utxo, nil
 }
@@ -117,7 +117,7 @@ func (g *TxGenerator) assetAmount(assetAlias string, amount uint64) (*bc.AssetAm
                return a, nil
        }
 
-       asset, err := g.Assets.FindByAlias(nil, assetAlias)
+       asset, err := g.Assets.FindByAlias(assetAlias)
        if err != nil {
                return nil, err
        }
@@ -128,11 +128,11 @@ func (g *TxGenerator) assetAmount(assetAlias string, amount uint64) (*bc.AssetAm
 }
 
 func (g *TxGenerator) createControlProgram(accountAlias string, change bool) (*account.CtrlProgram, error) {
-       acc, err := g.AccountManager.FindByAlias(nil, accountAlias)
+       acc, err := g.AccountManager.FindByAlias(accountAlias)
        if err != nil {
                return nil, err
        }
-       return g.AccountManager.CreateAddress(nil, acc.ID, change)
+       return g.AccountManager.CreateAddress(acc.ID, change)
 }
 
 // AddSpendInput add a spend input
@@ -142,7 +142,7 @@ func (g *TxGenerator) AddSpendInput(accountAlias, assetAlias string, amount uint
                return err
        }
 
-       acc, err := g.AccountManager.FindByAlias(nil, accountAlias)
+       acc, err := g.AccountManager.FindByAlias(accountAlias)
        if err != nil {
                return err
        }
@@ -170,7 +170,7 @@ func (g *TxGenerator) AddTxInput(txInput *types.TxInput, signInstruction *txbuil
 
 // AddTxInputFromUtxo add a tx input which spent the utxo
 func (g *TxGenerator) AddTxInputFromUtxo(utxo *account.UTXO, accountAlias string) error {
-       acc, err := g.AccountManager.FindByAlias(nil, accountAlias)
+       acc, err := g.AccountManager.FindByAlias(accountAlias)
        if err != nil {
                return err
        }
@@ -184,7 +184,7 @@ func (g *TxGenerator) AddTxInputFromUtxo(utxo *account.UTXO, accountAlias string
 
 // AddIssuanceInput add a issue input
 func (g *TxGenerator) AddIssuanceInput(assetAlias string, amount uint64) error {
-       asset, err := g.Assets.FindByAlias(nil, assetAlias)
+       asset, err := g.Assets.FindByAlias(assetAlias)
        if err != nil {
                return err
        }
@@ -196,7 +196,7 @@ func (g *TxGenerator) AddIssuanceInput(assetAlias string, amount uint64) error {
        }
        issuanceInput := types.NewIssuanceInput(nonce[:], amount, asset.IssuanceProgram, nil, asset.RawDefinitionByte)
        signInstruction := &txbuilder.SigningInstruction{}
-       path := signers.Path(asset.Signer, signers.AssetKeySpace)
+       path := signers.GetBip0032Path(asset.Signer, signers.AssetKeySpace)
        signInstruction.AddRawWitnessKeys(asset.Signer.XPubs, path, asset.Signer.Quorum)
        g.Builder.RestrictMinTime(time.Now())
        return g.Builder.AddInput(issuanceInput, signInstruction)
@@ -292,11 +292,11 @@ func CreateSpendInput(tx *types.Tx, outputIndex uint64) (*types.SpendInput, erro
 }
 
 // SignInstructionFor read CtrlProgram from db, construct SignInstruction for SpendInput
-func SignInstructionFor(input *types.SpendInput, db db.DB, signer *signers.Signer) (*txbuilder.SigningInstruction, error) {
+func SignInstructionFor(input *types.SpendInput, db dbm.DB, signer *signers.Signer) (*txbuilder.SigningInstruction, error) {
        cp := account.CtrlProgram{}
        var hash [32]byte
        sha3pool.Sum256(hash[:], input.ControlProgram)
-       bytes := db.Get(account.CPKey(hash))
+       bytes := db.Get(account.ContractKey(hash))
        if bytes == nil {
                return nil, fmt.Errorf("can't find CtrlProgram for the SpendInput")
        }
@@ -312,7 +312,11 @@ func SignInstructionFor(input *types.SpendInput, db db.DB, signer *signers.Signe
        }
 
        // FIXME: code duplicate with account/builder.go
-       path := signers.Path(signer, signers.AccountKeySpace, cp.KeyIndex)
+       path, err := signers.Path(signer, signers.AccountKeySpace, cp.Change, cp.KeyIndex)
+       if err != nil {
+               return nil, err
+       }
+
        if cp.Address == "" {
                sigInst.AddWitnessKeys(signer.XPubs, path, signer.Quorum)
                return sigInst, nil
@@ -332,7 +336,10 @@ func SignInstructionFor(input *types.SpendInput, db db.DB, signer *signers.Signe
 
        case *common.AddressWitnessScriptHash:
                sigInst.AddRawWitnessKeys(signer.XPubs, path, signer.Quorum)
-               path := signers.Path(signer, signers.AccountKeySpace, cp.KeyIndex)
+               path, err := signers.Path(signer, signers.AccountKeySpace, cp.Change, cp.KeyIndex)
+               if err != nil {
+                       return nil, err
+               }
                derivedXPubs := chainkd.DeriveXPubs(signer.XPubs, path)
                derivedPKs := chainkd.XPubKeys(derivedXPubs)
                script, err := vmutil.P2SPMultiSigProgram(derivedPKs, signer.Quorum)
@@ -387,8 +394,12 @@ func CreateTxFromTx(baseTx *types.Tx, outputIndex uint64, outputAmount uint64, c
        }
        output := types.NewTxOutput(*consensus.BTMAssetID, outputAmount, ctrlProgram)
        builder := txbuilder.NewBuilder(time.Now())
-       builder.AddInput(txInput, &txbuilder.SigningInstruction{})
-       builder.AddOutput(output)
+       if err := builder.AddInput(txInput, &txbuilder.SigningInstruction{}); err != nil {
+               return nil, err
+       }
+       if err := builder.AddOutput(output); err != nil {
+               return nil, err
+       }
 
        tpl, _, err := builder.Build()
        if err != nil {