rpc: implement personal_importRawKey (#552)

This commit is contained in:
Federico Kunze
2020-09-29 20:34:01 -04:00
committed by GitHub
parent 592eca96bc
commit 811ca7f045
6 changed files with 112 additions and 89 deletions
+21 -2
View File
@@ -6,6 +6,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
)
@@ -42,6 +43,24 @@ func TestPersonal_Sign(t *testing.T) {
// TODO: check that signature is same as with geth, requires importing a key
}
func TestPersonal_ImportRawKey(t *testing.T) {
privkey, err := ethcrypto.GenerateKey()
require.NoError(t, err)
// parse priv key to hex
hexPriv := common.Bytes2Hex(ethcrypto.FromECDSA(privkey))
rpcRes := call(t, "personal_importRawKey", []string{hexPriv, "password"})
var res hexutil.Bytes
err = json.Unmarshal(rpcRes.Result, &res)
require.NoError(t, err)
addr := ethcrypto.PubkeyToAddress(privkey.PublicKey)
resAddr := common.BytesToAddress(res)
require.Equal(t, addr.String(), resAddr.String())
}
func TestPersonal_EcRecover(t *testing.T) {
data := hexutil.Bytes{0x88}
rpcRes := call(t, "personal_sign", []interface{}{data, hexutil.Bytes(from), ""})
@@ -67,7 +86,7 @@ func TestPersonal_UnlockAccount(t *testing.T) {
// try to sign, should be locked
_, err = callWithError("personal_sign", []interface{}{hexutil.Bytes{0x88}, addr, ""})
require.NotNil(t, err)
require.Error(t, err)
rpcRes = call(t, "personal_unlockAccount", []interface{}{addr, ""})
var unlocked bool
@@ -104,5 +123,5 @@ func TestPersonal_LockAccount(t *testing.T) {
// try to sign, should be locked
_, err = callWithError("personal_sign", []interface{}{hexutil.Bytes{0x88}, addr, ""})
require.NotNil(t, err)
require.Error(t, err)
}