Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 585a453

Browse files
committed
PR review enhancements
1 parent c3b7153 commit 585a453

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

docs/tutorials.rst

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,6 @@ In this example, you will learn how to:
512512

513513
- **Convert Python data structures to JSON format.**
514514
- **Encrypt data and include it in a zero-value transaction.**
515-
- **Generate an address with a PyOTA specific utility tool.**
516515
- **Store the zero-value transaction with encrypted data on the Tangle.**
517516

518517
.. warning::
@@ -530,37 +529,37 @@ Code
530529
Discussion
531530
~~~~~~~~~~
532531
.. literalinclude:: ../examples/tutorials/06_store_encrypted.py
533-
:lines: 1-15
532+
:lines: 1-18
534533
:lineno-start: 1
535534

536-
The first odd thing we notice among the imports is
537-
:py:class:`~iota.crypto.addresses.AddressGenerator`. It is a PyOTA specific
538-
utility that generates addresses given your seed.
539-
540-
We will use the ``encrypt`` method to enchiper the data, and ``b64encode`` for
535+
We will use the ``encrypt`` method to encipher the data, and ``b64encode`` for
541536
representing it as ASCII characters. ``getpass`` will prompt the user for a
542537
password, and the ``json`` library is used for JSON formatting.
543538

539+
We will need an address to upload the data, therefore we need to supply the
540+
seed to the ``Iota`` API instance. The address will be generated from this
541+
seed.
542+
544543
.. literalinclude:: ../examples/tutorials/06_store_encrypted.py
545-
:lines: 17-23
546-
:lineno-start: 17
544+
:lines: 20-26
545+
:lineno-start: 20
547546

548547
The data to be stored is considered confidential information, therefore we
549548
can't just put it on the Tangle as plaintext so everyone can read it. Think of
550549
what would happen if the world's most famous secret agent's identity was leaked
551550
on the Tangle...
552551

553552
.. literalinclude:: ../examples/tutorials/06_store_encrypted.py
554-
:lines: 25-26
555-
:lineno-start: 25
553+
:lines: 28-29
554+
:lineno-start: 28
556555

557556
Notice, that ``data`` is a Python ``dict`` object. As a common way of exchanging
558557
data on the web, we would like to convert it to JSON format. The ``json.dumps()``
559558
method does exactly that, and the result is a JSON formatted plaintext.
560559

561560
.. literalinclude:: ../examples/tutorials/06_store_encrypted.py
562-
:lines: 28-37
563-
:lineno-start: 28
561+
:lines: 31-40
562+
:lineno-start: 31
564563

565564
Next, we will encrypt this data with a secret password we obtain from the user.
566565

@@ -577,20 +576,18 @@ Therefore, we first encode our binary data into ASCII characters with `Base64`_
577576
encoding.
578577

579578
.. literalinclude:: ../examples/tutorials/06_store_encrypted.py
580-
:lines: 39-57
581-
:lineno-start: 39
579+
:lines: 42-58
580+
:lineno-start: 42
582581

583582
Now, we are ready to construct the transfer. We convert the encrypted `Base64`_
584583
encoded data to trytes and assign it to the :py:class:`ProposedTransaction`
585-
object's message argument.
584+
object's ``message`` argument.
586585

587586
An address is also needed, so we generate one with the help of
588-
:py:class:`~iota.crypto.addresses.AddressGenerator` and its
589-
:py:meth:`~iota.crypto.addresses.AddressGenerator.get_addresses` method. Feel
590-
free to chose the index of the generated address, and don't forget, that the
591-
method returns a list of addresses, even if it contains only one. Put your seed
592-
(from `4.a Generate Address`_) onto line 44 to generate an address that belongs
593-
to you. For more detailed explanation on how addresses are generated in PyOTA,
587+
:py:meth:`~Iota.get_new_addresses` extended API method. Feel free to choose the
588+
index of the generated address, and don't forget, that the method returns a
589+
``dict`` with a list of addresses, even if it contains only one.
590+
For more detailed explanation on how addresses are generated in PyOTA,
594591
refer to the :ref:`Generating Addresses` page.
595592

