def seller_send_request_stream(self, order_info): logger.debug("seller send request to proxy ...") order_id = list(order_info.keys())[0] new_order_info = order_info[order_id] market_hash = new_order_info[0] seller_addr = eth_addr_to_string(new_order_info[3]) buyer_addr = eth_addr_to_string(new_order_info[2]) buyer_rsa_public_key = new_order_info[1] proxy_id = eth_addr_to_string(new_order_info[4]) session = get_session() raw_aes_key = session.query(FileInfo.aes_key) \ .filter(FileInfo.market_hash == Encoder.bytes_to_base64_str(market_hash)) \ .all()[0][0] logger.debug("start to encrypt aes key with buyer rsa public key") logger.debug("raw aes key: %s", raw_aes_key) logger.debug("buyer rsa public key: %s", buyer_rsa_public_key) encrypted_aes_key = load_der_public_key( buyer_rsa_public_key, backend=default_backend()).encrypt( raw_aes_key, padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None)) logger.debug("encrypted aes key: %s", encrypted_aes_key) logger.debug("order info got generated") message = Message() seller_data = message.seller_data message.type = Message.SELLER_DATA seller_data.order_id = order_id seller_data.order_type = 'stream' seller_data.seller_addr = seller_addr seller_data.buyer_addr = buyer_addr seller_data.market_hash = Encoder.bytes_to_base64_str(market_hash) seller_data.AES_key = encrypted_aes_key storage = seller_data.storage storage_type = session.query(FileInfo.remote_type) \ .filter(FileInfo.market_hash == Encoder.bytes_to_base64_str(market_hash)) \ .all()[0][0] remote_uri = session.query(FileInfo.remote_uri) \ .filter(FileInfo.market_hash == Encoder.bytes_to_base64_str(market_hash)) \ .all()[0][0] storage.type = storage_type storage.path = remote_uri sign_message = SignMessage() sign_message.public_key = self.wallet.market_client.public_key sign_message.data = message.SerializeToString() sign_message.signature = ECCipher.create_signature( self.wallet.market_client.account.private_key, sign_message.data) try: error, AES_key, urls = yield start_proxy_request( sign_message, proxy_id) if error: logger.error(error) else: logger.debug(AES_key) logger.debug(urls) event.emit(events.SELLER_DELIVERY, order_id) except Exception as e: logger.error(e)
def buyer_send_request(self, order_info): logger.debug("buyer send request to proxy ...") order_id = list(order_info.keys())[0] new_order_info = order_info[order_id] seller_addr = eth_addr_to_string(new_order_info[3]) buyer_addr = eth_addr_to_string(new_order_info[2]) proxy_id = eth_addr_to_string(new_order_info[4]) message = Message() buyer_data = message.buyer_data message.type = Message.BUYER_DATA buyer_data.order_id = order_id buyer_data.order_type = 'file' buyer_data.seller_addr = seller_addr buyer_data.buyer_addr = buyer_addr buyer_data.market_hash = Encoder.bytes_to_base64_str(new_order_info[0]) sign_message = SignMessage() sign_message.public_key = self.wallet.market_client.public_key sign_message.data = message.SerializeToString() sign_message.signature = ECCipher.create_signature( self.wallet.market_client.account.private_key, sign_message.data) error, AES_key, urls = yield start_proxy_request( sign_message, proxy_id) def update_buyer_db(file_uri, file_path): market_hash = Encoder.bytes_to_base64_str(new_order_info[0]) file_uuid = file_uri.split('/')[3] session = get_session() session.query(BuyerFileInfo).filter( BuyerFileInfo.order_id == order_id).update( { BuyerFileInfo.market_hash: market_hash, BuyerFileInfo.is_downloaded: True, BuyerFileInfo.file_uuid: file_uuid, BuyerFileInfo.path: file_path, BuyerFileInfo.size: os.path.getsize(file_path) }, synchronize_session=False) session.commit() if error: logger.error(error) else: file_name = urls[0].split('/')[3] file_dir = join_with_rc(config.wallet.download_dir) # create if not exists os.makedirs(file_dir, exist_ok=True) file_path = os.path.join(file_dir, file_name) yield download_proxy_file(urls[0], file_path) logger.debug("downloaded file path: %s", file_path) decrypted_file = decrypt_file_aes(file_path, AES_key) logger.debug('Decrypted file path: %s', str(decrypted_file)) update_buyer_db(urls[0], decrypted_file) logger.debug("file has been downloaded") logger.debug("put order into confirmed queue, order id: %s", order_id) event.emit(events.BUYER_RECEIVE, order_id)
def get_username(self): key = utils.eth_addr_to_string(self.address) def cb(r): self.name.value = r['username'] wallet.market_client.query_username(public_key=key).addCallbacks(cb)
def buyer_send_request_stream(self, order_info): logger.debug("buyer send request to proxy ...") order_id = list(order_info.keys())[0] new_order_info = order_info[order_id] seller_addr = eth_addr_to_string(new_order_info[3]) buyer_addr = eth_addr_to_string(new_order_info[2]) proxy_id = eth_addr_to_string(new_order_info[4]) message = Message() buyer_data = message.buyer_data message.type = Message.BUYER_DATA buyer_data.order_id = order_id buyer_data.order_type = 'stream' buyer_data.seller_addr = seller_addr buyer_data.buyer_addr = buyer_addr buyer_data.market_hash = Encoder.bytes_to_base64_str(new_order_info[0]) sign_message = SignMessage() sign_message.public_key = self.wallet.market_client.public_key sign_message.data = message.SerializeToString() sign_message.signature = ECCipher.create_signature( self.wallet.market_client.account.private_key, sign_message.data) error, AES_key, urls = yield start_proxy_request( sign_message, proxy_id) def update_buyer_db(stream_id): session = get_session() session.query(BuyerFileInfo).filter( BuyerFileInfo.order_id == order_id).update( { BuyerFileInfo.is_downloaded: True, BuyerFileInfo.path: stream_id, }, synchronize_session=False) session.commit() if error: logger.error(error) else: update_buyer_db(json.dumps(urls)) logger.debug("Stream data has been geted") event.emit(events.BUYER_RECEIVE, order_id)
def seller_send_request(self, order_info): logger.debug("seller send request to proxy ...") order_id = list(order_info.keys())[0] new_order_info = order_info[order_id] market_hash = new_order_info[0] seller_addr = eth_addr_to_string(new_order_info[3]) buyer_addr = eth_addr_to_string(new_order_info[2]) buyer_rsa_public_key = new_order_info[1] proxy_id = eth_addr_to_string(new_order_info[4]) session = get_session() raw_aes_key = session.query(FileInfo.aes_key) \ .filter(FileInfo.market_hash == Encoder.bytes_to_base64_str(market_hash)) \ .all()[0][0] logger.debug("start to encrypt aes key with buyer rsa public key") logger.debug("raw aes key: %s", raw_aes_key) logger.debug("buyer rsa public key: %s", buyer_rsa_public_key) encrypted_aes_key = load_der_public_key( buyer_rsa_public_key, backend=default_backend()).encrypt( raw_aes_key, padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None)) logger.debug("encrypted aes key: %s", encrypted_aes_key) file_hash = session.query(FileInfo.hashcode) \ .filter(FileInfo.market_hash == Encoder.bytes_to_base64_str(market_hash)) \ .all()[0][0] logger.debug("order info got generated") message = Message() seller_data = message.seller_data message.type = Message.SELLER_DATA seller_data.order_id = order_id seller_data.seller_addr = seller_addr seller_data.buyer_addr = buyer_addr seller_data.market_hash = 'market hash' seller_data.AES_key = encrypted_aes_key storage = seller_data.storage # ipfs storage example storage.type = Message.Storage.IPFS ipfs = storage.ipfs ipfs.file_hash = file_hash ipfs.gateway = "192.168.0.132:5001" sign_message = SignMessage() sign_message.public_key = self.wallet.market_client.public_key sign_message.data = message.SerializeToString() sign_message.signature = ECCipher.create_signature( self.wallet.accounts.default_account.private_key, sign_message.data) seller_sign_message = sign_message logger.debug("message created") d_proxy_reply = start_proxy_request(seller_sign_message, proxy_id=proxy_id) def seller_deliver_proxy_callback(proxy_reply): logger.debug("in seller request callback.") if not proxy_reply.error: logger.debug("receive reply from proxy") logger.debug('file_uri: %s', proxy_reply.file_uri) else: logger.debug(proxy_reply.error) d_proxy_reply.addCallback(seller_deliver_proxy_callback)
def buyer_send_request(self, order_info): logger.debug("buyer send request to proxy ...") order_id = list(order_info.keys())[0] new_order_info = order_info[order_id] seller_addr = eth_addr_to_string(new_order_info[3]) buyer_addr = eth_addr_to_string(new_order_info[2]) proxy_id = eth_addr_to_string(new_order_info[4]) message = Message() buyer_data = message.buyer_data message.type = Message.BUYER_DATA buyer_data.order_id = order_id buyer_data.seller_addr = seller_addr buyer_data.buyer_addr = buyer_addr buyer_data.market_hash = 'market hash' sign_message = SignMessage() sign_message.public_key = self.wallet.market_client.public_key sign_message.data = message.SerializeToString() sign_message.signature = ECCipher.create_signature( self.wallet.accounts.default_account.private_key, sign_message.data) buyer_sign_message = sign_message d_proxy_reply = start_proxy_request(buyer_sign_message, proxy_id=proxy_id) def update_buyer_db(file_uri, file_path): market_hash = Encoder.bytes_to_base64_str(new_order_info[0]) file_uuid = file_uri.split('/')[3] session = get_session() session.query(BuyerFileInfo).filter( BuyerFileInfo.order_id == order_id).update( { BuyerFileInfo.market_hash: market_hash, BuyerFileInfo.is_downloaded: True, BuyerFileInfo.file_uuid: file_uuid, BuyerFileInfo.path: file_path, BuyerFileInfo.size: os.path.getsize(file_path) }, synchronize_session=False) session.commit() return market_hash def buyer_request_proxy_callback(proxy_reply): logger.debug("in buyer request callback.") if not proxy_reply.error: logger.debug('file_uri: %s', proxy_reply.file_uri) file_dir = os.path.expanduser(config.wallet.download_dir) file_name = proxy_reply.file_uri.split('/')[3] file_path = os.path.join(file_dir, file_name) logger.debug("downloaded file path: %s", file_path) decrypted_file = decrypt_file_aes(file_path, proxy_reply.AES_key) logger.debug('Decrypted file path: %s', str(decrypted_file)) update_buyer_db(proxy_reply.file_uri, decrypted_file) logger.debug("file has been downloaded") logger.debug("put order into confirmed queue, order id: %s", order_id) self.confirmed_order_queue.put(order_id) self.wallet.main_wnd.update_purchased_tab('downloaded') else: logger.debug(proxy_reply.error) d_proxy_reply.addCallback(buyer_request_proxy_callback)
def add_orders_ui(self, widget): height = widget.height() layout = widget.layout() order = None status = 0 start = 1 is_seller = self.owner_address == wallet.market_client.public_key # Sales if app.products_order.get(self.market_hash): self.sales_header = DetailHeader("{} Sales".format( len(app.products_order.get(self.market_hash)))) layout.insertWidget(start, self.sales_header) start += 1 # enum State { # Created, # SellerConfirmed, # ProxyFetched, # ProxyDelivered, # BuyerConfirmed, # Finished, # SellerRated, # BuyerRated, # Disputed, # Withdrawn # } myaddress = get_address_from_public_key_object( wallet.market_client.public_key) self.salesElem = [] need_show = False for order in app.products_order[self.market_hash]: # not buyer and not seller buyer_addr = eth_addr_to_string(order['buyer_addr']) is_buyer = buyer_addr == myaddress if not is_buyer and not is_seller: continue need_show = True self.buy.setEnabled(False) status = order['status'] if status == 0: current = 1 elif status == 1: current = 1 elif status == 2: current = 2 elif status == 3: current = 3 else: current = 4 sale1 = Sale( image=abs_path('icons/avatar.jpeg'), name=order['public_key'], current=current, timestamps=[ "May 2, 08:09:08", "May 2, 08:09:08", "May 2, 08:09:08", "May 2, 08:09:08" ], order_id=order["order_id"], mhash=self.market_hash, is_buyer=is_buyer, is_seller=is_seller, order_type='stream' if self.ptype == 'stream' else 'file') layout.insertWidget(start, sale1) self.salesElem.append(sale1) start += 1 height += 200 if not need_show: self.sales_header.hide() # Order Detail if status < 1: self.status_text.value = "Created on May 2, 08:09:08" elif status == 1: self.status_text.value = "Delivered on May 2, 08:09:08" elif status <= 3: self.status_text.value = "Received on May 2, 08:09:08" elif status > 3: self.status_text.value = "Confirmed on May 2, 08:09:08" if order and self.owner_address != wallet.market_client.public_key: if status > 2: order_header = DetailHeader('Order Detail') layout.insertWidget(start, order_header) start += 1 height += 100 has_comfirmed = status > 3 if self.ptype == 'file': self.data_type = 'batch' self.order_detail = OrderDetail( order_time=Model("2018/6/15 08:40:39"), status=self.status_text, order_id=order["order_id"], name=self.name.value, data_type=self.data_type, has_comfirmed=has_comfirmed) layout.insertWidget(start, self.order_detail) start += 1 height += 100 if self.ptype == 'stream': self.data_type = 'stream' order_detail = OrderDetail( order_time=Model("2018/6/15 08:40:39"), status=self.status_text, order_id=order["order_id"], market_hash=self.market_hash, name=self.name.value, data_type=self.data_type, has_comfirmed=has_comfirmed) layout.insertWidget(start, order_detail) start += 1 height += 100 widget.setFixedHeight(height)