def as_tryte_string(self): msgfrag = self.signature_message_fragment seed = TryteString(str(self.bundle.node.seed)) value = TryteString.from_trits(trits_from_int(self.value, pad=81)) legacytag = self.legacy_tag timestamp = TryteString.from_trits( trits_from_int(self.timestamp, pad=27)) curridx = TryteString.from_trits( trits_from_int(self.current_index, pad=27)) lastidx = TryteString.from_trits( trits_from_int(self.last_index, pad=27)) bundlehash = self.bundle.bundle_hash branchtrxnhash = TryteString(self.trunk_transaction_hash) trunkhash = TryteString(self.branch_transaction_hash) tag = self.tag attachtime = TryteString.from_trits( trits_from_int(self.attachment_timestamp, pad=27)) timelower = TryteString.from_trits( trits_from_int(self.attachment_timestamp_lower_bound, pad=27)) timeupper = TryteString.from_trits( trits_from_int(self.attachment_timestamp_upper_bound, pad=27)) nonce = TryteString.from_trits(trits_from_int(self.nonce, pad=81)) sum = nonce + timeupper + timelower + tag + trunkhash + branchtrxnhash + bundlehash + lastidx + timestamp \ +legacytag + value + seed + msgfrag + curridx + attachtime return TransactionTrytes(sum).__str__()
def timestamp_as_trytes(self) -> TryteString: """ Returns a TryteString representation of the transaction's :py:attr:`timestamp`. """ # Note that we are padding to 27 *trits*. return TryteString.from_trits(trits_from_int(self.timestamp, pad=27))
def last_index_as_trytes(self) -> TryteString: """ Returns a TryteString representation of the transaction's :py:attr:`last_index`. """ # Note that we are padding to 27 *trits*. return TryteString.from_trits(trits_from_int(self.last_index, pad=27))
def value_as_trytes(self): # type: () -> TryteString """ Returns a TryteString representation of the transaction's value. """ # Note that we are padding to 81 _trits_. return TryteString.from_trits(trits_from_int(self.value, pad=81))
def value_as_trytes(self) -> TryteString: """ Returns a TryteString representation of the transaction's :py:attr:`value`. """ # Note that we are padding to 81 *trits*. return TryteString.from_trits(trits_from_int(self.value, pad=81))
def as_trytes(self) -> List[List[int]]: """ Converts the TryteString into a sequence of trytes. Each tryte is represented as a list with 3 trit values. See :py:meth:`as_trits` for more info. .. important:: :py:class:`TryteString` is not a numeric type, so the result of this method should not be interpreted as an integer! :return: ``List[List[int]]`` Example usage:: from iota import TryteString trytes = TryteString(b'RBTC9D9DCDQAEASBYBCCKBFA') tryte_list = trytes.as_trytes() """ return [ trits_from_int(n, pad=3) for n in self.as_integers() ]
def attachment_timestamp_upper_bound_as_trytes(self): # type: () -> TryteString """ Returns a TryteString representation of the transaction's attachment timestamp upper bound. """ #Note that we are padding to 27 _trits_. return TryteString.from_trits(trits_from_int(self.attachment_timestamp_upper_bound, pad=27))
def last_index_as_trytes(self): # type: () -> TryteString """ Returns a TryteString representation of the transaction's ``last_index`` value. """ # Note that we are padding to 27 _trits_. return TryteString.from_trits(trits_from_int(self.last_index, pad=27))
def timestamp_as_trytes(self): # type: () -> TryteString """ Returns a TryteString representation of the transaction's timestamp. """ # Note that we are padding to 27 _trits_. return TryteString.from_trits(trits_from_int(self.timestamp, pad=27))
def attachment_timestamp_upper_bound_as_trytes(self) -> TryteString: """ Returns a TryteString representation of the transaction's :py:attr:`attachment_timestamp_upper_bound`. """ # Note that we are padding to 27 *trits*. return TryteString.from_trits( trits_from_int(self.attachment_timestamp_upper_bound, pad=27), )
def get_bundle_essence_trits(self): # Format trxn features as trytes (same as python IOTA API) addr = self.bundle.node.seed value = TryteString.from_trits(trits_from_int( self.value, pad=81)) # Pad to 81 trits (27 trytes) tstamp = TryteString.from_trits(trits_from_int( self.timestamp, pad=27)) # Pad to 27 trits (9 trytes) currIdx = TryteString.from_trits( trits_from_int(self.current_index, pad=27)) # Pad to 27 trits (9 trytes) lastIdx = TryteString.from_trits( trits_from_int(self.last_index, pad=27)) # Pad to 27 trits (9 trytes) legacyTag = self.legacy_tag sum = addr + value + tstamp + currIdx + lastIdx + legacyTag return sum.as_trits()
def current_index_as_trytes(self): # type: () -> TryteString """ Returns a TryteString representation of the transaction's :py:attr:`current_index`. """ # Note that we are padding to 27 *trits*. return TryteString.from_trits( trits_from_int(self.current_index, pad=27), )
def as_trytes(self): # type: () -> List[List[int]] """ Converts the TryteString into a sequence of trytes. Each tryte is represented as a list with 3 trit values. See :py:meth:`as_trits` for more info. IMPORTANT: TryteString is not a numeric type, so the result of this method should not be interpreted as an integer! """ return [trits_from_int(n, pad=3) for n in self.as_integers()]
def as_trytes(self): # type: () -> List[List[int]] """ Converts the TryteString into a sequence of trytes. Each tryte is represented as a list with 3 trit values. See :py:meth:`as_trits` for more info. IMPORTANT: TryteString is not a numeric type, so the result of this method should not be interpreted as an integer! """ return [ trits_from_int(n, pad=3) for n in self.as_integers() ]
def send_transaction(node_url, address, tag, messages, values): propose_bundle = ProposedBundle() # Setting output transaction ... txn_output = ProposedTransaction( address = Address(address), value = values, tag = Tag(tag), message = TryteString.from_string(messages) ) propose_bundle.add_transaction(txn_output) propose_bundle.finalize() trytes = propose_bundle.as_tryte_strings() api = Iota(node_url) # Tips trunk_hash, branch_hash = gtta(node_url) for tx_tryte in trytes: # Attachment timestamp insert timestamp = TryteString.from_trits( trits_from_int(int(time.time() * 1000), pad=27)) tx_tryte = insert_to_trytes(2619, 2628, str(timestamp), tx_tryte) # timestamp_lower_bound = MIN_VALUE # timestamp_upper_bound = MAX_VALUE tx_tryte = insert_to_trytes(2637, 2646, str("MMMMMMMMM"), tx_tryte) # Tips insert - trunk tx_tryte = insert_to_trytes(2430, 2511, str(trunk_hash), tx_tryte) # Tips insert - branch tx_tryte = insert_to_trytes(2511, 2592, str(branch_hash), tx_tryte) # Do PoW for this transaction tx_tryte = attach_to_tangle(node_url, trunk_hash, branch_hash, tx_tryte) # Prepare to store and broadcast ... try: api.broadcast_and_store(tx_tryte) except Exception as e: return str("Error: %s" % (str(e))) return str(propose_bundle.hash)
def _create_sponge(self, index): # type: (int) -> Kerl """ Prepares the hash sponge for the generator. """ seed = self.seed_as_trits[:] sponge = Kerl() sponge.absorb(add_trits(seed, trits_from_int(index))) # Squeeze all of the trits out of the sponge and re-absorb them. # Note that the sponge transforms several times per operation, so # this sequence is not as redundant as it looks at first glance. sponge.squeeze(seed) sponge.reset() sponge.absorb(seed) return sponge
def _create_sponge(self, index: int) -> Kerl: """ Prepares the hash sponge for the generator. """ seed = self.seed_as_trits[:] sponge = Kerl() sponge.absorb(add_trits(seed, trits_from_int(index))) # Squeeze all of the trits out of the sponge and re-absorb them. # Note that the sponge transforms several times per operation, # so this sequence is not as redundant as it looks at first # glance. sponge.squeeze(seed) sponge.reset() sponge.absorb(seed) return sponge
def send_transfer(tag, messages, address, values, dict_tips, debug=0): # Initialize PoW Library PoWlib = PoW_load_library(DCURL_PATH) PoW_interface_init(PoWlib) # Set output transaction print("Start to transfer ... ") time_start_send = time.time() propose_bundle = iota.ProposedBundle() print("Setting output transaction ...") txn_output = iota.ProposedTransaction( address=iota.Address(address), value=values, tag=iota.Tag(tag), message=TryteString.from_unicode(messages) ) propose_bundle.add_transaction(txn_output) # Get input address if int(values) > 0: print("DEBUG values = %s" % (str(values))) print("Checking input balance ...") dict_inputs = api.get_inputs() if int(dict_inputs['totalBalance']) < int(values): print("Balance not enough") return 0 # Setting intput transaction if int(values) > 0: print("Setting input transaction ...") value_input = 0 index_input = 0 while (int(value_input) < int(values)): addy = iota.Address(dict_inputs['inputs'][index_input]) addy.balance = dict_inputs['inputs'][index_input].balance addy.key_index = dict_inputs['inputs'][index_input].key_index addy.security_level = TXN_SECURITY_LEVEL propose_bundle.add_inputs([addy]) value_input = value_input + int(dict_inputs['inputs'][0].balance) # Send unspent inputs to print("Setting unspent input to a new address ...") unspent = iota.Address(generate_address()['addresses'][0]) propose_bundle.send_unspent_inputs_to(unspent) # This will get the bundle hash print("Bundle finalize ...") time_start_bundle_finz = time.time() propose_bundle.finalize() time_end_bundle_finz = time.time() elapsed_bundle_finz = time_end_bundle_finz - time_start_bundle_finz # Signing # If the transaction need sign, it will then sign-up the transaction # to fill up signature fragements if int(values) > 0: print("Signing...") propose_bundle.sign_inputs(iota.crypto.signing.KeyGenerator(SEED)) trytes = propose_bundle.as_tryte_strings() # Get tips by getTransactionsToApprove trunk_hash = dict_tips['trunkTransaction'] branch_hash = dict_tips['branchTransaction'] # Do PoW (attach to tangle) elapsed_pow = 0 time_start_pow = time.time() for tx_tryte in trytes: # Attachment timestamp insert timestamp = TryteString.from_trits( trits_from_int(int(time.time() * 1000), pad=27)) tx_tryte = insert_to_trytes(2619, 2628, str(timestamp), tx_tryte) # timestamp_lower_bound = MIN_VALUE # timestamp_upper_bound = MAX_VALUE tx_tryte = insert_to_trytes(2637, 2646, str("MMMMMMMMM"), tx_tryte) # Tips insert - trunk tx_tryte = insert_to_trytes(2430, 2511, str(trunk_hash), tx_tryte) # Tips insert - branch tx_tryte = insert_to_trytes(2511, 2592, str(branch_hash), tx_tryte) # Do PoW for this transaction print("Do POW for this transaction ...") nonce = PoW_interface_search(PoWlib, tx_tryte, MWM) tx_tryte = insert_to_trytes(2646, 2673, str(nonce), tx_tryte) time_end_pow = time.time() elapsed_pow = elapsed_pow + (time_end_pow - time_start_pow) # Update previous tx hash for next transaction trunk_hash = Transaction.from_tryte_string(tx_tryte[0:2673]).hash print("Prepare to store and broadcast ...") try: api.broadcast_and_store([tx_tryte[0:2673]]) except Exception as e: print("Error: %s" % (str(e.context))) time_end_send = time.time() elapsed_send = time_end_send - time_start_send if debug == 1: data = [{'platform': 'pi3', 'total_time': str(elapsed_send), 'elapsed_pow': str( elapsed_pow), 'elqpsed_bundle_finished': str(elapsed_bundle_finz)}] json_data = json.dumps(data) print(json_data) # attach_debug_message_to_tangle(json_data) obj_txn = api.find_transactions(bundles=[propose_bundle.hash]) return str(obj_txn['hashes'][0])
def new_seed(old_seed_trytes, used_addresses_int): old_seed_int = int_from_trits(TryteString(old_seed_trytes).as_trits()) old_seed_int += used_addresses_int return TryteString.from_trits(trits_from_int(old_seed_int))