From bbb5228828f556126b1f2d64c4fb3410e5b36fdb Mon Sep 17 00:00:00 2001 From: SoulByte Date: Sat, 18 May 2013 14:32:39 +0200 Subject: [PATCH] Server now generates addresses The BlooCoin server now generates the address for registering clients. --- register.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/register.py b/register.py index 29dd8e2..55cebe2 100644 --- a/register.py +++ b/register.py @@ -1,29 +1,35 @@ # -*- coding: utf-8 -*- import json - +import random import mongo import command +from hashlib import sha1 class Register(command.Command): - """ Allows clients to register their address with the - server's central database. If a given address - already exists, they should regenerate and try - again with a new one, if the user requested a - registration. + """Allows clients to register an account. The +server generates a random address and password, +registers it, and sends it back to the client. - fingerprint: {"cmd": "register", "addr": _, "pwd": _} - """ - required = ['addr', 'pwd'] +fingerprint: {"cmd": "register"} +""" + required = [] def handle(self, *args, **kwargs): - addr = self.data['addr'] - pwd = self.data['pwd'] - if len(addr) != 40: - self.error("Registration unsuccessful") - return - if mongo.db.addresses.find_one({"addr": addr}): - self.error("That address already exists") - return + + # Send error message to old clients, that used to chose the address themselves. + try: + self.data['addr'], self.data['pwd'] + self.error("Registration failed. Update your client.") + except KeyError: + pass + + addr = sha1(str(random.randint(0, 100000000000000))).hexdigest() + pwd = sha1(str(random.randint(0, 100000000000000))).hexdigest() + + # Making sure the address doesn't exist already. + while mongo.db.addresses.find_one({"addr": addr}): + addr = sha1(str(random.randint(0, 100000000000000))).hexdigest() + mongo.db.addresses.insert({"addr": addr, "pwd": pwd}) - self.success({"addr": addr}) + self.success({"addr": addr, "pwd": pwd})