[Hyperledger Iroha] Create an account using Python library

Article content

Create an account using Hyperledger Iroha's Python library.

Preparation

Install the libraries needed to create an account key pair

pip install ed25519

Implementation

Create an account

It is a code to create an account immediately

from iroha import Iroha, IrohaCrypto, IrohaGrpc
import ed25519
import iroha_config

# Connection information to iroha
net = IrohaGrpc(iroha_config.IROHA_HOST)
iroha = Iroha(iroha_config.ADMIN_ACCOUNT)

# Creating a key pair
signing_key, verifying_key = ed25519.create_keypair()

# Save the created key pair
open("[email protected]","wb").write(signing_key.to_ascii(encoding="hex"))
open("[email protected]","wb").write(verifying_key.to_ascii(encoding="hex"))

# Convert from binary to hexadecimal
vkey_hex = verifying_key.to_ascii(encoding="hex")

# Creating a Transaction
transfer_tx = iroha.transaction(
 [iroha.command(
    'CreateAccount',
    account_name ='iroha',
    domain_id = 'test',
    public_key = vkey_hex
 )]
)

# Transaction signature
IrohaCrypto.sign_transaction(transfer_tx, iroha_config.ADMIN_PRIV_KEY)

# Send Transaction
net.send_tx(transfer_tx)

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

When you create an account using iroha-cli, the key information is also created, but if you create an account from the library, you need to create it yourself. I am using a library called "ed25519" to create it.

Execution result

If the following result is output, it is successful.

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

I will also check the contents of the block.

{
  "blockV1": {
    "payload": {
      "transactions": [
        {
          "payload": {
            "reducedPayload": {
              "commands": [
                {
                  "createAccount": {
                    "accountName": "iroha",
                    "domainId": "test",
                    "publicKey": "efdc215eab6dd2c4435d370da73e4b88350e1fed9d39afed503fcbee985fce1f"
                  }
                }
              ],
              "creatorAccountId": "admin@test",
              "createdTime": "1594016319157",
              "quorum": 1
            }
          },
          "signatures": [
            {
              "publicKey": "313a07e6384776ed95447710d15e59148473ccfc052a681317a72a69f2a49910",
              "signature": "429bf3ae70b60d31ab3fc3c15fa11cb8155b6a824f831652c1c46068feaeb866846b8048e0959cbcbeed532423f050e77d2fe192ea90d0827a6f1489bf67ec0a"
            }
          ]
        }
      ],
      "height": "4",
      "prevBlockHash": "039c70e4d990318c7373a59b14f39c9dd6199b949f1ba74b28c44d100adf8218",
      "createdTime": "1594016319568"
    },
    "signatures": [
      {
        "publicKey": "bddd58404d1315e0eb27902c5d7c8eb0602c16238f005773df406bc191308929",
        "signature": "e2a7dc68e837fc4bdefc2e7f60131661c9567f0f2b19af7e0069ced69a4054f7402fb93abd38e96aef850d865c760f3983632654db61fc0ab0181224e44fb20d"
      }
    ]
  }
}

I was able to confirm that the transaction of the createAccount command was captured in the block.

Since the same user name and the same domain cannot be registered, if you execute with the same contents, the following result will be obtained and the transaction will be rejected.

('ENOUGH_SIGNATURES_COLLECTED', 9, 0)
('STATEFUL_VALIDATION_FAILED', 2, 4)
('REJECTED', 4, 0)
{
  "blockV1": {
    "payload": {
      "height": "5",
      "prevBlockHash": "fe0c9b0e59efe7de5f67710ad66fdb5aab3f1e41b79bed8eda13f9c3d122d8cf",
      "createdTime": "1594017524600",
      "rejectedTransactionsHashes": [
        "ef17d5f4e82c7e9690676110b3d84c9ff721fd2ac78678e1b893cf3c024c5156"
      ]
    },
    "signatures": [
      {
        "publicKey": "bddd58404d1315e0eb27902c5d7c8eb0602c16238f005773df406bc191308929",
        "signature": "0ea48cca0b0db7ee525edafa961e6f2469989cbbb1346cc31e140a98b8260693ef939fe30bc9ef10a5ab4d0a6e7ffeccc1cc15124cb472be1d4f658b5aac1b0d"
      }
    ]
  }
}

Get account information

from iroha import Iroha, IrohaCrypto, IrohaGrpc
import iroha_config

net = IrohaGrpc(iroha_config.IROHA_HOST)

iroha = Iroha(iroha_config.ADMIN_ACCOUNT)
admin_priv_key = iroha_config.ADMIN_PRIV_KEY

# Creating a Query
get_block_query = iroha.query(
        'GetAccount',
        account_id = 'iroha@test'  
)

# Sign Query
IrohaCrypto.sign_query(get_block_query, admin_priv_key)

# Send Query
response = net.send_query(get_block_query)

# Response output
print(response)

The method of acquiring account information is basically the same as sending a transaction. The content of the transmission is just a query, not a command

Execution result

account_response {
  account {
    account_id: "iroha@test"
    domain_id: "test"
    quorum: 1
    json_data: "{}"
  }
  account_roles: "user"
}
query_hash: "a07e90ca85d9c4f0bd1b12ba8cd9c11886729fcd50ee4c2fc50e6c9a27047a2e"

I was able to get the execution result

reference

-Iroha API Reference # create-account

Iroha API Reference # get-account

Related article

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

Recommended Posts

[Hyperledger Iroha] Create an account using Python library
Create an image file using PIL (Python Imaging Library).
[Hyperledger Iroha] Query with Python library
[Python] Create an infrastructure diagram in 3 minutes using diagrams
Create JIRA tickets using Python
[Python] Create an event-driven web crawler using AWS's serverless architecture
Create a python GUI using tkinter
Create an Excel file with Python3
Easily create homemade RPA using Python
Quickly create an excel file with Python #python
Install python library on Lambda using [/ tmp]
[Python] Create a Batch environment using AWS-CDK
Create an application using the Spotify API
Create an OpenCV3 + python3 environment on OSX
[Python] Quickly create an API with Flask
Create an English word app with python
Create an elliptical scatter plot in Python without using a multivariate normal distribution
Feel free to turn Python using the library into an AWS Lambda function
[Personal memo] julia --Using Python library with julia using PyCall
Create a GIF file using Pillow in Python
Aggregate test results using the QualityForward Python library
Create an app that guesses students with python
Chord recognition using chromagram of python library librosa
Email attachments using your gmail account in python.
Create polka dot wallpaper with Python Image Library
[Python pandas] Create an empty DataFrame from an existing DataFrame
Create an environment of 64bit Windows + python 2.7 + MeCab 0.996
Introduction of Python Imaging Library (PIL) using HomeBrew
Create a web map using Python and GDAL
Create wav file from GLSL shader using python3
Create an image with characters in python (Japanese)
[Python] Create an asynchronous task execution environment + monitoring environment
Try to create an HTTP server using Node.js
Create an account from CentOS mail server construction
Create a Mac app using py2app and Python3! !!
Create a MIDI file in Python using pretty_midi
Solve simultaneous equations in an instant using Python
Create an API server quickly with Python + Falcon
Python 3.6 email library
Python ast library
Start using Python
★ Memo ★ Python Iroha
Python Library notes
Scraping using Python
Create an animated GIF local server with Python + Flask
Create a data collection bot in Python using Selenium
Check SSL certificate renewal deadline using Python3 M2Crypto library
[Blender x Python] How to create an original object
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
[Python] Create a ValueObject with a complete constructor using dataclasses
Create your first GDSII file in Python using gdspy
How to create an image uploader in Bottle (Python)
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
Using the National Diet Library Search API in Python
Create a company name extractor with python using JCLdic
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
How to create an instance of a particular class from dict using __new__ () in python