コード例 #1
0
ファイル: server.py プロジェクト: alexdpl/cpchain-snapshot
    def stringReceived(self, string):
        sign_message = SignMessage()
        sign_message.ParseFromString(string)

        valid = sign_message_verify(sign_message)

        if not valid:
            error = 'wrong signature'
            return self.proxy_reply_error(error)

        message = Message()
        message.ParseFromString(sign_message.data)
        valid = message_sanity_check(message)
        if not valid or message.type == Message.PROXY_REPLY:
            error = "wrong client request"
            return self.proxy_reply_error(error)

        public_key = sign_message.public_key

        if message.type == Message.SELLER_DATA:
            public_addr = message.seller_data.seller_addr
        elif message.type == Message.BUYER_DATA:
            public_addr = message.buyer_data.buyer_addr

        if not is_address_from_key(public_addr, public_key):
            error = "public key does not match with address"
            return self.proxy_reply_error(error)

        self.handle_message(message)
コード例 #2
0
    def stringReceived(self, string):
        sign_message = SignMessage()
        sign_message.ParseFromString(string)

        valid = sign_message_verify(sign_message)

        if not valid:
            error = 'wrong signature'
            self.proxy_reply_error(error)
            return

        message = Message()
        message.ParseFromString(sign_message.data)
        valid = message_sanity_check(message)
        if not valid or message.type == Message.PROXY_REPLY:
            error = "wrong client request"
            self.proxy_reply_error(error)
            return

        public_key = sign_message.public_key

        proxy_db = self.proxy_db
        self.trade = Trade()
        trade = self.trade
        error = None

        if message.type == Message.SELLER_DATA:
            data = message.seller_data
            trade.order_id = data.order_id
            trade.seller_addr = data.seller_addr
            trade.buyer_addr = data.buyer_addr
            trade.market_hash = data.market_hash
            trade.AES_key = data.AES_key

            if pub_key_der_to_addr(public_key) != data.seller_addr:
                print("pubkey seller addr")
                print(pub_key_der_to_addr(public_key))
                print(data.seller_addr)
                error = "not seller's signature"
                self.proxy_reply_error(error)
                return

            storage = data.storage
            if storage.type == Message.Storage.IPFS:
                ipfs = storage.ipfs
                trade.file_hash = ipfs.file_hash
                if (proxy_db.count(trade)):
                    error = "trade record already in database"
                    self.proxy_reply_error(error)
                    return
                else:
                    file_path = os.path.join(server_root,
                                             trade.file_hash.decode())

                    # seller sold the same file to another buyer
                    if os.path.isfile(file_path):
                        mtime = time.time()
                        os.utime(file_path, (mtime, mtime))
                        proxy_db.insert(trade)
                        self.proxy_reply_success()

                        self.proxy_claim_relay()

                        return
                    else:
                        d = threads.deferToThread(self.get_ipfs_file,
                                                  ipfs.gateway,
                                                  trade.file_hash)

                        d.addBoth(self.ipfs_callback)

            elif storage.type == Message.Storage.S3:
                error = "not support S3 storage yet"
                self.proxy_reply_error(error)
                return

        elif message.type == Message.BUYER_DATA:
            data = message.buyer_data
            trade.order_id = data.order_id
            trade.seller_addr = data.seller_addr
            trade.buyer_addr = data.buyer_addr
            trade.market_hash = data.market_hash

            if pub_key_der_to_addr(public_key) != data.buyer_addr:
                error = "not buyer's signature"
                self.proxy_reply_error(error)
                return

            if proxy_db.count(trade):
                self.trade = proxy_db.query(trade)
                self.proxy_reply_success()
                return
            else:
                error = "trade record not found in database"
                self.proxy_reply_error(error)
                return