2021-05-11 11:54:55 +00:00
|
|
|
package rpc
|
2020-08-13 17:14:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2020-10-06 18:57:55 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-08-13 17:14:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2021-09-03 18:06:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-08-13 17:14:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPersonal_ListAccounts(t *testing.T) {
|
2020-12-15 19:52:09 +00:00
|
|
|
rpcRes := Call(t, "personal_listAccounts", []string{})
|
2020-08-13 17:14:48 +00:00
|
|
|
|
|
|
|
var res []hexutil.Bytes
|
|
|
|
err := json.Unmarshal(rpcRes.Result, &res)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPersonal_NewAccount(t *testing.T) {
|
2020-12-15 19:52:09 +00:00
|
|
|
rpcRes := Call(t, "personal_newAccount", []string{"password"})
|
2020-08-13 17:14:48 +00:00
|
|
|
var addr common.Address
|
|
|
|
err := json.Unmarshal(rpcRes.Result, &addr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-15 19:52:09 +00:00
|
|
|
rpcRes = Call(t, "personal_listAccounts", []string{})
|
2020-08-13 17:14:48 +00:00
|
|
|
var res []hexutil.Bytes
|
|
|
|
err = json.Unmarshal(rpcRes.Result, &res)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPersonal_Sign(t *testing.T) {
|
2021-04-17 10:00:07 +00:00
|
|
|
rpcRes := Call(t, "personal_unlockAccount", []interface{}{hexutil.Bytes(from), ""})
|
|
|
|
require.Nil(t, rpcRes.Error)
|
2020-08-13 17:14:48 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
rpcRes = Call(t, "personal_sign", []interface{}{hexutil.Bytes{0x88}, hexutil.Bytes(from), ""})
|
|
|
|
require.Nil(t, rpcRes.Error)
|
2020-08-13 17:14:48 +00:00
|
|
|
var res hexutil.Bytes
|
|
|
|
err := json.Unmarshal(rpcRes.Result, &res)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 65, len(res))
|
|
|
|
// TODO: check that signature is same as with geth, requires importing a key
|
|
|
|
}
|
|
|
|
|
2020-09-30 00:34:01 +00:00
|
|
|
func TestPersonal_ImportRawKey(t *testing.T) {
|
2021-09-03 18:06:36 +00:00
|
|
|
privkey, err := crypto.GenerateKey()
|
2020-09-30 00:34:01 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// parse priv key to hex
|
2021-09-03 18:06:36 +00:00
|
|
|
hexPriv := common.Bytes2Hex(crypto.FromECDSA(privkey))
|
2020-12-15 19:52:09 +00:00
|
|
|
rpcRes := Call(t, "personal_importRawKey", []string{hexPriv, "password"})
|
2020-09-30 00:34:01 +00:00
|
|
|
|
|
|
|
var res hexutil.Bytes
|
|
|
|
err = json.Unmarshal(rpcRes.Result, &res)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-03 18:06:36 +00:00
|
|
|
addr := crypto.PubkeyToAddress(privkey.PublicKey)
|
2020-09-30 00:34:01 +00:00
|
|
|
resAddr := common.BytesToAddress(res)
|
|
|
|
|
|
|
|
require.Equal(t, addr.String(), resAddr.String())
|
|
|
|
}
|
|
|
|
|
2020-08-13 17:14:48 +00:00
|
|
|
func TestPersonal_EcRecover(t *testing.T) {
|
|
|
|
data := hexutil.Bytes{0x88}
|
2020-12-15 19:52:09 +00:00
|
|
|
rpcRes := Call(t, "personal_sign", []interface{}{data, hexutil.Bytes(from), ""})
|
2020-08-13 17:14:48 +00:00
|
|
|
|
|
|
|
var res hexutil.Bytes
|
|
|
|
err := json.Unmarshal(rpcRes.Result, &res)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 65, len(res))
|
|
|
|
|
2020-12-15 19:52:09 +00:00
|
|
|
rpcRes = Call(t, "personal_ecRecover", []interface{}{data, res})
|
2020-08-13 17:14:48 +00:00
|
|
|
var ecrecoverRes common.Address
|
|
|
|
err = json.Unmarshal(rpcRes.Result, &ecrecoverRes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, from, ecrecoverRes[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPersonal_UnlockAccount(t *testing.T) {
|
|
|
|
pswd := "nootwashere"
|
2020-12-15 19:52:09 +00:00
|
|
|
rpcRes := Call(t, "personal_newAccount", []string{pswd})
|
2020-08-13 17:14:48 +00:00
|
|
|
var addr common.Address
|
|
|
|
err := json.Unmarshal(rpcRes.Result, &addr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// try to sign, should be locked
|
2020-12-15 19:52:09 +00:00
|
|
|
_, err = CallWithError("personal_sign", []interface{}{hexutil.Bytes{0x88}, addr, ""})
|
2020-09-30 00:34:01 +00:00
|
|
|
require.Error(t, err)
|
2020-08-13 17:14:48 +00:00
|
|
|
|
2020-12-15 19:52:09 +00:00
|
|
|
rpcRes = Call(t, "personal_unlockAccount", []interface{}{addr, ""})
|
2020-08-13 17:14:48 +00:00
|
|
|
var unlocked bool
|
|
|
|
err = json.Unmarshal(rpcRes.Result, &unlocked)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, unlocked)
|
|
|
|
|
|
|
|
// try to sign, should work now
|
2020-12-15 19:52:09 +00:00
|
|
|
rpcRes = Call(t, "personal_sign", []interface{}{hexutil.Bytes{0x88}, addr, pswd})
|
2020-08-13 17:14:48 +00:00
|
|
|
var res hexutil.Bytes
|
|
|
|
err = json.Unmarshal(rpcRes.Result, &res)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 65, len(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPersonal_LockAccount(t *testing.T) {
|
|
|
|
pswd := "nootwashere"
|
2020-12-15 19:52:09 +00:00
|
|
|
rpcRes := Call(t, "personal_newAccount", []string{pswd})
|
2020-08-13 17:14:48 +00:00
|
|
|
var addr common.Address
|
|
|
|
err := json.Unmarshal(rpcRes.Result, &addr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-15 19:52:09 +00:00
|
|
|
rpcRes = Call(t, "personal_unlockAccount", []interface{}{addr, ""})
|
2020-08-13 17:14:48 +00:00
|
|
|
var unlocked bool
|
|
|
|
err = json.Unmarshal(rpcRes.Result, &unlocked)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, unlocked)
|
|
|
|
|
2020-12-15 19:52:09 +00:00
|
|
|
rpcRes = Call(t, "personal_lockAccount", []interface{}{addr})
|
2020-08-13 17:14:48 +00:00
|
|
|
var locked bool
|
|
|
|
err = json.Unmarshal(rpcRes.Result, &locked)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, locked)
|
|
|
|
|
|
|
|
// try to sign, should be locked
|
2020-12-15 19:52:09 +00:00
|
|
|
_, err = CallWithError("personal_sign", []interface{}{hexutil.Bytes{0x88}, addr, ""})
|
2020-09-30 00:34:01 +00:00
|
|
|
require.Error(t, err)
|
2020-08-13 17:14:48 +00:00
|
|
|
}
|