This repository was archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathapi.py
1706 lines (1347 loc) · 58.4 KB
/
api.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from typing import Dict, Iterable, Optional
from iota import AdapterSpec, Address, BundleHash, ProposedTransaction, Tag, \
TransactionHash, TransactionTrytes, TryteString, TrytesCompatible
from iota.crypto.addresses import AddressGenerator
from iota.api_async import AsyncStrictIota, AsyncIota
import asyncio
__all__ = [
'InvalidCommand',
'Iota',
'StrictIota',
]
class InvalidCommand(ValueError):
"""
Indicates that an invalid command name was specified.
"""
pass
# There is a compact and easy way to create the synchronous version of the async
# classes:
# import inspect
# def make_synchronous(new_name, async_class: type):
# def make_sync(method):
# def sync_version(*args, **kwargs):
# return asyncio.get_event_loop().run_until_complete(method(*args, **kwargs))
# return sync_version
# return type(new_name, (async_class,), {
# name: make_sync(method) if inspect.iscoroutinefunction(method) else method
# for name, method in inspect.getmembers(async_class)
# })
# # create the sync version of the class
# Iota = make_synchronous('Iota', AsyncIota)
# While this approach would work, no IDE static analysis would pick up the
# method definitions or docstrings for the new `Iota` class, meaning no
# suggestions, intellisense, code completion, etc. for the user.
# Therefore we keep the manual approach.
class StrictIota(AsyncStrictIota):
"""
Synchronous API to send HTTP requests for communicating with an IOTA node.
This implementation only exposes the "core" API methods. For a more
feature-complete implementation, use :py:class:`Iota` instead.
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference
:param AdapterSpec adapter:
URI string or BaseAdapter instance.
:param Optional[bool] devnet:
Whether to use devnet settings for this instance.
On the devnet, minimum weight magnitude is set to 9, on mainnet
it is 1 by default.
:param Optional[bool] local_pow:
Whether to perform proof-of-work locally by redirecting all calls
to :py:meth:`attach_to_tangle` to
`ccurl pow interface <https://pypi.org/project/PyOTA-PoW/>`_.
See :ref:`README:Optional Local Pow` for more info and
:ref:`find out<pow-label>` how to use it.
"""
def __init__(
self,
adapter: AdapterSpec,
devnet: bool = False,
local_pow: bool = False
) -> None:
"""
:param AdapterSpec adapter:
URI string or BaseAdapter instance.
:param bool devnet:
Whether to use devnet settings for this instance.
On the devnet, minimum weight magnitude is set to 9, on mainnet
it is 1 by default.
:param Optional[bool] local_pow:
Whether to perform proof-of-work locally by redirecting all calls
to :py:meth:`attach_to_tangle` to
`ccurl pow interface <https://pypi.org/project/PyOTA-PoW/>`_.
See :ref:`README:Optional Local Pow` for more info and
:ref:`find out<pow-label>` how to use it.
"""
super().__init__(adapter, devnet, local_pow)
def add_neighbors(self, uris: Iterable[str]) -> dict:
"""
Add one or more neighbors to the node. Lasts until the node is
restarted.
:param Iterable[str] uris:
Use format ``<protocol>://<ip address>:<port>``.
Example: ``add_neighbors(['udp://example.com:14265'])``
.. note::
These URIs are for node-to-node communication (e.g.,
weird things will happen if you specify a node's HTTP
API URI here).
:return:
``dict`` with the following structure::
{
'addedNeighbors': int,
Total number of added neighbors.
'duration': int,
Number of milliseconds it took to complete the request.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#addneighbors
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().add_neighbors(uris)
)
def attach_to_tangle(
self,
trunk_transaction: TransactionHash,
branch_transaction: TransactionHash,
trytes: Iterable[TryteString],
min_weight_magnitude: Optional[int] = None,
) -> dict:
"""
Attaches the specified transactions (trytes) to the Tangle by
doing Proof of Work. You need to supply branchTransaction as
well as trunkTransaction (basically the tips which you're going
to validate and reference with this transaction) - both of which
you'll get through the :py:meth:`get_transactions_to_approve` API call.
The returned value is a different set of tryte values which you
can input into :py:meth:`broadcast_transactions` and
:py:meth:`store_transactions`.
:param TransactionHash trunk_transaction:
Trunk transaction hash.
:param TransactionHash branch_transaction:
Branch transaction hash.
:param Iterable[TransactionTrytes] trytes:
List of transaction trytes in the bundle to be attached.
:param Optional[int] min_weight_magnitude:
Minimum weight magnitude to be used for attaching trytes.
14 by default on mainnet, 9 on devnet/devnet.
:return:
``dict`` with the following structure::
{
'trytes': List[TransactionTrytes],
Transaction trytes that include a valid nonce field.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#attachtotangle
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().attach_to_tangle(
trunk_transaction,
branch_transaction,
trytes,
min_weight_magnitude,
)
)
def broadcast_transactions(self, trytes: Iterable[TryteString]) -> dict:
"""
Broadcast a list of transactions to all neighbors.
The input trytes for this call are provided by
:py:meth:`attach_to_tangle`.
:param Iterable[TransactionTrytes] trytes:
List of transaction trytes to be broadcast.
:return:
``dict`` with the following structure::
{
'duration': int,
Number of milliseconds it took to complete the request.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#broadcasttransactions
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().broadcast_transactions(
trytes,
)
)
def check_consistency(self, tails: Iterable[TransactionHash]) -> dict:
"""
Used to ensure tail resolves to a consistent ledger which is
necessary to validate before attempting promotion. Checks
transaction hashes for promotability.
This is called with a pending transaction (or more of them) and
it will tell you if it is still possible for this transaction
(or all the transactions simultaneously if you give more than
one) to be confirmed, or not (because it conflicts with another
already confirmed transaction).
:param Iterable[TransactionHash] tails:
Transaction hashes. Must be tail transactions.
:return:
``dict`` with the following structure::
{
'state': bool,
Whether tails resolve to consistent ledger.
'info': str,
This field will only exist if 'state' is ``False``.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#checkconsistency
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().check_consistency(
tails,
)
)
def find_transactions(
self,
bundles: Optional[Iterable[BundleHash]] = None,
addresses: Optional[Iterable[Address]] = None,
tags: Optional[Iterable[Tag]] = None,
approvees: Optional[Iterable[TransactionHash]] = None,
) -> dict:
"""
Find the transactions which match the specified input and
return.
All input values are lists, for which a list of return values
(transaction hashes), in the same order, is returned for all
individual elements.
Using multiple of these input fields returns the intersection of
the values.
:param Optional[Iterable[BundleHash] bundles:
List of bundle IDs.
:param Optional[Iterable[Address]] addresses:
List of addresses.
:param Optional[Iterable[Tag]] tags:
List of tags.
:param Optional[Iterable[TransactionHash]] approvees:
List of approvee transaction IDs.
:return:
``dict`` with the following structure::
{
'hashes': List[TransationHash],
Found transactions.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#findtransactions
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().find_transactions(
bundles,
addresses,
tags,
approvees,
)
)
def get_balances(
self,
addresses: Iterable[Address],
tips: Optional[Iterable[TransactionHash]] = None,
) -> dict:
"""
Returns the confirmed balance which a list of addresses have at the
latest confirmed milestone.
In addition to the balances, it also returns the milestone as
well as the index with which the confirmed balance was
determined. The balances are returned as a list in the same
order as the addresses were provided as input.
:param Iterable[Address] addresses:
List of addresses to get the confirmed balance for.
:param Optional[Iterable[TransactionHash]] tips:
Tips whose history of transactions to traverse to find the balance.
:return:
``dict`` with the following structure::
{
'balances': List[int],
List of balances in the same order as the addresses
parameters that were passed to the endpoint.
'references': List[TransactionHash],
The referencing tips. If no tips parameter was passed
to the endpoint, this field contains the hash of the
latest milestone that confirmed the balance.
'milestoneIndex': int,
The index of the milestone that confirmed the most
recent balance.
'duration': int,
Number of milliseconds it took to process the request.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#getbalances
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().get_balances(
addresses,
tips,
)
)
def get_inclusion_states(
self,
transactions: Iterable[TransactionHash],
) -> dict:
"""
Get the inclusion states of a set of transactions. This is for
determining if a transaction was accepted and confirmed by the
network or not.
:param Iterable[TransactionHash] transactions:
List of transactions you want to get the inclusion state
for.
:return:
``dict`` with the following structure::
{
'states': List[bool],
List of boolean values in the same order as the
transactions parameters. A ``True`` value means the
transaction was confirmed.
'duration': int,
Number of milliseconds it took to process the request.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#getinclusionstates
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().get_inclusion_states(
transactions,
)
)
# Add an alias for this call, more descriptive
is_confirmed = get_inclusion_states
def get_missing_transactions(self) -> dict:
"""
Returns all transaction hashes that a node is currently requesting
from its neighbors.
:return:
``dict`` with the following structure::
{
'hashes': List[TransactionHash],
Array of missing transaction hashes.
'duration': int,
Number of milliseconds it took to process the request.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#getmissingtransactions
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().get_missing_transactions()
)
def get_neighbors(self) -> dict:
"""
Returns the set of neighbors the node is connected with, as well
as their activity count.
The activity counter is reset after restarting IRI.
:return:
``dict`` with the following structure::
{
'neighbors': List[dict],
Array of objects, including the following fields with
example values:
"address": "/8.8.8.8:14265",
"numberOfAllTransactions": 158,
"numberOfRandomTransactionRequests": 271,
"numberOfNewTransactions": 956,
"numberOfInvalidTransactions": 539,
"numberOfStaleTransactions": 663,
"numberOfSentTransactions": 672,
"connectiontype": "TCP"
'duration': int,
Number of milliseconds it took to process the request.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#getneighbors
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().get_neighbors()
)
def get_node_api_configuration(self) -> dict:
"""
Returns a node's API configuration settings.
:return:
``dict`` with the following structure::
{
'<API-config-settings>': type,
Configuration parameters for a node.
...
...
...
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/iri-configuration-options
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#getnodeapiconfiguration
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().get_node_api_configuration()
)
def get_node_info(self) -> dict:
"""
Returns information about the node.
:return:
``dict`` with the following structure::
{
'appName': str,
Name of the IRI network.
'appVersion': str,
Version of the IRI.
'jreAvailableProcessors': int,
Available CPU cores on the node.
'jreFreeMemory': int,
Amount of free memory in the Java virtual machine.
'jreMaxMemory': int,
Maximum amount of memory that the Java virtual machine
can use,
'jreTotalMemory': int,
Total amount of memory in the Java virtual machine.
'jreVersion': str,
The version of the Java runtime environment.
'latestMilestone': TransactionHash
Transaction hash of the latest milestone.
'latestMilestoneIndex': int,
Index of the latest milestone.
'latestSolidSubtangleMilestone': TransactionHash,
Transaction hash of the latest solid milestone.
'latestSolidSubtangleMilestoneIndex': int,
Index of the latest solid milestone.
'milestoneStartIndex': int,
Start milestone for the current version of the IRI.
'neighbors': int,
Total number of connected neighbor nodes.
'packetsQueueSize': int,
Size of the packet queue.
'time': int,
Current UNIX timestamp.
'tips': int,
Number of tips in the network.
'transactionsToRequest': int,
Total number of transactions that the node is missing in
its ledger.
'features': List[str],
Enabled configuration options.
'coordinatorAddress': Address,
Address (Merkle root) of the Coordinator.
'duration': int,
Number of milliseconds it took to process the request.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#getnodeinfo
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().get_node_info()
)
def get_transactions_to_approve(
self,
depth: int,
reference: Optional[TransactionHash] = None,
) -> dict:
"""
Tip selection which returns ``trunkTransaction`` and
``branchTransaction``.
:param int depth:
Number of milestones to go back to start the tip selection algorithm.
The higher the depth value, the more "babysitting" the node
will perform for the network (as it will confirm more
transactions that way).
:param TransactionHash reference:
Transaction hash from which to start the weighted random walk.
Use this parameter to make sure the returned tip transaction hashes
approve a given reference transaction.
:return:
``dict`` with the following structure::
{
'trunkTransaction': TransactionHash,
Valid trunk transaction hash.
'branchTransaction': TransactionHash,
Valid branch transaction hash.
'duration': int,
Number of milliseconds it took to complete the request.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#gettransactionstoapprove
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().get_transactions_to_approve(
depth,
reference,
)
)
def get_trytes(self, hashes: Iterable[TransactionHash]) -> dict:
"""
Returns the raw transaction data (trytes) of one or more
transactions.
:return:
``dict`` with the following structure::
{
'trytes': List[TransactionTrytes],
List of transaction trytes for the given transaction
hashes (in the same order as the parameters).
'duration': int,
Number of milliseconds it took to complete the request.
}
.. note::
If a node doesn't have the trytes for a given transaction hash in
its ledger, the value at the index of that transaction hash is either
``null`` or a string of 9s.
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#gettrytes
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().get_trytes(
hashes,
)
)
def interrupt_attaching_to_tangle(self) -> dict:
"""
Interrupts and completely aborts the :py:meth:`attach_to_tangle`
process.
:return:
``dict`` with the following structure::
{
'duration': int,
Number of milliseconds it took to complete the request.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#interruptattachingtotangle
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().interrupt_attaching_to_tangle()
)
def remove_neighbors(self, uris: Iterable[str]) -> dict:
"""
Removes one or more neighbors from the node. Lasts until the
node is restarted.
:param str uris:
Use format ``<protocol>://<ip address>:<port>``.
Example: `remove_neighbors(['udp://example.com:14265'])`
:return:
``dict`` with the following structure::
{
'removedNeighbors': int,
Total number of removed neighbors.
'duration': int,
Number of milliseconds it took to complete the request.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#removeneighbors
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().remove_neighbors(uris)
)
def store_transactions(self, trytes: Iterable[TryteString]) -> dict:
"""
Store transactions into local storage of the node.
The input trytes for this call are provided by
:py:meth:`attach_to_tangle`.
:param TransactionTrytes trytes:
Valid transaction trytes returned by :py:meth:`attach_to_tangle`.
:return:
``dict`` with the following structure::
{
'trytes': TransactionTrytes,
Stored trytes.
'duration': int,
Number of milliseconds it took to complete the request.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#storetransactions
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().store_transactions(trytes)
)
def were_addresses_spent_from(
self,
addresses: Iterable[Address]
) -> dict:
"""
Check if a list of addresses was ever spent from, in the current
epoch, or in previous epochs.
If an address has a pending transaction, it's also considered 'spent'.
:param Iterable[Address] addresses:
List of addresses to check.
:return:
``dict`` with the following structure::
{
'states': List[bool],
States of the specified addresses in the same order as
the values in the addresses parameter. A ``True`` value
means that the address has been spent from.
'duration': int,
Number of milliseconds it took to complete the request.
}
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference#wereaddressesspentfrom
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().were_addresses_spent_from(addresses)
)
class Iota(StrictIota, AsyncIota):
"""
Implements the synchronous core API, plus additional synchronous wrapper
methods for common operations.
:param AdapterSpec adapter:
URI string or BaseAdapter instance.
:param Optional[Seed] seed:
Seed used to generate new addresses.
If not provided, a random one will be generated.
.. note::
This value is never transferred to the node/network.
:param Optional[bool] devnet:
Whether to use devnet settings for this instance.
On the devnet, minimum weight magnitude is decreased, on mainnet
it is 14 by default.
For more info on the Mainnet and the Devnet, visit
`the official docs site<https://docs.iota.org/docs/getting-started/0.1/network/iota-networks/>`.
:param Optional[bool] local_pow:
Whether to perform proof-of-work locally by redirecting all calls
to :py:meth:`attach_to_tangle` to
`ccurl pow interface <https://pypi.org/project/PyOTA-PoW/>`_.
See :ref:`README:Optional Local Pow` for more info and
:ref:`find out<pow-label>` how to use it.
References:
- https://docs.iota.org/docs/node-software/0.1/iri/references/api-reference
- https://github.com/iotaledger/wiki/blob/master/api-proposal.md
"""
def __init__(
self,
adapter: AdapterSpec,
seed: Optional[TrytesCompatible] = None,
devnet: bool = False,
local_pow: bool = False
) -> None:
"""
:param seed:
Seed used to generate new addresses.
If not provided, a random one will be generated.
.. note::
This value is never transferred to the node/network.
"""
# Explicitly call AsyncIota's init, as we need the seed
AsyncIota.__init__(self, adapter, seed, devnet, local_pow)
def broadcast_and_store(
self,
trytes: Iterable[TransactionTrytes]
) -> dict:
"""
Broadcasts and stores a set of transaction trytes.
:param Iterable[TransactionTrytes] trytes:
Transaction trytes to broadcast and store.
:return:
``dict`` with the following structure::
{
'trytes': List[TransactionTrytes],
List of TransactionTrytes that were broadcast.
Same as the input ``trytes``.
}
References:
- https://github.com/iotaledger/wiki/blob/master/api-proposal.md#broadcastandstore
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().broadcast_and_store(trytes)
)
def broadcast_bundle(
self,
tail_transaction_hash: TransactionHash
) -> dict:
"""
Re-broadcasts all transactions in a bundle given the tail transaction hash.
It might be useful when transactions did not properly propagate,
particularly in the case of large bundles.
:param TransactionHash tail_transaction_hash:
Tail transaction hash of the bundle.
:return:
``dict`` with the following structure::
{
'trytes': List[TransactionTrytes],
List of TransactionTrytes that were broadcast.
}
References:
- https://github.com/iotaledger/iota.js/blob/next/api_reference.md#module_core.broadcastBundle
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().broadcast_bundle(tail_transaction_hash)
)
def find_transaction_objects(
self,
bundles: Optional[Iterable[BundleHash]] = None,
addresses: Optional[Iterable[Address]] = None,
tags: Optional[Iterable[Tag]] = None,
approvees: Optional[Iterable[TransactionHash]] = None,
) -> dict:
"""
A more extensive version of :py:meth:`find_transactions` that
returns transaction objects instead of hashes.
Effectively, this is :py:meth:`find_transactions` +
:py:meth:`get_trytes` + converting the trytes into
transaction objects.
It accepts the same parameters as :py:meth:`find_transactions`.
Find the transactions which match the specified input.
All input values are lists, for which a list of return values
(transaction hashes), in the same order, is returned for all
individual elements. Using multiple of these input fields returns the
intersection of the values.
:param Optional[Iterable[BundleHash]] bundles:
List of bundle IDs.
:param Optional[Iterable[Address]] addresses:
List of addresses.
:param Optional[Iterable[Tag]] tags:
List of tags.
:param Optional[Iterable[TransactionHash]] approvees:
List of approvee transaction IDs.
:return:
``dict`` with the following structure::
{
'transactions': List[Transaction],
List of Transaction objects that match the input.
}
"""
# Execute original coroutine inside an event loop to make this method
# synchronous
return asyncio.get_event_loop().run_until_complete(
super().find_transaction_objects(
bundles,
addresses,
tags,
approvees,
)
)
def get_account_data(
self,
start: int = 0,
stop: Optional[int] = None,
inclusion_states: bool = False,
security_level: Optional[int] = None
) -> dict:
"""
More comprehensive version of :py:meth:`get_transfers` that
returns addresses and account balance in addition to bundles.
This function is useful in getting all the relevant information
of your account.
:param int start:
Starting key index.
:param Optional[int] stop:
Stop before this index.
Note that this parameter behaves like the ``stop`` attribute
in a :py:class:`slice` object; the stop index is *not*
included in the result.
If ``None`` (default), then this method will check every
address until it finds one that is unused.
.. note::
An unused address is an address that **has not been spent from**
and **has no transactions** referencing it on the Tangle.
A snapshot removes transactions from the Tangle. As a
consequence, after a snapshot, it may happen that this API does
not return the correct account data with ``stop`` being ``None``.
As a workaround, you can save your used addresses and their
``key_index`` attribute in a local database. Use the
``start`` and ``stop`` parameters to tell the API from where to
start checking and where to stop.
:param bool inclusion_states:
Whether to also fetch the inclusion states of the transfers.
This requires an additional API call to the node, so it is
disabled by default.
:param Optional[int] security_level:
Number of iterations to use when generating new addresses
(see :py:meth:`get_new_addresses`).
This value must be between 1 and 3, inclusive.
If not set, defaults to
:py:attr:`AddressGenerator.DEFAULT_SECURITY_LEVEL`.
:return:
``dict`` with the following structure::
{
'addresses': List[Address],
List of generated addresses.
Note that this list may include unused
addresses.
'balance': int,
Total account balance. Might be 0.