Exemple #1
0
    def send_request(self, order_id):
        new_order_info = self.buyer.query_order(order_id)
        message = Message()
        buyer_data = message.buyer_data
        message.type = Message.BUYER_DATA
        buyer_data.order_id = order_id
        buyer_data.seller_addr = to_bytes(hexstr=new_order_info[3])
        buyer_data.buyer_addr = to_bytes(hexstr=new_order_info[2])
        buyer_data.market_hash = new_order_info[0]

        sign_message = SignMessage()
        sign_message.public_key = Encoder.str_to_base64_byte(
            self.market_client.pub_key)
        sign_message.data = message.SerializeToString()
        sign_message.signature = crypto.ECCipher.generate_signature(
            Encoder.str_to_base64_byte(self.market_client.priv_key),
            sign_message.data)
        d = start_client(sign_message)

        def update_buyer_db(file_uuid, file_path, new_order_id):
            market_hash = Encoder.bytes_to_base64_str(
                self.buyer.query_order(new_order_id)[0])
            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(message):
            print("Inside buyer request callback.")
            assert message.type == Message.PROXY_REPLY
            proxy_reply = message.proxy_reply

            if not proxy_reply.error:
                print('file_uuid: %s' % proxy_reply.file_uuid)
                print('AES_key: ')
                print(len(proxy_reply.AES_key))
                print(proxy_reply.AES_key)
                file_dir = os.path.expanduser(config.wallet.download_dir)
                file_path = os.path.join(file_dir, proxy_reply.file_uuid)
                print(file_path)
                decrypted_file = decrypt_file_aes(file_path,
                                                  proxy_reply.AES_key)
                print('Decrypted file path ' + str(decrypted_file))

                update_buyer_db(proxy_reply.file_uuid, decrypted_file,
                                order_id)
                self.update_treasure_pane()

                self.confirm_order(order_id)
                self.order_id_list.remove(order_id)

            else:
                print(proxy_reply.error)

        d.addBoth(buyer_request_proxy_callback)
Exemple #2
0
    def buy_product(self, msg_hash, file_title, proxy, seller, value):
        logger.debug("start to buy product")
        seller_addr = get_address_from_public_key_object(seller)
        desc_hash = Encoder.str_to_base64_byte(msg_hash)
        rsa_key = RSACipher.load_public_key()
        # rsa_key = self.broker.wallet.market_client.public_key
        logger.debug("desc hash: %s", desc_hash)
        logger.info('Value: %s', value)
        product = OrderInfo(
            desc_hash=desc_hash,
            buyer_rsa_pubkey=rsa_key,
            seller=self.broker.buyer.web3.toChecksumAddress(seller_addr),
            proxy=self.broker.buyer.web3.toChecksumAddress(proxy),
            secondary_proxy=self.broker.buyer.web3.toChecksumAddress(proxy),
            proxy_value=10,
            value=value,
            time_allowed=3600 * 24
        )
        logger.debug("product info has been created")
        logger.debug("product info: %s", product)
        logger.debug("seller address: %s", product.seller)
        d_placed_order = deferToThread(self.broker.buyer.place_order, product)

        def add_bought_order(order_id):
            logger.debug("start to update local database")
            new_buyer_file_info = BuyerFileInfo(order_id=order_id,
                                                market_hash=Encoder.bytes_to_base64_str(desc_hash),
                                                file_title=file_title, is_downloaded=False)
            add_file(new_buyer_file_info)
            logger.debug("update local db completed")
            event.emit(events.PAY, (order_id, msg_hash))
            # Update the purchased downloaded tab in the main window of wallet
        d_placed_order.addCallback(add_bought_order)
Exemple #3
0
    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
