Skip to content

Commit

Permalink
Update geth.personal to compose personal endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Feb 28, 2019
1 parent b23bc72 commit 92534c6
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 198 deletions.
105 changes: 86 additions & 19 deletions docs/web3.personal.rst
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
Personal API
============

.. py:module:: web3.personal
There are 2 objects that expose methods to interact with the RPC APIS
under the ``personal_`` namespace, each supporting the endpoints
implemented in the respective clients.

.. py:class:: Personal
- ``web3.geth.personal``
- ``web3.parity.personal``

The ``web3.personal`` object exposes methods to interact with the RPC APIs
under the ``personal_`` namespace.

.. py:module:: web3.geth.personal
Properties
----------
Geth Methods
------------

The following properties are available on the ``web3.personal`` namespace.
The following methods are available on the ``web3.geth.personal`` namespace.

.. py:attribute:: listAccounts
.. py:method:: listAccounts
* Delegates to ``personal_listAccounts`` RPC Method

Returns the list of known accounts.

.. code-block:: python
>>> web3.personal.listAccounts
>>> web3.geth.personal.listAccounts()
['0xd3cda913deb6f67967b99d67acdfa1712c293601']
Methods
-------

The following methods are available on the ``web3.personal`` namespace.

.. py:method:: importRawKey(self, private_key, passphrase)
* Delegates to ``personal_importRawKey`` RPC Method
Expand All @@ -40,7 +37,7 @@ The following methods are available on the ``web3.personal`` namespace.

.. code-block:: python
>>> web3.personal.importRawKey(some_private_key, 'the-passphrase')
>>> web3.geth.personal.importRawKey(some_private_key, 'the-passphrase')
'0xd3cda913deb6f67967b99d67acdfa1712c293601'
Expand All @@ -53,7 +50,7 @@ The following methods are available on the ``web3.personal`` namespace.

.. code-block:: python
>>> web3.personal.newAccount('the-passphrase')
>>> web3.geth.personal.newAccount('the-passphrase')
'0xd3cda913deb6f67967b99d67acdfa1712c293601'
Expand All @@ -65,7 +62,74 @@ The following methods are available on the ``web3.personal`` namespace.

.. code-block:: python
>>> web3.personal.lockAccount('0xd3cda913deb6f67967b99d67acdfa1712c293601')
>>> web3.geth.personal.lockAccount('0xd3cda913deb6f67967b99d67acdfa1712c293601')
.. py:method:: unlockAccount(self, account, passphrase, duration=None)
* Delegates to ``personal_unlockAccount`` RPC Method

Unlocks the given ``account`` for ``duration`` seconds. If ``duration`` is
``None`` then the account will remain unlocked indefinitely. Returns
boolean as to whether the account was successfully unlocked.

.. code-block:: python
>>> web3.geth.personal.unlockAccount('0xd3cda913deb6f67967b99d67acdfa1712c293601', 'wrong-passphrase')
False
>>> web3.geth.personal.unlockAccount('0xd3cda913deb6f67967b99d67acdfa1712c293601', 'the-passphrase')
True
.. py:method:: sendTransaction(self, transaction, passphrase)
* Delegates to ``personal_sendTransaction`` RPC Method

Sends the transaction.


.. py:module:: web3.parity.personal
Parity Methods
--------------

The following methods are available on the ``web3.parity.personal`` namespace.

.. py:method:: listAccounts
* Delegates to ``personal_listAccounts`` RPC Method

Returns the list of known accounts.

.. code-block:: python
>>> web3.parity.personal.listAccounts()
['0xd3cda913deb6f67967b99d67acdfa1712c293601']
.. py:method:: importRawKey(self, private_key, passphrase)
* Delegates to ``personal_importRawKey`` RPC Method

Adds the given ``private_key`` to the node's keychain, encrypted with the
given ``passphrase``. Returns the address of the imported account.

.. code-block:: python
>>> web3.parity.personal.importRawKey(some_private_key, 'the-passphrase')
'0xd3cda913deb6f67967b99d67acdfa1712c293601'
.. py:method:: newAccount(self, password)
* Delegates to ``personal_newAccount`` RPC Method

Generates a new account in the node's keychain encrypted with the
given ``passphrase``. Returns the address of the created account.

.. code-block:: python
>>> web3.parity.personal.newAccount('the-passphrase')
'0xd3cda913deb6f67967b99d67acdfa1712c293601'
.. py:method:: unlockAccount(self, account, passphrase, duration=None)
Expand All @@ -78,13 +142,16 @@ The following methods are available on the ``web3.personal`` namespace.

