Esempio n. 1
0
    def __validate_parameters(self, params):
        """
        Validate parameter dictionary for existence of 
        fields and mandatory fields 
        Returns False and string with error message on failure and 
        True and empty string on success
        """
        key_list = []
        for key in params.keys():
            if not key in self.__param_key_map.keys():
                logging.error("Invalid parameter %s", key)
                return False, "Invalid parameter {}".format(key)
            else:
                key_list.append(key)
        for k, v in self.__param_key_map.items():
            if v == True and not k in key_list:
                logging.error("Missing parameter %s", k)
                return False, "Missing parameter {}".format(k)

        if not is_hex(params["workOrderId"]):
            logging.error("Invalid work order id")
            return False, "Invalid work order id"
        if not is_hex(params["workloadId"]):
            logging.error("Invalid work load id")
            return False, "Invalid work load id"
        if not is_hex(params["requesterId"]):
            logging.error("Invalid requester id id")
            return False, "Invalid requester id id"
        if not is_hex(params["workerEncryptionKey"]):
            logging.error("Invalid worker encryption key")
            return False, "Invalid worker encryption key"

        return True, ""
Esempio n. 2
0
 def worker_lookup(self, worker_type, org_id, application_id):
     """
     Lookup a worker identified worker_type, org_id and application_id
     all fields are optional and if present condition should match for all
     fields. If none passed it should return all workers.
     """
     if (self.__contract_instance != None):
         if not isinstance(worker_type, WorkerType):
             logging.info("Invalid workerType {}".format(worker_type))
             return construct_message(
                 "failed", "Invalid workerType {}".format(worker_type))
         if not is_hex(binascii.hexlify(org_id).decode("utf8")):
             logging.info("Invalid organization id {}".format(org_id))
             return construct_message(
                 "failed", "Invalid organization id {}".format(org_id))
         if not is_hex(binascii.hexlify(application_id).decode("utf8")):
             logging.info(
                 "Invalid application id {}".format(application_id))
             return construct_message(
                 "failed",
                 "Invalid application id {}".format(application_id))
         lookupResult = self.__contract_instance.functions.workerLookUp(
             worker_type.value, org_id, application_id).call()
         return lookupResult
     else:
         logging.error(
             "worker registry contract instance is not initialized")
         return construct_message(
             "failed",
             "worker registry contract instance is not initialized")
    def worker_lookup_next(self, worker_type, organization_id, 
        application_type_id, lookup_tag, id=None):
        """ Similar to workerLookUp with additional parameter lookup_tag """
        if not isinstance(worker_type, WorkerType):
            logging.error("Worker type is invalid")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                "Worker type is invalid")
        if organization_id is None or not is_hex(organization_id):
            logging.error("Invalid organization id")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                "Invalid organization id")
        if application_type_id is not None:
            for appId in application_type_id:
                if not is_hex(appId):
                    logging.error("Invalid application id")
                    return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                        "Invalid application id")
                    break
        

        json_rpc_request = {
            "jsonrpc": "2.0",
            "method": "WorkerLookUpNext",
            "id": id,
            "params": {
                "workerType": worker_type.value,
                "organizationId": organization_id,
                "applicationTypeId": application_type_id,
                "lookUpTag": lookup_tag
            }
        }
        response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
        return response
    def work_order_receipt_update_retrieve(self, work_order_id,
        updater_id,
        update_index, id=None):
        """
        Retrieving a Work Order Receipt Update
        """
        if work_order_id is None or not is_hex(work_order_id):
            logging.error("Work order id is empty or Invalid")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                "Worker id is empty or Invalid")

        if updater_id is None or not is_hex(updater_id):
            logging.error("Updater id is empty or Invalid")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                "Worker id is empty or Invalid")

        json_rpc_request = {
            "jsonrpc": "2.0",
            "method": "WorkOrderReceiptUpdateRetrieve",
            "id": id,
            "params": {
                "workOrderId": work_order_id,
                "updaterId": updater_id,
                "updateIndex": update_index
            }
        }
        response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
        return response
