forked from hyperledger-iroha/iroha-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx-example.py
More file actions
executable file
·238 lines (202 loc) · 8.17 KB
/
Copy pathtx-example.py
File metadata and controls
executable file
·238 lines (202 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python3
#
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
#
"""
The example demonstrates how to use few Iroha commands and queries.
"""
# Here are Iroha dependencies.
# Python library generally consists of 3 parts:
# Iroha, IrohaCrypto and IrohaGrpc which we need to import:
import os
import sys
import binascii
from grpc import RpcError, StatusCode
import inspect # inspect.stack(0)
from iroha import Iroha, IrohaGrpc, IrohaCrypto
from functools import wraps
from utilities.errorCodes2Hr import get_proper_functions_for_commands
# The following line is actually about the permissions
# you might be using for the transaction.
# You can find all the permissions here:
# https://iroha.readthedocs.io/en/main/develop/api/permissions.html
from iroha.primitive_pb2 import can_set_my_account_detail
if sys.version_info[0] < 3:
raise Exception('Python 3 or a more recent version is required.')
# Here is the information about the environment and admin account information:
IROHA_HOST_ADDR = os.getenv('IROHA_HOST_ADDR', '127.0.0.1')
IROHA_PORT = os.getenv('IROHA_PORT', '50051')
ADMIN_ACCOUNT_ID = os.getenv('ADMIN_ACCOUNT_ID', 'admin@test')
ADMIN_PRIVATE_KEY = os.getenv(
'ADMIN_PRIVATE_KEY', 'f101537e319568c765b2cc89698325604991dca57b9716b58016b253506cab70')
# Here we will create user keys
user_private_key = IrohaCrypto.private_key()
user_public_key = IrohaCrypto.derive_public_key(user_private_key)
iroha = Iroha(ADMIN_ACCOUNT_ID)
net = IrohaGrpc(f'{IROHA_HOST_ADDR}:{IROHA_PORT}')
def trace(func):
"""
A decorator for tracing methods' begin/end execution points
"""
@wraps(func)
def tracer(*args, **kwargs):
name = func.__name__
stack_size = int(len(inspect.stack(0)) / 2) # @wraps(func) is also increasing the size
indent = stack_size*'\t'
print(f'{indent} > Entering "{name}": args: {args}')
result = func(*args, **kwargs)
print(f'{indent} < Leaving "{name}"')
return result
return tracer
@trace
def send_transaction_and_print_status(transaction):
hex_hash = binascii.hexlify(IrohaCrypto.hash(transaction))
creator_id = transaction.payload.reduced_payload.creator_account_id
commands = get_commands_from_tx(transaction)
print(f'Transaction "{commands}",'
f' hash = {hex_hash}, creator = {creator_id}')
net.send_tx(transaction)
for i, status in enumerate(net.tx_status_stream(transaction)):
status_name, status_code, error_code = status
print(f"{i}: status_name={status_name}, status_code={status_code}, "
f"error_code={error_code}")
if status_name in ('STATEFUL_VALIDATION_FAILED', 'STATELESS_VALIDATION_FAILED', 'REJECTED'):
error_code_hr = get_proper_functions_for_commands(commands)(error_code)
raise RuntimeError(f"{status_name} failed on tx: "
f"{transaction} due to reason {error_code}: "
f"{error_code_hr}")
def get_commands_from_tx(transaction):
commands_from_tx = []
for command in transaction.payload.reduced_payload.__getattribute__("commands"):
listed_fields = command.ListFields()
commands_from_tx.append(listed_fields[0][0].name)
return commands_from_tx
# For example, below we define a transaction made of 2 commands:
# CreateDomain and CreateAsset.
# Each of Iroha commands has its own set of parameters and there are many commands.
# You can check out all of them here:
# https://iroha.readthedocs.io/en/main/develop/api/commands.html
@trace
def create_domain_and_asset(domain: str, asset_short_id: str, precision=2, default_role='user'):
"""
Creates domain and asset with specific precision provided by arguments
"""
commands = [
iroha.command('CreateDomain', domain_id=domain, default_role=default_role),
iroha.command('CreateAsset', asset_name=asset_short_id,
domain_id=domain, precision=precision)
]
# And sign the transaction using the keys from earlier:
tx = IrohaCrypto.sign_transaction(
iroha.transaction(commands), ADMIN_PRIVATE_KEY)
send_transaction_and_print_status(tx)
# You can define queries
# (https://iroha.readthedocs.io/en/main/develop/api/queries.html)
# the same way.
@trace
def add_coin_to_admin(asset: str, amount='1000.00'):
"""
Add provided amount of specific units to admin account
"""
tx = iroha.transaction([
iroha.command('AddAssetQuantity',
asset_id=asset, amount=amount)
])
IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
send_transaction_and_print_status(tx)
@trace
def create_account(account_id: str, domain: str):
"""
Create account
"""
tx = iroha.transaction([
iroha.command('CreateAccount', account_name=account_id, domain_id=domain,
public_key=user_public_key)
])
IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
send_transaction_and_print_status(tx)
@trace
def transfer_coin(source_account, destination_account, asset_id, amount='2.00'):
"""
Transfer assets between accounts
"""
tx = iroha.transaction([
iroha.command('TransferAsset', src_account_id=source_account,
dest_account_id=destination_account, asset_id=asset_id,
description='init top up', amount=amount)
])
IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
send_transaction_and_print_status(tx)
@trace
def user_grants_to_admin_set_account_detail_permission(account_id: str):
"""
Make admin account able to set detail of account
"""
tx = iroha.transaction([
iroha.command('GrantPermission', account_id=ADMIN_ACCOUNT_ID,
permission=can_set_my_account_detail)
], creator_account=account_id)
IrohaCrypto.sign_transaction(tx, user_private_key)
send_transaction_and_print_status(tx)
@trace
def set_age_to_user(account_id: str):
"""
Set age to user by admin account
"""
tx = iroha.transaction([
iroha.command('SetAccountDetail',
account_id=account_id, key='age', value='18')
])
IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
send_transaction_and_print_status(tx)
@trace
def get_coin_info(asset: str):
"""
Get asset info for provided asset
"""
query = iroha.query('GetAssetInfo', asset_id=asset)
IrohaCrypto.sign_query(query, ADMIN_PRIVATE_KEY)
response = net.send_query(query)
data = response.asset_response.asset
print(f'Asset id = {data.asset_id}, precision = {data.precision}')
@trace
def get_account_assets(account_id: str):
"""
List all the assets of provided user account
"""
query = iroha.query('GetAccountAssets', account_id=account_id)
IrohaCrypto.sign_query(query, ADMIN_PRIVATE_KEY)
response = net.send_query(query)
data = response.account_assets_response.account_assets
for asset in data:
print(f'Asset id = {asset.asset_id}, balance = {asset.balance}')
@trace
def get_user_details(account_id: str):
"""
Get all the kv-storage entries for userone@domain
"""
query = iroha.query('GetAccountDetail', account_id=account_id)
IrohaCrypto.sign_query(query, ADMIN_PRIVATE_KEY)
response = net.send_query(query)
data = response.account_detail_response
print(f'Account id = {account_id}, details = {data.detail}')
if __name__ == '__main__':
try:
create_domain_and_asset(domain='domain', asset_short_id='coin')
add_coin_to_admin(asset='coin#domain')
create_account(account_id='userone', domain='domain')
transfer_coin('admin@test', 'userone@domain', 'coin#domain')
user_grants_to_admin_set_account_detail_permission(account_id='userone@domain')
set_age_to_user(account_id='userone@domain')
get_coin_info(asset='coin#domain')
get_account_assets(account_id='userone@domain')
get_user_details(account_id='userone@domain')
except RpcError as rpc_error:
if rpc_error.code() == StatusCode.UNAVAILABLE:
print(f'[E] Iroha is not running in address:'
f'{IROHA_HOST_ADDR}:{IROHA_PORT}!')
else:
print(e)
except RuntimeError as e:
print(e)