.. code-block:: python
>>> web3.personal.unlockAccount('0xd3cda913deb6f67967b99d67acdfa1712c293601', 'wrong-passphrase')
>>> web3.parity.personal.unlockAccount('0xd3cda913deb6f67967b99d67acdfa1712c293601', 'wrong-passphrase')
False
>>> web3.personal.unlockAccount('0xd3cda913deb6f67967b99d67acdfa1712c293601', 'the-passphrase')
>>> web3.parity.personal.unlockAccount('0xd3cda913deb6f67967b99d67acdfa1712c293601', 'the-passphrase')
True
.. py:method:: sendTransaction(self, transaction, passphrase)
* Delegates to ``personal_sendTransaction`` RPC Method

Sends the transaction.



8 changes: 2 additions & 6 deletions tests/integration/go_ethereum/common.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pytest

from web3._utils.module_testing import (
from web3._utils.module_testing import ( # noqa: F401
EthModuleTest,
GoEthereumPersonalModuleTest,
NetModuleTest,
PersonalModuleTest,
VersionModuleTest,
Web3ModuleTest,
)
Expand Down Expand Up @@ -66,7 +66,3 @@ class GoEthereumVersionModuleTest(VersionModuleTest):

class GoEthereumNetModuleTest(NetModuleTest):
pass


class GoEthereumPersonalModuleTest(PersonalModuleTest):
pass
8 changes: 4 additions & 4 deletions tests/integration/go_ethereum/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ def emitter_contract_address(emitter_contract, address_conversion_func):

@pytest.fixture
def unlocked_account(web3, unlockable_account, unlockable_account_pw):
web3.personal.unlockAccount(unlockable_account, unlockable_account_pw)
web3.geth.personal.unlockAccount(unlockable_account, unlockable_account_pw)
yield unlockable_account
web3.personal.lockAccount(unlockable_account)
web3.geth.personal.lockAccount(unlockable_account)


@pytest.fixture(scope='module')
Expand All @@ -206,9 +206,9 @@ def unlockable_account_dual_type(unlockable_account, address_conversion_func):

@pytest.yield_fixture
def unlocked_account_dual_type(web3, unlockable_account_dual_type, unlockable_account_pw):
web3.personal.unlockAccount(unlockable_account_dual_type, unlockable_account_pw)
web3.geth.personal.unlockAccount(unlockable_account_dual_type, unlockable_account_pw)
yield unlockable_account_dual_type
web3.personal.lockAccount(unlockable_account_dual_type)
web3.geth.personal.lockAccount(unlockable_account_dual_type)


@pytest.fixture(scope="module")
Expand Down
52 changes: 2 additions & 50 deletions tests/integration/parity/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
flaky,
)