Esempio n. 5
0
    def work_order_receipt_lookup_next(self, last_lookup_tag,
        worker_service_id=None, worker_id=None, requester_id=None,
        receipt_status=None, id=None):
        """
        Work Order Receipt Lookup Next
        """
        json_rpc_request = {
            "jsonrpc": "2.0",
            "method": "WorkOrderReceiptLookUpNext",
            "id": id,
            "params": {
                "lastLookUpTag": last_lookup_tag
            }
        }

        if worker_service_id is not None:
            if not is_hex(worker_service_id):
                logging.error("Worker service id is Invalid")
                return create_jrpc_response(
                    id, JsonRpcErrorCode.INVALID_PARAMETER,
                    "Worker service id is Invalid")
            json_rpc_request["params"]["workerServiceId"] = worker_service_id

        if worker_id is not None:
            if not is_hex(worker_id):
                logging.error("Worker id is Invalid")
                return create_jrpc_response(
                    id, JsonRpcErrorCode.INVALID_PARAMETER,
                    "Worker id is Invalid")
            json_rpc_request["params"]["workerId"] = worker_id

        if requester_id is not None:
            if not is_hex(requester_id):
                logging.error("Requester id is Invalid")
                return create_jrpc_response(
                    id, JsonRpcErrorCode.INVALID_PARAMETER,
                    "Requester id is Invalid")
            json_rpc_request["params"]["requesterId"] = requester_id

        if receipt_status is not None:
            if not isinstance(receipt_status, ReceiptCreateStatus):
                logging.error("Invalid receipt status")
                return create_jrpc_response(
                    id, JsonRpcErrorCode.INVALID_PARAMETER,
                    "Invalid receipt status")
            json_rpc_request["params"]["receiptStatus"] = receipt_status

        if last_lookup_tag is None or not is_hex(last_lookup_tag):
            logging.error("Last lookup tag is empty or Invalid")
            return create_jrpc_response(
                id, JsonRpcErrorCode.INVALID_PARAMETER,
                "Last lookup tag is empty or Invalid")

        response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
        return response
    def encryption_key_set(self,
                           worker_id,
                           encryption_key,
                           encryption_key_nonce,
                           tag,
                           signature_nonce,
                           signature,
                           id=None):
        if not is_hex(worker_id):
            logging.error("Invalid Worker id")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Invalid Worker id")
        if not is_hex(encryption_key):
            logging.error("Invalid encryption key")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Invalid encryption key")
        if encryption_key_nonce is not None and not is_hex(
                encryption_key_nonce):
            logging.error("Invalid encryption nonce")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Invalid encryption nonce")
        if tag is not None and not is_hex(tag):
            logging.error("Invalid tag")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Invalid tag")
        if signature_nonce is not None and not is_hex(signature_nonce):
            logging.error("Invalid signature nonce")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Invalid signature nonce")
        if signature is not None:
            try:
                base64.b64decode(signature)
            except Exception as e:
                logging.error("Invalid signature")
                return create_jrpc_response(id,
                                            JsonRpcErrorCode.INVALID_PARAMETER,
                                            "Invalid signature")

        json_rpc_request = {
            "jsonrpc": "2.0",
            "method": "EncryptionKeySet",
            "id": id,
            "params": {
                "workerId": worker_id,
                "encryptionKey": encryption_key,
                "encryptionKeyNonce": encryption_key_nonce,
                "tag": tag,
                "signatureNonce": signature_nonce,
                "signature": signature
            }
        }
        response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
        return response
    def work_order_receipt_create(self,
                                  work_order_id,
                                  worker_id,
                                  worker_service_id,
                                  requester_id,
                                  receipt_create_status,
                                  work_order_request_hash,
                                  id=None):
        """
        Creating a Work Order Receipt json rpc request and submit tcs listener
        """
        if work_order_id is None or not is_hex(work_order_id):
            logging.error("Work order id is empty or Invalid")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Worker id is empty or Invalid")

        if worker_id is None or not is_hex(worker_id):
            logging.error("Worker id is empty or Invalid")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Worker id is empty or Invalid")

        if not isinstance(worker_type, ReceiptCreateStatus):
            logging.error("Invalid receipt create status")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Invalid receipt create status")

        if worker_service_id is None or not is_hex(worker_service_id):
            logging.error("Worker service id is empty or Invalid")
            return create_jrpc_response(
                id, JsonRpcErrorCode.INVALID_PARAMETER,
                "Worker service id is empty or Invalid")

        if requester_id is None or not is_hex(requester_id):
            logging.error("requester id is empty or Invalid")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "requester id is empty or Invalid")

        json_rpc_request = {
            "jsonrpc": "2.0",
            "method": "WorkOrderReceiptCreate",
            "id": id,
            "params": {
                "workOrderId": work_order_id,
                "workerId": worker_id,
                "workerServiceId": worker_service_id,
                "requesterId": requester_id,
                "receiptCreateStatus": receipt_create_status,
                "workOrderRequestHash": work_order_request_hash
            }
        }
        response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
        return response
    def work_order_receipt_update(self,
                                  work_order_id,
                                  updater_id,
                                  update_type,
                                  update_data,
                                  update_signature,
                                  signature_rules,
                                  id=None):
        """
        Update a Work Order Receipt json rpc request and submit tcs listener
        """
        if work_order_id is None or not is_hex(work_order_id):
            logging.error("Work order id is empty or Invalid")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Worker id is empty or Invalid")

        if updater_id is None or not is_hex(updater_id):
            logging.error("Updater id is empty or Invalid")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Worker id is empty or Invalid")

        if not isinstance(update_type, ReceiptCreateStatus):
            logging.error("Invalid Update type")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Invalid update type")

        if update_data is None or not is_hex(update_data):
            logging.error("Update data is empty or Invalid")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Update data is empty or Invalid")

        if update_signature is None or not is_hex(update_signature):
            logging.error("update signature is empty or Invalid")
            return create_jrpc_response(
                id, JsonRpcErrorCode.INVALID_PARAMETER,
                "update signature is empty or Invalid")

        json_rpc_request = {
            "jsonrpc": "2.0",
            "method": "WorkOrderReceiptUpdate",
            "id": id,
            "params": {
                "workOrderId": work_order_id,
                "updaterId": updater_id,
                "updateType": update_type,
                "updateData": update_data,
                "updateSignature": update_signature,
                "signatureRules": signature_rules
            }
        }
        response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
        return response
