def comment_react(): my_bulletin_secret = Config.get_bulletin_secret() rids = sorted( [str(my_bulletin_secret), str(request.json.get('bulletin_secret'))], key=str.lower) rid = hashlib.sha256(str(rids[0]) + str(rids[1])).digest().encode('hex') res1 = Mongo.site_db.usernames.find({'rid': rid}) if res1.count(): username = res1[0]['username'] else: username = humanhash.humanize(rid) Mongo.site_db.comment_reacts.insert({ 'rid': rid, 'emoji': request.json.get('react'), 'comment_id': request.json.get('_id') }) comment = Mongo.site_db.comments.find( {'_id': ObjectId(str(request.json.get('_id')))})[0] res = Mongo.site_db.fcmtokens.find({"rid": comment['rid']}) for token in res: result = push_service.notify_single_device( registration_id=token['token'], message_title='%s reacted to your comment!' % username, message_body='Go see how they reacted!', extra_kwargs={'priority': 'high'}) return 'ok'
def search(): phrase = request.args.get('phrase') bulletin_secret = request.args.get('bulletin_secret') my_bulletin_secret = Config.get_bulletin_secret() rids = sorted([str(my_bulletin_secret), str(bulletin_secret)], key=str.lower) rid = hashlib.sha256(str(rids[0]) + str(rids[1])).digest().encode('hex') friend = Mongo.site_db.usernames.find({'username': phrase.lower().strip()}) if friend.count(): friend = friend[0] to = friend['to'] else: return '{}', 404 out = json.dumps( { 'bulletin_secret': friend['relationship']['bulletin_secret'], 'requested_rid': friend['rid'], 'requester_rid': rid, 'to': to }, indent=4) return out
def dispatch_request(self): data = { 'bulletin_secret': Config.get_bulletin_secret(), 'username': Config.username, 'callbackurl': Config.callbackurl, 'to': Config.address } return json.dumps(data, indent=4)
def get_rid(): my_bulletin_secret = Config.get_bulletin_secret() rids = sorted( [str(my_bulletin_secret), str(request.args.get('bulletin_secret'))], key=str.lower) rid = hashlib.sha256(str(rids[0]) + str(rids[1])).digest().encode('hex') return json.dumps({'rid': rid})
def comment(): my_bulletin_secret = Config.get_bulletin_secret() rids = sorted( [str(my_bulletin_secret), str(request.json.get('bulletin_secret'))], key=str.lower) rid = hashlib.sha256(str(rids[0]) + str(rids[1])).digest().encode('hex') res1 = Mongo.site_db.usernames.find({'rid': rid}) if res1.count(): username = res1[0]['username'] else: username = humanhash.humanize(rid) Mongo.site_db.comments.insert({ 'rid': rid, 'body': request.json.get('comment'), 'txn_id': request.json.get('txn_id') }) txn = Mongo.db.posts_cache.find({'id': request.json.get('txn_id')})[0] rids = sorted([str(my_bulletin_secret), str(txn.get('bulletin_secret'))], key=str.lower) rid = hashlib.sha256(str(rids[0]) + str(rids[1])).digest().encode('hex') res = Mongo.site_db.fcmtokens.find({"rid": rid}) for token in res: result = push_service.notify_single_device( registration_id=token['token'], message_title='%s commented on your post!' % username, message_body='Go see what they said!', extra_kwargs={'priority': 'high'}) comments = Mongo.site_db.comments.find({ 'rid': { '$ne': rid }, 'txn_id': request.json.get('txn_id') }) for comment in comments: res = Mongo.site_db.fcmtokens.find({"rid": comment['rid']}) for token in res: result = push_service.notify_single_device( registration_id=token['token'], message_title='%s commented on a post you commented on!' % username, message_body='Go see what they said!', extra_kwargs={'priority': 'high'}) return 'ok'
def generate(mongodb_host=None): config = { "private_key": pk.to_hex(), "wif": to_wif(pk.to_hex()), "public_key": pk.public_key.format().encode('hex'), "address": str(P2PKHBitcoinAddress.from_pubkey(pk.public_key.format())), "serve_host": "0.0.0.0", "serve_port": 8000, "peer_host": public_ip, "peer_port": 8000, "web_server_host": "0.0.0.0", "web_server_port": 5000, "peer": "http://localhost:8000", "callbackurl": "http://0.0.0.0:5000/create-relationship", "fcm_key": "", "database": "yadacoin", "site_database": "yadacoinsite", "mongodb_host": mongodb_host or "localhost", "mixpanel": "", "username": Config.username } Config.from_dict(config) return json.dumps(Config.to_dict(), indent=4)
view_func=endpoints.GraphMessagesView.as_view('graphmessages'), methods=['GET', 'POST']) app.add_url_rule( '/get-graph-new-messages', view_func=endpoints.GraphNewMessagesView.as_view('graphnewmessages'), methods=['GET', 'POST']) app.add_url_rule('/wallet', view_func=endpoints.WalletView.as_view('wallet')) app.add_url_rule('/faucet', view_func=endpoints.FaucetView.as_view('faucet')) app.add_url_rule( '/explorer-search', view_func=endpoints.ExplorerSearchView.as_view('explorer-search')) app.add_url_rule( '/get-latest-block', view_func=endpoints.GetLatestBlockView.as_view('get-latest-block')) app.add_url_rule( '/create-relationship', view_func=endpoints.CreateRelationshipView.as_view('create-relationship')) app.add_url_rule('/yada_config.json', view_func=endpoints.GetYadaConfigView.as_view('yada-config')) parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('--conf', help='set your config file') args = parser.parse_args() conf = args.conf or 'config/config.json' with open(conf) as f: config = Config(json.loads(f.read())) app.config['yada_config'] = config app.config['yada_mongo'] = Mongo(config) #push_service = FCMNotification(api_key=config.fcm_key)
auto_parser = subparsers.add_parser('auto') auto_parser.set_defaults(which='auto') auto_parser.add_argument('-f', '--force', help='Forcefully create file, possibly overwriting existing, use with caution!') auto_parser.add_argument('-c', '--create', help='Create a new config file if one does not already exist') auto_parser.add_argument('-m', '--mongo-host', help='Specify a mongodb host') args = parser.parse_args() public_ip = requests.get('https://api.ipify.org').text if args.which == 'new': if args.password.value: num = from_wif(args.password.value) else: num = os.urandom(32).encode('hex') pk = PrivateKey.from_hex(num) config = Config.generate(pk.to_hex()) config.username = args.username elif args.which == 'update': with open(args.config) as f: identity = json.loads(f.read()) config = Config.generate(xprv=identity['xprv']) if 'username' in identity and identity['username']: username = identity['username'] else: username = args.username config.username = username config.bulletin_secret = config.get_bulletin_secret() print config.to_json() elif args.which == 'auto': config = Config.generate() config.username = ''
from bitcoin.wallet import CBitcoinSecret, P2PKHBitcoinAddress from bson.son import SON def output(percent): sys.stdout.write(str(percent)) # write the next character sys.stdout.flush() # flush stdout buffer (actual character display) sys.stdout.write(''.join(['\b' for i in range(len(percent)) ])) # erase the last written char if float(percent) >= 100: print "\n\n\nDone!" with open('config/config.json') as f: config = json.loads(f.read()) config = Config.from_dict(config) mongo = Mongo(config) blocks = BU.get_blocks(config, mongo) blockchain = Blockchain(blocks) blockchain.verify(output) res = mongo.db.blocks.aggregate([{ "$unwind": "$transactions" }, { "$project": { "_id": 0, "txn": "$transactions" } }, { "$unwind": "$txn.inputs"
default=multiprocessing.cpu_count(), help='Specify number of cores to use') args = parser.parse_args() if args.mode == 'config' and args.config: if not os.path.isfile(args.config): with open(args.config, 'w+') as f: from utils import generate_config f.write(generate_config.generate()) else: print '\'%s\' already exists! You must rename, move, or delete the existing file.' % args.config exit() if os.path.isfile(args.config): with open(args.config) as f: Config.from_dict(json.loads(f.read())) else: print 'no config file found at \'%s\'' % args.config exit() if args.mode == 'consensus': Peers.init() consensus = Consensus() while 1: consensus.sync() """ p = Process(target=) p.start() p.join() """ time.sleep(1)
from pymongo import MongoClient from flask import Flask, request, session, redirect, render_template import sys from flask_cors import CORS sys.path.insert(0, '/home/mvogel/yadacoin') from yadacoin import TransactionFactory, TU, BU, Config, Mongo, Peers app = Flask(__name__) app.debug = True app.secret_key = '23ljk2l9a08sd7f09as87df09as87df3k4j' CORS(app) app.template_folder = '/home/mvogel/dev/yadaserviceprovider/templates' try: with open('config.json') as f: Config.from_dict(json.loads(f.read())) print Config.to_json() except: print "you must generate a config and save it to a file named config.json" Peers.init() Mongo.init() @app.route('/', methods=['GET', 'POST']) def home(): session.setdefault('id', str(uuid.uuid4())) if request.method == 'POST': bulletin_secret = request.form.get('bulletin_secret', '') if not bulletin_secret: return redirect('/?error')
from bitcoin.wallet import CBitcoinSecret, P2PKHBitcoinAddress from bson.son import SON def output(percent): sys.stdout.write(str(percent)) # write the next character sys.stdout.flush() # flush stdout buffer (actual character display) sys.stdout.write(''.join(['\b' for i in range(len(percent)) ])) # erase the last written char if float(percent) >= 100: print "\n\n\nDone!" with open('config/config.json') as f: config = json.loads(f.read()) Config.from_dict(config) Mongo.init() blocks = BU.get_blocks() blockchain = Blockchain(blocks) blockchain.verify(output) res = Mongo.db.blocks.aggregate([{ "$unwind": "$transactions" }, { "$project": { "_id": 0, "txn": "$transactions" } }, { "$unwind": "$txn.inputs"
def create_relationship(): # demo site if request.method == 'GET': bulletin_secret = request.args.get('bulletin_secret', '') username = request.args.get('username', '') to = request.args.get('to', '') else: bulletin_secret = request.json.get('bulletin_secret', '') username = request.json.get('username', '') to = request.json.get('to', '') if not bulletin_secret: return 'error: "bulletin_secret" missing', 400 if not username: return 'error: "username" missing', 400 if not to: return 'error: "to" missing', 400 rid = TU.generate_rid(bulletin_secret) dup = Mongo.db.blocks.find({'transactions.rid': rid}) if dup.count(): for txn in dup: if txn['public_key'] == Config.public_key: return json.dumps({ "success": False, "status": "Already added" }) input_txns = BU.get_wallet_unspent_transactions(Config.address) miner_transactions = Mongo.db.miner_transactions.find() mtxn_ids = [] for mtxn in miner_transactions: for mtxninput in mtxn['inputs']: mtxn_ids.append(mtxninput['id']) checked_out_txn_ids = Mongo.db.checked_out_txn_ids.find() for mtxn in checked_out_txn_ids: mtxn_ids.append(mtxn['id']) a = os.urandom(32) dh_public_key = scalarmult_base(a).encode('hex') dh_private_key = a.encode('hex') transaction = TransactionFactory(bulletin_secret=bulletin_secret, username=username, fee=0.01, public_key=Config.public_key, dh_public_key=dh_public_key, private_key=Config.private_key, dh_private_key=dh_private_key, outputs=[Output(to=to, value=1)]) TU.save(transaction.transaction) Mongo.db.miner_transactions.insert(transaction.transaction.to_dict()) job = Process(target=endpoints.TxnBroadcaster.txn_broadcast_job, args=(transaction.transaction, )) job.start() my_bulletin_secret = Config.get_bulletin_secret() bulletin_secrets = sorted([str(my_bulletin_secret), str(bulletin_secret)], key=str.lower) rid = hashlib.sha256(str(bulletin_secrets[0]) + str(bulletin_secrets[1])).digest().encode('hex') Mongo.site_db.friends.insert({ 'rid': rid, 'relationship': { 'bulletin_secret': bulletin_secret } }) return json.dumps({"success": True})
def do_push(self, txn, bulletin_secret): my_bulletin_secret = Config.get_bulletin_secret() rids = sorted([str(my_bulletin_secret), str(bulletin_secret)], key=str.lower) rid = hashlib.sha256(str(rids[0]) + str(rids[1])).digest().encode('hex') res1 = Mongo.site_db.usernames.find({'rid': rid}) if res1.count(): username = res1[0]['username'] else: username = humanhash.humanize(rid) if txn.get('relationship') and txn.get('dh_public_key') and txn.get('requester_rid') == rid: #friend request #if rid is the requester_rid, then we send a friend request notification to the requested_rid res = Mongo.site_db.fcmtokens.find({"rid": txn['requested_rid']}) for token in res: result = push_service.notify_single_device( registration_id=token['token'], message_title='%s sent you a friend request!' % username, message_body="See the request and approve!", extra_kwargs={'priority': 'high'} ) elif txn.get('relationship') and txn.get('dh_public_key') and txn.get('requested_rid') == rid: #friend accept #if rid is the requested_rid, then we send a friend accepted notification to the requester_rid res = Mongo.site_db.fcmtokens.find({"rid": txn['requester_rid']}) for token in res: result = push_service.notify_single_device( registration_id=token['token'], message_title='%s approved your friend request!' % username, message_body='Say "hi" to your friend!', extra_kwargs={'priority': 'high'} ) elif txn.get('relationship') and not txn.get('dh_public_key') and not txn.get('rid'): #post #we find all mutual friends of rid and send new post notifications to them rids = [] rids.extend([x['requested_rid'] for x in BU.get_sent_friend_requests(rid)]) rids.extend([x['requester_rid'] for x in BU.get_friend_requests(rid)]) for friend_rid in rids: res = Mongo.site_db.fcmtokens.find({"rid": friend_rid}) used_tokens = [] for token in res: if token['token'] in used_tokens: continue used_tokens.append(token['token']) result = push_service.notify_single_device( registration_id=token['token'], message_title='%s has posted something!' % username, message_body='Check out what your friend posted!', extra_kwargs={'priority': 'high'} ) elif txn.get('relationship') and not txn.get('dh_public_key') and txn.get('rid'): #message #we find the relationship of the transaction rid and send a new message notification to the rid #of the relationship that does not match the arg rid txns = [x for x in BU.get_transactions_by_rid(txn['rid'], rid=True, raw=True)] rids = [] rids.extend([x['requested_rid'] for x in txns if 'requested_rid' in x and rid != x['requested_rid']]) rids.extend([x['requester_rid'] for x in txns if 'requester_rid' in x and rid != x['requester_rid']]) for friend_rid in rids: res = Mongo.site_db.fcmtokens.find({"rid": friend_rid}) used_tokens = [] for token in res: if token['token'] in used_tokens: continue used_tokens.append(token['token']) result = push_service.notify_single_device( registration_id=token['token'], message_title='New message from %s!' % username, message_body='Go see what your friend said!', extra_kwargs={'priority': 'high'} ) print result
def node(nonces=None, config=None): Config.from_dict(json.loads(config)) Peers.init() latest_block_index = Value('i', 0) my_peer = Config.peer_host + ":" + str(Config.peer_port) Config.max_duration = 300000 Config.block_version = 1 #p = Process(target=new_block_checker, args=(latest_block_index,)) status = Array('c', 'asldkjf') #p.start() Mongo.init() # default run state will be to mine some blocks! block = BU.get_latest_block() if block: latest_block_index.value = block.get('index') + 1 else: genesis_block = BlockFactory.get_genesis_block() genesis_block.save() Mongo.db.consensus.insert({ 'block': genesis_block.to_dict(), 'peer': 'me', 'id': genesis_block.signature, 'index': 0 }) block = BU.get_latest_block() latest_block_index.value = block.get('index') dup_test = Mongo.db.consensus.find({ 'peer': 'me', 'index': latest_block_index.value, 'block.version': BU.get_version_for_height(latest_block_index.value + 1) }).sort([('index', -1)]) if dup_test.count(): #print 'returning', latest_block_index.value + 1, BU.get_version_for_height(latest_block_index.value + 1) return transactions = Mongo.db.miner_transactions.find() transaction_objs = [] unspent_indexed = {} for txn in transactions: try: transaction = Transaction.from_dict(txn) transaction.verify() #check double spend address = str( P2PKHBitcoinAddress.from_pubkey( transaction.public_key.decode('hex'))) if address in unspent_indexed: unspent_ids = unspent_indexed[address] else: needed_value = sum( [float(x.value) for x in transaction.outputs]) + float(transaction.fee) res = BU.get_wallet_unspent_transactions( address, needed_value=needed_value) unspent_ids = [x['id'] for x in res] unspent_indexed[address] = unspent_ids failed1 = False failed2 = False used_ids_in_this_txn = [] for x in transaction.inputs: if x.id not in unspent_ids: failed1 = True if x.id in used_ids_in_this_txn: failed2 = True used_ids_in_this_txn.append(x.id) if failed1: Mongo.db.miner_transactions.remove( {'id': transaction.transaction_signature}) print 'transaction removed: input presumably spent already, not in unspent outputs', transaction.transaction_signature Mongo.db.failed_transactions.insert({ 'reason': 'input presumably spent already', 'txn': transaction.to_dict() }) elif failed2: Mongo.db.miner_transactions.remove( {'id': transaction.transaction_signature}) print 'transaction removed: using an input used by another transaction in this block', transaction.transaction_signature Mongo.db.failed_transactions.insert({ 'reason': 'using an input used by another transaction in this block', 'txn': transaction.to_dict() }) else: transaction_objs.append(transaction) except MissingInputTransactionException as e: #print 'missing this input transaction, will try again later' pass except InvalidTransactionSignatureException as e: print 'InvalidTransactionSignatureException: transaction removed' Mongo.db.miner_transactions.remove( {'id': transaction.transaction_signature}) Mongo.db.failed_transactions.insert({ 'reason': 'InvalidTransactionSignatureException', 'txn': transaction.to_dict() }) except InvalidTransactionException as e: print 'InvalidTransactionException: transaction removed' Mongo.db.miner_transactions.remove( {'id': transaction.transaction_signature}) Mongo.db.failed_transactions.insert({ 'reason': 'InvalidTransactionException', 'txn': transaction.to_dict() }) except Exception as e: #print e #print 'rejected transaction', txn['id'] pass except BaseException as e: #print e #print 'rejected transaction', txn['id'] pass try: block = BlockFactory.mine(transaction_objs, Config.public_key, Config.private_key, Config.max_duration, output, latest_block_index, status, nonces) except Exception as e: raise if block: dup_test = Mongo.db.consensus.find_one({ 'peer': 'me', 'index': block.index, 'block.version': BU.get_version_for_height(block.index) }) if not dup_test: print '\r\nCandidate submitted for index:', block.index print '\r\nTransactions:' for x in block.transactions: print x.transaction_signature Mongo.db.consensus.insert({ 'peer': 'me', 'index': block.index, 'id': block.signature, 'block': block.to_dict() }) print '\r\nSent block to:' for peer in Peers.peers: try: block_dict = block.to_dict() block_dict['peer'] = my_peer requests.post('http://{peer}/newblock'.format( peer=peer.host + ":" + str(peer.port)), json=block_dict, timeout=3, headers={'Connection': 'close'}) print peer.host + ":" + str(peer.port) except Exception as e: print e try: print 'reporting bad peer' requests.post('https://yadacoin.io/peers', json={ 'host': peer.host, 'port': str(peer.port), 'failed': True }, timeout=3, headers={'Connection': 'close'}) except: print 'failed to report bad peer' pass
parser.add_argument('to', default="", nargs="?", help='to') parser.add_argument('value', default=0, nargs="?", help='amount') parser.add_argument('-n', '--network', default='mainnet', help='Specify maintnet or testnet') parser.add_argument('-c', '--cores', default=multiprocessing.cpu_count(), help='Specify number of cores to use') parser.add_argument('-p', '--pool', default='', help='Specify pool to use') args = parser.parse_args() if os.path.isfile(args.config): with open(args.config) as f: config = Config(json.loads(f.read())) else: print 'no config file found at \'%s\'' % args.config exit() mongo = Mongo(config) if args.mode == 'consensus': consensus = Consensus(config, mongo) consensus.verify_existing_blockchain() while 1: consensus.sync_top_down() consensus.sync_bottom_up() time.sleep(1) elif args.mode == 'send': Send.run(config, mongo, args.to, float(args.value))
import sys import json import time from base64 import b64encode from yadacoin import Config, BU, BlockFactory, Mongo with open('config/config.json') as f: config = Config.from_dict(json.loads(f.read())) iteration = 0 config.max_duration = 100000000 config.grace = 10 config.block_version = '1' mongo = Mongo(config) start = time.time() genesis_block = BlockFactory.mine([], config.public_key, config.private_key, config.max_duration) end = time.time() print genesis_block.to_json()
from rpc import app from yadacoin import Config print 'RUNNING SERVER WITH CONFIG:' print Config.to_json() app.run(host=Config.web_server_host, port=Config.web_server_port, threaded=True)