From 646dbf5f79ab75e1318ae312d032732988c3ad57 Mon Sep 17 00:00:00 2001 From: nolash Date: Sat, 8 Aug 2020 11:33:15 +0200 Subject: [PATCH] Fix setup.py to include all modules --- crypto_dev_signer/__init__.py | 4 + {src => crypto_dev_signer}/common.py | 0 crypto_dev_signer/eth/__init__.py | 0 crypto_dev_signer/eth/signer/__init__.py | 1 + .../eth}/signer/defaultsigner.py | 0 {src => crypto_dev_signer/eth}/transaction.py | 2 +- .../eth}/web3ext/__init__.py | 2 +- .../eth}/web3ext/middleware.py | 0 crypto_dev_signer/keystore/__init__.py | 1 + .../keystore/interface.py | 0 .../keystore/postgres.py | 4 +- scripts/crypto-dev-daemon | 159 +++++++++++++++++- scripts/crypto-dev-daemon.py | 159 +----------------- setup.py | 12 +- src/keystore/__init__.py | 1 - src/signer/__init__.py | 1 - test/test_keystore.py | 2 +- test/test_sign.py | 4 +- 18 files changed, 181 insertions(+), 171 deletions(-) create mode 100644 crypto_dev_signer/__init__.py rename {src => crypto_dev_signer}/common.py (100%) create mode 100644 crypto_dev_signer/eth/__init__.py create mode 100644 crypto_dev_signer/eth/signer/__init__.py rename {src => crypto_dev_signer/eth}/signer/defaultsigner.py (100%) rename {src => crypto_dev_signer/eth}/transaction.py (96%) rename {src => crypto_dev_signer/eth}/web3ext/__init__.py (94%) rename {src => crypto_dev_signer/eth}/web3ext/middleware.py (100%) create mode 100644 crypto_dev_signer/keystore/__init__.py rename {src => crypto_dev_signer}/keystore/interface.py (100%) rename {src => crypto_dev_signer}/keystore/postgres.py (96%) mode change 120000 => 100755 scripts/crypto-dev-daemon mode change 100755 => 120000 scripts/crypto-dev-daemon.py delete mode 100644 src/keystore/__init__.py delete mode 100644 src/signer/__init__.py diff --git a/crypto_dev_signer/__init__.py b/crypto_dev_signer/__init__.py new file mode 100644 index 0000000..5efe4c8 --- /dev/null +++ b/crypto_dev_signer/__init__.py @@ -0,0 +1,4 @@ +import crypto_dev_signer.eth.signer +import crypto_dev_signer.eth.web3ext +import crypto_dev_signer.eth.transaction +import crypto_dev_signer.common diff --git a/src/common.py b/crypto_dev_signer/common.py similarity index 100% rename from src/common.py rename to crypto_dev_signer/common.py diff --git a/crypto_dev_signer/eth/__init__.py b/crypto_dev_signer/eth/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/crypto_dev_signer/eth/signer/__init__.py b/crypto_dev_signer/eth/signer/__init__.py new file mode 100644 index 0000000..cc8c0e3 --- /dev/null +++ b/crypto_dev_signer/eth/signer/__init__.py @@ -0,0 +1 @@ +from crypto_dev_signer.eth.signer.defaultsigner import ReferenceSigner, Signer diff --git a/src/signer/defaultsigner.py b/crypto_dev_signer/eth/signer/defaultsigner.py similarity index 100% rename from src/signer/defaultsigner.py rename to crypto_dev_signer/eth/signer/defaultsigner.py diff --git a/src/transaction.py b/crypto_dev_signer/eth/transaction.py similarity index 96% rename from src/transaction.py rename to crypto_dev_signer/eth/transaction.py index a6dccd7..8c931ec 100644 --- a/src/transaction.py +++ b/crypto_dev_signer/eth/transaction.py @@ -6,7 +6,7 @@ import binascii from rlp import encode as rlp_encode # local imports -from common import strip_hex_prefix, add_hex_prefix +from crypto_dev_signer.common import strip_hex_prefix, add_hex_prefix logg = logging.getLogger(__name__) diff --git a/src/web3ext/__init__.py b/crypto_dev_signer/eth/web3ext/__init__.py similarity index 94% rename from src/web3ext/__init__.py rename to crypto_dev_signer/eth/web3ext/__init__.py index ad4cf40..1b07e52 100644 --- a/src/web3ext/__init__.py +++ b/crypto_dev_signer/eth/web3ext/__init__.py @@ -2,7 +2,7 @@ import re from web3 import Web3 as Web3super from web3 import WebsocketProvider, HTTPProvider -from web3ext.middleware import PlatformMiddleware +from .middleware import PlatformMiddleware re_websocket = re.compile('^wss?://') re_http = re.compile('^https?://') diff --git a/src/web3ext/middleware.py b/crypto_dev_signer/eth/web3ext/middleware.py similarity index 100% rename from src/web3ext/middleware.py rename to crypto_dev_signer/eth/web3ext/middleware.py diff --git a/crypto_dev_signer/keystore/__init__.py b/crypto_dev_signer/keystore/__init__.py new file mode 100644 index 0000000..1032f3f --- /dev/null +++ b/crypto_dev_signer/keystore/__init__.py @@ -0,0 +1 @@ +from .postgres import ReferenceKeystore diff --git a/src/keystore/interface.py b/crypto_dev_signer/keystore/interface.py similarity index 100% rename from src/keystore/interface.py rename to crypto_dev_signer/keystore/interface.py diff --git a/src/keystore/postgres.py b/crypto_dev_signer/keystore/postgres.py similarity index 96% rename from src/keystore/postgres.py rename to crypto_dev_signer/keystore/postgres.py index dc52513..2e1854f 100644 --- a/src/keystore/postgres.py +++ b/crypto_dev_signer/keystore/postgres.py @@ -12,8 +12,8 @@ from eth_keys.backends import NativeECCBackend import sha3 # local imports -from common import strip_hex_prefix -from keystore.interface import Keystore +from crypto_dev_signer.common import strip_hex_prefix +from .interface import Keystore keyapi = KeyAPI(NativeECCBackend) diff --git a/scripts/crypto-dev-daemon b/scripts/crypto-dev-daemon deleted file mode 120000 index bb57e37..0000000 --- a/scripts/crypto-dev-daemon +++ /dev/null @@ -1 +0,0 @@ -scripts/crypto-dev-daemon.py \ No newline at end of file diff --git a/scripts/crypto-dev-daemon b/scripts/crypto-dev-daemon new file mode 100755 index 0000000..5f79456 --- /dev/null +++ b/scripts/crypto-dev-daemon @@ -0,0 +1,158 @@ +#!/usr/bin/python3 + +import socket +import json +import logging +import sys +import os + +from jsonrpc.exceptions import * + +from crypto_dev_signer.eth.signer import ReferenceSigner +from crypto_dev_signer.eth.transaction import EIP155Transaction +from crypto_dev_signer.keystore import ReferenceKeystore + +logging.basicConfig(level=logging.DEBUG) +logg = logging.getLogger() + +db = None +signer = None +chainId = 8995 + + +def personal_new_account(p): + password = p + if p.__class__.__name__ != 'str': + if p.__class__.__name__ != 'list': + e = JSONRPCInvalidParams() + e.data = 'parameter must be list containing one string' + raise ValueError(e) + logg.error('foo {}'.format(p)) + if len(p) != 1: + e = JSONRPCInvalidParams() + e.data = 'parameter must be list containing one string' + raise ValueError(e) + if p[0].__class__.__name__ != 'str': + e = JSONRPCInvalidParams() + e.data = 'parameter must be list containing one string' + raise ValueError(e) + password = p[0] + + r = db.new(password) + + return r + + +def personal_sign_transaction(p): + t = EIP155Transaction(p[0], 0, 8995) + z = signer.signTransaction(t, p[1]) + raw_signed_tx = t.rlp_serialize() + o = { + 'raw': '0x' + raw_signed_tx.hex(), + 'tx': t.serialize(), + } + return o + + +# TODO: temporary workaround for platform, since personal_signTransaction is missing from web3.py +def eth_signTransaction(tx): + return personal_sign_transaction([tx, '']) + + +methods = { + 'personal_newAccount': personal_new_account, + 'personal_signTransaction': personal_sign_transaction, + 'eth_signTransaction': eth_signTransaction, + } + + +def jsonrpc_error(rpc_id, err): + return { + 'json-rpc': '2.0', + 'id': rpc_id, + 'error': { + 'code': err.CODE, + 'message': err.MESSAGE, + }, + } + + +def jsonrpc_ok(rpc_id, response): + return { + 'json-rpc': '2.0', + 'id': rpc_id, + 'result': response, + } + + +def is_valid_json(j): + if j.get('id') == 'None': + raise ValueError('id missing') + return True + + +def process_input(j): + rpc_id = j['id'] + m = j['method'] + p = j['params'] + return (rpc_id, methods[m](p)) + + +def start_server(): + try: + os.unlink('/tmp/foo.ipc') + except FileNotFoundError: + pass + s = socket.socket(family = socket.AF_UNIX, type = socket.SOCK_STREAM) + s.bind('/tmp/foo.ipc') + s.listen(10) + while True: + (csock, caddr) = s.accept() + d = csock.recv(4096) + j = None + try: + j = json.loads(d) + is_valid_json(j) + logg.debug('{}'.format(d.decode('utf-8'))) + except: + csock.send(json.dumps(jsonrpc_error(None, JSONRPCParseError)).encode('utf-8')) + csock.close() + continue + + try: + (rpc_id, r) = process_input(j) + csock.send(json.dumps(jsonrpc_ok(rpc_id, r)).encode('utf-8')) + except: + # TODO: handle cases to give better error context to caller + csock.send(json.dumps(jsonrpc_error(j['id'], JSONRPCServerError)).encode('utf-8')) + + csock.close() + s.close() + + os.unlink('/tmp/foo.ipc') + + +def init(): + global db, signer + secret_hex = os.environ.get('SIGNER_SECRET') + secret = bytes.fromhex(secret_hex) + kw = { + 'symmetric_key': secret, + } + db = ReferenceKeystore('cic_signer', **kw) + signer = ReferenceSigner(db) + + +if __name__ == '__main__': + init() + arg = None + try: + arg = json.loads(sys.argv[1]) + except: + logg.info('no json rpc command detected, starting socket server') + start_server() + sys.exit(0) + + (rpc_id, response) = process_input(arg) + r = jsonrpc_ok(rpc_id, response) + sys.stdout.write(json.dumps(r)) diff --git a/scripts/crypto-dev-daemon.py b/scripts/crypto-dev-daemon.py deleted file mode 100755 index eae8590..0000000 --- a/scripts/crypto-dev-daemon.py +++ /dev/null @@ -1,158 +0,0 @@ -#!/usr/bin/python3 - -import socket -import json -import logging -import sys -import os - -from jsonrpc.exceptions import * - -from signer import ReferenceSigner -from keystore import ReferenceKeystore -from transaction import EIP155Transaction - -logging.basicConfig(level=logging.DEBUG) -logg = logging.getLogger() - -db = None -signer = None -chainId = 8995 - - -def personal_new_account(p): - password = p - if p.__class__.__name__ != 'str': - if p.__class__.__name__ != 'list': - e = JSONRPCInvalidParams() - e.data = 'parameter must be list containing one string' - raise ValueError(e) - logg.error('foo {}'.format(p)) - if len(p) != 1: - e = JSONRPCInvalidParams() - e.data = 'parameter must be list containing one string' - raise ValueError(e) - if p[0].__class__.__name__ != 'str': - e = JSONRPCInvalidParams() - e.data = 'parameter must be list containing one string' - raise ValueError(e) - password = p[0] - - r = db.new(password) - - return r - - -def personal_sign_transaction(p): - t = EIP155Transaction(p[0], 0, 8995) - z = signer.signTransaction(t, p[1]) - raw_signed_tx = t.rlp_serialize() - o = { - 'raw': '0x' + raw_signed_tx.hex(), - 'tx': t.serialize(), - } - return o - - -# TODO: temporary workaround for platform, since personal_signTransaction is missing from web3.py -def eth_signTransaction(tx): - return personal_sign_transaction([tx, '']) - - -methods = { - 'personal_newAccount': personal_new_account, - 'personal_signTransaction': personal_sign_transaction, - 'eth_signTransaction': eth_signTransaction, - } - - -def jsonrpc_error(rpc_id, err): - return { - 'json-rpc': '2.0', - 'id': rpc_id, - 'error': { - 'code': err.CODE, - 'message': err.MESSAGE, - }, - } - - -def jsonrpc_ok(rpc_id, response): - return { - 'json-rpc': '2.0', - 'id': rpc_id, - 'result': response, - } - - -def is_valid_json(j): - if j.get('id') == 'None': - raise ValueError('id missing') - return True - - -def process_input(j): - rpc_id = j['id'] - m = j['method'] - p = j['params'] - return (rpc_id, methods[m](p)) - - -def start_server(): - try: - os.unlink('/tmp/foo.ipc') - except FileNotFoundError: - pass - s = socket.socket(family = socket.AF_UNIX, type = socket.SOCK_STREAM) - s.bind('/tmp/foo.ipc') - s.listen(10) - while True: - (csock, caddr) = s.accept() - d = csock.recv(4096) - j = None - try: - j = json.loads(d) - is_valid_json(j) - logg.debug('{}'.format(d.decode('utf-8'))) - except: - csock.send(json.dumps(jsonrpc_error(None, JSONRPCParseError)).encode('utf-8')) - csock.close() - continue - - try: - (rpc_id, r) = process_input(j) - csock.send(json.dumps(jsonrpc_ok(rpc_id, r)).encode('utf-8')) - except: - # TODO: handle cases to give better error context to caller - csock.send(json.dumps(jsonrpc_error(j['id'], JSONRPCServerError)).encode('utf-8')) - - csock.close() - s.close() - - os.unlink('/tmp/foo.ipc') - - -def init(): - global db, signer - secret_hex = os.environ.get('SIGNER_SECRET') - secret = bytes.fromhex(secret_hex) - kw = { - 'symmetric_key': secret, - } - db = ReferenceKeystore('cic_signer', **kw) - signer = ReferenceSigner(db) - - -if __name__ == '__main__': - init() - arg = None - try: - arg = json.loads(sys.argv[1]) - except: - logg.info('no json rpc command detected, starting socket server') - start_server() - sys.exit(0) - - (rpc_id, response) = process_input(arg) - r = jsonrpc_ok(rpc_id, response) - sys.stdout.write(json.dumps(r)) diff --git a/scripts/crypto-dev-daemon.py b/scripts/crypto-dev-daemon.py new file mode 120000 index 0000000..f54c2e9 --- /dev/null +++ b/scripts/crypto-dev-daemon.py @@ -0,0 +1 @@ +crypto-dev-daemon \ No newline at end of file diff --git a/setup.py b/setup.py index 39e0c52..efb789d 100644 --- a/setup.py +++ b/setup.py @@ -6,9 +6,15 @@ setup( description="A signer and keystore daemon and library for cryptocurrency software development", author="Louis Holbrook", author_email="dev@holbrook.no", - packages=['crypto-dev-signer'], - install_requires=['web3', 'psycopg2', 'cryptography', 'eth-keys', 'pysha3', 'rlp'], + packages=[ + 'crypto_dev_signer.eth.signer', + 'crypto_dev_signer.eth.web3ext', + 'crypto_dev_signer.eth', + 'crypto_dev_signer.keystore', + 'crypto_dev_signer', + ], + install_requires=['web3', 'psycopg2', 'cryptography', 'eth-keys', 'pysha3', 'rlp', 'json-rpc'], scripts = [ - 'scripts/crypto-dev-daemon.py', + 'scripts/crypto-dev-daemon', ], ) diff --git a/src/keystore/__init__.py b/src/keystore/__init__.py deleted file mode 100644 index 081abad..0000000 --- a/src/keystore/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from keystore.postgres import ReferenceKeystore diff --git a/src/signer/__init__.py b/src/signer/__init__.py deleted file mode 100644 index c95d792..0000000 --- a/src/signer/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from signer.defaultsigner import ReferenceSigner, Signer diff --git a/test/test_keystore.py b/test/test_keystore.py index 6909e6a..618fa63 100644 --- a/test/test_keystore.py +++ b/test/test_keystore.py @@ -8,7 +8,7 @@ import psycopg2 from psycopg2 import sql from cryptography.fernet import Fernet, InvalidToken -from keystore import ReferenceKeystore +from crypto_dev_signer.keystore import ReferenceKeystore logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() diff --git a/test/test_sign.py b/test/test_sign.py index c44c4a9..c941f72 100644 --- a/test/test_sign.py +++ b/test/test_sign.py @@ -5,8 +5,8 @@ import logging from rlp import encode as rlp_encode -from signer import ReferenceSigner -from transaction import EIP155Transaction +from crypto_dev_signer.eth.signer import ReferenceSigner +from crypto_dev_signer.eth.transaction import EIP155Transaction logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger()