[Hyperledger Iroha] Notes on how to use the Python SDK

Article content

Using Hyperledger Iroha's Python SDK, ・ Creating Asset ・ Send Asset ・ Acquisition of block information I will try.

Premise

・ The environment must be built according to the official guide of iroha. ・ Environment that can use Python3

reference

Iroha Python Library documentation https://iroha.readthedocs.io/en/latest/develop/libraries/python.html

The Example Code on this page is written from Sending Asset, so it will not work unless you create Asset in advance. I'll try that code using Python in this article as well.

Creating an Asset

from iroha import Iroha, IrohaCrypto, IrohaGrpc

net = IrohaGrpc('Host IP Address:50051')

iroha = Iroha('admin@test')
 admin_priv_key ='Admin Private Key' #Private Key for the account specified one line before

# Creating a Transaction
create_asset_tx = iroha.transaction(
    [iroha.command(
 Create'CreateAsset', #Asset
        asset_name = 'samplecoin',
        domain_id = 'test',
        precision = 10 
    ), iroha.command(
 Setting the initial holding amount of'AddAssetQuantity', #Asset
        asset_id = 'samplecoin#test',
        amount = '100000000'
    )]
)

# Sign Transaction
IrohaCrypto.sign_transaction(create_asset_tx, admin_priv_key)

# Send Transaction
net.send_tx(create_asset_tx)

# Check the result
for status in net.tx_status_stream(create_asset_tx):
    print(status)

This code creates an Asset with the "admin @ test" account and sets the amount of Assets the "admin @ test" account holds.

The flow of processing is as follows: Transaction creation-> Signature-> Send-> Confirmation of result.

If it is executed normally, the result will be as follows.

('ENOUGH_SIGNATURES_COLLECTED', 9, 0)
('STATEFUL_VALIDATION_SUCCESS', 3, 0)
('COMMITTED', 5, 0)

A block has been added under the "/ tmp / block_store" directory of the iroha container.

{
  "blockV1": {
    "payload": {
      "transactions": [
        {
          "payload": {
            "reducedPayload": {
              "commands": [
                {
                  "createAsset": {
                    "assetName": "samplecoin",
                    "domainId": "test",
                    "precision": 10
                  }
                },
                {
                  "addAssetQuantity": {
                    "assetId": "samplecoin#test",
                    "amount": "100000000"
                  }
                }
              ],
              "creatorAccountId": "admin@test",
              "createdTime": "1578361629026",
              "quorum": 1
            }
          },
          "signatures": [
            {
              "publicKey": "313a07e6384776ed95447710d15e59148473ccfc052a681317a72a69f2a49910",
              "signature": "a414d71e6b2022a81d375d2a367827800a072b465ace7e919c23336b572e5c7511854c2abcff08736d6a687e261a21e1121180f561b8e18729a0fdeaffbbc401"
            }
          ]
        }
      ],
      "height": "2",
      "prevBlockHash": "9debdb1a70db2cede2222427b849f6bf7ab20845da7c3db1837c0df25ec1c61a",
      "createdTime": "1578361629022"
    },
    "signatures": [
      {
        "publicKey": "bddd58404d1315e0eb27902c5d7c8eb0602c16238f005773df406bc191308929",
        "signature": "20a900f3587ccdadcb128a6d597cd0406e90882cbc9b12017c938d4f0c4a4cd6bd6cd09b674289c5146f1026bfdfc33a922a6cb925429db29e49605ffc392c09"
      }
    ]
  }
}

Send Asset

The code here remains the code found in the official documentation mentioned in the references above.

from iroha import Iroha, IrohaCrypto, IrohaGrpc

net = IrohaGrpc('Host IP Address:50051')

iroha = Iroha('admin@test')
 admin_priv_key ='Admin Private Key' #Private Key for the account specified one line before

# Creating a Transaction
transfer_asset_tx = iroha.transaction(
 [iroha.command(
     'TransferAsset',
     src_account_id='admin@test',
     dest_account_id='test@test',
     asset_id='samplecoin#test',
     description='test',
     amount='10000'
 )]
)

# Transaction signature
IrohaCrypto.sign_transaction(transfer_asset_tx, admin_priv_key)

# Send Transaction
net.send_tx(transfer_asset_tx)

# Check the result
for status in net.tx_status_stream(transfer_asset_tx):
    print(status)

