-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiroha_functions.py
468 lines (411 loc) · 17.2 KB
/
iroha_functions.py
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/env python3
from iroha.primitive_pb2 import can_set_my_account_detail
from iroha import IrohaCrypto, Iroha
import binascii
import iroha_config
import sys
if sys.version_info[0] < 3:
raise Exception('Python 3 or a more recent version is required.')
def trace(func):
"""
A decorator for tracing methods' begin/end execution points
"""
def tracer(*args, **kwargs):
name = func.__name__
print('\tEntering "{}"'.format(name))
result = func(*args, **kwargs)
print('\tLeaving "{}"'.format(name))
return result
return tracer
@trace
def send_transaction_and_print_status(transaction):
"""
Send a transaction to the Blockchain (BSMD)
:param transaction: Transaction we are sending to the BSMD
:return: null:
"""
print('This print will make the transactions run slower. When developing is useful to have this for debugging')
print('Comment all prints in function send_transaction_and_print_status to make faster transactions')
hex_hash = binascii.hexlify(IrohaCrypto.hash(transaction))
print('Transaction hash = {}, creator = {}'.format(
hex_hash, transaction.payload.reduced_payload.creator_account_id))
iroha_config.NETWORK.send_tx(transaction)
for status in iroha_config.NETWORK.tx_status_stream(transaction):
print(status)
@trace
def create_domain_and_asset(domain, default_role, asset_name, asset_precision):
"""
Creates a domain and an asset in the domain
:param domain: name of the domain, e.g. entity, vehicle, individual
:param default_role: default role users have, e.g., user
:param asset_name: name of the asset, e.g., entityCoin
:param asset_precision: number of decimals accepted in the asset
:return: null
"""
print(domain, default_role, asset_name, asset_precision)
commands = [iroha_config.IROHA_ADMIN.command('CreateDomain',
domain_id=domain,
default_role=default_role),
iroha_config.IROHA_ADMIN.command('CreateAsset',
asset_name=asset_name,
domain_id=domain,
precision=asset_precision)]
tx = IrohaCrypto.sign_transaction(iroha_config.IROHA_ADMIN.transaction(commands), iroha_config.ADMIN_PRIVATE_KEY)
send_transaction_and_print_status(tx)
@trace
def create_domain(domain, default_role):
"""
Creates a domain
:param domain: name of the domain, e.g. entity, vehicle, individual
:param default_role: default role users have, e.g., user
:return:
"""
print(domain, default_role)
tx = iroha_config.IROHA_ADMIN.transaction(
[iroha_config.IROHA_ADMIN.command('CreateDomain',
domain_id=domain,
default_role=default_role)])
IrohaCrypto.sign_transaction(tx, iroha_config.ADMIN_PRIVATE_KEY)
send_transaction_and_print_status(tx)
@trace
def create_asset(domain, asset_name, asset_precision):
"""
Creates an asset in the domain_id
:param domain: name of the domain, e.g. entity, vehicle, individual
:param asset_name: name of the asset, e.g., entityCoin
:param asset_precision: number of decimals accepted in the asset
:return:
"""
tx = iroha_config.IROHA_ADMIN.transaction(
[iroha_config.IROHA_ADMIN.command('CreateAsset',
asset_name=asset_name,
domain_id=domain,
precision=asset_precision)])
IrohaCrypto.sign_transaction(tx, iroha_config.ADMIN_PRIVATE_KEY)
send_transaction_and_print_status(tx)
@trace
def create_account_with_assets(domain, name, public_key, asset_name, asset_qty):
"""
Create a personal account. This function works in three steps
1. Create an account with a name, in a domain and a public key
2. The admin create credit (assets) for the account (credit is created only if the user
buy it)
3. The admin transfer the credit to the user
:param domain: (str) Name of the domain the user wants to join
:param name: (str) Name of the node we are creating
:param public_key: (str) public key of the node
:param asset_name: Name of asset the node buy
:param asset_qty: (float) Quantity of assets the node buy
:return: null:
Usage example:
create_account_user('Tommy', 'key', 'federated', '5', fedcoin#federated)
"""
asset_id = asset_name + '#' + domain
# 1. Create account
tx = iroha_config.IROHA_ADMIN.transaction(
[iroha_config.IROHA_ADMIN.command('CreateAccount',
account_name=name,
domain_id=domain,
public_key=public_key)])
IrohaCrypto.sign_transaction(tx, iroha_config.ADMIN_PRIVATE_KEY)
send_transaction_and_print_status(tx)
# 2. Create credit for the user
tx = iroha_config.IROHA_ADMIN.transaction([iroha_config.IROHA_ADMIN.command('AddAssetQuantity',
asset_id=asset_id,
amount=asset_qty)])
IrohaCrypto.sign_transaction(tx, iroha_config.ADMIN_PRIVATE_KEY)
send_transaction_and_print_status(tx)
# 3. Transfer credit to the user
dest_account_id = name + '@' + domain
tx = iroha_config.IROHA_ADMIN.transaction([
iroha_config.IROHA_ADMIN.command('TransferAsset',
src_account_id='admin@test',
dest_account_id=dest_account_id,
asset_id=asset_id,
description='initial credit',
amount=asset_qty)])
IrohaCrypto.sign_transaction(tx, iroha_config.ADMIN_PRIVATE_KEY)
send_transaction_and_print_status(tx)
@trace
def create_assets_for_user(domain, name, asset_name, asset_qty):
"""
Create a personal account. This function works in three steps
1. Create an account with a name, in a domain and a public key
2. The admin create credit (assets) for the account (credit is created only if the user
buy it)
3. The admin transfer the credit to the user
:param domain: (str) Name of the domain the user wants to join
:param name: (str) Name of the node we are creating
:param asset_name: (str) Name of asset the node buy
:param asset_qty: (float) Quantity of assets the node buy
:return: null:
"""
asset_id = asset_name + '#' + domain
tx = iroha_config.IROHA_ADMIN.transaction([iroha_config.IROHA_ADMIN.command('AddAssetQuantity',
asset_id=asset_id,
amount=asset_qty)])
IrohaCrypto.sign_transaction(tx, iroha_config.ADMIN_PRIVATE_KEY)
send_transaction_and_print_status(tx)
dest_account_id = name + '@' + domain
tx = iroha_config.IROHA_ADMIN.transaction([
iroha_config.IROHA_ADMIN.command('TransferAsset',
src_account_id='admin@test',
dest_account_id=dest_account_id,
asset_id=asset_id,
description='asset created for node',
amount=asset_qty)])
IrohaCrypto.sign_transaction(tx, iroha_config.ADMIN_PRIVATE_KEY)
send_transaction_and_print_status(tx)
@trace
def create_account(domain, name, public_key):
"""
Create a personal account.
:param domain: (str) Name of the domain the user wants to join
:param name: (str) Name of the node we are creating
:param public_key: (str) public key of the node
:return: null:
"""
# 1. Create account
tx = iroha_config.IROHA_ADMIN.transaction(
[iroha_config.IROHA_ADMIN.command('CreateAccount',
account_name=name,
domain_id=domain,
public_key=public_key)])
IrohaCrypto.sign_transaction(tx, iroha_config.ADMIN_PRIVATE_KEY)
send_transaction_and_print_status(tx)
@trace
def get_balance(domain, name, private_key):
"""
Get the balance of the account
:param domain: (str) name of the domain
:param name: (str) name of the transaction signer
:param private_key: (str) Private key of the user
:return: data: (array) asset id and assets quantity
Return example:
[asset_id: "fedcoin#federated"
account_id: "generator@federated"
balance: "1000"
]
"""
account_id = name + '@' + domain
iroha = Iroha(account_id)
query = iroha.query('GetAccountAssets',
account_id=account_id)
IrohaCrypto.sign_query(query, private_key)
response = iroha_config.NETWORK.send_query(query)
data = response.account_assets_response.account_assets
for asset in data:
print('Asset id = {}, balance = {}'.format(asset.asset_id, asset.balance))
return data
@trace
def grants_access_to_set_details(your_domain, your_name, private_key, grant_domain, grant_account):
"""
Grant node to set details on your profile
:param your_domain: (str) your domain. Domain of the granter
:param your_name: (str) you name. Name of the granter
:param private_key: (str) private key for signing. Private key of the granter
:param grant_domain: (str) domain of the node how will have access permissions
:param grant_account: (str) name of the node how will have access permissions
:return:
"""
my_id_account = your_name + '@' + your_domain
grant_account_id = grant_account + '@' + grant_domain
iroha = Iroha(my_id_account)
tx = iroha.transaction([
iroha.command('GrantPermission',
account_id=grant_account_id,
permission=can_set_my_account_detail)
],
creator_account=my_id_account)
IrohaCrypto.sign_transaction(tx, private_key)
send_transaction_and_print_status(tx)
@trace
def set_detail(domain, name, private_key, detail_key, detail_value):
"""
Set the details of a node. The details can be stored in JSON format with limit of 4096 characters per detail
:param domain: (str) name of the domain
:param name: (str) name of the transaction signer
:param private_key: (str) Private key of the user
:param detail_key: (str) Name of the detail we want to set
:param detail_value: (str) Value of the detail
:return: null:
Usage example:
set_detail('vehicle'),'Ford fiesta', 'key', 'age', '33')
"""
account_id = name + '@' + domain
iroha = Iroha(account_id)
tx = iroha.transaction([
iroha.command('SetAccountDetail',
account_id=account_id,
key=detail_key,
value=detail_value)
])
IrohaCrypto.sign_transaction(tx, private_key)
send_transaction_and_print_status(tx)
@trace
def set_detail_to_node(domain, name, private_key, to_domain, to_name, detail_key, detail_value):
"""
Set the details of a node. The details can be stored in JSON format with limit of 4096 characters per detail
:param domain: (str) domain of the signer
:param name: (str) name signer
:param private_key: (str) Private key of the signer
:param to_domain: (str) domain of the receptor
:param to_name: (str) name of the receptor
:param detail_key: (str) Name of the detail we want to set
:param detail_value: (str) Value of the detail
:return: null:
Usage example:
set_detail('vehicle'),'Ford fiesta', 'key', 'age', '33')
"""
account = name + '@' + domain
iroha = Iroha(account)
account_id = to_name + '@' + to_domain
tx = iroha.transaction([
iroha.command('SetAccountDetail',
account_id=account_id,
key=detail_key,
value=detail_value)
])
IrohaCrypto.sign_transaction(tx, private_key)
send_transaction_and_print_status(tx)
@trace
def transfer_assets(domain, name, private_key, to_name, asset_name, quantity, description):
"""
Transfer assets from one account to another
:param domain: (str) name of the domain from where the node is sending the assets
:param name: (str) name of the node who is sending the assets
:param private_key: (str) pk of the the sender
:param to_name: (str) name of the node receiving the assets
:param asset_name: (str) name of the asset to be transferred
:param quantity: (float) Number of assets we want to transfer
:param description: (str) Small message to the receiver of assets
:return:
Example:
transfer_assets('individuals','Dante', 'key', 'Toro', 'coin', '2', 'Shut up and take my money')
"""
account_id = name + '@' + domain
iroha = Iroha(account_id)
destination_account = to_name + '@' + domain
asset_id = asset_name + '#' + domain
tx = iroha.transaction([
iroha.command('TransferAsset',
src_account_id=account_id,
dest_account_id=destination_account,
asset_id=asset_id,
description=description,
amount=quantity)
])
IrohaCrypto.sign_transaction(tx, private_key)
send_transaction_and_print_status(tx)
@trace
def get_detail_from_generator(domain, name, private_key, generator_domain, generator_name, detail_id):
"""
Consult a single detail writen by some generator
:param domain: (str) name of the domain
:param name: (str) name of the node
:param private_key: (str) Private key of the user
:param generator_domain: (str) domain of the user who create de detail
:param generator_name: (str) name of the user who create de detail
:param detail_id: (string) Name of the detail
:return: data: (json) solicited details of the user
Usage example:
get_detail_from_generator('individual', 'David' 'key', 'vehicle', 'Sara' , 'Age')
Return example:
{
"nodeA@domain":{
"Age":"35"
}
}
"""
account_id = name + '@' + domain
generator_id = generator_name + '@' + generator_domain
iroha = Iroha(account_id)
query = iroha.query('GetAccountDetail',
account_id=account_id,
writer=generator_id,
key=detail_id)
IrohaCrypto.sign_query(query, private_key)
response = iroha_config.NETWORK.send_query(query)
data = response.account_detail_response
print('Account id = {}, details = {}'.format(account_id, data.detail))
return data.detail
@trace
def get_all_details_from_generator(domain, name, private_key, generator_domain, generator_name):
"""
Consult all the details generated by some node
:param domain: (str) name of the domain
:param name: (str) name of the node
:param private_key: (str) Private key of the user
:param generator_domain: (str) domain of the user who create de detail
:param generator_name: (str) name of the user who create de detail
:return: data: (json) solicited details of the user
Usage example:
get_detail_from_generator('vehicle', 'Ford Fiesta', key, 'individual', 'david' )
Return example:
{
"nodeA@domain":{
"Age":"35",
"Name":"Quetzacolatl"
}
}
"""
account_id = name + '@' + domain
generator_id = generator_name + '@' + generator_domain
iroha = Iroha(account_id)
query = iroha.query('GetAccountDetail',
account_id=account_id,
writer=generator_id)
IrohaCrypto.sign_query(query, private_key)
response = iroha_config.NETWORK.send_query(query)
data = response.account_detail_response
print('Account id = {}, details = {}'.format(account_id, data.detail))
return data.detail
@trace
def get_all_details(domain, name, private_key):
"""
Consult all details of the node
:param domain: (str) name of the domain
:param name: (str) name of the node
:param private_key: (str) Private key of the user
:return: data: (json) solicited details of the user
Usage example:
get_detail_from_generator(Iroha('david@federated'),IrohaGrpc('127.0.0.1'), 'david@federated', 'key')
Return example:
{
"nodeA@domain":{
"Age":"35",
"Name":"Quetzacoatl"
},
"nodeB@domain":{
"Location":"35.3333535,-45.2141556464",
"Status":"valid"
},
"nodeA@domainB":{
"FederatingParam":"35.242553",
"Loop":"3"
}
}
"""
account_id = name + '@' + domain
iroha = Iroha(account_id)
query = iroha.query('GetAccountDetail',
account_id=account_id)
IrohaCrypto.sign_query(query, private_key)
response = iroha_config.NETWORK.send_query(query)
data = response.account_detail_response
print('Account id = {}, details = {}'.format(account_id, data.detail))
return data.detail
@trace
def get_block(height):
"""
Query a block in the ledger
:param height:
:return:
"""
iroha_config.IROHA_ADMIN.blocks_query()
query = iroha_config.IROHA_ADMIN.query('GetBlock',
height=height)
IrohaCrypto.sign_query(query, iroha_config.ADMIN_PRIVATE_KEY)
block = iroha_config.NETWORK.send_query(query)
print(block)
return block