From 6c61c20f621af45564865e1ef267f12b282e5c99 Mon Sep 17 00:00:00 2001 From: Andre Miras Date: Sun, 8 Apr 2018 20:48:04 +0200 Subject: [PATCH] Use the new eth-keyfile module, fixes #292 Use the new eth-keyfile module to encrypt/decrypt keyfile. This makes it way faster to create and unlock accounts. For instance with 100K PBKDF2 iteration rather than 100. Running `pyethapp/tests/test_accounts.py` was outputing: ``` 9 passed in 112.07 seconds ``` After the pull request it's now: ``` 9 passed in 2.41 seconds ``` --- pyethapp/accounts.py | 7 +++++-- pyethapp/tests/test_accounts.py | 7 +++++-- requirements.txt | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pyethapp/accounts.py b/pyethapp/accounts.py index c8789b4f..0adf2ec6 100644 --- a/pyethapp/accounts.py +++ b/pyethapp/accounts.py @@ -2,6 +2,7 @@ from builtins import object import json import os +import eth_keyfile from random import SystemRandom import shutil from uuid import UUID @@ -74,7 +75,7 @@ def new(cls, password, key=None, uuid=None, path=None): if not is_string(password): password = to_string(password) - keystore = keys.make_keystore_json(key, password) + keystore = eth_keyfile.create_keyfile_json(key, password) keystore['id'] = uuid return Account(keystore, password, path) @@ -121,7 +122,9 @@ def unlock(self, password): account is locked) """ if self.locked: - self._privkey = keys.decode_keystore_json(self.keystore, password) + if not is_string(password): + password = to_string(password) + self._privkey = eth_keyfile.decode_keyfile_json(self.keystore, password) self.locked = False self.address # get address such that it stays accessible after a subsequent lock diff --git a/pyethapp/tests/test_accounts.py b/pyethapp/tests/test_accounts.py index 3c0f8a3f..561f04a4 100644 --- a/pyethapp/tests/test_accounts.py +++ b/pyethapp/tests/test_accounts.py @@ -3,7 +3,7 @@ from past.utils import old_div import json from uuid import uuid4 -import ethereum.tools.keys +import eth_keyfile.keyfile from ethereum.tools.keys import privtoaddr from ethereum.transactions import Transaction from ethereum.utils import ( @@ -14,7 +14,10 @@ import pytest # reduce key derivation iterations -ethereum.tools.keys.PBKDF2_CONSTANTS['c'] = 100 +def get_default_work_factor_for_kdf(kdf): + return 100 +eth_keyfile.keyfile.get_default_work_factor_for_kdf = \ + get_default_work_factor_for_kdf @pytest.fixture() diff --git a/requirements.txt b/requirements.txt index 17065682..2f638c9b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,3 +22,4 @@ tinyrpc[gevent,httpclient,jsonext,websocket,wsgi] pycryptodome==3.4.6 future https://github.com/ethereum/serpent/tarball/develop +eth-keyfile