Using Hyperledger Iroha's Python SDK, ・ Creating Asset ・ Send Asset ・ Acquisition of block information I will try.
・ The environment must be built according to the official guide of iroha. ・ Environment that can use Python3
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.
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"
}
]
}
}
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"
}
]
}
}
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.
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