Esempio n. 9
0
 def worker_register(self,
                     worker_id,
                     worker_type,
                     org_id,
                     application_type_ids,
                     details,
                     id=None):
     """ Adds worker details to registry """
     if worker_id is None or not is_hex(worker_id):
         logging.error("Worker id is empty or Invalid")
         return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                     "Worker id is empty or Invalid")
     if not isinstance(worker_type, WorkerType):
         logging.error("Invalid worker type")
         return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                     "Invalid worker type")
     if org_id is not None and not is_hex(org_id):
         logging.error("Invalid organization id")
         return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                     "Invalid organization id")
     if application_type_ids is not None:
         for app_id in application_type_ids:
             if not is_hex(app_id):
                 logging.error("Invalid application type id")
                 return create_jrpc_response(
                     id, JsonRpcErrorCode.INVALID_PARAMETER,
                     "Invalid application type id")
                 break
     if details is not None:
         is_valid = validate_details(details)
         if is_valid is not None:
             logging.error(is_valid)
             return create_jrpc_response(id,
                                         JsonRpcErrorCode.INVALID_PARAMETER,
                                         is_valid)
     json_rpc_request = {
         "jsonrpc": "2.0",
         "method": "WorkerRegister",
         "id": id,
         "params": {
             "workerId": worker_id,
             "workerType": worker_type.value,
             "organizationId": org_id,
             "applicationTypeId": application_type_ids,
             "details": json.loads(details)
         }
     }
     response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
     return response
Esempio n. 10
0
    def worker_register(self, worker_id, worker_type, org_id, application_ids,
                        details):
        """
        Registry new worker with details of worker
        """
        if (self.__contract_instance != None):
            if not is_hex(binascii.hexlify(worker_id).decode("utf8")):
                logging.info("Invalid worker id {}".format(worker_id))
                return construct_message(
                    "failed", "Invalid worker id {}".format(worker_id))
            if not isinstance(worker_type, WorkerType):
                logging.info("Invalid workerType {}".format(worker_type))
                return construct_message(
                    "failed", "Invalid workerType {}".format(worker_type))
            if not is_hex(binascii.hexlify(org_id).decode("utf8")):
                logging.info("Invalid organization id {}".format(org_id))
                return construct_message(
                    "failed", "Invalid organization id {}".format(org_id))
            for aid in application_ids:
                if not is_hex(binascii.hexlify(aid).decode("utf8")):
                    logging.info("Invalid application id {}".format(aid))
                    return construct_message(
                        "failed", "Invalid application id {}".format(aid))
            if details is not None:
                is_valid = validate_details(details)
                if is_valid is not None:
                    return construct_message("failed", is_valid)

            txn_hash = self.__contract_instance.functions.workerRegister(
                worker_id, worker_type.value, org_id, application_ids,
                details).buildTransaction({
                    "chainId":
                    self.__eth_client.get_channel_id(),
                    "gas":
                    self.__eth_client.get_gas_limit(),
                    "gasPrice":
                    self.__eth_client.get_gas_price(),
                    "nonce":
                    self.__eth_client.get_txn_nonce()
                })
            tx = self.__eth_client.execute_transaction(txn_hash)
            return tx
        else:
            logging.error(
                "worker registry contract instance is not initialized")
            return construct_message(
                "failed",
                "worker registry contract instance is not initialized")
Esempio n. 11
0
 def worker_set_status(self, worker_id, status):
     """
     Set the registry status identified by worker id
     status is worker type enum type
     """
     if (self.__contract_instance != None):
         if not is_hex(binascii.hexlify(worker_id).decode("utf8")):
             logging.info("Invalid worker id {}".format(worker_id))
             return construct_message(
                 "failed", "Invalid worker id {}".format(worker_id))
         if not isinstance(status, WorkerStatus):
             logging.info("Invalid worker status {}".format(status))
             return construct_message(
                 "failed", "Invalid worker status {}".format(status))
         txn_hash = self.__contract_instance.functions.workerSetStatus(
             worker_id, status.value).buildTransaction({
                 "chainId":
                 self.__eth_client.get_channel_id(),
                 "gas":
                 self.__eth_client.get_gas_limit(),
                 "gasPrice":
                 self.__eth_client.get_gas_price(),
                 "nonce":
                 self.__eth_client.get_txn_nonce()
             })
         tx = self.__eth_client.execute_transaction(txn_hash)
         return tx
     else:
         logging.error(
             "worker registry contract instance is not initialized")
         return construct_message(
             "failed",
             "worker registry contract instance is not initialized")
 def registry_set_status(self, org_id, status):
     if (self.__contract_instance != None):
         if (is_hex(binascii.hexlify(org_id).decode("utf8")) == False):
             logging.info("Invalid Org id {}".format(org_id))
             return construct_message("failed", "Invalid argument")
         if not isinstance(status, RegistryStatus):
             logging.info("Invalid registry status {}".format(status))
             return construct_message(
                 "failed", "Invalid worker status {}".format(status))
         txn_hash = self.__contract_instance.functions.registrySetStatus(
             org_id, status.value).buildTransaction({
                 "chainId":
                 self.__eth_client.get_channel_id(),
                 "gas":
                 self.__eth_client.get_gas_limit(),
                 "gasPrice":
                 self.__eth_client.get_gas_price(),
                 "nonce":
                 self.__eth_client.get_txn_nonce()
             })
         tx = self.__eth_client.execute_transaction(txn_hash)
         return tx
     else:
         logging.error(
             "direct registry contract instance is not initialized")
         return construct_message(
             "failed", "direct registry contract instance is \
             not initialized")
Esempio n. 13
0
 def worker_update(self, worker_id, details):
     """
     Update the worker with details data which is json string
     """
     if (self.__contract_instance != None):
         if not is_hex(binascii.hexlify(worker_id).decode("utf8")):
             logging.error("Invalid worker id {}".format(worker_id))
             return construct_message(
                 "failed", "Invalid worker id {}".format(worker_id))
         if details is not None:
             is_valid = validate_details(details)
             if is_valid is not None:
                 logging.error(is_valid)
                 return construct_message("failed", is_valid)
         txn_hash = self.__contract_instance.functions.workerUpdate(
             worker_id, details).buildTransaction({
                 "chainId":
                 self.__eth_client.get_channel_id(),
                 "gas":
                 self.__eth_client.get_gas_limit(),
                 "gasPrice":
                 self.__eth_client.get_gas_price(),
                 "nonce":
                 self.__eth_client.get_txn_nonce()
             })
         tx = self.__eth_client.execute_transaction(txn_hash)
         return tx
     else:
         logging.error(
             "worker registry contract instance is not initialized")
         return construct_message(
             "failed",
             "worker registry contract instance is not initialized")
Esempio n. 14
0
    def on_pushButtonSearch_clicked(self):
        self.pushButtonSearch.setFocus()
        host = eth.get_host()
        if host == "" or (":" not in host):
            QMessageBox.information(self, "Information", "请输入有效节点地址")
            return
        client = eth.get_ethclient(host)
        if not client:
            QMessageBox.warning(self, "Warning", "请先链接节点")
            return
        txhash = self.lineEdit_txHash.text()
        if not is_hex(txhash) or len(txhash) != 66:
            QMessageBox.warning(self, "Warning", "请输入有效的交易Hash")
            return

        if client and txhash:
            resp = client.eth_getTransactionByHash(txhash)
            if resp:
                self.label_txHash.setText(resp['hash'])
                self.label_status.setText(
                    client.eth_getTransactionReceipt(txhash)['status'])
                self.label_blockTxHash.setText(resp['blockHash'])
                self.label_from.setText(resp['from'])
                self.label_to.setText(resp['to'])
                self.label_nonce.setText(str(int(resp['nonce'], 16)))
                self.textBrowser_data.setText(resp['input'])
                return
            else:
                QMessageBox.information(self, "Information", "无交易信息")
Esempio n. 15
0
    def __validate_data_format(self, data):
        """
        Validate data format of the params data field (in data or out data)
        Returns False and string with error message on failure and 
        True and empty string on success
        """
        for data_item in data:
            in_data_keys = []
            for key in data_item.keys():
                if not key in self.__data_key_map.keys():
                    logging.error("Invalid in data parameter %s", key)
                    return False, "Invalid in data parameter {}".format(key)
                else:
                    in_data_keys.append(key)
            for k, v in self.__data_key_map.items():
                if v == True and not k in in_data_keys:
                    logging.error("Missing in data parameter %s", k)
                    return False, "Missing in data parameter {}".format(k)

            if "dataHash" in data_item:
                data_hash = data_item["dataHash"]
                if not is_hex(data_hash) and data_hash != "":
                    logging.error("Invalid data hash of in data")
                    return False, "Invalid data hash of in data"
            if "encryptedDataEncryptionKey" in data_item:
                enc_key = data_item["encryptedDataEncryptionKey"]
                if enc_key != "-" and \
                    enc_key != "" and \
                    enc_key != "null" and \
                    not is_hex(enc_key):
                    logging.error("Invalid Encryption key of in data")
                    return False, \
                        "Invalid Encryption key of in data"
            if "iv" in data_item and data_item["iv"] != "" and \
                data_item["iv"] != "0" and not is_hex(data_item["iv"]):
                logging.error("Invalid initialization vector of in data")
                return False, \
                    "Invalid initialization vector of in data"
            try:
                base64.b64decode(data_item["data"])
            except Exception as e:
                logging.error("Invalid base64 format of in data")
                return False, \
                    "Invalid base64 format of in data"

        return True, ""
    def work_order_receipt_lookup_next(self,
                                       worker_service_id,
                                       worker_id,
                                       requester_id,
                                       receipt_status,
                                       last_lookup_tag,
                                       id=None):
        """
        Work Order Receipt Lookup Next
        """
        if worker_id is None or not is_hex(worker_id):
            logging.error("Worker id is empty or Invalid")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Worker id is empty or Invalid")

        if worker_service_id is None or not is_hex(worker_service_id):
            logging.error("Worker service id is empty or Invalid")
            return create_jrpc_response(
                id, JsonRpcErrorCode.INVALID_PARAMETER,
                "Worker service id is empty or Invalid")

        if requester_id is None or not is_hex(requester_id):
            logging.error("requester id is empty or Invalid")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "requester id is empty or Invalid")

        if not isinstance(receipt_status, ReceiptCreateStatus):
            logging.error("Invalid receipt status")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Invalid receipt status")

        json_rpc_request = {
            "jsonrpc": "2.0",
            "method": "WorkOrderReceiptLookUpNext",
            "id": id,
            "params": {
                "workerServiceId": worker_service_id,
                "workerId": worker_id,
                "requesterId": requester_id,
                "updateIndex": receipt_status,
                "lastLookUpTag": last_lookup_tag
            }
        }
        response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
        return response
