From 62123850c87d94b3512d32c85b8b815142c8229e Mon Sep 17 00:00:00 2001 From: nolash Date: Thu, 11 Feb 2021 08:45:26 +0100 Subject: [PATCH] Rename package --- {cic_tools => chainlib}/eth/address.py | 0 chainlib/eth/block.py | 21 ++ {cic_tools => chainlib}/eth/connection.py | 0 {cic_tools => chainlib}/eth/constant.py | 0 {cic_tools => chainlib}/eth/erc20.py | 0 {cic_tools => chainlib}/eth/error.py | 0 {cic_tools => chainlib}/eth/gas.py | 0 {cic_tools => chainlib}/eth/hash.py | 0 {cic_tools => chainlib}/eth/nonce.py | 0 {cic_tools => chainlib}/eth/rpc.py | 0 .../eth/runnable/__init__.py | 0 .../eth/runnable/balance.py | 0 .../eth/runnable/checksum.py | 0 .../eth/runnable/decode.py | 0 {cic_tools => chainlib}/eth/runnable/gas.py | 0 .../eth/runnable/subscribe.py | 0 .../eth/runnable/transfer.py | 4 +- {cic_tools => chainlib}/eth/tx.py | 0 cic_tools/cic_eth/runnable/users.py | 252 ------------------ requirements.txt | 7 - setup.cfg | 15 +- 21 files changed, 31 insertions(+), 268 deletions(-) rename {cic_tools => chainlib}/eth/address.py (100%) create mode 100644 chainlib/eth/block.py rename {cic_tools => chainlib}/eth/connection.py (100%) rename {cic_tools => chainlib}/eth/constant.py (100%) rename {cic_tools => chainlib}/eth/erc20.py (100%) rename {cic_tools => chainlib}/eth/error.py (100%) rename {cic_tools => chainlib}/eth/gas.py (100%) rename {cic_tools => chainlib}/eth/hash.py (100%) rename {cic_tools => chainlib}/eth/nonce.py (100%) rename {cic_tools => chainlib}/eth/rpc.py (100%) rename {cic_tools => chainlib}/eth/runnable/__init__.py (100%) rename {cic_tools => chainlib}/eth/runnable/balance.py (100%) rename {cic_tools => chainlib}/eth/runnable/checksum.py (100%) rename {cic_tools => chainlib}/eth/runnable/decode.py (100%) rename {cic_tools => chainlib}/eth/runnable/gas.py (100%) rename {cic_tools => chainlib}/eth/runnable/subscribe.py (100%) rename {cic_tools => chainlib}/eth/runnable/transfer.py (98%) rename {cic_tools => chainlib}/eth/tx.py (100%) delete mode 100644 cic_tools/cic_eth/runnable/users.py diff --git a/cic_tools/eth/address.py b/chainlib/eth/address.py similarity index 100% rename from cic_tools/eth/address.py rename to chainlib/eth/address.py diff --git a/chainlib/eth/block.py b/chainlib/eth/block.py new file mode 100644 index 0000000..6eccad5 --- /dev/null +++ b/chainlib/eth/block.py @@ -0,0 +1,21 @@ +from cic_tools.eth.rpc import jsonrpc_template + + +def block(self): + o = jsonrpc_template() + o['method'] = 'eth_blockNumber' + return o + + +def block_by_hash(self, hsh): + o = jsonrpc_template() + o['method'] = 'eth_getBlock' + o['params'].append(hsh) + return o + + +def block_by_number(self, n): + o = jsonrpc_template() + o['method'] = 'eth_getBlock' + o['params'].append(n) + return o diff --git a/cic_tools/eth/connection.py b/chainlib/eth/connection.py similarity index 100% rename from cic_tools/eth/connection.py rename to chainlib/eth/connection.py diff --git a/cic_tools/eth/constant.py b/chainlib/eth/constant.py similarity index 100% rename from cic_tools/eth/constant.py rename to chainlib/eth/constant.py diff --git a/cic_tools/eth/erc20.py b/chainlib/eth/erc20.py similarity index 100% rename from cic_tools/eth/erc20.py rename to chainlib/eth/erc20.py diff --git a/cic_tools/eth/error.py b/chainlib/eth/error.py similarity index 100% rename from cic_tools/eth/error.py rename to chainlib/eth/error.py diff --git a/cic_tools/eth/gas.py b/chainlib/eth/gas.py similarity index 100% rename from cic_tools/eth/gas.py rename to chainlib/eth/gas.py diff --git a/cic_tools/eth/hash.py b/chainlib/eth/hash.py similarity index 100% rename from cic_tools/eth/hash.py rename to chainlib/eth/hash.py diff --git a/cic_tools/eth/nonce.py b/chainlib/eth/nonce.py similarity index 100% rename from cic_tools/eth/nonce.py rename to chainlib/eth/nonce.py diff --git a/cic_tools/eth/rpc.py b/chainlib/eth/rpc.py similarity index 100% rename from cic_tools/eth/rpc.py rename to chainlib/eth/rpc.py diff --git a/cic_tools/eth/runnable/__init__.py b/chainlib/eth/runnable/__init__.py similarity index 100% rename from cic_tools/eth/runnable/__init__.py rename to chainlib/eth/runnable/__init__.py diff --git a/cic_tools/eth/runnable/balance.py b/chainlib/eth/runnable/balance.py similarity index 100% rename from cic_tools/eth/runnable/balance.py rename to chainlib/eth/runnable/balance.py diff --git a/cic_tools/eth/runnable/checksum.py b/chainlib/eth/runnable/checksum.py similarity index 100% rename from cic_tools/eth/runnable/checksum.py rename to chainlib/eth/runnable/checksum.py diff --git a/cic_tools/eth/runnable/decode.py b/chainlib/eth/runnable/decode.py similarity index 100% rename from cic_tools/eth/runnable/decode.py rename to chainlib/eth/runnable/decode.py diff --git a/cic_tools/eth/runnable/gas.py b/chainlib/eth/runnable/gas.py similarity index 100% rename from cic_tools/eth/runnable/gas.py rename to chainlib/eth/runnable/gas.py diff --git a/cic_tools/eth/runnable/subscribe.py b/chainlib/eth/runnable/subscribe.py similarity index 100% rename from cic_tools/eth/runnable/subscribe.py rename to chainlib/eth/runnable/subscribe.py diff --git a/cic_tools/eth/runnable/transfer.py b/chainlib/eth/runnable/transfer.py similarity index 98% rename from cic_tools/eth/runnable/transfer.py rename to chainlib/eth/runnable/transfer.py index d9c4091..4f76eb2 100644 --- a/cic_tools/eth/runnable/transfer.py +++ b/chainlib/eth/runnable/transfer.py @@ -62,8 +62,8 @@ if args.vv: elif args.v: logg.setLevel(logging.INFO) -block_last = args.w -block_all = args.ww +block_all = args.ww +block_last = args.w or block_all signer_address = None keystore = DictKeystore() diff --git a/cic_tools/eth/tx.py b/chainlib/eth/tx.py similarity index 100% rename from cic_tools/eth/tx.py rename to chainlib/eth/tx.py diff --git a/cic_tools/cic_eth/runnable/users.py b/cic_tools/cic_eth/runnable/users.py deleted file mode 100644 index 8399f0f..0000000 --- a/cic_tools/cic_eth/runnable/users.py +++ /dev/null @@ -1,252 +0,0 @@ -#!/usr/bin/python - -# standard imports -import json -import time -import datetime -import random -import logging -import os -import base64 -import hashlib -import sys -import uuid -import argparse - -# third-party imports -import redis -import vobject -import celery -from faker import Faker -import cic_registry -import confini -from cic_eth.api import Api - -logging.basicConfig(level=logging.DEBUG) -logg = logging.getLogger() - -fake = Faker(['sl', 'en_US', 'no', 'de', 'ro']) - -#f = open('cic.conf', 'r') -#config = json.load(f) -#f.close() -# - -default_config_dir = os.environ.get('CONFINI_DIR', '/usr/local/etc/cic') - -argparser = argparse.ArgumentParser() -argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)') -argparser.add_argument('-c', type=str, default=default_config_dir, help='config root to use') -argparser.add_argument('-q', type=str, default='cic-eth', help='Task queue') -argparser.add_argument('-i', '--chain-spec', dest='i', type=str, help='chain spec') -argparser.add_argument('--redis-host-callback', dest='redis_host_callback', default='localhost', type=str, help='redis host to use for callback') -argparser.add_argument('--redis-port-callback', dest='redis_port_callback', default=6379, type=int, help='redis port to use for callback') -argparser.add_argument('--timeout', default=1.0, type=int, help='timeout to wait for account create callback') -argparser.add_argument('-v', action='store_true', help='Be verbose') -argparser.add_argument('-vv', help='be more verbose', action='store_true') -argparser.add_argument('count', help='Number of users to generate', type=int) -args = argparser.parse_args() - -if args.v == True: - logging.getLogger().setLevel(logging.INFO) -elif args.vv == True: - logging.getLogger().setLevel(logging.DEBUG) - -config_dir = os.path.join(args.c) -config = confini.Config(config_dir, os.environ.get('CONFINI_ENV_PREFIX')) -config.process() -args_override = { - 'ETH_PROVIDER': getattr(args, 'p'), - 'CIC_CHAIN_SPEC': getattr(args, 'i'), -} -config.dict_override(args_override, 'cli flag') -logg.debug('config loaded from {}:\n{}'.format(config_dir, config)) - - -dt_now = datetime.datetime.utcnow() -dt_then = dt_now - datetime.timedelta(weeks=150) -ts_now = int(dt_now.timestamp()) -ts_then = int(dt_then.timestamp()) - -queue = args.q - -celery_app = celery.Celery(broker=config.get('CELERY_BROKER_URL'), backend=config.get('CELERY_RESULT_URL')) - -redis_host = config.get('REDIS_HOST') -redis_port = config.get('REDIS_PORT') -redis_db = config.get('REDIS_DB') -redis_channel = str(uuid.uuid4()) -r = redis.Redis(redis_host, redis_port, redis_db) -ps = r.pubsub() -ps.subscribe(redis_channel) -ps.get_message() - -api = Api( - config.get('CIC_CHAIN_SPEC'), - queue=args.q, - callback_param='{}:{}:{}:{}'.format(args.redis_host_callback, args.redis_port_callback, redis_db, redis_channel), - callback_task='cic_eth.callbacks.redis.redis', - callback_queue=queue, - ) - -gift_max = 10000 -gift_factor = (10**9) - -user_count = args.count - -categories = [ - "food/water", - "fuel/energy", - "education", - "health", - "shop", - "environment", - "transport", - "farming/labor", - "savingsgroup", - ] - -phone_idx = [] - - -def genPhoneIndex(phone): - h = hashlib.new('sha256') - h.update(phone.encode('utf-8')) - h.update(b'cic.msisdn') - return h.digest().hex() - - -def genId(addr, typ): - h = hashlib.new('sha256') - h.update(bytes.fromhex(addr[2:])) - h.update(typ.encode('utf-8')) - return h.digest().hex() - - -def genDate(): - - logg.info(ts_then) - ts = random.randint(ts_then, ts_now) - return datetime.datetime.fromtimestamp(ts).timestamp() - - -def genPhone(): - return fake.msisdn() - - -def genPersonal(phone): - fn = fake.first_name() - ln = fake.last_name() - e = fake.email() - - v = vobject.vCard() - first_name = fake.first_name() - last_name = fake.last_name() - v.add('n') - v.n.value = vobject.vcard.Name(family=last_name, given=first_name) - v.add('fn') - v.fn.value = '{} {}'.format(first_name, last_name) - v.add('tel') - v.tel.typ_param = 'CELL' - v.tel.value = phone - v.add('email') - v.email.value = fake.email() - - vcard_serialized = v.serialize() - vcard_base64 = base64.b64encode(vcard_serialized.encode('utf-8')) - - return vcard_base64.decode('utf-8') - - -def genCats(): - i = random.randint(0, 3) - return random.choices(categories, k=i) - - -def genAmount(): - return random.randint(0, gift_max) * gift_factor - - -def gen(): - old_blockchain_address = '0x' + os.urandom(20).hex() - t = api.create_account(register=True) - - ps.get_message() - m = ps.get_message(timeout=args.timeout) - new_blockchain_address = json.loads(m['data']) - - #new_blockchain_address = t.get() - gender = random.choice(['female', 'male', 'other']) - phone = genPhone() - v = genPersonal(phone) - o = { - 'date_registered': genDate(), - 'vcard': v, - 'gender': gender, - 'key': { - 'ethereum': [ - old_blockchain_address, - new_blockchain_address, - ], - }, - 'location': { - 'latitude': str(fake.latitude()), - 'longitude': str(fake.longitude()), - 'external': { # add osm lookup - } - }, - 'selling': genCats(), - } - uid = genId(new_blockchain_address, 'cic.person') - - return (uid, phone, o) - - -def prepareLocalFilePath(datadir, address): - parts = [ - address[:2], - address[2:4], - ] - dirs = '{}/{}/{}'.format( - datadir, - parts[0], - parts[1], - ) - os.makedirs(dirs, exist_ok=True) - return dirs - - -if __name__ == '__main__': - - os.makedirs('data/person', exist_ok=True) - os.makedirs('data/phone', exist_ok=True) - - fa = open('./data/amounts', 'w') - fb = open('./data/addresses', 'w') - - #for i in range(10): - for i in range(int(user_count)): - - (uid, phone, o) = gen() - eth = o['key']['ethereum'][1] - - print(o) - - d = prepareLocalFilePath('./data/person', uid) - f = open('{}/{}'.format(d, uid), 'w') - json.dump(o, f) - f.close() - - pidx = genPhoneIndex(phone) - d = prepareLocalFilePath('./data/phone', uid) - f = open('{}/{}'.format(d, pidx), 'w') - f.write(eth) - f.close() - - amount = genAmount() - fa.write('{},{}\n'.format(eth,amount)) - fb.write('{}\n'.format(eth)) - logg.debug('pidx {}, uid {}, eth {}, amount {}'.format(pidx, uid, eth, amount)) - - fb.close() - fa.close() diff --git a/requirements.txt b/requirements.txt index bd54b0a..6adb395 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,12 +1,5 @@ cryptocurrency-cli-tools==0.0.4 -giftable-erc20-token~=0.0.7b7 -eth-accounts-index~=0.0.10a7 -erc20-single-shot-faucet~=0.2.0a6 -erc20-approval-escrow~=0.3.0a5 -cic-eth~=0.10.0a25 -vobject==0.9.6.1 faker==4.17.1 -eth-address-index~=0.1.0a6 crypto-dev-signer~=0.4.13rc2 pysha3==1.0.2 hexathon==0.0.1a2 diff --git a/setup.cfg b/setup.cfg index e211848..638f842 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,7 +1,7 @@ [metadata] -name = cic-tools +name = chainlib version = 0.0.1a4 -description = Executable tools for CIC network +description = Generic blockchain access library and tooling author = Louis Holbrook author_email = dev@holbrook.no url = https://gitlab.com/grassrootseconomics/cic-tools @@ -27,11 +27,12 @@ licence_files = [options] python_requires = >= 3.6 packages = - cic_tools.eth - cic_tools.eth.runnable + chainlib.eth + chainlib.eth.runnable [options.entry_points] console_scripts = - eth-balance = cic_tools.eth.runnable.balance:main - eth-checksum = cic_tools.eth.runnable.checksum:main - eth-gas = cic_tools.eth.runnable.gas:main + eth-balance = chainlib.eth.runnable.balance:main + eth-checksum = chainlib.eth.runnable.checksum:main + eth-gas = chainlib.eth.runnable.gas:main + eth-transfer = chainlib.eth.runnable.transfer:main