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)
def callback(path): try: # Save proxy = self.proxy.current self.aes_key = AESCipher.generate_key() remote_uri = str(path) new_file_info = FileInfo( name=self.data_name.value, data_type='stream', proxy=proxy, remote_type='stream', remote_uri=remote_uri, public_key=wallet.market_client.public_key, is_published=False, created=func.current_timestamp(), aes_key=self.aes_key) fs.add_file(new_file_info) self._id = new_file_info.id encrypted_key = RSACipher.encrypt(self.aes_key) encrypted_key = Encoder.bytes_to_base64_str(encrypted_key) wallet.market_client.upload_file_info(None, None, 0, self._id, 'stream', json.dumps(remote_uri), self.data_name.value, encrypted_key) path = json.loads(path) self.uploaded.emit(path) except Exception as err: logger.error(err)
def decrypt_file_aes(file_path, aes_key): decrypted_aes_key = RSACipher.decrypt(aes_key) print('In Decrypt file aes:' + str(len(decrypted_aes_key))) decrypter = AESCipher(decrypted_aes_key) decrypted_path = file_path + "_decrypted" decrypter.decrypt(file_path, decrypted_path) return decrypted_path
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
def handle_ok_callback(self, file_id): file_info = fs.get_file_by_id(file_id) hashcode = file_info.hashcode path = file_info.path size = file_info.size product_id = file_info.id remote_type = file_info.remote_type remote_uri = file_info.remote_uri name = file_info.name encrypted_key = RSACipher.encrypt(file_info.aes_key) encrypted_key = Encoder.bytes_to_base64_str(encrypted_key) d = wallet.market_client.upload_file_info(hashcode, path, size, product_id, remote_type, remote_uri, name, encrypted_key) def cb(status): self.okSignal.emit(status) d.addCallbacks(cb)
def test_place_order(bAgent): sellerAccount = get_addr_from_public_key(Accounts()[1].public_key) proxyAccount = get_addr_from_public_key(Accounts()[2].public_key) buyer_rsa_pubkey = RSACipher.load_public_key() order_info = OrderInfo( desc_hash=bytes([0, 1, 2, 3] * 8), buyer_rsa_pubkey=buyer_rsa_pubkey, seller=bAgent.web3.toChecksumAddress(sellerAccount), proxy=bAgent.web3.toChecksumAddress(proxyAccount), secondary_proxy=bAgent.web3.toChecksumAddress(proxyAccount), proxy_value=10, value=20, time_allowed=time_allowed) global order_id order_id = bAgent.place_order(order_info) #assert order_id == 1 test_record = bAgent.query_order(order_id) assert test_record[0] == bytes([0, 1, 2, 3] * 8) assert test_record[2] == bAgent.account # Check state is Created assert test_record[10] == 0
import os.path as osp import subprocess import cpchain from cpchain.crypto import RSACipher from cryptography.hazmat.primitives import hashes, serialization priv_key = RSACipher.generate_private_key(password=b'cpchainisawesome') pub_key = priv_key.public_key() pub_bytes = pub_key.public_bytes(encoding=serialization.Encoding.DER, format=serialization.PublicFormat.SubjectPublicKeyInfo) print(len(pub_bytes))