Esempio n. 17
0
    def worker_lookup(self,
                      worker_type=None,
                      organization_id=None,
                      application_type_id=None,
                      id=None):
        """ Worker lookup based on worker type, organization id 
        and application id"""
        json_rpc_request = {
            "jsonrpc": "2.0",
            "method": "WorkerLookUp",
            "id": id,
            "params": {}
        }

        if worker_type is not None:
            if not isinstance(worker_type, WorkerType):
                logging.error("Invalid worker type")
                return create_jrpc_response(id,
                                            JsonRpcErrorCode.INVALID_PARAMETER,
                                            "Invalid worker type")
            json_rpc_request["params"]["workerType"] = worker_type.value

        if organization_id is not None:
            if not is_hex(organization_id):
                logging.error("Invalid organization id")
                return create_jrpc_response(id,
                                            JsonRpcErrorCode.INVALID_PARAMETER,
                                            "Invalid organization id")
            json_rpc_request["params"]["organizationId"] = organization_id

        if application_type_id is not None:
            for app_id in application_type_id:
                if not is_hex(app_id):
                    logging.error("Invalid application type id")
                    return create_jrpc_response(
                        id, JsonRpcErrorCode.INVALID_PARAMETER,
                        "Invalid application type id")
            json_rpc_request["params"][
                "applicationTypeId"] = application_type_id

        response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
        return response
    def registry_update(self, org_id, uri, sc_addr, app_type_ids):
        if (self.__contract_instance != None):
            if (is_hex(binascii.hexlify(org_id).decode("utf8")) == False):
                logging.error("Invalid Org id {}".format(org_id))
                return construct_message("failed", "Invalid Org id")
            if (sc_addr is not None and is_hex(
                    binascii.hexlify(sc_addr).decode("utf8")) == False):
                logging.error(
                    "Invalid smart contract address {}".format(sc_addr))
                return construct_message("failed",
                                         "Invalid smart contract address")
            if (not uri):
                logging.error("Empty uri {}".format(uri))
                return construct_message("failed", "Empty uri")
            for aid in app_type_ids:
                if (is_hex(binascii.hexlify(aid).decode("utf8")) == False):
                    logging.error("Invalid application id {}".format(aid))
                    return construct_message("failed",
                                             "Invalid application id")

            txn_hash = self.__contract_instance.functions.registryUpdate(
                org_id, uri, sc_addr, app_type_ids).buildTransaction({
                    "chainId":
                    self.__eth_client.get_channel_id(),
                    "gas":
                    self.__eth_client.get_gas_limit(),
                    "gasPrice":
                    self.__eth_client.get_gas_price(),
                    "nonce":
                    self.__eth_client.get_txn_nonce()
                })
            tx = self.__eth_client.execute_transaction(txn_hash)
            return tx
        else:
            logging.error(
                "direct registry contract instance is not initialized")
            return construct_message(
                "failed", "direct registry contract instance is \
                not initialized")
    def workOrder_submit(self, workOrder_id, worker_id, requester_id,
                         workOrder_request):
        """
        Registry new worker with details of worker
        """
        if (self.__contract_instance != None):
            if not is_hex(binascii.hexlify(worker_id).decode("utf8")):
                logging.info("Invalid worker id {}".format(worker_id))
                return construct_message(
                    "failed", "Invalid worker id {}".format(worker_id))
            if not is_hex(binascii.hexlify(workOrder_id).decode("utf8")):
                logging.info("Invalid workOrder id {}".format(workOrder_id))
                return construct_message(
                    "failed", "Invalid workerType {}".format(workeOrder_id))
            if not is_hex(binascii.hexlify(requester_id).decode("utf8")):
                logging.info("Invalid Requester id {}".format(requester_id))
                return construct_message(
                    "failed", "Invalid oRequester id {}".format(requester_id))

            txn_hash = self.__contract_instance.functions.workOrderSubmit(
                workOrder_id, worker_id, requester_id,
                workOrder_request).buildTransaction({
                    "chainId":
                    self.__eth_client.get_channel_id(),
                    "gas":
                    self.__eth_client.get_gas_limit(),
                    "gasPrice":
                    self.__eth_client.get_gas_price(),
                    "nonce":
                    self.__eth_client.get_txn_nonce()
                })
            tx = self.__eth_client.execute_transaction(txn_hash)
            return tx
        else:
            logging.error(
                "Submit registry contract instance is not initialized")
            return construct_message(
                "failed", "Submit contract instance is not initialized")
 def registry_retrieve(self, org_id):
     if (self.__contract_instance != None):
         if (is_hex(binascii.hexlify(org_id).decode("utf8")) == False):
             logging.info("Invalid Org id {}".format(org_id))
             return construct_message("failed", "Invalid Org id")
         else:
             registryDetails = self.__contract_instance.functions.registryRetrieve(
                 org_id).call()
             return registryDetails
     else:
         logging.error(
             "direct registry contract instance is not initialized")
         return construct_message(
             "failed", "direct registry contract instance is \
             not initialized")
