OSDN Git Service

Password array (#316)
authoricodezjb <icodezjb@163.com>
Tue, 23 Jan 2018 08:37:17 +0000 (16:37 +0800)
committerPaladz <yzhu101@uottawa.ca>
Tue, 23 Jan 2018 08:37:17 +0000 (16:37 +0800)
* Fix Key not Found Problem

Remove XPrv cache
Change password string to array

* use password array

* code clean

blockchain/accesstokens.go
blockchain/blockchain_reactor.go
blockchain/transact.go
blockchain/txfeeds.go
cmd/bytomcli/commands/key.go
cmd/bytomcli/commands/transaction.go
config/genesis.go

index 153e559..4752a4e 100644 (file)
@@ -4,12 +4,8 @@ import (
        "context"
 
        log "github.com/sirupsen/logrus"
-
-       "github.com/bytom/errors"
 )
 
-var errCurrentToken = errors.New("token cannot delete itself")
-
 func (bcr *BlockchainReactor) createAccessToken(ctx context.Context, x struct {
        ID   string `json:"id"`
        Type string `json:"type"`
@@ -37,9 +33,9 @@ func (bcr *BlockchainReactor) deleteAccessToken(ctx context.Context, x struct {
 }) Response {
        //TODO Add delete permission verify.
        if err := bcr.accessTokens.Delete(ctx, x.ID); err != nil {
-               return resWrapper(nil, err)
+               return NewErrorResponse(err)
        }
-       return resWrapper(nil)
+       return NewSuccessResponse(nil)
 }
 
 func (bcr *BlockchainReactor) checkAccessToken(ctx context.Context, x struct {
index 33810ff..0ba3a88 100644 (file)
@@ -181,17 +181,3 @@ func (bcr *BlockchainReactor) gasRate() Response {
        gasrate := map[string]int64{"gasRate": validation.GasRate}
        return NewSuccessResponse(gasrate)
 }
-
-// wrapper json for response
-// Deprecated: We should use Response factor to create response
-func resWrapper(data interface{}, errWrapper ...error) Response {
-       var response Response
-
-       if errWrapper != nil {
-               response = Response{Status: FAIL, Msg: errWrapper[0].Error()}
-       } else {
-               response = Response{Status: SUCCESS, Data: data}
-       }
-
-       return response
-}
index 02009cb..831cd25 100644 (file)
@@ -135,10 +135,10 @@ func (bcr *BlockchainReactor) build(ctx context.Context, buildReqs *BuildRequest
 
        tmpl, err := bcr.buildSingle(subctx, buildReqs)
        if err != nil {
-               return resWrapper(nil, err)
+               return NewErrorResponse(err)
        }
 
-       return resWrapper(tmpl)
+       return NewSuccessResponse(tmpl)
 }
 
 func (bcr *BlockchainReactor) submitSingle(ctx context.Context, tpl *txbuilder.Template) (map[string]string, error) {
@@ -236,8 +236,8 @@ func (bcr *BlockchainReactor) submit(ctx context.Context, tpl *txbuilder.Templat
 
 // POST /sign-submit-transaction
 func (bcr *BlockchainReactor) signSubmit(ctx context.Context, x struct {
-       Password []string             `json:"password"`
-       Txs  txbuilder.Template `json:"transaction"`
+       Password []string           `json:"password"`
+       Txs      txbuilder.Template `json:"transaction"`
 }) Response {
 
        var err error
index 011150c..b081f37 100644 (file)
@@ -6,7 +6,6 @@ import (
 
        log "github.com/sirupsen/logrus"
 
-       "github.com/bytom/blockchain/query"
        "github.com/bytom/blockchain/txfeed"
        "github.com/bytom/errors"
 )
@@ -41,16 +40,16 @@ func (bcr *BlockchainReactor) getTxFeedByAlias(ctx context.Context, filter strin
 func (bcr *BlockchainReactor) getTxFeed(ctx context.Context, in struct {
        Alias string `json:"alias,omitempty"`
 }) Response {
-       var txfeed interface{}
+       var tmpTxFeed interface{}
        rawTxfeed, err := bcr.getTxFeedByAlias(ctx, in.Alias)
        if err != nil {
                return NewErrorResponse(err)
        }
-       err = json.Unmarshal(rawTxfeed, &txfeed)
+       err = json.Unmarshal(rawTxfeed, &tmpTxFeed)
        if err != nil {
                return NewErrorResponse(err)
        }
-       data := map[string]interface{}{"txfeed": txfeed}
+       data := map[string]interface{}{"txfeed": tmpTxFeed}
        return NewSuccessResponse(data)
 }
 
@@ -79,24 +78,6 @@ func (bcr *BlockchainReactor) updateTxFeed(ctx context.Context, in struct {
        return NewSuccessResponse(nil)
 }
 
-// txAfterIsBefore returns true if a is before b. It returns an error if either
-// a or b are not valid query.TxAfters.
-func txAfterIsBefore(a, b string) (bool, error) {
-       aAfter, err := query.DecodeTxAfter(a)
-       if err != nil {
-               return false, err
-       }
-
-       bAfter, err := query.DecodeTxAfter(b)
-       if err != nil {
-               return false, err
-       }
-
-       return aAfter.FromBlockHeight < bAfter.FromBlockHeight ||
-               (aAfter.FromBlockHeight == bAfter.FromBlockHeight &&
-                       aAfter.FromPosition < bAfter.FromPosition), nil
-}
-
 func (bcr *BlockchainReactor) getTxFeeds() ([]txfeed.TxFeed, error) {
        txFeed := txfeed.TxFeed{}
        txFeeds := make([]txfeed.TxFeed, 0)
index 5d7b28e..6cc3e6f 100644 (file)
@@ -7,9 +7,10 @@ import (
 
        "github.com/spf13/cobra"
        jww "github.com/spf13/jwalterweatherman"
+
+       "github.com/bytom/blockchain"
        "github.com/bytom/crypto/ed25519/chainkd"
        "github.com/bytom/util"
-       "github.com/bytom/blockchain"
 )
 
 var createKeyCmd = &cobra.Command{
@@ -20,7 +21,7 @@ var createKeyCmd = &cobra.Command{
                var key = struct {
                        Alias    string `json:"alias"`
                        Password string `json:"password"`
-               }{Alias: args[0], Password: "123456"}
+               }{Alias: args[0], Password: args[1]}
 
                data, exitCode := util.ClientCall("/create-key", &key)
                if exitCode != util.Success {
@@ -45,7 +46,7 @@ var deleteKeyCmd = &cobra.Command{
                var key = struct {
                        Password string
                        XPub     chainkd.XPub `json:"xpubs"`
-               }{XPub: *xpub, Password: "123456"}
+               }{XPub: *xpub, Password: args[1]}
 
                if _, exitCode := util.ClientCall("/delete-key", &key); exitCode != util.Success {
                        os.Exit(exitCode)
index 7bd941d..e658e74 100644 (file)
@@ -21,10 +21,10 @@ func init() {
        buildTransactionCmd.PersistentFlags().BoolVar(&pretty, "pretty", false, "pretty print json result")
        buildTransactionCmd.PersistentFlags().BoolVar(&alias, "alias", false, "use alias build transaction")
 
-       signTransactionCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "password of the account which sign these transaction(s)")
+       signTransactionCmd.PersistentFlags().StringArrayVarP(&password, "password", "p", []string{}, "password of the account which sign these transaction(s)")
        signTransactionCmd.PersistentFlags().BoolVar(&pretty, "pretty", false, "pretty print json result")
 
-       signSubTransactionCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "password of the account which sign these transaction(s)")
+       signSubTransactionCmd.PersistentFlags().StringArrayVarP(&password, "password", "p", []string{}, "password of the account which sign these transaction(s)")
 
        listTransactionsCmd.PersistentFlags().StringVar(&txID, "id", "", "transaction id")
        listTransactionsCmd.PersistentFlags().StringVar(&account, "account_id", "", "account id")
@@ -36,7 +36,7 @@ var (
        btmGas          = ""
        receiverProgram = ""
        address         = ""
-       password        = ""
+       password        = make([]string, 0)
        pretty          = false
        alias           = false
        txID            = ""
@@ -194,9 +194,9 @@ var signTransactionCmd = &cobra.Command{
                }
 
                var req = struct {
-                       Auth string
-                       Txs  txbuilder.Template `json:"transaction"`
-               }{Auth: "123456", Txs: template}
+                       Password []string           `json:"password"`
+                       Txs      txbuilder.Template `json:"transaction"`
+               }{Password: password, Txs: template}
 
                jww.FEEDBACK.Printf("\n\n")
                data, exitCode := util.ClientCall("/sign-transaction", &req)
@@ -252,7 +252,7 @@ var signSubTransactionCmd = &cobra.Command{
        Short: "Sign and Submit transaction templates with account password",
        Args:  cobra.ExactArgs(1),
        PreRun: func(cmd *cobra.Command, args []string) {
-               // cmd.MarkFlagRequired("password")
+               cmd.MarkFlagRequired("password")
        },
        Run: func(cmd *cobra.Command, args []string) {
                template := txbuilder.Template{}
@@ -264,9 +264,9 @@ var signSubTransactionCmd = &cobra.Command{
                }
 
                var req = struct {
-                       Auth string
-                       Txs  txbuilder.Template `json:"transaction"`
-               }{Auth: "123456", Txs: template}
+                       Password []string           `json:"password"`
+                       Txs      txbuilder.Template `json:"transaction"`
+               }{Password: password, Txs: template}
 
                jww.FEEDBACK.Printf("\n\n")
                data, exitCode := util.ClientCall("/sign-submit-transaction", &req)
index 2658660..d023130 100644 (file)
@@ -65,7 +65,7 @@ func GenerateGenesisBlock() *legacy.Block {
        for {
                hash := block.Hash()
                if difficulty.CheckProofOfWork(&hash, block.Bits) {
-                       log.Info(block.Nonce)
+                       log.WithField("block.Nonce=", block.Nonce).Info()
                        break
                }
                block.Nonce++