OSDN Git Service

add list-pubkeys API (#1086)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Thu, 21 Jun 2018 13:43:06 +0000 (21:43 +0800)
committerPaladz <yzhu101@uottawa.ca>
Thu, 21 Jun 2018 13:43:06 +0000 (21:43 +0800)
* add list-pubkeys API

* modify response of list-pubkeys

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

index 4ace39c..72e11bd 100644 (file)
@@ -144,6 +144,18 @@ func (m *Manager) getNextContractIndex(accountID string) uint64 {
        return nextIndex
 }
 
+// GetContractIndex return the current index
+func (m *Manager) GetContractIndex(accountID string) uint64 {
+       m.accIndexMu.Lock()
+       defer m.accIndexMu.Unlock()
+
+       index := uint64(1)
+       if rawIndexBytes := m.db.Get(contractIndexKey(accountID)); rawIndexBytes != nil {
+               index = common.BytesToUnit64(rawIndexBytes)
+       }
+       return index
+}
+
 // Create creates a new Account.
 func (m *Manager) Create(ctx context.Context, xpubs []chainkd.XPub, quorum int, alias string) (*Account, error) {
        m.accountMu.Lock()
index 4c0beda..c1f1341 100644 (file)
@@ -201,6 +201,7 @@ func (a *API) buildHandler() {
                m.Handle("/create-account-receiver", jsonHandler(a.createAccountReceiver))
                m.Handle("/list-addresses", jsonHandler(a.listAddresses))
                m.Handle("/validate-address", jsonHandler(a.validateAddress))
+               m.Handle("/list-pubkeys", jsonHandler(a.listPubKeys))
 
                m.Handle("/create-asset", jsonHandler(a.createAsset))
                m.Handle("/update-asset-alias", jsonHandler(a.updateAssetAlias))
index 7463947..bbb66f9 100644 (file)
@@ -2,13 +2,16 @@ package api
 
 import (
        "context"
+       "encoding/hex"
        "fmt"
 
        log "github.com/sirupsen/logrus"
 
        "github.com/bytom/account"
        "github.com/bytom/blockchain/query"
+       "github.com/bytom/blockchain/signers"
        "github.com/bytom/consensus"
+       "github.com/bytom/crypto/ed25519/chainkd"
        chainjson "github.com/bytom/encoding/json"
        "github.com/bytom/protocol/bc"
        "github.com/bytom/protocol/bc/types"
@@ -264,3 +267,48 @@ func (a *API) gasRate() Response {
        gasrate := map[string]int64{"gas_rate": consensus.VMGasRate}
        return NewSuccessResponse(gasrate)
 }
+
+// PubKeyInfo is structure of pubkey info
+type PubKeyInfo struct {
+       Pubkey string               `json:"pubkey"`
+       Path   []chainjson.HexBytes `json:"derivation_path"`
+}
+
+// AccountPubkey is detail of account pubkey info
+type AccountPubkey struct {
+       RootXPub    chainkd.XPub `json:"root_xpub"`
+       PubKeyInfos []PubKeyInfo `json:"pubkey_infos"`
+}
+
+// POST /list-pubkeys
+func (a *API) listPubKeys(ctx context.Context, ins struct {
+       AccountID string `json:"account_id"`
+}) Response {
+       account, err := a.wallet.AccountMgr.FindByID(ctx, ins.AccountID)
+       if err != nil {
+               return NewErrorResponse(err)
+       }
+
+       pubKeyInfos := []PubKeyInfo{}
+       idx := a.wallet.AccountMgr.GetContractIndex(ins.AccountID)
+       for i := uint64(1); i <= idx; i++ {
+               rawPath := signers.Path(account.Signer, signers.AccountKeySpace, i)
+               derivedXPub := account.XPubs[0].Derive(rawPath)
+               pubkey := derivedXPub.PublicKey()
+
+               var path []chainjson.HexBytes
+               for _, p := range rawPath {
+                       path = append(path, chainjson.HexBytes(p))
+               }
+
+               pubKeyInfos = append([]PubKeyInfo{{
+                       Pubkey: hex.EncodeToString(pubkey),
+                       Path:   path,
+               }}, pubKeyInfos...)
+       }
+
+       return NewSuccessResponse(&AccountPubkey{
+               RootXPub:    account.XPubs[0],
+               PubKeyInfos: pubKeyInfos,
+       })
+}
index 0d556a2..1657fcc 100644 (file)
@@ -156,6 +156,24 @@ var validateAddressCmd = &cobra.Command{
        },
 }
 
+var listPubKeysCmd = &cobra.Command{
+       Use:   "list-pubkeys <accountID>",
+       Short: "list the account pubkeys",
+       Args:  cobra.ExactArgs(1),
+       Run: func(cmd *cobra.Command, args []string) {
+               var ins = struct {
+                       AccountID string `json:"account_id"`
+               }{AccountID: args[0]}
+
+               data, exitCode := util.ClientCall("/list-pubkeys", &ins)
+               if exitCode != util.Success {
+                       os.Exit(exitCode)
+               }
+
+               printJSON(data)
+       },
+}
+
 var listBalancesCmd = &cobra.Command{
        Use:   "list-balances",
        Short: "List the accounts balances",
index dd1abf9..ddb5542 100644 (file)
@@ -117,6 +117,7 @@ func AddCommands() {
        BytomcliCmd.AddCommand(createAccountReceiverCmd)
        BytomcliCmd.AddCommand(listAddressesCmd)
        BytomcliCmd.AddCommand(validateAddressCmd)
+       BytomcliCmd.AddCommand(listPubKeysCmd)
 
        BytomcliCmd.AddCommand(createAssetCmd)
        BytomcliCmd.AddCommand(getAssetCmd)
@@ -178,6 +179,7 @@ func AddTemplateFunc() {
                createAccountReceiverCmd.Name(),
                listAddressesCmd.Name(),
                validateAddressCmd.Name(),
+               listPubKeysCmd.Name(),
 
                createAssetCmd.Name(),
                getAssetCmd.Name(),