示例#1
0
    def __init__(self, recipient=None, start=None, end=None, limit=None):
        if recipient is None:
            self._recipient = ''
        else:
            self._recipient = recipient

        if start is None or end is None:
            self._dataURL = DataURL()
        else:
            self._dataURL = DataURL(start, end)

        if limit is None:
            self._limit = Limit()
        else:
            self._limit = Limit(limit)
        return
示例#2
0
    def from_json(self, json):
        required = ['recipient', 'data_url', 'limit']
        if not all(k in json for k in required):
            logging.warning(f"value missing in {required}")
            return False

        data_url = DataURL()
        if not data_url.from_json(json['data_url']):
            return False
        self._dataURL = data_url

        if not isinstance(json['recipient'], str) or not isinstance(
                json['limit'], int):
            logging.warning(
                "recipient should be all type<str> and limit should be type<int>"
            )
            return False

        self._recipient = json['recipient']
        self._limit = Limit(json['limit'])
        return True
示例#3
0
    def upload_data(self, server_address, data, address_index=0):
        """
        Upload data to server and get back the dataURL

        :param server_address: x.x.x.x:port
        :param data: data you want to upload
        :param address_index: address index in address_list
        :return: dataURL where the data stores
        """
        public_key = self._account.get_address(address_index).get_pubkey()
        timestamp = time.time()
        hash = hashlib.sha256(
            (str(data) + str(timestamp) + str(0)).encode()).hexdigest()
        signature = self._account.sign_message(hash, address_index)
        send_json = {
            'public_key': public_key,
            'data': data,
            'timestamp': timestamp,
            'op': 0,
            'signature': str(signature)
        }
        # print(signature)
        # print(type(signature))
        # b = str(signature)
        # print(b)
        # print(type(b))
        # c = eval(b)
        # print(c)
        # print(type(c))
        # print(verify_signature(public_key, hash, c))

        response = requests.post(f"http://{server_address}/data/upload",
                                 json=send_json)

        if response.status_code == 200:
            get_json = response.json()
            required = ['data_url', 'limit', 'signature']
            if not all(k in get_json for k in required):
                return 'Missing values', 400

            data_url = DataURL(get_json['data_url']['start'],
                               get_json['data_url']['end'])
            out = Output(public_key, data_url, 7)
            self._outputs.add(out)
            return True
        else:
            # print(response)
            return False
示例#4
0
        if response.status_code == 200:
            get_json = response.json()
            required = ['data_url', 'limit', 'signature']
            if not all(k in get_json for k in required):
                return 'Missing values', 400
            output = Output(public_key,
                            get_json['data_url'],
                            limit=get_json['limit'])
            return True
        else:
            logging.error('invalid response')
            return False


if __name__ == '__main__':

    client = Client()
    #client.create_address()
    # client.store_account()
    client.load_account()

    #client.upload_data(server_address, 'hello')

    data_url = DataURL(0, 1)
    output_position = {
        'block_number': 1,
        'authorization_number': 0,
        'output_number': 0
    }
    client.read_data(server_address, data_url, output_position)
示例#5
0
    def get_limit(self):
        return self._limit

    def valid(self, givens):
        for given in givens:
            if self._dataURL.valid(given[0], given[1]) and self._limit.valid(
                    given[2]):
                return True
        return False

    def to_json(self):
        json = {
            'recipient': self._recipient,
            'dataURL': self._dataURL.to_json(),
            'limit': self._limit
        }
        return json

    def __str__(self):
        return str(self._recipient) + str(self._dataURL) + str(self._limit)


if __name__ == '__main__':
    output = Output()
    output.set_recipient("aaxxbb")
    output.set_data_url(DataURL(10, 3))
    print(str(output))
    output.set_limit(2)
    print(str(output))
