From 903f65936e66f3dae23e04b07d1ff434fbe1015c Mon Sep 17 00:00:00 2001 From: lash Date: Thu, 20 Jan 2022 21:39:01 +0000 Subject: [PATCH 1/6] Upgrade rlp --- CHANGELOG | 5 +++++ requirements.txt | 3 ++- setup.py | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 CHANGELOG diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..ff8c227 --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,5 @@ +* 0.5.3 + - Upgrade RLP to 3.0.0 (eliminates really annoying cytoolz warning on stdout) +--- + +changelog before 0.5.3 will be written later, sorry diff --git a/requirements.txt b/requirements.txt index c082677..9522511 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ cryptography==3.2.1 pysha3==1.0.2 -rlp==2.0.1 +#rlp==2.0.1 +rlp==3.0.0 json-rpc==1.13.0 confini~=0.5.1 coincurve==15.0.0 diff --git a/setup.py b/setup.py index 2a82afe..6ca3ce9 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ f.close() setup( name="funga-eth", - version="0.5.2", + version="0.5.3", description="Ethereum implementation of the funga keystore and signer", author="Louis Holbrook", author_email="dev@holbrook.no", -- 2.40.1 From b9c6db414b928fa580a83a82d9e741c30f787c49 Mon Sep 17 00:00:00 2001 From: idaapayo Date: Fri, 21 Jan 2022 11:13:35 +0300 Subject: [PATCH 2/6] making review changes --- tests/test_cli.py | 53 ++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index d7a1898..27a8aa6 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,7 +4,7 @@ import logging import os # external imports -from hexathon import strip_0x +from hexathon import strip_0x, add_0x # local imports from funga.eth.signer import EIP155Signer @@ -18,30 +18,30 @@ logg = logging.getLogger() script_dir = os.path.dirname(os.path.realpath(__file__)) data_dir = os.path.join(script_dir, 'testdata') + class TestCli(unittest.TestCase): def setUp(self): - #pk = bytes.fromhex('5087503f0a9cc35b38665955eb830c63f778453dd11b8fa5bd04bc41fd2cc6d6') - #pk_getter = pkGetter(pk) + # pk = bytes.fromhex('5087503f0a9cc35b38665955eb830c63f778453dd11b8fa5bd04bc41fd2cc6d6') + # pk_getter = pkGetter(pk) self.keystore = DictKeystore() SignRequestHandler.keystore = self.keystore self.signer = EIP155Signer(self.keystore) SignRequestHandler.signer = self.signer self.handler = SignRequestHandler() - def test_new_account(self): q = { - 'id': 0, - 'method': 'personal_newAccount', - 'params': [''], - } + 'id': 0, + 'method': 'personal_newAccount', + 'params': [''], + } (rpc_id, result) = self.handler.process_input(q) self.assertTrue(self.keystore.get(result)) - def test_sign_tx(self): - keystore_file = os.path.join(data_dir, 'UTC--2021-01-08T18-37-01.187235289Z--00a329c0648769a73afac7f9381e08fb43dbea72') + keystore_file = os.path.join(data_dir, + 'UTC--2021-01-08T18-37-01.187235289Z--00a329c0648769a73afac7f9381e08fb43dbea72') sender = self.keystore.import_keystore_file(keystore_file) tx_hexs = { 'nonce': '0x', @@ -62,26 +62,37 @@ class TestCli(unittest.TestCase): # eth_signTransaction wraps personal_signTransaction, so here we test both already q = { - 'id': 0, - 'method': 'eth_signTransaction', - 'params': [tx_s], - } + 'id': 0, + 'method': 'eth_signTransaction', + 'params': [tx_s], + } (rpc_id, result) = self.handler.process_input(q) logg.debug('result {}'.format(result)) - self.assertEqual(strip_0x(result), 'f86c2a8504a817c8008252089435353535353535353535353535353535353535358203e884deadbeef82466aa0b7c1bbf52f736ada30fe253c7484176f44d6fd097a9720dc85ae5bbc7f060e54a07afee2563b0cf6d00333df51cc62b0d13c63108b2bce54ce2ad24e26ce7b4f25') + self.assertEqual(strip_0x(result), + 'f86c2a8504a817c8008252089435353535353535353535353535353535353535358203e884deadbeef82466aa0b7c1bbf52f736ada30fe253c7484176f44d6fd097a9720dc85ae5bbc7f060e54a07afee2563b0cf6d00333df51cc62b0d13c63108b2bce54ce2ad24e26ce7b4f25') + + + def pbkdf2_test(self): + keystore_filepath = os.path.join(data_dir, 'foo2.json') + address_hex = self.keystore.import_keystore_file(keystore_filepath) + logg.debug('getting {}'.format(address_hex)) + pk = self.keystore.get(strip_0x(address_hex), '') + self.assertEqual(pk.lower(), 'a1De08A738F0bD3B261dC41d7E2102599bf648CA') def test_sign_msg(self): - keystore_file = os.path.join(data_dir, 'UTC--2021-01-08T18-37-01.187235289Z--00a329c0648769a73afac7f9381e08fb43dbea72') + keystore_file = os.path.join(data_dir, + 'UTC--2021-01-08T18-37-01.187235289Z--00a329c0648769a73afac7f9381e08fb43dbea72') sender = self.keystore.import_keystore_file(keystore_file) q = { - 'id': 0, - 'method': 'eth_sign', - 'params': [sender, '0xdeadbeef'], - } + 'id': 0, + 'method': 'eth_sign', + 'params': [sender, '0xdeadbeef'], + } (rpc_id, result) = self.handler.process_input(q) logg.debug('result msg {}'.format(result)) - self.assertEqual(strip_0x(result), '50320dda75190a121b7b5979de66edadafd02bdfbe4f6d49552e79c01410d2464aae35e385c0e5b61663ff7b44ef65fa0ac7ad8a57472cf405db399b9dba3e1600') + self.assertEqual(strip_0x(result), + '50320dda75190a121b7b5979de66edadafd02bdfbe4f6d49552e79c01410d2464aae35e385c0e5b61663ff7b44ef65fa0ac7ad8a57472cf405db399b9dba3e1600') if __name__ == '__main__': -- 2.40.1 From b20853312d53c68fa8292fe0d04624149fcc6d6c Mon Sep 17 00:00:00 2001 From: idaapayo Date: Mon, 24 Jan 2022 13:23:12 +0300 Subject: [PATCH 3/6] review changes with tests --- funga/eth/keystore/keyfile.py | 24 ++++++++++-------------- funga/eth/runnable/keyfile.py | 9 +++++++-- tests/test_cli.py | 10 +++++++++- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/funga/eth/keystore/keyfile.py b/funga/eth/keystore/keyfile.py index 0d1664a..a4b0f58 100644 --- a/funga/eth/keystore/keyfile.py +++ b/funga/eth/keystore/keyfile.py @@ -28,7 +28,7 @@ hash_keywords = [ 'pbkdf2' ] -default_kdfparams = { +default_scrypt_kdfparams = { 'dklen': 32, 'n': 1 << 18, 'p': 1, @@ -53,7 +53,7 @@ def to_mac(mac_key, ciphertext_bytes): class Hashes: @staticmethod - def from_scrypt(kdfparams=default_kdfparams, passphrase=''): + def from_scrypt(kdfparams=default_scrypt_kdfparams, passphrase=''): dklen = int(kdfparams['dklen']) n = int(kdfparams['n']) p = int(kdfparams['p']) @@ -65,20 +65,16 @@ class Hashes: @staticmethod def from_pbkdf2(kdfparams=pbkdf2_kdfparams, passphrase=''): - hashname = kdfparams['prf'] - pwd = passphrase.encode('utf-8') - salt = bytes.fromhex(kdfparams['salt']) - itr = int(kdfparams['c']) - dklen = int(kdfparams['dklen']) + if kdfparams['prf'] == 'hmac-sha256': + kdfparams['prf'].replace('sha256') derived_key = hashlib.pbkdf2_hmac( - hash_name=hashname, - password=pwd, - salt=salt, - iterations=itr, - dklen=dklen + hash_name=kdfparams['prf'], + password=passphrase.encode('utf-8'), + salt=bytes.fromhex(kdfparams['salt']), + iterations=int(kdfparams['c']), + dklen=int(kdfparams['dklen']) ) - return derived_key @@ -106,7 +102,7 @@ def to_dict(private_key_bytes, kdf :str, passphrase=''): if kdf == 'scrypt': encryption_key = Hashes.from_scrypt(passphrase=passphrase) - kdfparams = default_kdfparams + kdfparams = default_scrypt_kdfparams elif kdf == 'pbkdf2': encryption_key = Hashes.from_pbkdf2(passphrase=passphrase) diff --git a/funga/eth/runnable/keyfile.py b/funga/eth/runnable/keyfile.py index c708123..b1f2a39 100644 --- a/funga/eth/runnable/keyfile.py +++ b/funga/eth/runnable/keyfile.py @@ -16,10 +16,15 @@ from funga.eth.keystore.keyfile import ( from_file, to_dict, ) -# from testkeyfile import ( +# from pathlib import Path +# import sys +# path_root = Path(__file__).parents[1] +# sys.path.append(str(path_root)) +# from keystore.testkeyfile import ( # from_file, # to_dict # ) + from funga.eth.encoding import ( private_key_to_address, private_key_from_bytes, @@ -81,7 +86,7 @@ def main(): else: pk_bytes = os.urandom(32) pk = coincurve.PrivateKey(secret=pk_bytes) - o = to_dict(pk_bytes, 'pbkdf2', passphrase) + o = to_dict(pk_bytes, 'scrypt', passphrase) r = json.dumps(o) print(r) diff --git a/tests/test_cli.py b/tests/test_cli.py index 27a8aa6..978835b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -5,10 +5,18 @@ import os # external imports from hexathon import strip_0x, add_0x +from pathlib import Path + +import sys +path_root = Path('/home/vincent/ida/grassroots/funga-eth/funga/eth/keystore') +sys.path.append(str(path_root)) +print(sys.path) # local imports from funga.eth.signer import EIP155Signer from funga.eth.keystore.dict import DictKeystore + +# import dicttest as d from funga.eth.cli.handle import SignRequestHandler from funga.eth.transaction import EIP155Transaction @@ -73,7 +81,7 @@ class TestCli(unittest.TestCase): 'f86c2a8504a817c8008252089435353535353535353535353535353535353535358203e884deadbeef82466aa0b7c1bbf52f736ada30fe253c7484176f44d6fd097a9720dc85ae5bbc7f060e54a07afee2563b0cf6d00333df51cc62b0d13c63108b2bce54ce2ad24e26ce7b4f25') - def pbkdf2_test(self): + def test_pbkdf2(self): keystore_filepath = os.path.join(data_dir, 'foo2.json') address_hex = self.keystore.import_keystore_file(keystore_filepath) logg.debug('getting {}'.format(address_hex)) -- 2.40.1 From 1c0047398ad378f729a66960edbbdfc597d82fb4 Mon Sep 17 00:00:00 2001 From: idaapayo Date: Mon, 24 Jan 2022 15:12:38 +0300 Subject: [PATCH 4/6] making final review changes --- funga/eth/keystore/keyfile.py | 4 +- funga/eth/runnable/keyfile.py | 9 +-- tests/test_cli.py | 6 -- tests/test_pbkdf2.py | 59 +++++++++++++++++++ ...-04Z--cc47ad90-71a0-7fbe-0224-63326e27263a | 1 + tests/testdata/foo2.json | 1 - 6 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 tests/test_pbkdf2.py create mode 100644 tests/testdata/UTC--2022-01-24T10-34-04Z--cc47ad90-71a0-7fbe-0224-63326e27263a delete mode 100644 tests/testdata/foo2.json diff --git a/funga/eth/keystore/keyfile.py b/funga/eth/keystore/keyfile.py index a4b0f58..6ad5b6b 100644 --- a/funga/eth/keystore/keyfile.py +++ b/funga/eth/keystore/keyfile.py @@ -66,10 +66,10 @@ class Hashes: @staticmethod def from_pbkdf2(kdfparams=pbkdf2_kdfparams, passphrase=''): if kdfparams['prf'] == 'hmac-sha256': - kdfparams['prf'].replace('sha256') + kdfparams['prf'].replace('hmac-sha256','sha256') derived_key = hashlib.pbkdf2_hmac( - hash_name=kdfparams['prf'], + hash_name='sha256', password=passphrase.encode('utf-8'), salt=bytes.fromhex(kdfparams['salt']), iterations=int(kdfparams['c']), diff --git a/funga/eth/runnable/keyfile.py b/funga/eth/runnable/keyfile.py index b1f2a39..ca82e04 100644 --- a/funga/eth/runnable/keyfile.py +++ b/funga/eth/runnable/keyfile.py @@ -16,14 +16,7 @@ from funga.eth.keystore.keyfile import ( from_file, to_dict, ) -# from pathlib import Path -# import sys -# path_root = Path(__file__).parents[1] -# sys.path.append(str(path_root)) -# from keystore.testkeyfile import ( -# from_file, -# to_dict -# ) + from funga.eth.encoding import ( private_key_to_address, diff --git a/tests/test_cli.py b/tests/test_cli.py index 978835b..146302e 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -81,12 +81,6 @@ class TestCli(unittest.TestCase): 'f86c2a8504a817c8008252089435353535353535353535353535353535353535358203e884deadbeef82466aa0b7c1bbf52f736ada30fe253c7484176f44d6fd097a9720dc85ae5bbc7f060e54a07afee2563b0cf6d00333df51cc62b0d13c63108b2bce54ce2ad24e26ce7b4f25') - def test_pbkdf2(self): - keystore_filepath = os.path.join(data_dir, 'foo2.json') - address_hex = self.keystore.import_keystore_file(keystore_filepath) - logg.debug('getting {}'.format(address_hex)) - pk = self.keystore.get(strip_0x(address_hex), '') - self.assertEqual(pk.lower(), 'a1De08A738F0bD3B261dC41d7E2102599bf648CA') def test_sign_msg(self): keystore_file = os.path.join(data_dir, diff --git a/tests/test_pbkdf2.py b/tests/test_pbkdf2.py new file mode 100644 index 0000000..da7741a --- /dev/null +++ b/tests/test_pbkdf2.py @@ -0,0 +1,59 @@ +#!/usr/bin/python + +# standard imports +import unittest +import logging +import base64 +import os + +# external imports +from hexathon import ( + strip_0x, + add_0x, +) + +# local imports +from funga.error import UnknownAccountError +from funga.eth.keystore.dict import DictKeystore +from funga.eth.signer import EIP155Signer + +logging.basicConfig(level=logging.DEBUG) +logg = logging.getLogger() + +script_dir = os.path.realpath(os.path.dirname(__file__)) + + +class TestDict(unittest.TestCase): + address_hex = None + db = None + + def setUp(self): + self.db = DictKeystore() + + keystore_filepath = os.path.join(script_dir, 'testdata', + 'UTC--2022-01-24T10-34-04Z--cc47ad90-71a0-7fbe-0224-63326e27263a') + + address_hex = self.db.import_keystore_file(keystore_filepath, 'test') + self.address_hex = add_0x(address_hex) + + def tearDown(self): + pass + + def test_get_key(self): + logg.debug('getting {}'.format(strip_0x(self.address_hex))) + pk = self.db.get(strip_0x(self.address_hex), '') + + self.assertEqual(self.address_hex.lower(), '0xb8df77e1b4fa142e83bf9706f66fd76ad2a564f8') + + bogus_account = os.urandom(20).hex() + with self.assertRaises(UnknownAccountError): + self.db.get(bogus_account, '') + + def test_sign_message(self): + s = EIP155Signer(self.db) + z = s.sign_ethereum_message(strip_0x(self.address_hex), b'foo') + logg.debug('zzz {}'.format(str(z))) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/testdata/UTC--2022-01-24T10-34-04Z--cc47ad90-71a0-7fbe-0224-63326e27263a b/tests/testdata/UTC--2022-01-24T10-34-04Z--cc47ad90-71a0-7fbe-0224-63326e27263a new file mode 100644 index 0000000..641fedd --- /dev/null +++ b/tests/testdata/UTC--2022-01-24T10-34-04Z--cc47ad90-71a0-7fbe-0224-63326e27263a @@ -0,0 +1 @@ +{"id":"cc47ad90-71a0-7fbe-0224-63326e27263a","version":3,"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"7bff67c888a9878a88e8548a4598322d"},"ciphertext":"0cb0e3c69d224d0a645f2784b64f507e5aecdc7bb8a7ea31963d25e6b8020ccf","kdf":"pbkdf2","kdfparams":{"c":10240,"dklen":32,"prf":"hmac-sha256","salt":"02f8b51b07a66a357c2d812952e6bee70fccc2e6a55e7cbd5c22d97d32fa8873"},"mac":"bb45aaabdb9fbbbde89631444ac39f8d76107381f16591799664274fd5d8c5bb"},"address":"b8df77e1b4fa142e83bf9706f66fd76ad2a564f8","name":"","meta":"{}"} \ No newline at end of file diff --git a/tests/testdata/foo2.json b/tests/testdata/foo2.json deleted file mode 100644 index 0749889..0000000 --- a/tests/testdata/foo2.json +++ /dev/null @@ -1 +0,0 @@ -{"address": "a1De08A738F0bD3B261dC41d7E2102599bf648CA", "version": 3, "crypto": {"cipher": "aes-128-ctr", "ciphertext": "2240149943557906e6ee9bcb864d6148dce0c0d8245d1da8d466dcb39edcde6a", "cipherparams": {"iv": "a0c366fdfe86f21a4168b4f12059e7fa"}, "kdf": "pbkdf2", "kdfparams": {"c": 100000, "dklen": 32, "prf": "sha256", "salt": "73cb66c8cf0e60e36f7ec835a057638a17ed315cebb3b691574c32cfed827b6f"}, "mac": "6760ad0841da7976a41f134e021be450baa453efdec8fe5ba7790cfa79d0c14b"}, "id": "22e597ea-777e-11ec-be42-a8a7956d3851"} -- 2.40.1 From 23a94c3ba1c0730b65496906c710edd19b6d6386 Mon Sep 17 00:00:00 2001 From: idaapayo Date: Mon, 24 Jan 2022 20:07:22 +0300 Subject: [PATCH 5/6] defaulting to scrypt in to_dict fun --- funga/eth/keystore/keyfile.py | 2 +- funga/eth/runnable/keyfile.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/funga/eth/keystore/keyfile.py b/funga/eth/keystore/keyfile.py index 6ad5b6b..6575795 100644 --- a/funga/eth/keystore/keyfile.py +++ b/funga/eth/keystore/keyfile.py @@ -97,7 +97,7 @@ class Ciphers: return ciphertext -def to_dict(private_key_bytes, kdf :str, passphrase=''): +def to_dict(private_key_bytes, kdf='scrypt', passphrase=''): private_key = coincurve.PrivateKey(secret=private_key_bytes) if kdf == 'scrypt': diff --git a/funga/eth/runnable/keyfile.py b/funga/eth/runnable/keyfile.py index ca82e04..738524e 100644 --- a/funga/eth/runnable/keyfile.py +++ b/funga/eth/runnable/keyfile.py @@ -79,7 +79,7 @@ def main(): else: pk_bytes = os.urandom(32) pk = coincurve.PrivateKey(secret=pk_bytes) - o = to_dict(pk_bytes, 'scrypt', passphrase) + o = to_dict(pk_bytes, passphrase) r = json.dumps(o) print(r) -- 2.40.1 From 4a4f76b19c94d0a769258c68ef47eba7e2c70b34 Mon Sep 17 00:00:00 2001 From: idaapayo Date: Mon, 24 Jan 2022 20:12:34 +0300 Subject: [PATCH 6/6] remove unused import and renaming pbkdf2 default params --- funga/eth/keystore/keyfile.py | 4 ++-- tests/test_cli.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/funga/eth/keystore/keyfile.py b/funga/eth/keystore/keyfile.py index 6575795..2385fc6 100644 --- a/funga/eth/keystore/keyfile.py +++ b/funga/eth/keystore/keyfile.py @@ -36,7 +36,7 @@ default_scrypt_kdfparams = { 'salt': os.urandom(32).hex(), } -pbkdf2_kdfparams = { +default_pbkdf2_kdfparams = { 'c': 100000, 'dklen': 32, 'prf': 'sha256', @@ -64,7 +64,7 @@ class Hashes: dklen=dklen) @staticmethod - def from_pbkdf2(kdfparams=pbkdf2_kdfparams, passphrase=''): + def from_pbkdf2(kdfparams=default_pbkdf2_kdfparams, passphrase=''): if kdfparams['prf'] == 'hmac-sha256': kdfparams['prf'].replace('hmac-sha256','sha256') diff --git a/tests/test_cli.py b/tests/test_cli.py index 146302e..8653064 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,7 +4,7 @@ import logging import os # external imports -from hexathon import strip_0x, add_0x +from hexathon import strip_0x from pathlib import Path import sys @@ -16,7 +16,7 @@ print(sys.path) from funga.eth.signer import EIP155Signer from funga.eth.keystore.dict import DictKeystore -# import dicttest as d + from funga.eth.cli.handle import SignRequestHandler from funga.eth.transaction import EIP155Transaction -- 2.40.1