Exemple #1
0
    def get(self, request, id):
        try:
            dapp = Dapp.objects.prefetch_related('constructor',
                                                 'users').get(slug=id)
        except Dapp.DoesNotExist:
            return error_response("Dapp not found")

        user = auth(request)
        if not dapp.has_public_access:
            if isinstance(user, HttpResponse):
                return user  # error

            if user not in dapp.users.all():
                return error_response('Dapp not found')

        if not dapp.address:
            return error_response('Dapp is not yet deployed')

        cust_title = None
        if isinstance(user, User):
            try:
                user_dapp = UserDapp.objects.get(user=user, dapp=dapp)
                cust_title = user_dapp.title
            except UserDapp.DoesNotExist:
                pass

        return JsonResponse(
            dapp_pub_info(dapp, user in dapp.users.all(), cust_title))
Exemple #2
0
    def process_exception(self, request, exception):
        # todo maybe only for api
        self.logger.error("Unhandled error: {}. {}".format(
            str(exception),
            traceback.format_exc().replace("\n", ' ')))

        if isinstance(exception, PublicException):
            return error_response(exception.public_message, 200)
        else:
            return error_response('Something got wrong', 500)
Exemple #3
0
    def post(self, request):
        user = auth(request)
        if isinstance(user, HttpResponse):
            return user  # error

        if 'address' not in request.data or type(
                request.data['address']
        ) is not str or not request.data['address']:
            return error_response("Address is missing")

        if 'network_id' not in request.data or type(
                request.data['network_id']
        ) is not str or not request.data['network_id']:
            return error_response("Network id is missing")

        if 'abi' not in request.data:
            return error_response('Abi not specified')

        if 'blockchain' not in request.data or request.data[
                'blockchain'] not in dict(BLOCKCHAINS):
            return error_response('Invalid blockchain')

        if 'name' not in request.data or type(
                request.data['name']) is not str or not request.data['name']:
            title = 'Dapp'
        else:
            title = request.data['name']

        dapp = Dapp.create()

        dapp.blockchain = request.data['blockchain']
        dapp.address = request.data['address']
        dapp.network_id = request.data['network_id']

        dapp.title = title
        dapp.abi = json.dumps(request.data['abi'])
        dapp.source = ''
        dapp.binary = ''
        dapp.function_specs = json.dumps(
            self.contracts_processors_manager.require_contract_processor(dapp.blockchain)\
            .process_functions_specs(request.data['abi'], {})
        )
        dapp.dashboard_functions = json.dumps([])
        dapp.has_public_access = True

        dapp.save()
        UserDapp.objects.create(user=user, dapp=dapp, title=dapp.title)

        return JsonResponse({'ok': True})  # todo
Exemple #4
0
    def post(self, request, id):
        if 'address' not in request.data or type(
                request.data['address']
        ) is not str or not request.data['address']:
            return error_response("Address is missing")

        if 'network_id' not in request.data or type(
                request.data['network_id']
        ) is not str or not request.data['network_id']:
            return error_response("Network id is missing")

        user = auth(request)
        if isinstance(user, HttpResponse):
            return user  # error

        try:
            contract_ui = ContractUI.objects.get(id=id)
        except ContractUI.DoesNotExist:
            return error_response("Ui not found")

        dapp = Dapp.create()

        dapp.blockchain = contract_ui.blockchain
        dapp.address = request.data['address']
        dapp.network_id = request.data['network_id']

        dapp.title = contract_ui.name

        abi = contract_ui.abi if 'abi' not in request.data else request.data[
            'abi']
        dapp.abi = json.dumps(abi)

        dapp.source = ''
        dapp.binary = ''
        dapp.function_specs = json.dumps(
            self.contracts_processors_manager.require_contract_processor(
                contract_ui.blockchain).process_functions_specs(
                    abi, contract_ui.functions))
        dapp.dashboard_functions = json.dumps(contract_ui.dashboard_functions)
        dapp.contract_ui = contract_ui
        dapp.has_public_access = True

        dapp.save()

        UserDapp.objects.create(dapp=dapp, user=user, title=contract_ui.name)

        return JsonResponse({'ok': True})  # todo
Exemple #5
0
    def post(self, http_request, dapp_slug):
        user = auth(http_request)
        if isinstance(user, HttpResponse):  # todo (error)
            return user

        try:
            dapp = Dapp.objects.get(slug=dapp_slug)
        except Dapp.DoesNotExist:
            return error_response("Dapp not found")

        serializer = RequestSerializer(data=http_request.data)
        if not serializer.is_valid():
            self.logger.info("Bad request: %s", json.dumps(serializer.errors))
            return error_response('Bad request', status.HTTP_400_BAD_REQUEST)

        serializer.save(user=user, dapp=dapp)
        return Response(serializer.data, status=status.HTTP_201_CREATED)