from web3._utils.module_testing import (
from web3._utils.module_testing import ( # noqa: F401
EthModuleTest,
ParityModuleTest as TraceModuleTest,
PersonalModuleTest,
ParityPersonalModuleTest,
Web3ModuleTest,
)

Expand Down Expand Up @@ -120,54 +120,6 @@ def test_eth_call_old_contract_state(self, web3, math_contract, unlocked_account
raise AssertionError("pending call result was %d!" % pending_call_result)


class ParityPersonalModuleTest(PersonalModuleTest):
def test_personal_importRawKey(self, web3):
pytest.xfail('this non-standard json-rpc method is not implemented on parity')
super().test_personal_importRawKey(web3)

def test_personal_listAccounts(self, web3):
pytest.xfail('this non-standard json-rpc method is not implemented on parity')
super().test_personal_listAccounts(web3)

def test_personal_lockAccount(self, web3, unlocked_account):
pytest.xfail('this non-standard json-rpc method is not implemented on parity')
super().test_personal_lockAccount(web3, unlocked_account)

def test_personal_unlockAccount_success(self, web3):
pytest.xfail('this non-standard json-rpc method is not implemented on parity')
super().test_personal_unlockAccount_success(web3)

def test_personal_unlockAccount_failure(self, web3, unlockable_account):
pytest.xfail('this non-standard json-rpc method is not implemented on parity')
super().test_personal_unlockAccount_failure(web3, unlockable_account)

def test_personal_newAccount(self, web3):
pytest.xfail('this non-standard json-rpc method is not implemented on parity')
super().test_personal_newAccount(web3)

def test_personal_sendTransaction(
self,
web3,
unlockable_account,
unlockable_account_pw):
pytest.xfail('this non-standard json-rpc method is not implemented on parity')
super().test_personal_sendTransaction(
web3,
unlockable_account,
unlockable_account_pw)

def test_personal_sign_and_ecrecover(
self,
web3,
unlockable_account,
unlockable_account_pw):
pytest.xfail('this non-standard json-rpc method is not implemented on parity')
super().test_personal_sign_and_ecrecover(
web3,
unlockable_account,
unlockable_account_pw)


class ParityTraceModuleTest(TraceModuleTest):
def test_list_storage_keys_no_support(self, web3, emitter_contract_address):
super().test_list_storage_keys_no_support(web3, emitter_contract_address)
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/parity/test_parity_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def parity_command_arguments(
'--unlock', author,
'--password', passwordfile,
'--jsonrpc-port', rpc_port,
'--jsonrpc-apis', 'all',
'--no-ipc',
'--no-ws',
)
Expand All @@ -61,6 +62,7 @@ def parity_import_blocks_command(parity_binary, rpc_port, datadir, passwordfile)
'--base-path', datadir,
'--password', passwordfile,
'--jsonrpc-port', str(rpc_port),
'--jsonrpc-apis', 'all',
'--no-ipc',
'--no-ws',
'--tracing', 'on',
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/parity/test_parity_ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def parity_command_arguments(
'--base-path', datadir,
'--unlock', author,
'--password', passwordfile,
'--ipc-apis', 'all',
'--no-jsonrpc',
'--no-ws',
)
Expand All @@ -59,6 +60,7 @@ def parity_import_blocks_command(parity_binary, ipc_path, datadir, passwordfile)
'--ipc-path', ipc_path,
'--base-path', datadir,
'--password', passwordfile,
'--ipc-apis', 'all',
'--no-jsonrpc',
'--no-ws',
'--tracing', 'on',
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/parity/test_parity_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def parity_command_arguments(
'--password', passwordfile,
'--ws-port', ws_port,
'--ws-origins', '*',
'--ws-apis', 'all',
'--no-ipc',
'--no-jsonrpc',
)
Expand All @@ -64,6 +65,7 @@ def parity_import_blocks_command(parity_binary, ws_port, datadir, passwordfile):
'--password', passwordfile,
'--ws-port', str(ws_port),
'--ws-origins', '*',
'--ws-apis', 'all',
'--no-ipc',
'--no-jsonrpc',
'--tracing', 'on',
Expand Down
24 changes: 16 additions & 8 deletions tests/integration/test_ethereum_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from web3 import Web3
from web3._utils.module_testing import (
EthModuleTest,
GoEthereumPersonalModuleTest,
NetModuleTest,
PersonalModuleTest,
VersionModuleTest,
Web3ModuleTest,
)
Expand Down Expand Up @@ -146,7 +146,7 @@ def unlockable_account_pw(web3):

@pytest.fixture(scope='module')
def unlockable_account(web3, unlockable_account_pw):
account = web3.personal.importRawKey(UNLOCKABLE_PRIVATE_KEY, unlockable_account_pw)
account = web3.geth.personal.importRawKey(UNLOCKABLE_PRIVATE_KEY, unlockable_account_pw)
web3.eth.sendTransaction({
'from': web3.eth.coinbase,
'to': account,
Expand All @@ -157,9 +157,9 @@ def unlockable_account(web3, unlockable_account_pw):

@pytest.fixture
def unlocked_account(web3, unlockable_account, unlockable_account_pw):
web3.personal.unlockAccount(unlockable_account, unlockable_account_pw)
web3.geth.personal.unlockAccount(unlockable_account, unlockable_account_pw)
yield unlockable_account
web3.personal.lockAccount(unlockable_account)
web3.geth.personal.lockAccount(unlockable_account)


@pytest.fixture()
Expand All @@ -169,9 +169,9 @@ def unlockable_account_dual_type(unlockable_account, address_conversion_func):

@pytest.fixture
def unlocked_account_dual_type(web3, unlockable_account_dual_type, unlockable_account_pw):
web3.personal.unlockAccount(unlockable_account_dual_type, unlockable_account_pw)
web3.geth.personal.unlockAccount(unlockable_account_dual_type, unlockable_account_pw)
yield unlockable_account_dual_type
web3.personal.lockAccount(unlockable_account_dual_type)
web3.geth.personal.lockAccount(unlockable_account_dual_type)


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -297,8 +297,16 @@ class TestEthereumTesterNetModule(NetModuleTest):
pass


class TestEthereumTesterPersonalModule(PersonalModuleTest):
# Use web3.geth.personal namespace for testing eth-tester
class TestEthereumTesterPersonalModule(GoEthereumPersonalModuleTest):
test_personal_sign_and_ecrecover = not_implemented(
PersonalModuleTest.test_personal_sign_and_ecrecover,
GoEthereumPersonalModuleTest.test_personal_sign_and_ecrecover,
ValueError,
)

# Test overridden here since eth-tester returns False rather than None for failed unlock
def test_personal_unlockAccount_failure(self,
web3,
unlockable_account_dual_type):
result = web3.geth.personal.unlockAccount(unlockable_account_dual_type, 'bad-password')
assert result is False
3 changes: 2 additions & 1 deletion web3/_utils/module_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
NetModuleTest,
)
from .personal_module import ( # noqa: F401
PersonalModuleTest,
GoEthereumPersonalModuleTest,
ParityPersonalModuleTest,
)
from .version_module import ( # noqa: F401
VersionModuleTest,
Expand Down
Loading

0 comments on commit 92534c6

Please sign in to comment.