def __init__(self, wallet): self.wallet = wallet bin_path = join_with_root(config.chain.contract_bin_path) # deploy_contract(bin_path, config.chain.contract_name, default_w3) account = get_address_from_public_key_object(self.wallet.market_client.public_key) if default_w3: account = default_w3.toChecksumAddress(account) self.buyer = BuyerAgent(default_w3, bin_path, config.chain.contract_name, account) self.seller = SellerAgent(default_w3, bin_path, config.chain.contract_name, account) self.handler = Handler(self) self.monitor = Monitor(self)
def __init__(self, account): self.address = account.address self.account = account self.buyer_agent = BuyerAgent(w3, self.contract_path, self.contract_name, self.address) self.seller_agent = SellerAgent(w3, self.contract_path, self.contract_name, self.address) self.proxy_agent = ProxyAgent(w3, self.contract_path, self.contract_name, self.address) self.rsa_public_key = generate_rsa_public_key()
def __init__(self, wallet): self.wallet = wallet # order_queue: info, ready_order_queue: info, bought_order_queue: id, confirmed_order_queue: id self.order_queue = Queue() self.bought_order_queue = Queue() self.ready_order_queue = Queue() self.confirmed_order_queue = Queue() bin_path = join_with_root(config.chain.contract_bin_path) # deploy_contract(bin_path, config.chain.contract_name, default_w3) account = get_address_from_public_key_object( self.wallet.market_client.public_key) self.buyer = BuyerAgent(default_w3, bin_path, config.chain.contract_name, account) self.seller = SellerAgent(default_w3, bin_path, config.chain.contract_name, account) self.handler = Handler(self) self.monitor = Monitor(self)
class Broker: def __init__(self, wallet): self.wallet = wallet bin_path = join_with_root(config.chain.contract_bin_path) # deploy_contract(bin_path, config.chain.contract_name, default_w3) account = get_address_from_public_key_object(self.wallet.market_client.public_key) if default_w3: account = default_w3.toChecksumAddress(account) self.buyer = BuyerAgent(default_w3, bin_path, config.chain.contract_name, account) self.seller = SellerAgent(default_w3, bin_path, config.chain.contract_name, account) self.handler = Handler(self) self.monitor = Monitor(self) @defer.inlineCallbacks def query_seller_products_order(self, seller): # query all order of seller's products, then save it in memory orders = yield self.seller.get_all_orders() return orders def confirm_order(self, order_id_list): logger.debug("in buyer confirm order") for current_id in order_id_list: self.buyer.confirm_order(current_id) logger.debug("order %s completed", current_id) @defer.inlineCallbacks 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) @defer.inlineCallbacks 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) 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 = 'file' 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.exception(e) @defer.inlineCallbacks 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) @defer.inlineCallbacks 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)
class Player: contract_path = join_with_root(config.chain.contract_bin_path) contract_name = config.chain.contract_name def __init__(self, account): self.address = account.address self.account = account self.buyer_agent = BuyerAgent(w3, self.contract_path, self.contract_name, self.address) self.seller_agent = SellerAgent(w3, self.contract_path, self.contract_name, self.address) self.proxy_agent = ProxyAgent(w3, self.contract_path, self.contract_name, self.address) self.rsa_public_key = generate_rsa_public_key() def query_order(self, order_id): return self.seller_agent.query_order(order_id) def mockup_product(self): private_key = ECCipher.create_private_key(self.account.privateKey) public_key = ECCipher.create_public_key_from_private_key(private_key) public_key = ECCipher.serialize_public_key(public_key) price = randint(1, 10000) * 0.01 return { 'description': 'stress testing description', 'signature': '304502201b31544ff65ebbfff3743a0b2536388e1f6cf2533de4bbf0ffab57d762f54d50022100a8101face8c6a6d2f0e01a9a01ea49d80598c880872db9e3c1e9eb27a23a2554', 'cover_image': 'upload_images/20181127072441_13.png', 'end_date': '2018-12-04T15:24:41.664517Z', 'msg_hash': 'dRFWYcWC9TKujDo7mLiUCq00MthHezzkyncAGIdaEos=', 'id': 53, 'start_date': '2018-11-27T15:24:41.664517Z', 'ptype': 'file', 'seq': 0, 'owner_address': public_key, 'tags': 'stress testing tags', 'price': price, 'title': 'stress testing product', 'category': 'stress testing category', 'status': 0, 'created': '2018-11-27T07:24:41.715315Z' } def buyer_place_order(self, product, proxy): buyer_rsa_pubkey = self.rsa_public_key desc_hash = Encoder.str_to_base64_byte(product['msg_hash']) public_key = ECCipher.create_public_key( Encoder.hex_to_bytes(product['owner_address'])) seller_addr = w3.toChecksumAddress( ECCipher.get_address_from_public_key(public_key)) proxy = w3.toChecksumAddress(proxy) product = OrderInfo(desc_hash=desc_hash, buyer_rsa_pubkey=buyer_rsa_pubkey, seller=seller_addr, proxy=proxy, secondary_proxy=proxy, proxy_value=100, value=product['price'], time_allowed=3600 * 24) w3.personal.unlockAccount(self.account.address, _passphrase) order_id = self.buyer_agent.place_order(product) # w3.personal.lockAccount(self.account.address) # return -1 if failure return order_id def seller_confirm_order(self, order_id): w3.personal.unlockAccount(self.account.address, _passphrase) status = self.seller_agent.confirm_order(order_id) # w3.personal.lockAccount(self.account.address) return status def proxy_claim_fetched(self, order_id): w3.personal.unlockAccount(self.account.address, _passphrase) status = self.proxy_agent.claim_fetched(order_id) # w3.personal.lockAccount(self.account.address) return status def proxy_claim_delivered(self, order_id): w3.personal.unlockAccount(self.account.address, _passphrase) status = self.proxy_agent.claim_delivered(order_id, b'dummy') # w3.personal.lockAccount(self.account.address) return status def buyer_confirm_order(self, order_id): w3.personal.unlockAccount(self.account.address, _passphrase) status = self.buyer_agent.confirm_order(order_id) # w3.personal.lockAccount(self.account.address) return status