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 test_wireup(self): """ Verify that the command is wired up correctly. The API method indeed calls the appropiate command. """ with patch( 'iota.commands.extended.broadcast_and_store.BroadcastAndStoreCommand.__call__', MagicMock(return_value='You found me!')) as mocked_command: api = Iota(self.adapter) # Don't need to call with proper args here. response = api.broadcast_and_store('trytes') self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
]).get('trytes') gtta_response = api.get_transactions_to_approve(3) trunk = gtta_response.get('trunkTransaction') branch = gtta_response.get('branchTransaction') attached_original_trytes = api.attach_to_tangle(trunk, branch, original_trytes).get('trytes') # So we have the original bundle attached, time to construct the new one # We need to re-attach, but take special care, so we dont use the api, rather we do it ourself re_attached_trytes = custom_attach(attached_original_trytes, 9) original_bundle = Bundle.from_tryte_strings(attached_original_trytes) re_attached_bundle = Bundle.from_tryte_strings(re_attached_trytes) pprint('Original bundle is:') pprint(original_bundle.as_json_compatible()) pprint('Reattached bundle is:') pprint(re_attached_bundle.as_json_compatible()) #api.broadcast_and_store(attached_original_trytes) api.broadcast_and_store(re_attached_trytes) # bundles # BJRYHWKLREEUQCAZSUQFDNQAIL9LFRBZVEFFWPQZ99GNKATZBUZKFJYJIRYTFBPPESFJYDQRAYHNHH9LW good # UYBVN9EZKNA9KGGCSILJJGRTNGI9ZHVIUEYB9VXZXHLKZBTFGPHGJPJ9OHCUILSRBAA9XYSDKIXHYVLJW bad
class IotaCache(object): def __init__(self, uri=None, seed=None): if not uri: self.uri = "http://localhost:14700" else: self.uri = uri if not seed: self.seed = 'EBZYNR9YVFIOAZUPQOLRZXPPPIKRCJ9EJKVCXMYVLMNOCCOPYPJKCWUZNLJZZZZWTMVQUXZFYLVLZXJ9Q' else: self.seed = seed self.api = Iota(self.uri, self.seed, testnet=True) self.mwm = 1 self.depth = 15 def cache_txn_in_tangle_sdk(self, ipfs_addr, tag): api_response = self.api.get_new_addresses() addy = api_response['addresses'][0] address = binary_type(addy).decode('ascii') result = self.api.send_transfer( depth=self.depth, transfers=[ ProposedTransaction( address=Address(address), value=0, tag=Tag(tag), message=TryteString.from_string(ipfs_addr), ), ], min_weight_magnitude=self.mwm, ) return result def cache_txn_in_tangle_simple(self, data, tag): address = "JVSVAFSXWHUIZPFDLORNDMASGNXWFGZFMXGLCJQGFWFEZWWOA9KYSPHCLZHFBCOHMNCCBAGNACPIGHVYX" txns = self.api.get_transactions_to_approve(self.depth) tr = self.api.get_trytes([txns[u'branchTransaction']]) txn = Transaction.from_tryte_string(tr[u'trytes'][0], txns[u'branchTransaction']) txn.trunk_transaction_hash = txns[u'trunkTransaction'] txn.branch_transaction_hash = txns[u'branchTransaction'] txn.tag = Tag(TryteString.from_string(tag)) txn.signature_message_fragment = Fragment(TryteString.from_bytes(data)) attach_trytes = attachToTangle(self.uri, txns[u'trunkTransaction'].__str__(), txns[u'branchTransaction'].__str__(), 1, txn.as_tryte_string().__str__()) res = self.api.broadcast_and_store(attach_trytes[u'trytes']) return res def cache_txn_in_tangle_message(self, data, tag): #address = "JVSVAFSXWHUIZPFDLORNDMASGNXWFGZFMXGLCJQGFWFEZWWOA9KYSPHCLZHFBCOHMNCCBAGNACPIGHVYX" address = "14dD6ygPi5WXdwwBTt1FBZK3aD8uDem1FY" res = storeMessage(self.uri, address, data, tag) return res def get_balance(self, coin_type, account): address = "JVSVAFSXWHUIZPFDLORNDMASGNXWFGZFMXGLCJQGFWFEZWWOA9KYSPHCLZHFBCOHMNCCBAGNACPIGHVYX" res = getBalance(self.uri, address, coin_type, account) return res def get_approved_txns(self, tag): ret = [] transactions = self.api.find_transactions(None, None, [tag], None) if len(transactions['hashes']) == 0: return ret tips = self.api.get_tips() states = self.api.get_inclusion_states(transactions['hashes'], tips['hashes']) i = 0 for txn in transactions['hashes']: if states['states'][i] is True: ret.append(txn) return ret def get_non_consumed_txns(self, tag): ret = [] txn_hashes_all = self.get_approved_txns(tag) if len(txn_hashes_all) == 0: return ret txn_trytes_all = self.api.get_trytes(txn_hashes_all) consumedSet = [] txn_hashes_consumed = self.api.find_transactions(None, None, [tag+b"CONSUMED"], None) if len(txn_hashes_consumed['hashes']) != 0: txn_trytes_consumed = self.api.get_trytes(txn_hashes_consumed['hashes']) i=0 for txnTrytes in txn_trytes_consumed['trytes']: txn = Transaction.from_tryte_string(txnTrytes, txn_hashes_consumed['hashes'][i]) i+=1 consumedSet.append(txn.signature_message_fragment) i=0 for txnTrytes in txn_trytes_all['trytes']: txn = Transaction.from_tryte_string(txnTrytes, txn_hashes_all[i]) i+=1 if txn.signature_message_fragment not in consumedSet: msgTryte = txn.signature_message_fragment.decode() ret.append(msgTryte) return ret def set_txn_as_synced(self, ipfs_addr, tag): result = self.cache_txn_in_tangle_sdk(ipfs_addr, tag+b"CONSUMED") return result def add_neighbors(self, uris): res = addNeighbors(self.uri, uris) return res def get_block_content(self, hashes): res = getBlockContent(self.uri, hashes) return res def get_dag(self, dag_type): res = getDAG(self.uri, dag_type) return res def get_utxo(self, dag_type): res = getUTXO(self.uri, dag_type) return res def get_total_order(self): res = getTotalOrder(self.uri) return res
from iota import Iota server = 'http://140.116.247.123:14267' # another node : https://nodes.thetangle.org:443 api = Iota(server) # print(api.get_node_info()) trytes = [] trytes.append( 'DXRNZCCBZWWLHKSLG9MTZGNEXYFGIMVTHDOHXHZYOQTGRRLOI9T9CCD9STHXBWAICFBANV9GJDFFZ9999' ) print(api.broadcast_and_store(trytes))
host = yaml_file['nodes']['nodeA']['host'] port = yaml_file['nodes']['nodeA']['ports']['api'] api = Iota('http://{}:{}'.format(host, port)) txn = ProposedTransaction( address = Address('KSAFREMKHHYHSXNLGZPFVHANVHVMKWSGEAHGTXZCSQMXTCZXOGBLVPCWFKVAEQYDJMQALKZRKOTWLGBSC'), value = 0 ) bundle = ProposedBundle() bundle.add_transaction(txn) bundle.add_transaction(txn) index_trytes = str(int_to_trytes(index, 9)) bundle[0]._legacy_tag = Tag(index_trytes) finalize.finalize(bundle) bundle_trytes = bundle.as_tryte_strings() tips = api.get_transactions_to_approve(depth=3) branch = tips['branchTransaction'] trunk = tips['trunkTransaction'] milestone_bundle = api.attach_to_tangle(trunk,branch, bundle_trytes,3) api.broadcast_and_store(milestone_bundle.get('trytes')) print("Milestone {} attached and stored: {}".format(index, milestone_bundle))