Django Rest Framework - How to create a Rest api to send it to a blockchain

289 Views Asked by At

I have created a Django app, I wanted to build a REST Api within django using python sdk library given by the hyerledger iroha(https://github.com/hyperledger/iroha-python). here are my models.py

    #Iroha Test
class IrohaAccounts(models.Model):
    acc_name = models.CharField(max_length=30, null=False, blank=False, unique=True)
    dom_name = models.CharField(max_length=50,null=False, blank=False)

    def __str__(self):
        return self.acc_name

Serializer.py

    #Iroha Test
class IrohaAccountsSerializer(serializers.ModelSerializer):
    class Meta:
        model = IrohaAccounts
        fields = ['acc_name','dom_name']


    def save(self):
        account = IrohaAccounts(
            acc_name=self.validated_data['acc_name'],
            dom_name=self.validated_data['dom_name'],

        )
        account.save()
        return account

Views.py

    #Iroha Test 
@api_view(['POST',])
def iroha_account(request):
    """
    Create account 'userone@domain'
    """

    if request.method == "POST":
        serializer=serializers.IrohaAccountsSerializer(data=request.data)
        if serializer.is_valid():
            account=serializer.save()
            data = {
                'response' : "acc create",
                'acc_name' : account.acc_name,
                'dom_name' : account.dom_name,  }



            # these first two lines are enough to create the keys
            private_key = IrohaCrypto.private_key()
            public_key = IrohaCrypto.derive_public_key(private_key)



                
            tx = iroha.transaction([
                iroha.command('CreateAccount', account_name=acc_name, domain_id=dom_name,
                            public_key=public_key)
            ])
            IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
            send_transaction_and_print_status(tx)
            return Response(data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

BTW I'm new to django and learning i don't know what is wrong by default it goes to the postgres sql database and checks for the table. But i wanted the request to sent to the Blockchain instance which is running in the docker container. Do help thank you!

0

There are 0 best solutions below