From 1d6fd1db6312dc54fd4694f4a9c3feb86381c8c1 Mon Sep 17 00:00:00 2001 From: oysheng <33340252+oysheng@users.noreply.github.com> Date: Mon, 23 Jul 2018 15:03:41 +0800 Subject: [PATCH] add filter field pubkey for list-pubkeys (#1161) * add filter pubkey for list-pubkeys * optimise * optimise import --- api/query.go | 24 +++++++++++++++++++++--- cmd/bytomcli/commands/account.go | 23 ++++++++++++++++++----- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/api/query.go b/api/query.go index 1db59295..79315ae6 100644 --- a/api/query.go +++ b/api/query.go @@ -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, diff --git a/cmd/bytomcli/commands/account.go b/cmd/bytomcli/commands/account.go index 1657fcc2..57fd0d65 100644 --- a/cmd/bytomcli/commands/account.go +++ b/cmd/bytomcli/commands/account.go @@ -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 ", + Use: "list-pubkeys [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 { -- 2.11.0