def test_MarketProfile_update_success(self): u = objects.Profile() u.about = "updated world" p = Profile(self.db) p.update(u) updated_user = p.get() self.assertEqual("updated world", updated_user.about)
def createTestUser(self): u = objects.Profile() u.name = "test_name" u.location = 2 u.about = "hello world" s = u.SocialAccount() s.username = "******" s.type = s.SocialType.Value("FACEBOOK") u.social.extend([s]) self.db.profile.set_proto(u.SerializeToString()) self.db.profile.set_temp_handle("test_handle")
def get_result(result): try: pubkey = node_to_ask.signed_pubkey[64:] verify_key = nacl.signing.VerifyKey(pubkey) verify_key.verify(result[1][1] + result[1][0]) p = objects.Profile() p.ParseFromString(result[1][0]) if not os.path.isfile(DATA_FOLDER + 'cache/' + hexlify(p.avatar_hash)): self.get_image(node_to_ask, p.avatar_hash) if not os.path.isfile(DATA_FOLDER + 'cache/' + hexlify(p.header_hash)): self.get_image(node_to_ask, p.header_hash) return p except Exception: return None
def make_moderator(self): """ Set self as a moderator in the DHT. """ u = objects.Profile() k = u.PublicKey() k.public_key = bitcoin.bip32_deserialize(KeyChain(self.db).bitcoin_master_pubkey)[5] k.signature = self.signing_key.sign(k.public_key)[:64] u.bitcoin_key.MergeFrom(k) u.moderator = True Profile(self.db).update(u) proto = self.kserver.node.getProto().SerializeToString() self.kserver.set(digest("moderators"), digest(proto), proto)
def get_conversations(self): """ Get all 'conversations' composed of messages of type 'CHAT'. Returns: Array of dictionaries, one element for each guid. Dictionaries include last message only. """ conn = Database.connect_database(self.PATH) cursor = conn.cursor() cursor.execute('''SELECT DISTINCT guid FROM messages''', ) guids = cursor.fetchall() ret = [] unread = self.get_unread() for g in guids: cursor.execute( '''SELECT avatarHash, message, max(timestamp), pubkey FROM messages WHERE guid=? and messageType=?''', (g[0], "CHAT")) val = cursor.fetchone() avatar_hash = None handle = "" if val[0] is not None: try: with open(join(DATA_FOLDER, 'cache', g[0]), "r") as filename: profile = filename.read() p = objects.Profile() p.ParseFromString(profile) avatar_hash = p.avatar_hash.encode("hex") handle = p.handle except Exception: cursor.execute( '''SELECT avatarHash FROM messages WHERE guid=? and messageType=? and avatarHash NOT NULL''', (g[0], "CHAT")) avi = cursor.fetchone() if avi[0] is not None: avatar_hash = avi[0].encode("hex") ret.append({ "guid": g[0], "avatar_hash": avatar_hash, "handle": clean(handle), "last_message": clean(val[1]), "timestamp": val[2], "public_key": val[3].encode("hex"), "unread": 0 if g[0] not in unread else unread[g[0]] }) conn.close() return ret
def make_moderator(self): """ Set self as a moderator in the DHT. """ u = objects.Profile() k = u.PublicKey() k.public_key = unhexlify(bitcointools.bip32_extract_key(KeyChain(self.db).bitcoin_master_pubkey)) k.signature = self.signing_key.sign(k.public_key)[:64] u.bitcoin_key.MergeFrom(k) u.moderator = True Profile(self.db).update(u) proto = self.kserver.node.getProto().SerializeToString() self.kserver.set(digest("moderators"), digest(proto), proto) self.log.info("setting self as moderator on the network")
def update_profile(self, request): p = Profile() if not p.get().encryption_key \ and "name" not in request.args \ and "location" not in request.args: return "False" u = objects.Profile() if "name" in request.args: u.name = request.args["name"][0] if "location" in request.args: # This needs to be formatted. Either here or from the UI. u.location = CountryCode.Value(request.args["location"][0].upper()) if "handle" in request.args: u.handle = request.args["handle"][0] if "about" in request.args: u.about = request.args["about"][0] if "nsfw" in request.args: u.nsfw = True if "vendor" in request.args: u.vendor = True if "moderator" in request.args: u.moderator = True if "website" in request.args: u.website = request.args["website"][0] if "email" in request.args: u.email = request.args["email"][0] if "avatar" in request.args: with open(DATA_FOLDER + "store/avatar", 'wb') as outfile: outfile.write(request.args["avatar"][0]) avatar_hash = digest(request.args["avatar"][0]) HashMap().insert(avatar_hash, DATA_FOLDER + "store/avatar") u.avatar_hash = avatar_hash if "header" in request.args: with open(DATA_FOLDER + "store/header", 'wb') as outfile: outfile.write(request.args["header"][0]) header_hash = digest(request.args["header"][0]) HashMap().insert(header_hash, DATA_FOLDER + "store/header") u.header_hash = header_hash if "pgp_key" in request.args and "signature" in request.args: p.add_pgp_key(request.args["pgp_key"][0], request.args["signature"][0], KeyChain().guid.encode("hex")) u.encryption_key = KeyChain().encryption_pubkey p.update(u)
def get_result(result): try: verify_key = nacl.signing.VerifyKey(node_to_ask.pubkey) verify_key.verify(result[1][0], result[1][1]) p = objects.Profile() p.ParseFromString(result[1][0]) if p.pgp_key.public_key: gpg = gnupg.GPG() gpg.import_keys(p.pgp_key.publicKey) if not gpg.verify(p.pgp_key.signature) or \ node_to_ask.id.encode('hex') not in p.pgp_key.signature: p.ClearField("pgp_key") if not os.path.isfile(DATA_FOLDER + 'cache/' + p.avatar_hash.encode("hex")): self.get_image(node_to_ask, p.avatar_hash) if not os.path.isfile(DATA_FOLDER + 'cache/' + p.header_hash.encode("hex")): self.get_image(node_to_ask, p.header_hash) return p except Exception: return None
def setprofile(): parser = argparse.ArgumentParser( description="Sets a profile in the database.", usage='''usage: networkcli.py setprofile [options]''') parser.add_argument('-n', '--name', help="the name of the user/store") parser.add_argument('-o', '--onename', help="the onename id") parser.add_argument('-a', '--avatar', help="the file path to the avatar image") parser.add_argument('-hd', '--header', help="the file path to the header image") parser.add_argument( '-c', '--country', help= "a string consisting of country from protos.countries.CountryCode") # we could add all the fields here but this is good enough to test. args = parser.parse_args(sys.argv[2:]) p = Profile() u = objects.Profile() h = HashMap() if args.name is not None: u.name = args.name if args.country is not None: u.location = countries.CountryCode.Value(args.country.upper()) if args.onename is not None: u.handle = args.onename if args.avatar is not None: with open(args.avatar, "r") as filename: image = filename.read() hash_value = digest(image) u.avatar_hash = hash_value h.insert(hash_value, args.avatar) if args.header is not None: with open(args.header, "r") as filename: image = filename.read() hash_value = digest(image) u.header_hash = hash_value h.insert(hash_value, args.header) u.encryption_key = KeyChain().encryption_pubkey p.update(u)
def update_profile(self, request): try: p = Profile(self.db) if not p.get().encryption_key \ and "name" not in request.args \ and "location" not in request.args: request.write( json.dumps( { "success": False, "reason": "name or location not included" }, indent=4)) request.finish() return False u = objects.Profile() if "name" in request.args: u.name = request.args["name"][0] if "location" in request.args: # This needs to be formatted. Either here or from the UI. u.location = CountryCode.Value( request.args["location"][0].upper()) if "handle" in request.args: u.handle = request.args["handle"][0] if "about" in request.args: u.about = request.args["about"][0] if "short_description" in request.args: u.short_description = request.args["short_description"][0] if "nsfw" in request.args: u.nsfw = bool(request.args["nsfw"][0]) if "vendor" in request.args: u.vendor = bool(request.args["vendor"][0]) if "moderator" in request.args: u.moderator = bool(request.args["moderator"][0]) if "website" in request.args: u.website = request.args["website"][0] if "email" in request.args: u.email = request.args["email"][0] if "primary_color" in request.args: u.primary_color = int(request.args["primary_color"][0]) if "secondary_color" in request.args: u.secondary_color = int(request.args["secondary_color"][0]) if "background_color" in request.args: u.background_color = int(request.args["background_color"][0]) if "text_color" in request.args: u.text_color = int(request.args["text_color"][0]) if "avatar" in request.args: u.avatar_hash = unhexlify(request.args["avatar"][0]) if "header" in request.args: u.header_hash = unhexlify(request.args["header"][0]) if "pgp_key" in request.args and "signature" in request.args: p.add_pgp_key(request.args["pgp_key"][0], request.args["signature"][0], self.keychain.guid.encode("hex")) enc = u.PublicKey() enc.public_key = self.keychain.encryption_pubkey enc.signature = self.keychain.signing_key.sign(enc.public_key)[:64] u.encryption_key.MergeFrom(enc) p.update(u) request.write(json.dumps({"success": True})) request.finish() return server.NOT_DONE_YET except Exception, e: request.write( json.dumps({ "success": False, "reason": e.message }, indent=4)) request.finish() return server.NOT_DONE_YET
def __init__(self, db): self.profile = objects.Profile() self.db = db if self.db.profile.get_proto() is not None: self.profile.ParseFromString(self.db.profile.get_proto())
def __init__(self): self.profile = objects.Profile() self.db = ProfileStore() if self.db.get_proto() is not None: self.profile.ParseFromString(self.db.get_proto())