|
| 1 | +================ |
| 2 | +PyOTA Data Types |
| 3 | +================ |
| 4 | +.. important:: |
| 5 | + |
| 6 | + Before diving into the API, it's important to understand the fundamental data |
| 7 | + types of IOTA. |
| 8 | + |
| 9 | + For an introduction to the IOTA protocol and the Tangle, give the |
| 10 | + `protocol documentation`_ a once-over. |
| 11 | + |
| 12 | +PyOTA defines a few types that will make it easy for you to model objects like |
| 13 | +Transactions and Bundles in your own code. |
| 14 | + |
| 15 | +TryteString |
| 16 | +----------- |
| 17 | +A :py:class:`TryteString` is an ASCII representation of a sequence of trytes. |
| 18 | +In many respects, it is similar to a Python ``bytes`` object (which is an ASCII |
| 19 | +representation of a sequence of bytes). |
| 20 | + |
| 21 | +In fact, the two objects behave very similarly; they support concatenation, |
| 22 | +comparison, can be used as dict keys, etc. |
| 23 | + |
| 24 | +However, unlike ``bytes``, a :py:class:`TryteString` can only contain uppercase |
| 25 | +letters and the number 9 (as a regular expression: ``^[A-Z9]*$``). |
| 26 | + |
| 27 | +.. admonition:: Why only these characters? |
| 28 | + |
| 29 | + You can find the answer on the |
| 30 | + `IOTA Forum <https://forum.iota.org/t/1860/10>`__. |
| 31 | + |
| 32 | +As you go through the API documentation, you will see many references to |
| 33 | +:py:class:`TryteString` and its subclasses: |
| 34 | + |
| 35 | +- :py:class:`Fragment` |
| 36 | + |
| 37 | + A signature or message fragment inside a transaction. |
| 38 | + Fragments are always 2187 trytes long. |
| 39 | + |
| 40 | +- :py:class:`Hash` |
| 41 | + |
| 42 | + An object identifier. Hashes are always 81 trytes long. |
| 43 | + |
| 44 | + There are many different types of hashes: |
| 45 | + |
| 46 | + :py:class:`Address` |
| 47 | + Identifies an address on the Tangle. |
| 48 | + :py:class:`BundleHash` |
| 49 | + Identifies a bundle on the Tangle. |
| 50 | + :py:class:`TransactionHash` |
| 51 | + Identifies a transaction on the Tangle. |
| 52 | + |
| 53 | +- :py:class:`Seed` |
| 54 | + |
| 55 | + A TryteString that is used for crypto functions such as generating addresses, |
| 56 | + signing inputs, etc. |
| 57 | + |
| 58 | + .. important:: |
| 59 | + |
| 60 | + Seeds can be any length, but 81 trytes offers the best security. |
| 61 | + More information is available on the |
| 62 | + `IOTA Forum <https://forum.iota.org/t/1278>`__. |
| 63 | + |
| 64 | +- :py:class:`Tag` |
| 65 | + |
| 66 | + A tag used to classify a transaction. Tags are always 27 trytes long. |
| 67 | + |
| 68 | +- :py:class:`TransactionTrytes` |
| 69 | + |
| 70 | + A TryteString representation of a transaction on the Tangle. |
| 71 | + :py:class:`TransactionTrytes` are always 2673 trytes long. |
| 72 | + |
| 73 | +Creating TryteStrings |
| 74 | +~~~~~~~~~~~~~~~~~~~~~ |
| 75 | +To create a new :py:class:`TryteString` from a sequence of trytes, simply |
| 76 | +wrap the trytes inside the :py:class:`TryteString` initializer: |
| 77 | + |
| 78 | +.. code-block:: python |
| 79 | +
|
| 80 | + from iota import TryteString |
| 81 | +
|
| 82 | + trytes = TryteString('RBTC9D9DCDQAEASBYBCCKBFA') |
| 83 | +
|
| 84 | +To encode ASCII text into trytes, use the :py:meth:`TryteString.from_string` |
| 85 | +method: |
| 86 | + |
| 87 | +.. code-block:: python |
| 88 | +
|
| 89 | + from iota import TryteString |
| 90 | +
|
| 91 | + message_trytes = TryteString.from_string('Hello, IOTA!') |
| 92 | +
|
| 93 | + print(message_trytes) # RBTC9D9DCDQAEASBYBCCKBFA |
| 94 | +
|
| 95 | +To decode a sequence of trytes back into ASCII text, use |
| 96 | +:py:meth:`TryteString.as_string`: |
| 97 | + |
| 98 | +.. code-block:: python |
| 99 | +
|
| 100 | + from iota import TryteString |
| 101 | +
|
| 102 | + message_trytes = TryteString('RBTC9D9DCDQAEASBYBCCKBFA') |
| 103 | +
|
| 104 | + message_str = message_trytes.as_string() |
| 105 | +
|
| 106 | + print(message_str) # Hello, IOTA! |
| 107 | +
|
| 108 | +.. note:: |
| 109 | + |
| 110 | + PyOTA also supports encoding non-ASCII characters, but this functionality is |
| 111 | + **experimental** and has not yet been standardized. |
| 112 | + |
| 113 | + If you encode non-ASCII characters, be aware that other IOTA libraries |
| 114 | + (possibly including future versions of PyOTA!) might not be able to decode |
| 115 | + them! |
| 116 | + |
| 117 | +Transaction Types |
| 118 | +----------------- |
| 119 | +PyOTA defines two different types used to represent transactions: |
| 120 | + |
| 121 | +:py:class:`Transaction` |
| 122 | + A transaction that has been loaded from the Tangle. |
| 123 | + |
| 124 | +:py:class:`ProposedTransaction` |
| 125 | + A transaction that was created locally and hasn't been broadcast to the |
| 126 | + Tangle yet. |
| 127 | + |
| 128 | +Transaction |
| 129 | +~~~~~~~~~~~ |
| 130 | +Generally, you will never need to create `Transaction` objects; the API will |
| 131 | +build them for you, as the result of various API methods. |
| 132 | + |
| 133 | +.. tip:: |
| 134 | + |
| 135 | + If you have a TryteString representation of a transaction, and you'd like to |
| 136 | + convert it into a :py:class:`Transaction` object, use the |
| 137 | + :py:meth:`Transaction.from_tryte_string` method: |
| 138 | + |
| 139 | + .. code-block:: python |
| 140 | +
|
| 141 | + from iota import Transaction |
| 142 | +
|
| 143 | + txn_1 =\ |
| 144 | + Transaction.from_tryte_string( |
| 145 | + 'GYPRVHBEZOOFXSHQBLCYW9ICTCISLHDBNMMVYD9JJHQMPQCTIQ...', |
| 146 | + ) |
| 147 | +
|
| 148 | + This is equivalent to the `Paste Trytes`_ feature from the IOTA Wallet. |
| 149 | + |
| 150 | +Each :py:class:`Transaction` object has the following attributes: |
| 151 | + |
| 152 | +- ``address`` (:py:class:`Address`) |
| 153 | + |
| 154 | + The address associated with this transaction. Depending on the transaction's |
| 155 | + ``value``, this address may be a sender or a recipient. |
| 156 | + |
| 157 | +- ``attachment_timestamp`` (:py:class:`int`) |
| 158 | + Timestamp after completing the Proof of Work process. |
| 159 | + |
| 160 | + See the `timestamps white paper`_ for more information. |
| 161 | + |
| 162 | +- ``attachment_timestamp_lower_bound`` (:py:class:`int`) |
| 163 | + Lower bound of the timestamp. |
| 164 | + |
| 165 | + See the `timestamps white paper`_ for more information. |
| 166 | + |
| 167 | +- ``attachment_timestamp_upper_bound`` (:py:class:`int`) |
| 168 | + Upper bound of the timestamp. |
| 169 | + |
| 170 | + See the `timestamps white paper`_ for more information. |
| 171 | + |
| 172 | +- ``branch_transaction_hash`` (:py:class:`TransactionHash`) |
| 173 | + |
| 174 | + An unrelated transaction that this transaction "approves". |
| 175 | + Refer to the `protocol documentation`_ for more information. |
| 176 | + |
| 177 | +- ``bundle_hash`` (:py:class:`BundleHash`) |
| 178 | + |
| 179 | + The bundle hash, used to identify transactions that are part of the same |
| 180 | + bundle. This value is generated by taking a hash of the metadata from all |
| 181 | + transactions in the bundle. |
| 182 | + |
| 183 | +- ``current_index`` (:py:class:`int`) |
| 184 | + |
| 185 | + The transaction's position in the bundle. |
| 186 | + |
| 187 | + - If the ``current_index`` value is 0, then this is the "tail transaction". |
| 188 | + - If it is equal to ``last_index``, then this is the "head transaction". |
| 189 | + |
| 190 | +- ``hash`` (:py:class:`TransactionHash`) |
| 191 | + |
| 192 | + The transaction hash, used to uniquely identify the transaction on the |
| 193 | + Tangle. This value is generated by taking a hash of the raw transaction |
| 194 | + trytes. |
| 195 | + |
| 196 | +- ``last_index`` (:py:class:`int`) |
| 197 | + |
| 198 | + The index of the final transaction in the bundle. This value is attached to |
| 199 | + every transaction to make it easier to traverse and verify bundles. |
| 200 | + |
| 201 | +- ``nonce`` (:py:class:`Nonce`) |
| 202 | + |
| 203 | + This is the product of the PoW process. |
| 204 | + |
| 205 | + Refer to the `protocol documentation`_ for more information. |
| 206 | + |
| 207 | +- ``signature_message_fragment`` (:py:class:`Fragment`) |
| 208 | + |
| 209 | + Additional data attached to the transaction: |
| 210 | + |
| 211 | + - If ``value < 0``, this value contains a fragment of the cryptographic |
| 212 | + signature authorizing the spending of the IOTAs. |
| 213 | + - If ``value > 0``, this value is an (optional) string message attached to |
| 214 | + the transaction. |
| 215 | + - If ``value = 0``, this value could be either a signature or message |
| 216 | + fragment, depending on the previous transaction. |
| 217 | + |
| 218 | + .. tip:: |
| 219 | + |
| 220 | + Read this as "Signature/Message Fragment". That is, it could be a |
| 221 | + fragment of a signature **or** a message, depending on the transaction. |
| 222 | + |
| 223 | +- ``tag`` (:py:class:`Tag`) |
| 224 | + |
| 225 | + Used to classify the transaction. |
| 226 | + |
| 227 | + Every transaction has a tag, but many transactions have empty tags. |
| 228 | + |
| 229 | +- ``timestamp`` (:py:class:`int`) |
| 230 | + |
| 231 | + Unix timestamp when the transaction was created. |
| 232 | + |
| 233 | + Note that devices can specify any timestamp when creating transactions, so |
| 234 | + this value is not safe to use by itself for security measures (such as |
| 235 | + resolving double-spends). |
| 236 | + |
| 237 | + .. note:: |
| 238 | + |
| 239 | + The IOTA protocol does support verifiable timestamps. Refer to the |
| 240 | + `timestamps white paper`_ for more information. |
| 241 | + |
| 242 | +- ``trunk_transaction_hash`` (:py:class:`TransactionHash`) |
| 243 | + |
| 244 | + The transaction hash of the next transaction in the bundle. |
| 245 | + |
| 246 | + If this transaction is the head transaction, its ``trunk_transaction_hash`` |
| 247 | + will be pseudo-randomly selected, similarly to ``branch_transaction_hash``. |
| 248 | + |
| 249 | +- ``value`` (:py:class:`int`) |
| 250 | + |
| 251 | + The number of IOTAs being transferred in this transaction: |
| 252 | + |
| 253 | + - If this value is negative, then the ``address`` is spending IOTAs. |
| 254 | + - If it is positive, then the ``address`` is receiving IOTAs. |
| 255 | + - If it is zero, then this transaction is being used to carry metadata (such |
| 256 | + as a signature fragment or a message) instead of transferring IOTAs. |
| 257 | + |
| 258 | + |
| 259 | +:todo: ProposedTransaction |
| 260 | + |
| 261 | + |
| 262 | +.. _protocol documentation: https://iota.readme.io/docs/ |
| 263 | +.. _paste trytes: https://forum.iota.org/t/3457/3 |
| 264 | +.. _timestamps white paper: https://iota.org/timestamps.pdf |
0 commit comments