596593
We also attach a custom :py:class:`Tag` to our :py:class:`ProposedTransaction`.
@@ -599,8 +596,8 @@ of a transaction, the library would split it accross more transactions that
599596
together form the transfer bundle.
600597

601598
.. literalinclude:: ../examples/tutorials/06_store_encrypted.py
602-
:lines: 59-65
603-
:lineno-start: 59
599+
:lines: 60-66
600+
:lineno-start: 60
604601

605602
Finally, we use :py:meth:`Iota.send_transfer` to prepare the transfer and
606603
send it to the network.
@@ -611,8 +608,8 @@ The tail transaction (a tail transaction is the one with index 0 in the bundle)
611608
hash is printed on the console, because you will need it in the next tutorial,
612609
and anyway, it is a good practice to keep a reference to your transfers.
613610

614-
In the next example, we will try to decode the confidential information from the
615-
Tangle.
611+
In the next example, we will try to decode the confidential information from
612+
the Tangle.
616613

617614
7. Fetch Encrypted Data
618615
-----------------------
@@ -658,7 +655,7 @@ To fetch transactions or bundles from the Tangle, a reference is required to
658655
retreive them from the network. Transactions are identified by their
659656
transaction hash, while a group of transaction (a bundle) by bundle hash.
660657
Hashes ensure the integrity of the Tangle, since they contain verifiable
661-
information on the content of the transfer objects.
658+
information about the content of the transfer objects.
662659

663660
``input()`` asks the user to give the tail transaction hash of the bundle
664661
that holds the encrypted messages. The tail transaction is the first in the
@@ -680,7 +677,7 @@ To simplify the code, several operations are happening on line 21:
680677

681678
- Calling :py:meth:`~Iota.get_bundles` that returns a ``dict``,
682679
- accessing the ``'bundles'`` key in the ``dict``,
683-
- and taking the first element of the the list if bundles in the value
680+
- and taking the first element of the the list of bundles in the value
684681
associated with the key.
685682

686683
.. literalinclude:: ../examples/tutorials/07_fetch_encrypted.py
@@ -694,13 +691,14 @@ the ``signature_message_fragment`` fields of the transactions, decoded from
694691
trytes into unicode characters.
695692

696693
We then combine these message chunks into one stream of characters by using
697-
``sting.join()``.
694+
``string.join()``.
698695

699696
We know that at this stage that we can't make sense of our message, because it
700697
is encrypted and encoded into `Base64`_. Let's peel that onion layer by layer:
701698

702699
- On line 28, we decode the message into bytes with ``b64decode``.
703-
- On line 31, we ask the user for a decryption password.
700+
- On line 31, we ask the user for thr decryption password (from the previous
701+
tutorial).
704702
- On line 36, we decrypt the bytes cipher with the password and decode the
705703
result into a unicode string.
706704
- Since we used JSON formatting in the previous tutorial, there is one

examples/tutorials/06_store_encrypted.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
simplecrypt library is needed for this example (`pip install simple-crypt`)!
55
"""
66
from iota import Iota, TryteString, Tag, ProposedTransaction
7-
from iota.crypto.addresses import AddressGenerator
87
from simplecrypt import encrypt
98
from base64 import b64encode
109
from getpass import getpass
1110

1211
import json
1312

1413
# Declare an API object
15-
api = Iota('https://nodes.devnet.iota.org:443', testnet=True)
14+
api = Iota(
15+
adapter='https://nodes.devnet.iota.org:443',
16+
seed=b'YOURSEEDFROMTHEPREVIOUSTUTORIAL',
17+
testnet=True,
18+
)
1619

1720
# Some confidential information
1821
data = {
@@ -41,9 +44,7 @@
4144
trytes_encrypted_data = TryteString.from_bytes(b64_cipher)
4245

4346
# Generate an address from your seed to post the transfer to
44-
my_address = AddressGenerator(b'YOURSEEDFROMTHEPREVIOUSTUTORIAL').get_addresses(
45-
start=42,
46-
)[0]
47+
my_address = api.get_new_addresses(index=42)['addresses'][0]
4748

4849
# Tag is optional here
4950
my_tag = Tag(b'CONFIDENTIALINFORMATION')

0 commit comments

Comments
 (0)