Exemple #6
0
    def get(self, request, id):
        try:
            dapp = Dapp.objects.prefetch_related('constructor').get(slug=id)
        except Dapp.DoesNotExist:
            return error_response("Dapp not found")

        if not dapp.has_public_access:
            user = auth(request)
            if isinstance(user, HttpResponse):
                return user  # error

            if dapp.user_id != user.pk:
                return error_response('Dapp not found')

        if not dapp.address:
            return error_response('Dapp is not yet deployed')

        return JsonResponse(_prepare_instance_details(dapp))
Exemple #7
0
    def post(self, request, id):
        user = auth(request)
        if isinstance(user, HttpResponse):
            return user  # error

        try:
            dapp = Dapp.objects.get(slug=id, users=user)
        except Dapp.DoesNotExist:
            return error_response("Dapp not found")

        # [TODO] refactor all checks (address and network_id validation should be somewhere near Instance() class
        # Later validation of Ethereum address will differ from EOS address, so Instance() class will have some "blockchain_id" member to
        # differ parameters of dapps on different blockchains

        address = request.data.get('address')
        if address is not None:
            if not isinstance(address, str):
                return error_response("Param 'address' is empty or not string")
            dapp.address = address

        network_id = request.data.get('network_id')
        if network_id is not None:
            if type(network_id) not in (int, str):
                return error_response("Param 'network_id' is empty or not int")
            dapp.network_id = str(network_id)

        has_public_access = request.data.get('has_public_access')
        if has_public_access is not None:
            dapp.has_public_access = bool(has_public_access)

        title = request.data.get('title')
        if title is not None:
            for user_dapp in UserDapp.objects.filter(dapp=dapp, user=user):
                user_dapp.title = title
                user_dapp.save()

        dapp.save()

        return JsonResponse({'ok': True})  # todo
Exemple #8
0
    def post(self, request, id):
        user = auth(request)
        if isinstance(user, HttpResponse):
            return user  # error

        try:
            dapp = Dapp.objects.get(slug=id, has_public_access=True)
        except Dapp.DoesNotExist:
            return error_response("Dapp not found")

        if not UserDapp.objects.filter(dapp=dapp, user=user):
            UserDapp.objects.create(dapp=dapp, user=user, title=dapp.title)

        return JsonResponse({'ok': True})  # todo
Exemple #9
0
    def post(self, request, id):
        user = auth(request)
        if isinstance(user, HttpResponse):
            return user  # error

        try:
            dapp = Dapp.objects.get(slug=id, has_public_access=True)
        except Dapp.DoesNotExist:
            return error_response("Dapp not found")

        dapp.slug = Dapp.create().slug
        dapp.pk = None
        dapp.user = user
        dapp.save()

        return JsonResponse({'ok': True})  # todo
Exemple #10
0
    def post(self, request):
        blockchain = request.data.get('blockchain')
        identity = request.data.get('identity')
        rand_data = request.data.get('rand_data')
        signed_data = request.data.get('signed_data')

        service = self._require_service(blockchain)

        is_valid = service.check_sign(identity, signed_data, rand_data)
        if not is_valid:
            return error_response("Incorrect signature")

        user = self.users_service.find_user(blockchain=blockchain,
                                            identity=identity)
        if not user:
            user = self.users_service.register_user(blockchain=blockchain,
                                                    identity=identity)

        return JsonResponse(
            {"token": self.users_service.generate_token(user, blockchain)})
Exemple #11
0
    def __call__(self, request):
        if request.method == 'OPTIONS':
            request.is_swagger_schema_validated = True
            return self.get_response(request)

        schema = load(settings.SMARTZ_INTERNAL_API_SWAGGER_SCHEMA)

        request.is_swagger_schema_validated = False
        try:
            validate_api_request(schema, request)
            request.is_swagger_schema_validated = True
        except ValidationError as err:
            if 'path' in err.detail and isinstance(err.detail['path'], collections.Iterable) \
                    and len(err.detail['path'])>0 and 'No paths found for' in str(err.detail['path'][0]):
                self.logger.debug(str(err.detail['path'][0]))
                request.is_swagger_schema_validated = True
            else:
                self.logger.warning(str(err))
                return error_response("Invalid request", 500)

        return self.get_response(request)