personal API (#420)

This commit is contained in:
noot
2020-08-13 13:14:48 -04:00
committed by GitHub
parent a2a5799d13
commit 1cb712fb16
5 changed files with 371 additions and 38 deletions
+108
View File
@@ -0,0 +1,108 @@
package tests
import (
"encoding/json"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/stretchr/testify/require"
)
func TestPersonal_ListAccounts(t *testing.T) {
rpcRes := call(t, "personal_listAccounts", []string{})
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) {
rpcRes := call(t, "personal_newAccount", []string{""})
var addr common.Address
err := json.Unmarshal(rpcRes.Result, &addr)
require.NoError(t, err)
rpcRes = call(t, "personal_listAccounts", []string{})
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) {
rpcRes := call(t, "personal_sign", []interface{}{hexutil.Bytes{0x88}, hexutil.Bytes(from), ""})
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
}
func TestPersonal_EcRecover(t *testing.T) {
data := hexutil.Bytes{0x88}
rpcRes := call(t, "personal_sign", []interface{}{data, hexutil.Bytes(from), ""})
var res hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &res)
require.NoError(t, err)
require.Equal(t, 65, len(res))
rpcRes = call(t, "personal_ecRecover", []interface{}{data, res})
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"
rpcRes := call(t, "personal_newAccount", []string{pswd})
var addr common.Address
err := json.Unmarshal(rpcRes.Result, &addr)
require.NoError(t, err)
// try to sign, should be locked
_, err = callWithError("personal_sign", []interface{}{hexutil.Bytes{0x88}, addr, ""})
require.NotNil(t, err)
rpcRes = call(t, "personal_unlockAccount", []interface{}{addr, ""})
var unlocked bool
err = json.Unmarshal(rpcRes.Result, &unlocked)
require.NoError(t, err)
require.True(t, unlocked)
// try to sign, should work now
rpcRes = call(t, "personal_sign", []interface{}{hexutil.Bytes{0x88}, addr, pswd})
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"
rpcRes := call(t, "personal_newAccount", []string{pswd})
var addr common.Address
err := json.Unmarshal(rpcRes.Result, &addr)
require.NoError(t, err)
rpcRes = call(t, "personal_unlockAccount", []interface{}{addr, ""})
var unlocked bool
err = json.Unmarshal(rpcRes.Result, &unlocked)
require.NoError(t, err)
require.True(t, unlocked)
rpcRes = call(t, "personal_lockAccount", []interface{}{addr})
var locked bool
err = json.Unmarshal(rpcRes.Result, &locked)
require.NoError(t, err)
require.True(t, locked)
// try to sign, should be locked
_, err = callWithError("personal_sign", []interface{}{hexutil.Bytes{0x88}, addr, ""})
require.NotNil(t, err)
}
+59 -30
View File
@@ -3,10 +3,7 @@
// To run these tests please first ensure you have the ethermintd running
// and have started the RPC service with `ethermintcli rest-server`.
//
// You can configure the desired ETHERMINT_NODE_HOST and ETHERMINT_INTEGRATION_TEST_MODE
//
// to have it running
// You can configure the desired HOST and MODE as well
package tests
import (
@@ -42,6 +39,7 @@ var (
HOST = os.Getenv("HOST")
zeroString = "0x0"
from = []byte{}
)
type Request struct {
@@ -73,11 +71,33 @@ func TestMain(m *testing.M) {
HOST = "http://localhost:8545"
}
var err error
from, err = getAddress()
if err != nil {
fmt.Printf("failed to get account: %s\n", err)
os.Exit(1)
}
// Start all tests
code := m.Run()
os.Exit(code)
}
func getAddress() ([]byte, error) {
rpcRes, err := callWithError("eth_accounts", []string{})
if err != nil {
return nil, err
}
var res []hexutil.Bytes
err = json.Unmarshal(rpcRes.Result, &res)
if err != nil {
return nil, err
}
return res[0], nil
}
func createRequest(method string, params interface{}) Request {
return Request{
Version: "2.0",
@@ -109,6 +129,39 @@ func call(t *testing.T, method string, params interface{}) *Response {
return rpcRes
}
func callWithError(method string, params interface{}) (*Response, error) {
req, err := json.Marshal(createRequest(method, params))
if err != nil {
return nil, err
}
var rpcRes *Response
time.Sleep(1 * time.Second)
/* #nosec */
res, err := http.Post(HOST, "application/json", bytes.NewBuffer(req))
if err != nil {
return nil, err
}
decoder := json.NewDecoder(res.Body)
rpcRes = new(Response)
err = decoder.Decode(&rpcRes)
if err != nil {
return nil, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
if rpcRes.Error != nil {
return nil, fmt.Errorf(rpcRes.Error.Message)
}
return rpcRes, nil
}
// turns a 0x prefixed hex string to a big.Int
func hexToBigInt(t *testing.T, in string) *big.Int {
s := in[2:]
@@ -240,7 +293,7 @@ func TestEth_coinbase(t *testing.T) {
require.NoError(t, err)
t.Logf("Got coinbase block proposer: %s\n", res.String())
require.NotEqual(t, zeroAddress.String(), res.String(), "expected: %s got: %s\n", zeroAddress.String(), res.String())
require.NotEqual(t, zeroAddress.String(), res.String(), "expected: not %s got: %s\n", zeroAddress.String(), res.String())
}
func TestEth_GetBalance(t *testing.T) {
@@ -301,19 +354,7 @@ func TestEth_GetCode(t *testing.T) {
require.True(t, bytes.Equal(expectedRes, code), "expected: %X got: %X", expectedRes, code)
}
func getAddress(t *testing.T) []byte {
rpcRes := call(t, "eth_accounts", []string{})
var res []hexutil.Bytes
err := json.Unmarshal(rpcRes.Result, &res)
require.NoError(t, err)
return res[0]
}
func TestEth_SendTransaction_Transfer(t *testing.T) {
from := getAddress(t)
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
@@ -334,8 +375,6 @@ func TestEth_SendTransaction_Transfer(t *testing.T) {
}
func TestEth_SendTransaction_ContractDeploy(t *testing.T) {
from := getAddress(t)
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
@@ -422,7 +461,6 @@ func TestEth_GetFilterChanges_WrongID(t *testing.T) {
// sendTestTransaction sends a dummy transaction
func sendTestTransaction(t *testing.T) hexutil.Bytes {
from := getAddress(t)
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
@@ -455,8 +493,6 @@ func TestEth_GetTransactionReceipt(t *testing.T) {
// deployTestContract deploys a contract that emits an event in the constructor
func deployTestContract(t *testing.T) (hexutil.Bytes, map[string]interface{}) {
from := getAddress(t)
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
@@ -586,8 +622,6 @@ func deployTestContractWithFunction(t *testing.T) hexutil.Bytes {
bytecode := "0x608060405234801561001057600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a260d08061004d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063eb8ac92114602d575b600080fd5b606060048036036040811015604157600080fd5b8101908080359060200190929190803590602001909291905050506062565b005b8160008190555080827ff3ca124a697ba07e8c5e80bebcfcc48991fc16a63170e8a9206e30508960d00360405160405180910390a3505056fea265627a7a723158201d94d2187aaf3a6790527b615fcc40970febf0385fa6d72a2344848ebd0df3e964736f6c63430005110032"
from := getAddress(t)
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
@@ -701,7 +735,6 @@ func TestEth_PendingTransactionFilter(t *testing.T) {
}
func getNonce(t *testing.T) hexutil.Uint64 {
from := getAddress(t)
param := []interface{}{hexutil.Bytes(from), "latest"}
rpcRes := call(t, "eth_getTransactionCount", param)
@@ -712,7 +745,6 @@ func getNonce(t *testing.T) hexutil.Uint64 {
}
func TestEth_EstimateGas(t *testing.T) {
from := getAddress(t)
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
@@ -728,7 +760,6 @@ func TestEth_EstimateGas(t *testing.T) {
}
func TestEth_EstimateGas_ContractDeployment(t *testing.T) {
from := getAddress(t)
bytecode := "0x608060405234801561001057600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a260d08061004d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063eb8ac92114602d575b600080fd5b606060048036036040811015604157600080fd5b8101908080359060200190929190803590602001909291905050506062565b005b8160008190555080827ff3ca124a697ba07e8c5e80bebcfcc48991fc16a63170e8a9206e30508960d00360405160405180910390a3505056fea265627a7a723158201d94d2187aaf3a6790527b615fcc40970febf0385fa6d72a2344848ebd0df3e964736f6c63430005110032"
param := make([]map[string]string, 1)
@@ -742,7 +773,7 @@ func TestEth_EstimateGas_ContractDeployment(t *testing.T) {
err := json.Unmarshal(rpcRes.Result, &gas)
require.NoError(t, err)
require.Equal(t, hexutil.Uint64(0x1d46b), gas)
require.Equal(t, hexutil.Uint64(0x1cab2), gas)
}
func TestEth_ExportAccount(t *testing.T) {
@@ -773,12 +804,10 @@ func TestEth_ExportAccount_WithStorage(t *testing.T) {
// call function to set storage
calldata := "0xeb8ac92100000000000000000000000000000000000000000000000000000000000000630000000000000000000000000000000000000000000000000000000000000000"
from := getAddress(t)
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = addr
//param[0]["value"] = "0x1"
param[0]["data"] = calldata
rpcRes := call(t, "eth_sendTransaction", param)