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') # generate a transaction which contains a signin message containing the current sessions identifier txn = TransactionFactory(bulletin_secret=bulletin_secret, public_key=Config.public_key, private_key=Config.private_key, signin=session.get('id'), fee=0.01).transaction # send the transaction to our own serve instance, which saves it to miner_transactions # the miner looks in miner_transactions to include in a block when it finds a new block for peer in Peers.peers: print peer.host, peer.port requests.post("http://{host}:{port}/newtransaction".format( host=peer.host, port=peer.port), txn.to_json(), headers={"Content-Type": "application/json"}) return redirect('/?bulletin_secret=%s' % urllib.quote_plus(bulletin_secret)) elif request.method == 'GET': bulletin_secret = request.args.get('bulletin_secret', '') rid = TU.generate_rid(bulletin_secret) txns = BU.get_transactions_by_rid(rid, rid=True) txns2 = BU.get_transactions_by_rid(rid, rid=True, raw=True) half1 = False half2 = False for txn in txns: if txn['public_key'] == Config.public_key: half1 = True for txn in txns2: if txn['public_key'] != Config.public_key: half2 = True registered = half1 and half2 sent, received = BU.verify_message(rid, session['id']) session['loggedin'] = received return render_template('index.html', session_id=str(session.get('id')), registered=str(registered), sent=str(sent), received=str(received), loggedin=str(session['loggedin']), bulletin_secret=str(bulletin_secret), rid=str(rid)) else: return redirect('/')
def dispatch_request(self): config = app.config['yada_config'] mongo = app.config['yada_mongo'] if request.method == 'POST': items = request.json if not isinstance(items, list): items = [ items, ] else: items = [item for item in items] transactions = [] for txn in items: transaction = Transaction.from_dict( config, mongo, BU.get_latest_block(config, mongo)['index'], txn) try: transaction.verify() except InvalidTransactionException: mongo.db.failed_transactions.insert({ 'exception': 'InvalidTransactionException', 'txn': txn }) print 'InvalidTransactionException' return 'InvalidTransactionException', 400 except InvalidTransactionSignatureException: print 'InvalidTransactionSignatureException' mongo.db.failed_transactions.insert({ 'exception': 'InvalidTransactionSignatureException', 'txn': txn }) return 'InvalidTransactionSignatureException', 400 except MissingInputTransactionException: pass except: raise print 'uknown error' return 'uknown error', 400 transactions.append(transaction) for x in transactions: mongo.db.miner_transactions.insert(x.to_dict()) job = Process(target=TxnBroadcaster.txn_broadcast_job, args=(transaction, )) job.start() return json.dumps(request.get_json()) else: rid = request.args.get('rid') if rid: transactions = BU.get_transactions_by_rid( config, mongo, rid, config.bulletin_secret, rid=True, raw=True) else: transactions = [] return json.dumps([x for x in transactions])
def dispatch_request(self): if request.method == 'POST': items = request.json if not isinstance(items, list): items = [items, ] else: items = [item for item in items] transactions = [] for txn in items: transaction = Transaction.from_dict(txn) try: transaction.verify() except InvalidTransactionException: Mongo.db.failed_transactions.insert({ 'exception': 'InvalidTransactionException', 'txn': txn }) print 'InvalidTransactionException' return 'InvalidTransactionException', 400 except InvalidTransactionSignatureException: print 'InvalidTransactionSignatureException' Mongo.db.failed_transactions.insert({ 'exception': 'InvalidTransactionSignatureException', 'txn': txn }) return 'InvalidTransactionSignatureException', 400 except MissingInputTransactionException: pass except: raise print 'uknown error' return 'uknown error', 400 transactions.append(transaction) for x in transactions: Mongo.db.miner_transactions.insert(x.to_dict()) job = Process(target=TxnBroadcaster.txn_broadcast_job, args=(transaction,)) job.start() if Config.fcm_key: for txn in transactions: job = Process( target=self.do_push, args=(txn.to_dict(), request.args.get('bulletin_secret')) ) job.start() return json.dumps(request.get_json()) else: rid = request.args.get('rid') if rid: transactions = BU.get_transactions_by_rid(rid, rid=True, raw=True) else: transactions = [] return json.dumps([x for x in transactions])
def get_logged_in_user(): user = None tests = [] res = Mongo.db.blocks.aggregate([{ "$match": { "transactions.challenge_code": session['challenge_code'] } }, { '$unwind': "$transactions" }, { "$match": { "transactions.challenge_code": session['challenge_code'] } }]) for transaction in res: transaction = transaction['transactions'] tests = BU.get_transactions_by_rid(transaction['rid'], rid=True) for test in tests: if 'relationship' in test and 'shared_secret' in test[ 'relationship']: cipher = Crypt( hashlib.sha256(test['relationship'] ['shared_secret']).digest().encode('hex')) answer = cipher.decrypt(transaction['answer']) if answer == transaction['challenge_code']: for txn_output in transaction['outputs']: if txn_output['to'] != Config.address: to = txn_output['to'] user = { 'balance': BU.get_wallet_balance(to), 'authenticated': True, 'rid': transaction['rid'], 'bulletin_secret': test['relationship']['bulletin_secret'] } return user if user else {'authenticated': False}
def do_push(self, txn, bulletin_secret): config = app.config['yada_config'] mongo = app.config['yada_mongo'] my_bulletin_secret = config.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 = self.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 = self.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(config, mongo, rid) ]) rids.extend([ x['requester_rid'] for x in BU.get_friend_requests(config, mongo, 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 = self.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(config, mongo, txn['rid'], config.bulletin_secret, 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 = self.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