Esempio n. 21
0
 def worker_lookup_next(self, worker_type, org_id, application_id,
                        lookup_tag):
     if (self.__contract_instance != None):
         if not isinstance(worker_type, WorkerType):
             logging.info("Invalid workerType {}".format(worker_type))
             return construct_message(
                 "failed", "Invalid workerType {}".format(worker_type))
         if not is_hex(binascii.hexlify(org_id).decode("utf")):
             logging.info("Invalid organization id {}".format(org_id))
             return construct_message(
                 "failed", "Invalid organization id {}".format(org_id))
         if not is_hex(binascii.hexlify(application_id).decode("utf8")):
             logging.info("Invalid application id {}".format(org_id))
             return construct_message(
                 "failed", "Invalid application id {}".format(org_id))
         lookupResult = self.__contract_instance.functions.workerLookUpNext(
             worker_type.value, org_id, application_id, lookup_tag).call()
         return lookupResult
     else:
         logging.error(
             "worker registry contract instance is not initialized")
         return construct_message(
             "failed", "worker registry contract instance \
             is not initialized")
Esempio n. 22
0
    def work_order_get_result(self, work_order_id, id=None):
        if not is_hex(work_order_id):
            logging.error("Invalid workOrder Id")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Invalid workOrder Id")

        json_rpc_request = {
            "jsonrpc": "2.0",
            "method": "WorkOrderGetResult",
            "id": id,
            "params": {
                "workOrderId": work_order_id
            }
        }
        response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
        return response
 def worker_update(self, worker_id, details, id=None):
     """ Update worker with new information """
     if worker_id is None or not is_hex(worker_id):
         logging.error("Worker id is empty or invalid")
         return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
             "Worker id is empty or Invalid")
     json_rpc_request = {
         "jsonrpc": "2.0",
         "method": "WorkerUpdate",
         "id": id,
         "params": {
             "workerId": worker_id,
             "details": details
         }
     }
     response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
     return response
    def worker_retrieve(self, worker_id, id=None):
        """ Retrieve the worker identified by worker id """
        if worker_id is None or not is_hex(worker_id):
            logging.error("Worker id is empty or Invalid")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                "Worker id is empty or Invalid")

        json_rpc_request = {
            "jsonrpc": "2.0",
            "method": "WorkerRetrieve",
            "id": id,
            "params": {
                "workerId": worker_id
            }
        }
        response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
        return response
 def registry_lookup_next(self, app_type_id, lookup_tag):
     if (self.__contract_instance != None):
         if is_hex(binascii.hexlify(app_type_id).decode("utf8")):
             lookupResult = self.__contract_instance.functions.registryLookUpNext(
                 app_type_id, lookup_tag).call()
             return lookupResult
         else:
             logging.info(
                 "Invalid application type id {}".format(app_type_id))
             return construct_message("failed",
                                      "Invalid application type id")
     else:
         logging.error(
             "direct registry contract instance is not initialized")
         return construct_message(
             "failed", "direct registry contract instance \
             is not initialized")
Esempio n. 26
0
    def worker_retrieve(self, worker_id):
        """
        Retrieve the worker identified by worker id
        """
        if (self.__contract_instance != None):
            if not is_hex(binascii.hexlify(worker_id).decode("utf8")):
                logging.info("Invalid worker id {}".format(worker_id))
                return construct_message(
                    "failed", "Invalid worker id {}".format(worker_id))

            workerDetails = self.__contract_instance.functions.workerRetrieve(
                worker_id).call()
            return workerDetails
        else:
            logging.error(
                "worker registry contract instance is not initialized")
            return construct_message(
                "failed",
                "worker registry contract instance is not initialized")
    def worker_set_status(self, worker_id, status, id=None):
        """ Set the worker status to active, offline, decommissioned 
        or compromised state 
        """
        if worker_id is None or not is_hex(worker_id):
            logging.error("Worker id is empty or Invalid")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                "Worker id is empty or Invalid")

        json_rpc_request = {
            "jsonrpc": "2.0",
            "method": "WorkerSetStatus",
            "id": id,
            "params": {
                "workerId": worker_id,
                "status": status.value
            }
        }
        response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
        return response
Esempio n. 28
0
    def work_order_get_result_nonblocking(self, work_order_id, id=None):
        """
        Get the work order result in non-blocking way.
        It return json rpc response of dictionary type
        """
        if not is_hex(work_order_id):
            logging.error("Invalid work order Id")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Invalid work order Id")

        json_rpc_request = {
            "jsonrpc": "2.0",
            "method": "WorkOrderGetResult",
            "id": id,
            "params": {
                "workOrderId": work_order_id
            }
        }
        response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
        return response
    def workOrder_getResult(self, workOrder_id):
        """
        Lookup a worker identified worker_type, org_id and application_id
        all fields are optional and if present condition should match for all
        fields. If none passed it should return all workers.
        """
        if (self.__contract_instance != None):
            if not is_hex(binascii.hexlify(workOrder_id).decode("utf8")):
                logging.info("Invalid workOrder id {}".format(workOrder_id))
                return construct_message(
                    "failed", "Invalid workOrder id {}".format(workOrder_id))

            workOrderDetails = self.__contract_instance.functions.workOrderGetResult(
                workOrder_id).call()
            return workOrderDetails
        else:
            logging.error(
                "work_Order_get_Result contract instance is not initialized")
            return construct_message(
                "failed",
                "work_Order_get_Result contract instance is not initialized")
