def performTransactions(api_vendor, api_investor, readings1, readings2): #Extracting addresses from the generated vendor addresses vendorAddress = generateVendorAddresses(api_vendor) address_available = vendorAddress['addresses'] targetAddress1 = address_available[0] targetAddress2 = address_available[1] print(targetAddress1) print(targetAddress2) #Preparing the message, converting the values received into a string to send as a message readings_str1 = [str(i) for i in readings1] readings_str2 = [str(i) for i in readings2] message1 = ",".join(readings_str1) message2 = ",".join(readings_str2) NowIs = datetime.now( ) # get a actual date & time - just to have some meaningfull info message1 = message1 + ": " + str(NowIs) pt = iota.ProposedTransaction( address=iota.Address(targetAddress1), # 81 trytes long address message=iota.TryteString.from_unicode('Values and TimeStamp are %s.' % (message1)), tag=iota.Tag(b'HRIBEK999IOTA999TUTORIAL'), # Up to 27 trytes value=0) NowIs = datetime.now( ) # get a actual date & time - just to have some meaningfull info message2 = message2 + ": " + str(NowIs) pt2 = iota.ProposedTransaction( address=iota.Address(targetAddress2), # 81 trytes long address message=iota.TryteString.from_unicode('Values and TimeStamp are %s.' % (message2)), tag=iota.Tag(b'HRIBEK999IOTA999TUTORIAL'), # Up to 27 trytes value=0) # preparing bundle that consists of both transactions prepared in the previous example pb = iota.ProposedBundle( transactions=[pt, pt2]) # list of prepared transactions is needed at least pb.finalize() Trytes = pb.as_tryte_strings() # bundle as trytes gta = api_vendor.get_transactions_to_approve( depth=3) # get tips to be approved by your bundle att = api_vendor.attach_to_tangle( trunk_transaction=gta['trunkTransaction'], # first tip selected branch_transaction=gta['branchTransaction'], # second tip selected trytes=Trytes, # our finalized bundle in Trytes min_weight_magnitude=14) # MWMN print("Broadcasting transaction...") res = api_vendor.broadcast_and_store(att['trytes']) return (res, targetAddress1)
def promoteTX(promoter_mode, rx_address, tx_hash_to_be_promoted, iteration): ### TX CREATION ## print("\n\n ### TX CREATION ##") if (promoter_mode == True): tx_message = "They see me promoting, they hatin!" tx_tag = iota.Tag(b'SEMS9PROMOTER9DOT9PY') else: tx_message = "They see me spamming, they hatin!" tx_tag = iota.Tag(b'SEMS9SPAMMER9DOT9PY') tx = iota.ProposedTransaction(address = iota.Address(rx_address), message = iota.TryteString.from_unicode(tx_message), tag = tx_tag, value = 0) print(' Created first transaction: ') print(" " + str(vars(tx))) ### BUNDLE FINALIZATION ### print("\n\n ### BUNDLE FINALIZATION ###") bundle = iota.ProposedBundle(transactions = [tx]) bundle.finalize() print(" Bundle is finalized...") print(" Generated bundle hash: %s" % (bundle.hash)) print(" List of all transaction in the Bundle:\n") for txn in bundle: print(" " + str(vars(txn))) bundle_trytes = bundle.as_tryte_strings() # bundle as trytes ### TIP SELECTION ### print("\n\n ### TIP SELECTION ###") tips = api.get_transactions_to_approve(depth = 3) if (promoter_mode == True): tips['branchTransaction'] = tx_hash_to_be_promoted print(" " + str(tips)) ### POW ### print("\n\n ### POW ###") attached_tx = api.attach_to_tangle(trunk_transaction=tips['trunkTransaction'], branch_transaction=tips['branchTransaction'], trytes=bundle_trytes, min_weight_magnitude=14) ### BROADCASTING ### print(" Broadcasting transaction...") res = api.broadcast_and_store(attached_tx['trytes']) print(" " + str(res)) ### TRANSACTION RECAP ### print("\n\n ### TRANSACTION RECAP ###") print(" " + str(vars(attached_tx['trytes'][0]))) sent_tx = iota.Transaction.from_tryte_string(attached_tx['trytes'][0]) print(" Transaction Hash: " + str(sent_tx.hash)) if (promoter_mode == True): return (sent_tx.hash) else: return ("")
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])
NowIs = datetime.now() # get a actual date & time - just to have some meaningfull info # preparing transactions pt = iota.ProposedTransaction(address = iota.Address(addys[0]), # 81 trytes long address message = iota.TryteString.from_unicode('Hey hey, trying to figure this thing out. This is tx1, now is %s' % (NowIs)), tag = iota.Tag(b'LOCALATTACHINTERFACE99999'), # Up to 27 trytes value = 0) pt2 = iota.ProposedTransaction(address = iota.Address(addys[1]), # 81 trytes long address message = iota.TryteString.from_unicode('Hey hey, trying to figure this thing out. This is tx2, now is %s' % (NowIs)), tag = iota.Tag(b'LOCALATTACHINTERFACE99999'), # Up to 27 trytes value = 0) # besides the given attributes, library also adds a transaction timestamp # preparing bundle that consists of both transactions prepared in the previous example pb = iota.ProposedBundle(transactions=[pt2,pt]) # list of prepared transactions is needed at least # generate bundle hash using sponge/absorb function + normalize bundle hash + copy bundle hash into each transaction / bundle is finalized pb.finalize() #bundle is finalized, let's print it print("\nGenerated bundle hash: %s" % (pb.hash)) print("\nTail Transaction in the Bundle is a transaction #%s." % (pb.tail_transaction.current_index)) api = iota.Iota("https://nodes.thetangle.org:443") # selecting IOTA node gta = api.get_transactions_to_approve(depth=3) # get tips to be approved by your bundle mwm = 14 # target is mainnet bundle = entangled_interface.attach_to_tangle(pb, gta['branchTransaction'],gta['trunkTransaction'], mwm)
# Generate two addresses addres_generator = iota.crypto.addresses.AddressGenerator(myseed) addys = addres_generator.get_addresses(1, count=2) # Preparing transactions pt = iota.ProposedTransaction(address=iota.Address(addys[0]), tag=iota.Tag(b'LOCALATTACHINTERFACE99999'), value=0) pt2 = iota.ProposedTransaction(address=iota.Address(addys[1]), tag=iota.Tag(b'LOCALATTACHINTERFACE99999'), value=0) # Preparing bundle that consists of both transactions prepared in the previous example pb = iota.ProposedBundle(transactions=[pt2, pt]) # Generate bundle hash pb.finalize() # Declare an api instance api = iota.Iota("https://nodes.thetangle.org:443") # Get tips to be approved by your bundle gta = api.get_transactions_to_approve( depth=3) # Depth = how many milestones back minimum_weight_magnitude = 14 # target is mainnet # perform PoW locally bundle_trytes =\
def send_transfer(tag, message, address, values, dict_tips): ## Set output transaction print("Start to sransfer ... ") propose_bundle = iota.ProposedBundle() print("Setting output transaction ...") txn_output = iota.ProposedTransaction(address=iota.Address(address), value=values, tag=iota.Tag(tag), message=iota.TryteString(message)) propose_bundle.add_transaction(txn_output) # Get input address if int(values) > 0: print "DEBUG values = " + 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 = 1 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(gen_a_address()['addresses'][0]) propose_bundle.send_unspent_inputs_to(unspent) # This will get the bundle hash print("Bundle finalize ...") propose_bundle.finalize() ## 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['branchTransaction'] branch_hash = dict_tips['trunkTransaction'] # Do PoW (attach to tangle) for tx_tryte in trytes: # TODO: Timestamp # timestamp = None # timestamp_lower_bound = None # timestamp_upper_bound = None # 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 = '' with tempfile.TemporaryFile() as tempf: proc = subprocess.Popen( ['python', 'tx_search_nonce.py', str(tx_tryte), str(14)], stdout=tempf) proc.wait() tempf.seek(0) nonce = tempf.read().rstrip() tx_tryte = insert_to_trytes(2646, 2673, str(nonce), tx_tryte) print "Prepare to broadcast ..." try: api.broadcast_transactions([tx_tryte[0:2673]]) except Exception as e: print "Error: " + str(e.context) return propose_bundle.hash
def my_send_transfer(): add1 = iota.Address( b'BUFVPNVBDXLJSJTVBZTKLUOBVZIMXZODRTHOFGNNJOAXJOKUNIDLFMYSTXMUAYDBLHGTGFFSECFCQV9GZ' ) add2 = iota.Address( b'FWYJNMOZ9WSHNJQVZUACHUCTDFSREKJZSJGORAIEMOCKFHJZEBIYMGOIEAJHIFZHQDDEBPFZZC99SZKPY' ) unspend = iota.Address( b'HSQFLPJDQDWLRUFUSCCALKTFIOSAMLKJ9FXVGDMIHVGJZBATK99BOLDGKBTHVN9Z9MFXCYZK9YGHGQPZ9' ) now = datetime.now() add1.key_index = 2 add1.security_level = 2 # 1. create transactions pt = iota.ProposedTransaction(address=add1, message=iota.TryteString.from_unicode( 'hello, Now is %s' % (now)), tag=iota.Tag(b'HRIBEK999IOTA999TUTORIAL'), value=0) pt2 = iota.ProposedTransaction(address=add2, message=iota.TryteString.from_unicode( 'hello2, Now is %s' % (now)), tag=iota.Tag(b'HRIBEK999IOTA999TUTORIAL'), value=0) #pprint(vars(pt)) #print('\n') #pprint(vars(pt2)) # 2. create bundle pb = iota.ProposedBundle(transactions=[pt, pt2]) # receiever tx #pb._transactions.append(pt) # input tx #pb.send_unspent_inputs_to(unspend) # unspend tx # finalize: generate bundle hash + normalize bundle hash + copy bundle hash into each tx pb.finalize() print(pb.hash) # the first tx in bundle called tail transaction #print(pb.tail_transaction.current_index) # 0 for tx in pb: pprint(vars(tx)) # encode bundle into trytes trytes = pb.as_tryte_strings() # 3. tips selection api = iota.Iota("http://localhost:14700") tips = api.get_transactions_to_approve(depth=3) # return trunk and branch ''' tips = { 'branchTransaction': iota.Hash(b'IPSRKCCKMWM9VQVJDVHQYGYFXTAFYGZYAZPTUTANMDFLXYBHNKPTNBQRLRZXMFXVDPKPNJPOBFDDA9999'), 'duration': 1, 'trunkTransaction': iota.Hash(b'IPSRKCCKMWM9VQVJDVHQYGYFXTAFYGZYAZPTUTANMDFLXYBHNKPTNBQRLRZXMFXVDPKPNJPOBFDDA9999') } ''' pprint(tips) # 4. Do PoW # return the new bundle that each tx in it get the nonce and tx_hash att = api.attach_to_tangle(trunk_transaction=tips['trunkTransaction'], branch_transaction=tips['branchTransaction'], trytes=trytes, min_weight_magnitude=14) # every tx in final bundle #for i in att['trytes']: # pprint(vars(iota.Transaction.from_tryte_string(i))) # 5. Store & Brordcast res = api.broadcast_and_store(att['trytes'])
# Note: You will need to implement getTransactionsToApprove import iota import pearldiver SEED = 'GENERATE_YORU_SELF' tag = iota.Tag('TESTINGPYTHON') pt = iota.ProposedTransaction(address=iota.Address( '9TPHVCFLAZTZSDUWFBLCJOZICJKKPVDMAASWJZNFFBKRDDTEOUJHR9JVGTJNI9IYNVISZVXARWJFKUZWC' ), value=90, tag=tag, message=iota.TryteString('HELLO')) pb = iota.ProposedBundle([pt]) # pb.add_inputs([list]) # pb._create_input_transaction(addy) addy = iota.Address( 'INDTKDAH9GGWDAJDWQLWUKCIHSYNEFQUGVHOYWLZRYPEZIZYQHQJNDLDPCLWMMO9UAEZUWPHMWZRLWGOB' ) addy.balance = 100 addy.key_index = 2 addy.security_level = 2 inputs = [iota.ProposedTransaction(address=addy, tag=tag, value=-addy.balance)] for input in inputs: pb._transactions.append(input) for _ in range(addy.security_level - 1): pb._transactions.append(