Skip to content

Commit

Permalink
Merge PR: unsupport personal_xx in file keyring backend mode (#2078)
Browse files Browse the repository at this point in the history
* unsupport personal_xx in file keyring backend mode

* use backendTest for rpc

* revert edit

* edit for rpc

Co-authored-by: KamiD <44460798+KamiD@users.noreply.github.com>
  • Loading branch information
lcmmhcc and KamiD authored May 25, 2022
1 parent 8c9e10b commit dff85df
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
7 changes: 5 additions & 2 deletions app/rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,13 @@ func (api *PublicEthereumAPI) GetKeyringInfo() error {
if api.clientCtx.Keybase != nil {
return nil
}

backendType := viper.GetString(flags.FlagKeyringBackend)
if backendType == keys.BackendFile {
backendType = keys.BackendFileForRPC
}
keybase, err := keys.NewKeyring(
sdk.KeyringServiceName(),
viper.GetString(flags.FlagKeyringBackend),
backendType,
viper.GetString(cmserver.FlagUlockKeyHome),
api.clientCtx.Input,
hd.EthSecp256k1Options()...,
Expand Down
2 changes: 1 addition & 1 deletion app/rpc/namespaces/personal/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func NewAPI(ethAPI *eth.PublicEthereumAPI, log log.Logger) *PrivateAccountAPI {
if err != nil {
return api
}

api.keyInfos, err = api.ethAPI.ClientCtx().Keybase.List()
if err != nil {
return api
Expand All @@ -63,6 +62,7 @@ func NewAPI(ethAPI *eth.PublicEthereumAPI, log log.Logger) *PrivateAccountAPI {
// NOTE: The key will be both armored and encrypted using the same passphrase.
func (api *PrivateAccountAPI) ImportRawKey(privkey, password string) (common.Address, error) {
api.logger.Debug("personal_importRawKey")

priv, err := crypto.HexToECDSA(privkey)
if err != nil {
return common.Address{}, err
Expand Down
31 changes: 21 additions & 10 deletions libs/cosmos-sdk/crypto/keys/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import (
)

const (
BackendFile = "file"
BackendOS = "os"
BackendKWallet = "kwallet"
BackendPass = "pass"
BackendTest = "test"
BackendMemory = "memory"
BackendFile = "file"
BackendFileForRPC = "file4rpc"
BackendOS = "os"
BackendKWallet = "kwallet"
BackendPass = "pass"
BackendTest = "test"
BackendMemory = "memory"
)

const (
Expand Down Expand Up @@ -74,6 +75,8 @@ func NewKeyring(
config = lkbToKeyringConfig(appName, rootDir, nil, true)
case BackendFile:
config = newFileBackendKeyringConfig(appName, rootDir, userInput)
case BackendFileForRPC:
config = newFileBackendKeyringConfigForRPC(appName, rootDir, userInput)
case BackendOS:
config = lkbToKeyringConfig(appName, rootDir, userInput, false)
case BackendKWallet:
Expand All @@ -93,7 +96,6 @@ func NewKeyring(
return newKeyringKeybase(db, config.FileDir, opts...), nil
}


// CreateMnemonic generates a new key and persists it to storage, encrypted
// using the provided password. It returns the generated mnemonic and the key Info.
// An error is returned if it fails to generate a key for the given algo type,
Expand Down Expand Up @@ -519,9 +521,6 @@ func lkbToKeyringConfig(appName, dir string, buf io.Reader, test bool) keyring.C
}
}




func newKWalletBackendKeyringConfig(appName, _ string, _ io.Reader) keyring.Config {
return keyring.Config{
AllowedBackends: []keyring.BackendType{keyring.KWalletBackend},
Expand Down Expand Up @@ -550,6 +549,18 @@ func newFileBackendKeyringConfig(name, dir string, buf io.Reader) keyring.Config
}
}

func newFileBackendKeyringConfigForRPC(name, dir string, buf io.Reader) keyring.Config {
fileDir := filepath.Join(dir, fmt.Sprintf(keyringDirNameFmt, name))
return keyring.Config{
AllowedBackends: []keyring.BackendType{keyring.FileBackend},
ServiceName: name,
FileDir: fileDir,
FilePasswordFunc: func(_ string) (string, error) {
return "test", nil
},
}
}

func newRealPrompt(dir string, buf io.Reader) func(string) (string, error) {
return func(prompt string) (string, error) {
keyhashStored := false
Expand Down

0 comments on commit dff85df

Please sign in to comment.