示例#6
0
文件: node.py 项目: Roseswrs/myChain
        def upload_data():
            # check if got enough values
            get_json = request.get_json()
            print(get_json)
            print(type(get_json))
            required = ['public_key', 'data', 'timestamp', 'op', 'signature']
            if not all(k in get_json for k in required):
                return 'Missing values', 400
            public_key = get_json['public_key']
            data = get_json['data']
            timestamp = get_json['timestamp']
            op = get_json['op']
            hash = hashlib.sha256((str(data) + str(timestamp) + str(0)).encode()).hexdigest()
            signature = get_json['signature']

            # check if op is upload operation
            if op != 0:
                return 'op is not matched!', 400

            # check if there is enough room
            if len(self._database) + len(get_json['data']) >= MAX_DATA_RANGE:
                return 'no enough storage', 400

            # check if timestamp is not expired
            if timestamp + 600 < time.time():
                return 'Request expired', 400

            # check if signature is matched
            print(signature)
            print(type(signature))

            if not verify_signature(public_key, hash, eval(signature)):
                return 'Signature unmatched', 400

            # everything is fine then store data into database
            if type(data) is 'list':
                self._database += data
            else:
                self._database.append(data)

            # generate a new authorization
            input = Input(0, 0, 0)

            if type(data) is 'list':
                data_url = DataURL(len(self._database) - len(data), len(self._database))
            else:
                data_url = DataURL(len(self._database) - 1, len(self._database))
            output = Output(recipient=public_key, data_url=data_url, limit=Limit(7))

            authorization = Authorization(inputs=[input], outputs=[output], duration=Duration(), timestamp=time.time())

            # sign this authorization
            input.add_signature(self._account.sign_message(authorization.calc_hash()))

            # store this authorization and broadcast it
            auth_number = self.add_authorization(authorization)
            print(auth_number)
            # return the position of authorization to client
            response = {
                'block_number': self._blockchain.get_height(),
                'auth_number': auth_number,
                'output_number': 0
            }
            return jsonify(response), 201
示例#7
0
class Output:
    def __init__(self, recipient=None, start=None, end=None, limit=None):
        if recipient is None:
            self._recipient = ''
        else:
            self._recipient = recipient

        if start is None or end is None:
            self._dataURL = DataURL()
        else:
            self._dataURL = DataURL(start, end)

        if limit is None:
            self._limit = Limit()
        else:
            self._limit = Limit(limit)
        return

    def set_recipient(self, recipient):
        self._recipient = recipient
        return

    def get_recipient(self):
        return self._recipient

    def set_data_url(self, data_url):
        self._dataURL = data_url
        return

    def get_data_url(self):
        return self._dataURL

    def data_url_contains(self, start, end):
        return self._dataURL.contains(start, end)

    def data_url_belongs(self, start, end):
        return self._dataURL.belongs(start, end)

    def set_limit(self, limit):
        self._limit.set(limit)
        return

    def get_limit(self):
        return self._limit

    def valid_limit(self, limit):
        return self._limit.valid(limit)

    def valid(self, givens):
        # print(givens)
        for given in givens:
            if self.data_url_belongs(given[0], given[1]) and self.valid_limit(
                    given[2]):
                return True
        logging.warning(
            'this output does not match any given data_url or limit')
        return False

    def to_json(self):
        json = {
            'recipient': self._recipient,
            'data_url': self._dataURL.to_json(),
            'limit': self._limit.value()
        }
        return json

    def from_json(self, json):
        required = ['recipient', 'data_url', 'limit']
        if not all(k in json for k in required):
            logging.warning(f"value missing in {required}")
            return False

        data_url = DataURL()
        if not data_url.from_json(json['data_url']):
            return False
        self._dataURL = data_url

        if not isinstance(json['recipient'], str) or not isinstance(
                json['limit'], int):
            logging.warning(
                "recipient should be all type<str> and limit should be type<int>"
            )
            return False

        self._recipient = json['recipient']
        self._limit = Limit(json['limit'])
        return True

    def __str__(self):
        return str(self._recipient) + str(self._dataURL) + str(self._limit)