def parse_results(values): if values is not None: for v in values: try: val = Value() val.ParseFromString(v) n = objects.Node() n.ParseFromString(val.serializedData) node_to_ask = Node( n.guid, n.nodeAddress.ip, n.nodeAddress.port, n.publicKey, None if not n.HasField("relayAddress") else (n.relayAddress.ip, n.relayAddress.port), n.natType, n.vendor) if n.guid == KeyChain(self.factory.db).guid: proto = self.factory.db.listings.get_proto() l = Listings() l.ParseFromString(proto) for listing in l.listing: if listing.contract_hash == val.valueKey: respond(listing, node_to_ask) else: self.factory.mserver.get_contract_metadata(node_to_ask, val.valueKey)\ .addCallback(respond, node_to_ask) except Exception: pass
def setUp(self): datastore.create_database(":memory:") self.test_hash = "87e0555568bf5c7e4debd6645fc3f41e88df6ca8" self.test_hash2 = "97e0555568bf5c7e4debd6645fc3f41e88df6ca8" self.test_file = "Contents of test.txt" self.test_file2 = "Contents of test2.txt" self.sp = Profile() self.sp.name = "Test User" self.sp.encryption_key = "Key" self.sp.location = CountryCode.Value('UNITED_STATES') self.serialized_listings = Listings() self.lm = self.serialized_listings.ListingMetadata() self.lm.contract_hash = self.test_hash self.lm.title = "TEST CONTRACT TITLE" self.lm.price = 0 self.lm.currency_code = "USD" self.lm.nsfw = False self.lm.origin = CountryCode.Value('ALL') self.hm = datastore.HashMap() self.hm.delete_all() self.ps = datastore.ProfileStore() self.ls = datastore.ListingsStore() self.ks = datastore.KeyStore()
def test_addListing(self): self.ls.delete_all_listings() self.ls.add_listing(self.lm) l = self.ls.get_proto() val = Listings() val.ParseFromString(l) self.assertEqual(self.lm, val.listing[0])
def rpc_get_ratings(self, sender, listing_hash=None): a = "ALL" if listing_hash is None else listing_hash.encode("hex") self.log.info("serving ratings for contract %s to %s" % (a, sender)) self.router.addContact(sender) try: ratings = [] if listing_hash: for rating in self.db.Ratings().get_ratings( listing_hash.encode("hex")): ratings.append( json.loads(rating[0], object_pairs_hook=OrderedDict)) else: proto = self.db.ListingsStore().get_proto() l = Listings() l.ParseFromString(proto) for listing in l.listing: for rating in self.db.Ratings().get_ratings( listing.contract_hash.encode("hex")): ratings.append( json.loads(rating[0], object_pairs_hook=OrderedDict)) ret = json.dumps(ratings).encode("zlib") return [str(ret), self.signing_key.sign(ret)[:64]] except Exception: self.log.warning("could not load ratings for contract %s" % a) return None
def setUp(self): self.db = Database(filepath="test.db") self.test_hash = "87e0555568bf5c7e4debd6645fc3f41e88df6ca8" self.test_hash2 = "97e0555568bf5c7e4debd6645fc3f41e88df6ca8" self.test_file = "Contents of test.txt" self.test_file2 = "Contents of test2.txt" self.sp = Profile() self.key = Profile().PublicKey() self.key.public_key = "Key" self.key.signature = "Sig" self.sp.name = "Test User" self.sp.guid_key.MergeFrom(self.key) self.sp.location = CountryCode.Value('UNITED_STATES') self.serialized_listings = Listings() self.lm = self.serialized_listings.ListingMetadata() self.lm.contract_hash = self.test_hash self.lm.title = "TEST CONTRACT TITLE" self.lm.price = 0 self.lm.currency_code = "USD" self.lm.nsfw = False self.lm.origin = CountryCode.Value('ALL') self.u = Following.User() self.u.guid = '0000000000000000000000000000000000' self.u.pubkey = 'signed_pubkey' self.m = Metadata() self.m.name = 'Test User' self.m.handle = '@TestUser' self.m.avatar_hash = '' self.m.nsfw = False self.u.metadata.MergeFrom(self.m) self.f = Followers.Follower() self.f.guid = '0000000000000000000000000000000001' self.f.following = '' self.f.pubkey = '' self.f.metadata.MergeFrom(self.m) self.hm = self.db.filemap self.hm.delete_all() self.ps = self.db.profile self.ls = self.db.listings self.ks = self.db.keys self.fd = self.db.follow self.ms = self.db.messages self.ns = self.db.notifications self.vs = self.db.vendors self.bs = self.db.broadcasts self.moderators = self.db.moderators self.purchases = self.db.purchases self.sales = self.db.sales self.settings = self.db.settings
def save(self): """ Saves the json contract into the OpenBazaar/store/listings/contracts/ directory. It uses the title as the file name so it's easy on human eyes. A mapping of the hash of the contract and file path is stored in the database so we can retrieve the contract with only its hash. Additionally, the contract metadata (sent in response to the GET_LISTINGS query) is saved in the db for fast access. """ # get the contract title to use as the file name and format it file_name = str( self.contract["vendor_offer"]["listing"]["item"]["title"][:100]) file_name = re.sub(r"[^\w\s]", '', file_name) file_name = re.sub(r"\s+", '_', file_name) file_name += digest(json.dumps(self.contract, indent=4)).encode("hex")[:8] # save the json contract to the file system file_path = DATA_FOLDER + "store/listings/contracts/" + file_name + ".json" with open(file_path, 'w') as outfile: outfile.write(json.dumps(self.contract, indent=4)) # Create a `ListingMetadata` protobuf object using data from the full contract listings = Listings() data = listings.ListingMetadata() data.contract_hash = digest(json.dumps(self.contract, indent=4)) vendor_item = self.contract["vendor_offer"]["listing"]["item"] data.title = vendor_item["title"] if "image_hashes" in vendor_item: data.thumbnail_hash = unhexlify(vendor_item["image_hashes"][0]) if "category" in vendor_item: data.category = vendor_item["category"] if "bitcoin" not in vendor_item["price_per_unit"]: data.price = float(vendor_item["price_per_unit"]["fiat"]["price"]) data.currency_code = vendor_item["price_per_unit"]["fiat"][ "currency_code"] else: data.price = float(vendor_item["price_per_unit"]["bitcoin"]) data.currency_code = "BTC" data.nsfw = vendor_item["nsfw"] if "shipping" not in self.contract["vendor_offer"]["listing"]: data.origin = CountryCode.Value("NA") else: data.origin = CountryCode.Value( self.contract["vendor_offer"]["listing"]["shipping"] ["shipping_origin"].upper()) for region in self.contract["vendor_offer"]["listing"]["shipping"][ "shipping_regions"]: data.ships_to.append(CountryCode.Value(region.upper())) # save the mapping of the contract file path and contract hash in the database self.db.HashMap().insert(data.contract_hash, file_path) # save the `ListingMetadata` protobuf to the database as well self.db.ListingsStore().add_listing(data)
def test_deleteListing(self): self.ls.delete_all_listings() self.ls.add_listing(self.lm) self.ls.delete_listing(self.test_hash) l = self.ls.get_proto() val = Listings() val.ParseFromString(l) self.assertEqual(0, len(val.listing)) # Try to delete when table is already empty self.ls.delete_all_listings() self.assertEqual(None, self.ls.delete_listing(self.test_hash))
def rpc_get_listings(self, sender): self.log.info("serving store listings to %s" % sender) self.router.addContact(sender) try: p = Profile(self.db).get() l = Listings() l.ParseFromString(self.db.ListingsStore().get_proto()) l.handle = p.handle l.avatar_hash = p.avatar_hash return [l.SerializeToString(), self.signing_key.sign(l.SerializeToString())[:64]] except Exception: self.log.warning("could not find any listings in the database") return None
def delete_listing(self, hash_value): cursor = self.db.cursor() ser = self.get_proto() if ser is None: return l = Listings() l.ParseFromString(ser) for listing in l.listing: if listing.contract_hash == hash_value: l.listing.remove(listing) cursor.execute('''INSERT OR REPLACE INTO listings(id, serializedListings) VALUES (?,?)''', (1, l.SerializeToString())) self.db.commit()
def rpc_get_contract_metadata(self, sender, contract_hash): self.log.info("serving metadata for contract %s to %s" % (contract_hash.encode("hex"), sender)) self.router.addContact(sender) try: proto = self.db.ListingsStore().get_proto() l = Listings() l.ParseFromString(proto) for listing in l.listing: if listing.contract_hash == contract_hash: ser = listing.SerializeToString() return [ser, self.signing_key.sign(ser)[:64]] except Exception: self.log.warning("could not find metadata for contract %s" % contract_hash.encode("hex")) return None
def add_listing(self, proto): """ Will also update an existing listing if the contract hash is the same. """ cursor = self.db.cursor() l = Listings() ser = self.get_proto() if ser is not None: l.ParseFromString(ser) for listing in l.listing: if listing.contract_hash == proto.contract_hash: l.listing.remove(listing) l.listing.extend([proto]) cursor.execute('''INSERT OR REPLACE INTO listings(id, serializedListings) VALUES (?,?)''', (1, l.SerializeToString())) self.db.commit()
def rpc_get_contract_metadata(self, sender, contract_hash): self.log.info("Fetching metadata for contract %s" % hexlify(contract_hash)) self.router.addContact(sender) try: proto = ListingsStore().get_proto() l = Listings() l.ParseFromString(proto) for listing in l.listing: if listing.contract_hash == contract_hash: ser = listing.SerializeToString() return [ser, self.signing_key.sign(ser)[:64]] except Exception: self.log.warning("Could not find metadata for contract %s" % hexlify(contract_hash)) return ["None"]
def rpc_get_listings(self, sender): self.log.info("serving store listings to %s" % sender) self.audit.record(sender.id.encode("hex"), "GET_LISTINGS") self.router.addContact(sender) try: p = Profile(self.db).get() l = Listings() l.ParseFromString(self.db.listings.get_proto()) l.handle = p.handle l.avatar_hash = p.avatar_hash for listing in l.listing: if listing.hidden: l.listing.remove(listing) return [ l.SerializeToString(), self.signing_key.sign(l.SerializeToString())[:64] ] except Exception: self.log.warning("could not find any listings in the database") return None
def parse_results(values): if values is not None: for v in values: try: val = Value() val.ParseFromString(v) n = objects.Node() n.ParseFromString(val.serializedData) node_to_ask = Node(n.guid, n.ip, n.port, n.signedPublicKey, True) if n.guid == KeyChain(self.factory.db).guid: proto = self.factory.db.ListingsStore().get_proto() l = Listings() l.ParseFromString(proto) for listing in l.listing: if listing.contract_hash == val.valueKey: respond(listing, node_to_ask) else: self.factory.mserver.get_contract_metadata(node_to_ask, val.valueKey)\ .addCallback(respond, node_to_ask) except Exception: pass