Exemple #4
0
 def send_request(self):
     new_order = self.query_new_order()
     if len(new_order) != 0:
         for new_order_id in new_order:
             new_order_info = self.seller.query_order(new_order_id)
             print('In seller send request: new oder infomation:')
             market_hash = new_order_info[0]
             buyer_rsa_pubkey = new_order_info[1]
             raw_aes_key = session.query(FileInfo.aes_key)\
                 .filter(FileInfo.market_hash == Encoder.bytes_to_base64_str(market_hash))\
                 .all()[0][0]
             encrypted_aes_key = load_der_public_key(
                 buyer_rsa_pubkey, backend=default_backend()).encrypt(
                     raw_aes_key,
                     padding.OAEP(
                         mgf=padding.MGF1(algorithm=hashes.SHA256()),
                         algorithm=hashes.SHA256(),
                         label=None))
             print("Encrypted_aes_key length" + str(len(encrypted_aes_key)))
             storage_type = Message.Storage.IPFS
             ipfs_gateway = config.proxy.default_ipfs_gateway
             file_hash = session.query(FileInfo.hashcode)\
                 .filter(FileInfo.market_hash == Encoder.bytes_to_base64_str(market_hash))\
                 .all()[0][0]
             message = Message()
             seller_data = message.seller_data
             message.type = Message.SELLER_DATA
             seller_data.order_id = new_order_id
             seller_data.seller_addr = to_bytes(hexstr=new_order_info[3])
             seller_data.buyer_addr = to_bytes(hexstr=new_order_info[2])
             seller_data.market_hash = market_hash
             seller_data.AES_key = encrypted_aes_key
             storage = seller_data.storage
             storage.type = storage_type
             ipfs = storage.ipfs
             ipfs.file_hash = file_hash.encode('utf-8')
             ipfs.gateway = ipfs_gateway
             sign_message = SignMessage()
             sign_message.public_key = Encoder.str_to_base64_byte(
                 self.market_client.pub_key)
             sign_message.data = message.SerializeToString()
             sign_message.signature = crypto.ECCipher.generate_signature(
                 Encoder.str_to_base64_byte(self.market_client.priv_key),
                 sign_message.data)
             d = start_client(sign_message)
             d.addBoth(self.seller_deliver_proxy_callback)
Exemple #5
0
 def proxy_claim_relay(self):
     proxy_trans = ProxyTrans(default_web3, config.chain.core_contract)
     private_key_file_path = join_with_root(config.wallet.private_key_file)
     password_path = join_with_root(config.wallet.private_key_password_file)
     with open(password_path) as f:
         password = f.read()
     priv_key, pub_key = ECCipher.geth_load_key_pair_from_private_key(
         private_key_file_path, password)
     priv_key_bytes = Encoder.str_to_base64_byte(priv_key)
     digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
     digest.update(
         ECCipher.generate_signature(priv_key_bytes,
                                     to_bytes(self.trade.order_id),
                                     password))
     deliver_hash = digest.finalize()
     tx_hash = proxy_trans.claim_relay(self.trade.order_id, deliver_hash)
     return tx_hash
Exemple #6
0
    def buy_product(self, msg_hash, file_title, proxy, seller):
        logger.debug("start to buy product")
        seller_addr = get_address_from_public_key_object(seller)
        desc_hash = Encoder.str_to_base64_byte(msg_hash)
        rsa_key = RSACipher.load_public_key()
        logger.debug("desc hash: %s", desc_hash)
        product = OrderInfo(
            desc_hash=desc_hash,
            buyer_rsa_pubkey=rsa_key,
            seller=self.broker.buyer.web3.toChecksumAddress(seller_addr),
            proxy=self.broker.buyer.web3.toChecksumAddress(proxy),
            secondary_proxy=self.broker.buyer.web3.toChecksumAddress(proxy),
            proxy_value=10,
            value=20,
            time_allowed=1000)
        logger.debug("product info has been created")
        logger.debug("product info: %s", product)
        logger.debug("seller address: %s", product.seller)
        logger.debug("bought order queue size: %s",
                     self.broker.bought_order_queue.qsize())
        d_placed_order = deferToThread(self.broker.buyer.place_order, product)

        def add_bought_order(order_id):
            logger.debug("order has been placed to chain")
            self.broker.bought_order_queue.put(order_id)
            logger.debug("new order has been put into bought order queue")
            logger.debug("bought order queue size: %s",
                         self.broker.bought_order_queue.qsize())
            logger.debug("start to update local database")
            new_buyer_file_info = BuyerFileInfo(
                order_id=order_id,
                market_hash=Encoder.bytes_to_base64_str(desc_hash),
                file_title=file_title,
                is_downloaded=False)
            add_file(new_buyer_file_info)
            logger.debug("update local db completed")

            # Update the purchased downloaded tab in the main window of wallet
            self.broker.wallet.main_wnd.update_purchased_tab('downloading')

        d_placed_order.addCallback(add_bought_order)

        return self.broker.bought_order_queue