OSDN Git Service

add filter field pubkey for list-pubkeys (#1161)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Mon, 23 Jul 2018 07:03:41 +0000 (15:03 +0800)
committerPaladz <yzhu101@uottawa.ca>
Mon, 23 Jul 2018 07:03:41 +0000 (15:03 +0800)
* add filter pubkey for list-pubkeys

* optimise

* optimise import

api/query.go
cmd/bytomcli/commands/account.go

index 1db5929..79315ae 100644 (file)
@@ -13,6 +13,7 @@ import (
        "github.com/bytom/consensus"
        "github.com/bytom/crypto/ed25519/chainkd"
        chainjson "github.com/bytom/encoding/json"
+       "github.com/bytom/errors"
        "github.com/bytom/protocol/bc"
        "github.com/bytom/protocol/bc/types"
 )
@@ -285,20 +286,33 @@ type AccountPubkey struct {
 
 // POST /list-pubkeys
 func (a *API) listPubKeys(ctx context.Context, ins struct {
-       AccountID string `json:"account_id"`
+       AccountID    string `json:"account_id"`
+       AccountAlias string `json:"account_alias"`
+       PublicKey    string `json:"public_key"`
 }) Response {
-       account, err := a.wallet.AccountMgr.FindByID(ins.AccountID)
+       var err error
+       account := &account.Account{}
+       if ins.AccountAlias != "" {
+               account, err = a.wallet.AccountMgr.FindByAlias(ins.AccountAlias)
+       } else {
+               account, err = a.wallet.AccountMgr.FindByID(ins.AccountID)
+       }
+
        if err != nil {
                return NewErrorResponse(err)
        }
 
        pubKeyInfos := []PubKeyInfo{}
-       idx := a.wallet.AccountMgr.GetContractIndex(ins.AccountID)
+       idx := a.wallet.AccountMgr.GetContractIndex(account.ID)
        for i := uint64(1); i <= idx; i++ {
                rawPath := signers.Path(account.Signer, signers.AccountKeySpace, i)
                derivedXPub := account.XPubs[0].Derive(rawPath)
                pubkey := derivedXPub.PublicKey()
 
+               if ins.PublicKey != "" && ins.PublicKey != hex.EncodeToString(pubkey) {
+                       continue
+               }
+
                var path []chainjson.HexBytes
                for _, p := range rawPath {
                        path = append(path, chainjson.HexBytes(p))
@@ -310,6 +324,10 @@ func (a *API) listPubKeys(ctx context.Context, ins struct {
                }}, pubKeyInfos...)
        }
 
+       if len(pubKeyInfos) == 0 {
+               return NewErrorResponse(errors.New("Not found publickey for the account"))
+       }
+
        return NewSuccessResponse(&AccountPubkey{
                RootXPub:    account.XPubs[0],
                PubKeyInfos: pubKeyInfos,
index 1657fcc..57fd0d6 100644 (file)
@@ -2,6 +2,7 @@ package commands
 
 import (
        "os"
+       "strings"
 
        "github.com/spf13/cobra"
        jww "github.com/spf13/jwalterweatherman"
@@ -14,7 +15,7 @@ func init() {
        createAccountCmd.PersistentFlags().IntVarP(&accountQuorum, "quorom", "q", 1, "quorum must be greater than 0 and less than or equal to the number of signers")
        createAccountCmd.PersistentFlags().StringVarP(&accountToken, "access", "a", "", "access token")
 
-       listAccountsCmd.PersistentFlags().StringVar(&accountID, "id", "", "ID of account")
+       listAccountsCmd.PersistentFlags().StringVar(&accountID, "id", "", "account ID")
 
        listAddressesCmd.PersistentFlags().StringVar(&accountID, "id", "", "account ID")
        listAddressesCmd.PersistentFlags().StringVar(&accountAlias, "alias", "", "account alias")
@@ -157,13 +158,25 @@ var validateAddressCmd = &cobra.Command{
 }
 
 var listPubKeysCmd = &cobra.Command{
-       Use:   "list-pubkeys <accountID>",
+       Use:   "list-pubkeys <accountInfo> [publicKey]",
        Short: "list the account pubkeys",
-       Args:  cobra.ExactArgs(1),
+       Args:  cobra.RangeArgs(1, 2),
        Run: func(cmd *cobra.Command, args []string) {
                var ins = struct {
-                       AccountID string `json:"account_id"`
-               }{AccountID: args[0]}
+                       AccountID    string `json:"account_id"`
+                       AccountAlias string `json:"account_alias"`
+                       PublicKey    string `json:"public_key"`
+               }{}
+
+               if len(args[0]) == 13 && strings.HasPrefix(args[0], "0") {
+                       ins.AccountID = args[0]
+               } else {
+                       ins.AccountAlias = args[0]
+               }
+
+               if len(args) == 2 {
+                       ins.PublicKey = args[1]
+               }
 
                data, exitCode := util.ClientCall("/list-pubkeys", &ins)
                if exitCode != util.Success {