Esempio n. 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)
Esempio n. 2
0
    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)
Esempio n. 3
0
    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)
Esempio n. 4
0
    def proxy_reply_success(self):
        trade = self.trade
        message = Message()
        message.type = Message.PROXY_REPLY
        proxy_reply = message.proxy_reply
        proxy_reply.AES_key = trade.AES_key
        proxy_reply.file_uuid = trade.file_uuid

        string = message.SerializeToString()
        self.sendString(string)
        self.transport.loseConnection()
Esempio n. 5
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)
Esempio n. 6
0
    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)
Esempio n. 7
0
    def send_buyer_message(self, order):
        global kad_port

        message = Message()
        buyer_data = message.buyer_data
        message.type = Message.BUYER_DATA
        buyer_data.order_id = order['order_id']
        buyer_data.order_type = order['order_type']
        buyer_data.seller_addr = order['seller']
        buyer_data.buyer_addr = order['buyer']
        buyer_data.market_hash = order['market_hash']

        sign_message = SignMessage()
        sign_message.public_key = self.public_key
        sign_message.data = message.SerializeToString()
        sign_message.signature = ECCipher.create_signature(
            self.private_key,
            sign_message.data
            )

        kad_port += 1

        proxy_id = order['proxy']

        error, AES_key, urls = yield start_proxy_request(sign_message, proxy_id, kad_port)

        if error:
            print(error)
        else:
            print(urls)
            if order['order_type'] == 'file':
                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)
Esempio n. 8
0
    def send_seller_message(self, order):
        global kad_port

        message = Message()
        seller_data = message.seller_data
        message.type = Message.SELLER_DATA
        seller_data.order_id = order['order_id']
        seller_data.order_type = order['order_type']
        seller_data.seller_addr = order['seller']
        seller_data.buyer_addr = order['buyer']
        seller_data.market_hash = order['market_hash']
        seller_data.AES_key = order['AES_key']

        storage = seller_data.storage

        storage.type = order['storage_type']
        storage.path = order['storage_path']

        sign_message = SignMessage()
        sign_message.public_key = self.public_key
        sign_message.data = message.SerializeToString()
        sign_message.signature = ECCipher.create_signature(
            self.private_key,
            sign_message.data
            )

        kad_port += 1

        proxy_id = order['proxy']

        error, AES_key, urls = yield start_proxy_request(sign_message, proxy_id, kad_port)

        if error:
            print(error)
        else:
            print(urls)
Esempio n. 9
0
def buyer_request():
    message = Message()
    buyer_data = message.buyer_data
    message.type = Message.BUYER_DATA
    buyer_data.order_id = order_id
    buyer_data.order_type = order_type
    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 = buyer_public_key
    sign_message.data = message.SerializeToString()
    sign_message.signature = ECCipher.create_signature(buyer_private_key,
                                                       sign_message.data)

    proxy_list = yield pick_proxy()
    proxy_id = proxy_list[0]  # should be selected by UI from proxy list

    if proxy_id:
        error, AES_key, urls = yield start_proxy_request(
            sign_message, proxy_id)

        if error:
            print(error)
        else:
            print(AES_key)
            print(urls)

            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)
Esempio n. 10
0
    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)
Esempio n. 11
0
    test_type = 'buyer_data'

    buyer_private_key = None
    buyer_private_key = b'\xa6\xf8_\xee\x1c\x85\xc5\x95\x8d@\x9e\xfa\x80\x7f\xb6\xe0\xb4u\x12\xb6\xdf\x00\xda4\x98\x8e\xaeR\x89~\xf6\xb5'
    buyer_private_key, buyer_public_key = \
                ECCipher.generate_key_pair(buyer_private_key)

    seller_private_key = None
    seller_private_key = b'\xa6\xf8_\xee\x1c\x85\xc5\x95\x8d@\x9e\xfa\x80\x7f\xb6\xe0\xb4u\x12\xb6\xdf\x00\xda4\x98\x8e\xaeR\x89~\xf6\xb5'
    seller_private_key, seller_public_key = \
                ECCipher.generate_key_pair(seller_private_key)

    if test_type == 'seller_data':
        message = Message()
        seller_data = message.seller_data
        message.type = Message.SELLER_DATA
        seller_data.order_id = 1
        seller_data.seller_addr = seller_public_key
        seller_data.buyer_addr = buyer_public_key
        seller_data.market_hash = b'MARKET_HASH'
        seller_data.AES_key = b'AES_key'
        storage = seller_data.storage
        storage.type = Message.Storage.IPFS
        ipfs = storage.ipfs
        ipfs.file_hash = b'QmT4kFS5gxzQZJwiDJQ66JLVGPpyTCF912bywYkpgyaPsD'
        ipfs.gateway = "192.168.0.132:5001"

        sign_message = SignMessage()
        sign_message.public_key = seller_public_key
        sign_message.data = message.SerializeToString()
        sign_message.signature = ECCipher.generate_signature(
Esempio n. 12
0
def seller_request():
    # seller_request
    message = Message()
    seller_data = message.seller_data
    message.type = Message.SELLER_DATA
    seller_data.order_id = order_id
    seller_data.order_type = order_type
    seller_data.seller_addr = seller_addr
    seller_data.buyer_addr = buyer_addr
    seller_data.market_hash = 'MARKET_HASH'
    seller_data.AES_key = b'AES_key'

    storage = seller_data.storage

    #stream test
    storage.type = 'stream'

    storage_plugin = "cpchain.storage_plugin."
    module = importlib.import_module(storage_plugin + storage.type)
    s = module.Storage()
    param = yield s.user_input_param()
    param['proxy_id'] = param['proxy_id'][
        0]  # should be selected by UI from proxy list
    storage.path = yield s.upload_data(None, param)

    # proxy storage
    # storage.type = 'proxy'

    # storage_plugin = "cpchain.storage_plugin."
    # module = importlib.import_module(storage_plugin + storage.type)
    # s = module.Storage()
    # param = yield s.user_input_param()
    # param['proxy_id'] = param['proxy_id'][0] # should be selected by UI from proxy list
    # storage.path = yield s.upload_data('/bin/bash', param)

    # ipfs storage
    # storage.type = 'ipfs'

    # storage_plugin = "cpchain.storage_plugin."
    # module = importlib.import_module(storage_plugin + storage.type)
    # s = module.Storage()
    # param = yield s.user_input_param()
    # storage.path = yield s.upload_data('/bin/bash', param)

    # # S3 storage
    # storage.type = 's3'

    # storage_plugin = "cpchain.storage_plugin."
    # module = importlib.import_module(storage_plugin + storage.type)
    # s = module.Storage()
    # param = yield s.user_input_param()
    # storage.path = yield s.upload_data('/bin/bash', param)

    sign_message = SignMessage()
    sign_message.public_key = seller_public_key
    sign_message.data = message.SerializeToString()
    sign_message.signature = ECCipher.create_signature(seller_private_key,
                                                       sign_message.data)

    proxy_list = yield pick_proxy()
    proxy_id = proxy_list[0]  # should be selected by UI from proxy list

    if proxy_id:
        error, AES_key, urls = yield start_proxy_request(
            sign_message, proxy_id)

        if error:
            print(error)
        else:
            print(AES_key)
            print(urls)