Modify the source account (src_account_id), destination account (dest_account_id), and asset ID (asset_name # domain_id) according to your environment.

If it is executed normally, the result will be as follows.

('ENOUGH_SIGNATURES_COLLECTED', 9, 0)
('STATEFUL_VALIDATION_SUCCESS', 3, 0)
('COMMITTED', 5, 0)

A block has been added under the "/ tmp / block_store" directory of the iroha container.

{
  "blockV1": {
    "payload": {
      "transactions": [
        {
          "payload": {
            "reducedPayload": {
              "commands": [
                {
                  "transferAsset": {
                    "srcAccountId": "admin@test",
                    "destAccountId": "test@test",
                    "assetId": "samplecoin#test",
                    "description": "test",
                    "amount": "10000"
                  }
                }
              ],
              "creatorAccountId": "admin@test",
              "createdTime": "1578362174344",
              "quorum": 1
            }
          },
          "signatures": [
            {
              "publicKey": "313a07e6384776ed95447710d15e59148473ccfc052a681317a72a69f2a49910",
              "signature": "7bf5bec8110cbe618b05cd3bf8b69d0eb1e6e2eb4f3efcc41d0573b8e3638bc220b49c251c0f3238bb753d85d4b7a3e032f420f5c0e4f0e8fe18a72c9ec0800e"
            }
          ]
        }
      ],
      "height": "3",
      "prevBlockHash": "ac0f2ad5cdbea3c6cd8eb0e4e1b7679b595a9c26ddda1acc75a5ff031abf9c91",
      "createdTime": "1578362175915"
    },
    "signatures": [
      {
        "publicKey": "bddd58404d1315e0eb27902c5d7c8eb0602c16238f005773df406bc191308929",
        "signature": "507a96cbb0ac99fada62cf36097204fc166a4635b70e68b9c87d007300ca0f7b231f4787cc9fe8751cc640a9cf2b7c161cc0bedb31e606363d4c18256268660f"
      }
    ]
  }
}

Get Block information

The code for getting Block information is slightly different than before.

from iroha import Iroha, IrohaCrypto, IrohaGrpc

net = IrohaGrpc('Host IP Address:50051')

iroha = Iroha('admin@test')
 admin_priv_key ='Admin Private Key' #Private Key for the account specified one line before

# Creating a Query
get_block_query = iroha.query(
        'GetBlock',
 height = 3 # Specify the Block Height (number) to get
)

# Sign Query
IrohaCrypto.sign_query(get_block_query, admin_priv_key)

# Send Query
response = net.send_query(get_block_query)

# Response output
print(response)

What you are doing is creating a Query → signing → sending → checking the result, so it is the same as sending a Transaction. The Function you use will change a little.

Execution result

query_hash: "eb7035cdde49e26f2271b7991933a5eb8321f1a3176014a73f53a1c86d91b8bc"
block_response {
  block {
    block_v1 {
      payload {
        transactions {
          payload {
            reduced_payload {
              commands {
                transfer_asset {
                  src_account_id: "admin@test"
                  dest_account_id: "test@test"
                  asset_id: "samplecoin#test"
                  description: "test"
                  amount: "10000"
                }
              }
              creator_account_id: "admin@test"
              created_time: 1578362174344
              quorum: 1
            }
          }
          signatures {
            public_key: "313a07e6384776ed95447710d15e59148473ccfc052a681317a72a69f2a49910"
            signature: "7bf5bec8110cbe618b05cd3bf8b69d0eb1e6e2eb4f3efcc41d0573b8e3638bc220b49c251c0f3238bb753d85d4b7a3e032f420f5c0e4f0e8fe18a72c9ec0800e"
          }
        }
        height: 3
        prev_block_hash: "ac0f2ad5cdbea3c6cd8eb0e4e1b7679b595a9c26ddda1acc75a5ff031abf9c91"
        created_time: 1578362175915
      }
      signatures {
        public_key: "bddd58404d1315e0eb27902c5d7c8eb0602c16238f005773df406bc191308929"
        signature: "507a96cbb0ac99fada62cf36097204fc166a4635b70e68b9c87d007300ca0f7b231f4787cc9fe8751cc640a9cf2b7c161cc0bedb31e606363d4c18256268660f"
      }
    }
  }
}

I was able to get information with Block Height (number) of 3.

As far as I read the document, it seems that there is no library to get the latest Block and the latest Block Height (number). When "height" of query was specified in the above code, an error occurred because the required items were not entered.

Impressions

I wrote an article about using Iroha's Java SDK, but even I, who is a beginner of Python, could use Python more easily. The details of how to assemble the command are also written on the API page of the official document, so it is relatively easy to understand.

Official documentation https://iroha.readthedocs.io/en/latest/develop/api.html

Get block information with Hyperledger Iroha's Java SDK

Recommended Posts

[Hyperledger Iroha] Notes on how to use the Python SDK
Notes on how to use pywinauto
Notes on how to use featuretools
Notes on how to use doctest
Notes on how to use marshmallow in the schema library
Memorandum on how to use gremlin python
How to use the C library in Python
How to use Python Kivy ④ ~ Execution on Android ~
[Algorithm x Python] How to use the list
python3: How to use bottle (2)
How to use the generator
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
How to use the decorator
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to use the Raspberry Pi relay module Python
[Python] How to use the graph creation library Altair
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
Autoencoder with Chainer (Notes on how to use + trainer)
[Introduction to Udemy Python3 + Application] 30. How to use the set
How to use Django on Google App Engine / Python
How to use the model learned in Lobe in Python
How to enjoy Python on Android !! Programming on the go !!
[2015/11/19] How to register a service locally using the python SDK on naoqi os
Python: How to use async with
How to use the zip function
[python] How to use the library Matplotlib for drawing graphs
[Python] How to use Pandas Series
How to use Dataiku on Windows
How to use the __call__ method in a Python class
How to use Requests (Python Library)
How to use SQLite in Python
How to get the Python version
How to deploy the easiest python textbook pybot on Heroku
How to use homebrew on Debian
[Python] How to import the library
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
I didn't know how to use the [python] for statement
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
Notes on how to use StatsModels that can use linear regression and GLM in python
How to use python zip function
Sakura Use Python on the Internet
How to use the ConfigParser module
Notes on how to write requirements.txt
[Python] How to use Typetalk API
Let's use Watson from Python! --How to use Developer Cloud Python SDK
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
How to change the log level of Azure SDK for Python
How to use python put in pyenv on macOS with PyCall