0

我创建了一个 Django 应用程序,我想使用 hyerledger iroha ( https://github.com/hyperledger/iroha-python ) 提供的 python sdk 库在 django 中构建一个 REST Api。这是我的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

序列化程序.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

视图.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)

顺便说一句,我是 django 的新手,并且学习我不知道默认情况下出了什么问题,它会进入 postgres sql 数据库并检查表。但我希望将请求发送到在 docker 容器中运行的区块链实例。帮忙谢谢!

4

0 回答 0