Create an account using Hyperledger Iroha's Python library.
Install the libraries needed to create an account key pair
pip install ed25519
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.
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"
}
]
}
}
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
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
-Iroha API Reference # create-account
・ Iroha API Reference # get-account
[Hyperledger Iroha] Notes on how to use the Python SDK
Recommended Posts