Skip to content

Commit

Permalink
Merge PR: fix the problem of duplicate accounts when using personal_i…
Browse files Browse the repository at this point in the history
…mportRawKey api to add the same private key (#736)

Co-authored-by: Zhong Qiu <36867992+zhongqiuwood@users.noreply.github.com>
  • Loading branch information
Ray Green and zhongqiuwood committed Mar 12, 2021
1 parent 8e7cd7a commit 503c719
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
11 changes: 8 additions & 3 deletions app/rpc/namespaces/personal/api.go
Expand Up @@ -62,18 +62,23 @@ func (api *PrivateAccountAPI) ImportRawKey(privkey, password string) (common.Add
}

privKey := ethsecp256k1.PrivKey(crypto.FromECDSA(priv))

armor := mintkey.EncryptArmorPrivKey(privKey, password, ethsecp256k1.KeyType)
pubKey := privKey.PubKey()

// ignore error as we only care about the length of the list
list, _ := api.ethAPI.ClientCtx().Keybase.List()
for _, info := range list {
if info.GetPubKey().Equals(pubKey){
return common.BytesToAddress(info.GetAddress().Bytes()), nil
}
}
privKeyName := fmt.Sprintf("personal_%d", len(list))
armor := mintkey.EncryptArmorPrivKey(privKey, password, ethsecp256k1.KeyType)

if err := api.ethAPI.ClientCtx().Keybase.ImportPrivKey(privKeyName, armor, password); err != nil {
return common.Address{}, err
}

addr := common.BytesToAddress(privKey.PubKey().Address().Bytes())
addr := common.BytesToAddress(pubKey.Address().Bytes())

info, err := api.ethAPI.ClientCtx().Keybase.Get(privKeyName)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions app/rpc/tests/personal_test.go
Expand Up @@ -75,6 +75,35 @@ func TestPersonal_ImportRawKey(t *testing.T) {
require.Error(t, err)
}

func TestPersonal_ImportRawKey_Duplicate(t *testing.T) {
privkey, err := ethcrypto.GenerateKey()
require.NoError(t, err)
// parse priv key to hex, then add the key
hexPriv := common.Bytes2Hex(ethcrypto.FromECDSA(privkey))
rpcRes := Call(t, "personal_importRawKey", []string{hexPriv, defaultPassWd})
var resAddr common.Address
require.NoError(t, json.Unmarshal(rpcRes.Result, &resAddr))
require.True(t, ethcrypto.PubkeyToAddress(privkey.PublicKey) == resAddr)
addrCounter++

// record the key-list length
rpcRes = Call(t, "personal_listAccounts", nil)
var list []common.Address
require.NoError(t, json.Unmarshal(rpcRes.Result, &list))
originLen := len(list)

// add the same key again
rpcRes = Call(t, "personal_importRawKey", []string{hexPriv, defaultPassWd})
var newResAddr common.Address
require.NoError(t, json.Unmarshal(rpcRes.Result, &newResAddr))
require.Equal(t, resAddr, newResAddr)

// check the actual key-list length changed or not
rpcRes = Call(t, "personal_listAccounts", nil)
require.NoError(t, json.Unmarshal(rpcRes.Result, &list))
require.Equal(t, originLen, len(list))
}

func TestPersonal_EcRecover(t *testing.T) {
data := hexutil.Bytes{0x88}
rpcRes := Call(t, "personal_sign", []interface{}{data, hexutil.Bytes(from), ""})
Expand Down

0 comments on commit 503c719

Please sign in to comment.