|
| 1 | +Generating Addresses |
| 2 | +==================== |
| 3 | + |
| 4 | +In IOTA, addresses are generated deterministically from seeds. This |
| 5 | +ensures that your account can be accessed from any location, as long as |
| 6 | +you have the seed. |
| 7 | + |
| 8 | +Note that this also means that anyone with access to your seed can spend |
| 9 | +your IOTAs! Treat your seed(s) the same as you would the password for |
| 10 | +any other financial service. |
| 11 | + |
| 12 | +.. raw:: html |
| 13 | + |
| 14 | + <aside class="notice"> |
| 15 | + |
| 16 | +PyOTA's crytpo functionality is currently very slow; on average it takes |
| 17 | +8-10 seconds to generate each address. |
| 18 | + |
| 19 | +.. raw:: html |
| 20 | + |
| 21 | + <p> |
| 22 | + |
| 23 | +:: |
| 24 | + |
| 25 | + These performance issues will be fixed in a future version of the library; |
| 26 | + please bear with us! |
| 27 | + |
| 28 | +.. raw:: html |
| 29 | + |
| 30 | + </p> |
| 31 | + |
| 32 | +.. raw:: html |
| 33 | + |
| 34 | + <p> |
| 35 | + |
| 36 | +:: |
| 37 | + |
| 38 | + In the meantime, if you are using Python 3, you can install a C extension |
| 39 | + that boosts PyOTA's performance significantly (speedups of 60x are common!). |
| 40 | + |
| 41 | +.. raw:: html |
| 42 | + |
| 43 | + </p> |
| 44 | + |
| 45 | +.. raw:: html |
| 46 | + |
| 47 | + <p> |
| 48 | + |
| 49 | +:: |
| 50 | + |
| 51 | + To install the extension, run <code>pip install pyota[ccurl]</code>. |
| 52 | + |
| 53 | +.. raw:: html |
| 54 | + |
| 55 | + </p> |
| 56 | + |
| 57 | +.. raw:: html |
| 58 | + |
| 59 | + <p> |
| 60 | + |
| 61 | +:: |
| 62 | + |
| 63 | + <strong>Important:</strong> The extension is not yet compatible with Python |
| 64 | + 2. |
| 65 | + |
| 66 | +.. raw:: html |
| 67 | + |
| 68 | + </p> |
| 69 | + |
| 70 | +.. raw:: html |
| 71 | + |
| 72 | + <p> |
| 73 | + |
| 74 | +:: |
| 75 | + |
| 76 | + If you are familiar with Python 2's C API, we'd love to hear from you! |
| 77 | + Check the |
| 78 | + <a href="https://github.com/todofixthis/pyota-ccurl/issues/4">GitHub issue</a> |
| 79 | + for more information. |
| 80 | + |
| 81 | +.. raw:: html |
| 82 | + |
| 83 | + </p> |
| 84 | + |
| 85 | +.. raw:: html |
| 86 | + |
| 87 | + </aside> |
| 88 | + |
| 89 | +PyOTA provides two methods for generating addresses: |
| 90 | + |
| 91 | +Using the API |
| 92 | +------------- |
| 93 | + |
| 94 | +.. code:: python |
| 95 | +
|
| 96 | + from iota import Iota |
| 97 | +
|
| 98 | + api = Iota('http://localhost:14265', b'SEED9GOES9HERE') |
| 99 | +
|
| 100 | + # Generate 5 addresses, starting with index 0. |
| 101 | + gna_result = api.get_new_addresses(count=5) |
| 102 | + addresses = gna_result['addresses'] |
| 103 | +
|
| 104 | + # Generate 1 address, starting with index 42: |
| 105 | + gna_result = api.get_new_addresses(start=42) |
| 106 | + addresses = gna_result['addresses'] |
| 107 | +
|
| 108 | + # Find the first unused address, starting with index 86: |
| 109 | + gna_result = api.get_new_addresses(start=86, count=None) |
| 110 | + addresses = gna_result['addresses'] |
| 111 | +
|
| 112 | +To generate addresses using the API, invoke its ``get_new_addresses`` |
| 113 | +method, using the following parameters: |
| 114 | + |
| 115 | +- ``start: int``: The starting index (defaults to 0). This can be used |
| 116 | + to skip over addresses that have already been generated. |
| 117 | +- ``count: Optional[int]``: The number of addresses to generate |
| 118 | + (defaults to 1). |
| 119 | +- If ``None``, the API will generate addresses until it finds one that |
| 120 | + has not been used (has no transactions associated with it on the |
| 121 | + Tangle). It will then return the unused address and discard the rest. |
| 122 | +- ``security_level: int``: Determines the security level of the |
| 123 | + generated addresses. See `Security Levels <#security-levels>`__ |
| 124 | + below. |
| 125 | + |
| 126 | +``get_new_addresses`` returns a dict with the following items: |
| 127 | + |
| 128 | +- ``addresses: List[Address]``: The generated address(es). Note that |
| 129 | + this value is always a list, even if only one address was generated. |
| 130 | + |
| 131 | +Using AddressGenerator |
| 132 | +---------------------- |
| 133 | + |
| 134 | +.. code:: python |
| 135 | +
|
| 136 | + from iota.crypto.addresses import AddressGenerator |
| 137 | +
|
| 138 | + generator = AddressGenerator(b'SEED9GOES9HERE') |
| 139 | +
|
| 140 | + # Generate a list of addresses: |
| 141 | + addresses = generator.get_addresses(start=0, count=5) |
| 142 | +
|
| 143 | + # Generate a list of addresses in reverse order: |
| 144 | + addresses = generator.get_addresses(start=42, count=10, step=-1) |
| 145 | +
|
| 146 | + # Create an iterator, advancing 5 indices each iteration. |
| 147 | + iterator = generator.create_iterator(start=86, step=5) |
| 148 | + for address in iterator: |
| 149 | + ... |
| 150 | +
|
| 151 | +If you want more control over how addresses are generated, you can use |
| 152 | +the ``AddressGenerator`` class. |
| 153 | + |
| 154 | +``AddressGenerator`` can create iterators, allowing your application to |
| 155 | +generate addresses as needed, instead of having to generate lots of |
| 156 | +addresses up front. |
| 157 | + |
| 158 | +You can also specify an optional ``step`` parameter, which allows you to |
| 159 | +skip over multiple addresses between iterations... or even iterate over |
| 160 | +addresses in reverse order! |
| 161 | + |
| 162 | +``AddressGenerator`` provides two methods: |
| 163 | + |
| 164 | +- ``get_addresses: (int, int, int) -> List[Address]``: Returns a list |
| 165 | + of addresses. This is the same method that the ``get_new_addresses`` |
| 166 | + API command uses internally. |
| 167 | +- ``create_iterator: (int, int) -> Generator[Address]``: Returns an |
| 168 | + iterator that will create addresses endlessly. Use this if you have a |
| 169 | + feature that needs to generate addresses "on demand". |
| 170 | + |
| 171 | +Security Levels |
| 172 | +=============== |
| 173 | + |
| 174 | +.. code:: python |
| 175 | +
|
| 176 | + gna_result = api.get_new_addresses(security_level=3) |
| 177 | +
|
| 178 | + generator =\ |
| 179 | + AddressGenerator( |
| 180 | + seed = b'SEED9GOES9HERE', |
| 181 | + security_level = 3, |
| 182 | + ) |
| 183 | +
|
| 184 | +If desired, you may change the number of iterations that |
| 185 | +``AddressGenerator`` uses internally when generating new addresses, by |
| 186 | +specifying a different ``security_level`` when creating a new instance. |
| 187 | + |
| 188 | +``security_level`` should be between 1 and 3, inclusive. Values outside |
| 189 | +this range are not supported by the IOTA protocol. |
| 190 | + |
| 191 | +Use the following guide when deciding which security level to use: |
| 192 | + |
| 193 | +- ``security_level=1``: Least secure, but generates addresses the |
| 194 | + fastest. |
| 195 | +- ``security_level=2``: Default; good compromise between speed and |
| 196 | + security. |
| 197 | +- ``security_level=3``: Most secure; results in longer signatures in |
| 198 | + transactions. |
0 commit comments