Esempio n. 30
0
    def work_order_submit(self, params, in_data, out_data, id=None):
        is_valid = self.__validate_parameters(params, in_data, out_data)
        if is_valid is not None:
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        is_valid)
        if not is_hex(params["workOrderId"]):
            logging.error("Invalid work order id")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Invalid work order id")
        if not is_hex(params["workloadId"]):
            logging.error("Invalid work load id")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Invalid work load id")
        if not is_hex(params["requesterId"]):
            logging.error("Invalid requester id id")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Invalid requester id id")
        if not is_hex(params["workerEncryptionKey"]):
            logging.error("Invalid worker encryption key")
            return create_jrpc_response(id, JsonRpcErrorCode.INVALID_PARAMETER,
                                        "Invalid worker encryption key")
        for data in in_data:
            if not is_hex(data["dataHash"]):
                logging.error("Invalid data hash of in data")
                return create_jrpc_response(id,
                                            JsonRpcErrorCode.INVALID_PARAMETER,
                                            "Invalid data hash of in data")
            if not is_hex(data["encryptedDataEncryptionKey"]):
                logging.error("Invalid Encryption key of in data")
                return create_jrpc_response(
                    id, JsonRpcErrorCode.INVALID_PARAMETER,
                    "Invalid Encryption key of in data")
            if not is_hex(data["iv"]):
                logging.error("Invalid initialization vector of in data")
                return create_jrpc_response(
                    id, JsonRpcErrorCode.INVALID_PARAMETER,
                    "Invalid initialization vector of in data")
            try:
                base64.b64decode(data["data"])
            except Exception as e:
                logging.error("Invalid base64 format of in data")
                return create_jrpc_response(
                    id, JsonRpcErrorCode.INVALID_PARAMETER,
                    "Invalid base64 format of in data")

        for data in out_data:
            if not is_hex(data["dataHash"]):
                logging.error("Invalid data hash of out data")
                return create_jrpc_response(id,
                                            JsonRpcErrorCode.INVALID_PARAMETER,
                                            "Invalid data hash of out data")
            if not is_hex(data["encryptedDataEncryptionKey"]):
                logging.error("Invalid Encryption key of out data")
                return create_jrpc_response(
                    id, JsonRpcErrorCode.INVALID_PARAMETER,
                    "Invalid Encryption key of out data")
            if not is_hex(data["iv"]):
                logging.error("Invalid initialization vector of out data")
                return create_jrpc_response(
                    id, JsonRpcErrorCode.INVALID_PARAMETER,
                    "Invalid initialization vector of out data")
            try:
                base64.b64decode(data["data"])
            except Exception as e:
                logging.error("Invalid base64 format of out data")
                return create_jrpc_response(
                    id, JsonRpcErrorCode.INVALID_PARAMETER,
                    "Invalid base64 format of out data")

        json_rpc_request = {
            "jsonrpc": "2.0",
            "method": "WorkOrderSubmit",
            "id": id,
            "params": {
                "responseTimeoutMSecs": params["responseTimeoutMSecs"],
                "payloadFormat": params["payloadFormat"],
                "resultUri": params["resultUri"],
                "notifyUri": params["notifyUri"],
                "workOrderId": params["workOrderId"],
                "workerId": params["workerId"],
                "workloadId": params["workloadId"],
                "requesterId": params["requesterId"],
                "workerEncryptionKey": params["workerEncryptionKey"],
                "dataEncryptionAlgorithm": params["dataEncryptionAlgorithm"],
                "encryptedSessionKey": params["encryptedSessionKey"],
                "sessionKeyIv": params["sessionKeyIv"],
                "requesterNonce": params["requesterNonce"],
                "encryptedRequestHash": params["encryptedRequestHash"],
                "requesterSignature": params["requesterSignature"],
                "inData": in_data,
                "outData": out_data
            }
        }
        response = self.__uri_client._postmsg(json.dumps(json_rpc_request))
        return response