def get_utxos_for_address(address, testnet=False): if testnet: r = [] else: r = blockexplorer.get_unspent_outputs(address) assert type(r) == list return r
def Main(): print("***block data components***") hashValue = '000000000000000016f9a2c3e0f4c1245ff24856a79c34806969f5084f410680' blockDict = class_00.getBlockDict(hashValue) class_00.printDictvalue(blockDict) print("***transaction data components***") transactionDict = class_00.getTransactionDict(blockDict['transactions']) class_00.printDictvalue(transactionDict) print("***transaction output data***") outputDict = class_00.getOutputDict(transactionDict['outputs']) class_00.printDictvalue(outputDict) print("***address data components***") address = blockexplorer.get_address('1HS9RLmKvJ7D1ZYgfPExJZQZA1DMU3DEVd') addressDict = class_00.getAddressDict(address) class_00.printDictvalue(addressDict) print("***unspentoutputs data components***") outs = blockexplorer.get_unspent_outputs( '1HS9RLmKvJ7D1ZYgfPExJZQZA1DMU3DEVd') unspentoutputDict = class_00.getUnspentoutputsDict(outs[0]) class_00.printDictvalue(unspentoutputDict)
def listUTXO(utxoOfAddress): try: outs = blockexplorer.get_unspent_outputs(utxo) print("UTXOs for " + utxo) for i in outs: print(i.value / 100000000) except: print("No UXTOs available")
def blockexplorer(self): block = blockexplorer.get_block( '000000000000000016f9a2c3e0f4c1245ff24856a79c34806969f5084f410680') tx = blockexplorer.get_tx( 'd4af240386cdacab4ca666d178afc88280b620ae308ae8d2585e9ab8fc664a94') blocks = blockexplorer.get_block_height(2570) address = blockexplorer.get_address( '1HS9RLmKvJ7D1ZYgfPExJZQZA1DMU3DEVd') xpub = None #blockexplorer.get_xpub('xpub6CmZamQcHw2TPtbGmJNEvRgfhLwitarvzFn3fBYEEkFTqztus7W7CNbf48Kxuj1bRRBmZPzQocB6qar9ay6buVkQk73ftKE1z4tt9cPHWRn') addresses = None # blockexplorer.get_multi_address('1HS9RLmKvJ7D1ZYgfPExJZQZA1DMU3DEVd', 'xpub6CmZamQcHw2TPtbGmJNEvRgfhLwitarvzFn3fBYEEkFTqztus7W7CNbf48Kxuj1bRRBmZPzQocB6qar9ay6buVkQk73ftKE1z4tt9cPHWRn') outs = blockexplorer.get_unspent_outputs( '1HS9RLmKvJ7D1ZYgfPExJZQZA1DMU3DEVd') latest_block = blockexplorer.get_latest_block() txs = blockexplorer.get_unconfirmed_tx() blocks_by_name = None #blockexplorer.get_blocks(pool_name = 'Discus Fish')
def update_utxos(): address = DBSession.query(KeyStore).filter(KeyStore.name == PRIMARY).first().address DBSession.query(UTXOs).delete() try: outputs = blockexplorer.get_unspent_outputs(address) except APIException as e: outputs = [] for output in outputs: d = dict(output.__dict__) d['tx_hash'] = unhexlify(d['tx_hash']) d['script'] = unhexlify(d['script']) d['address'] = address del(d['tx_index'], d['value_hex']) if 0 == len(DBSession.query(UTXOs).filter(UTXOs.tx_hash == d['tx_hash'], UTXOs.tx_output_n == d['tx_output_n']).all()): DBSession.add(UTXOs(**d)) return len(outputs)
def update_utxos(): address = DBSession.query(KeyStore).filter( KeyStore.name == PRIMARY).first().address DBSession.query(UTXOs).delete() try: outputs = blockexplorer.get_unspent_outputs(address) except APIException as e: outputs = [] for output in outputs: d = dict(output.__dict__) d['tx_hash'] = unhexlify(d['tx_hash']) d['script'] = unhexlify(d['script']) d['address'] = address del (d['tx_index'], d['value_hex']) if 0 == len( DBSession.query(UTXOs).filter( UTXOs.tx_hash == d['tx_hash'], UTXOs.tx_output_n == d['tx_output_n']).all()): DBSession.add(UTXOs(**d)) return len(outputs)
def updateLock(self, num): """ Checks and updates lock if ownernship has changed :return: Result of checking and updating lock ownership """ try: utxo = blockexplorer.get_unspent_outputs(self.public_address) tx_hashes = [str(i.tx_hash) for i in utxo] if any(self.transaction in tx_hashes): return 'Not Updated' else: response = 'go' j = 0 while response == 'go': old_address = blockexplorer.get_address( self.public_address) outputs = old_address.transactions[0].outputs scripts = [str(i.script) for i in outputs] num_inputs = len(old_address.transactions[0].inputs) output_index = self.ProcessTransaction( scripts, True, num_inputs) if output_index is None: output_index = 0 else: output_index = int(output_index) last_trans_output = old_address.transactions[0].outputs[ output_index] included_block = old_address.transactions[0].block_height latest_block = blockexplorer.get_latest_block() if (latest_block.height - included_block < 3): if j == 0: return 'Not Updated because of lack of confidence' if j > 0: if trans == str(old_address.transaction[0].hash): return 'Not enough confidence in provided transaction (updated to new address though)' else: return 'Not enough confidence but not in the provided transaction (updated to new address though)' self.public_address = str(last_trans_output.address) self.transaction = str(old_address.transactions[0].hash) try: utxo_2 = blockexplorer.get_unspent_outputs( self.public_address) tx_hashes_2s = [str(i.tx_hash) for i in utxo_2] if any(self.transaction in tx_hashes_2): reponse = 'exit' except blockchain.exceptions.APIException: j = j + 1 f = open('lock.txt', 'w') f.write(self.transaction) f.write('\n') f.write(self.public_address) f.close() return 'Updated but not with transaction provided' except blockchain.exceptions.APIException: response = 'go' j = 0 while response == 'go': old_address = blockexplorer.get_address(self.public_address) outputs = old_address.transactions[0].outputs scripts = [str(i.script) for i in outputs] num_inputs = len(old_address.transactions[0].inputs) output_index = self.ProcessTransaction(scripts, True, num_inputs) if output_index is None: output_index = 0 else: output_index = int(output_index) last_trans_output = old_address.transactions[0].outputs[ output_index] included_block = old_address.transactions[0].block_height latest_block = blockexplorer.get_latest_block() if (latest_block.height - included_block < 3): if j == 0: return 'Not Updated because of lack of confidence' if j > 0: if trans == str(old_address.transaction[0].hash): return 'Not enough confidence in provided transaction (updated to new address though)' else: return 'Not enough confidence but not in the provided transaction (updated to new address though)' self.public_address = str(last_trans_output.address) self.transaction = str(old_address.transactions[0].hash) try: blockexplorer.get_unspent_outputs(self.public_address) reponse = 'exit' except blockchain.exceptions.APIException: j = j + 1 f = open('lock.txt', 'w') f.write(self.transaction) f.write('\n') f.write(self.public_address) f.close() return 'Updated but not with transaction provided'
# Check to see if transaction is valid and conerns this lock try: newTransaction = blockexplorer.get_tx(trans) except blockchain.exceptions.APIException, TypeError: return 'Invalid transaction' inputs = newTransaction.inputs input_index = -1 for i in range(0, len(inputs)): if str(inputs[i].address) == self.public_address: input_index = i # Traverse blockchain to update lock ownership if credentials are outdated if input_index == -1: try: utxo = blockexplorer.get_unspent_outputs(self.public_address) tx_hashes = [str(i.tx_hash) for i in utxo] if any(self.transaction in tx_hashes): return 'Not Updated' else: self.updateLock(1) except blockchain.exceptions.APIException: response = 'go' j = 0 while response == 'go': old_address = blockexplorer.get_address( self.public_address) outputs = old_address.transactions[0].outputs scripts = [str(i.script) for i in outputs] num_inputs = len(old_address.transactions[0].inputs) output_index = self.ProcessTransaction(
def updateLock(self, num): """ Checks and updates lock if ownernship has changed :return: Result of checking and updating lock ownership """ try: utxo = blockexplorer.get_unspent_outputs(self.public_address) tx_hashes = [str(i.tx_hash) for i in utxo] if any(self.transaction in tx_hashes): return 'Not Updated' else: response = 'go' j = 0 while response == 'go': old_address = blockexplorer.get_address(self.public_address) outputs = old_address.transactions[0].outputs scripts = [str(i.script) for i in outputs] num_inputs = len(old_address.transactions[0].inputs) output_index = self.ProcessTransaction(scripts, True,num_inputs) if output_index is None: output_index = 0 else: output_index = int(output_index) last_trans_output = old_address.transactions[0].outputs[output_index] included_block = old_address.transactions[0].block_height latest_block = blockexplorer.get_latest_block() if (latest_block.height - included_block < 3): if j == 0: return 'Not Updated because of lack of confidence' if j > 0: if trans == str(old_address.transaction[0].hash): return 'Not enough confidence in provided transaction (updated to new address though)' else: return 'Not enough confidence but not in the provided transaction (updated to new address though)' self.public_address = str(last_trans_output.address) self.transaction = str(old_address.transactions[0].hash) try: utxo_2 = blockexplorer.get_unspent_outputs(self.public_address) tx_hashes_2s = [str(i.tx_hash) for i in utxo_2] if any(self.transaction in tx_hashes_2): reponse = 'exit' except blockchain.exceptions.APIException: j = j + 1 f = open('lock.txt','w') f.write(self.transaction) f.write('\n') f.write(self.public_address) f.close() return 'Updated but not with transaction provided' except blockchain.exceptions.APIException: response = 'go' j = 0 while response == 'go': old_address = blockexplorer.get_address(self.public_address) outputs = old_address.transactions[0].outputs scripts = [str(i.script) for i in outputs] num_inputs = len(old_address.transactions[0].inputs) output_index = self.ProcessTransaction(scripts, True,num_inputs) if output_index is None: output_index = 0 else: output_index = int(output_index) last_trans_output = old_address.transactions[0].outputs[output_index] included_block = old_address.transactions[0].block_height latest_block = blockexplorer.get_latest_block() if (latest_block.height - included_block < 3): if j == 0: return 'Not Updated because of lack of confidence' if j > 0: if trans == str(old_address.transaction[0].hash): return 'Not enough confidence in provided transaction (updated to new address though)' else: return 'Not enough confidence but not in the provided transaction (updated to new address though)' self.public_address = str(last_trans_output.address) self.transaction = str(old_address.transactions[0].hash) try: blockexplorer.get_unspent_outputs(self.public_address) reponse = 'exit' except blockchain.exceptions.APIException: j = j + 1 f = open('lock.txt','w') f.write(self.transaction) f.write('\n') f.write(self.public_address) f.close() return 'Updated but not with transaction provided'
# Check to see if transaction is valid and conerns this lock try: newTransaction = blockexplorer.get_tx(trans) except blockchain.exceptions.APIException, TypeError: return 'Invalid transaction' inputs = newTransaction.inputs input_index = -1; for i in range(0,len(inputs)): if str(inputs[i].address) == self.public_address: input_index = i # Traverse blockchain to update lock ownership if credentials are outdated if input_index == -1: try: utxo = blockexplorer.get_unspent_outputs(self.public_address) tx_hashes = [str(i.tx_hash) for i in utxo] if any(self.transaction in tx_hashes): return 'Not Updated' else: self.updateLock(1) except blockchain.exceptions.APIException: response = 'go' j = 0 while response == 'go': old_address = blockexplorer.get_address(self.public_address) outputs = old_address.transactions[0].outputs scripts = [str(i.script) for i in outputs] num_inputs = len(old_address.transactions[0].inputs) output_index = self.ProcessTransaction(scripts, True,num_inputs) if output_index is None: