def get_unspent_outputs(self, address): unspent_outputs = [] # this calls a different api not accessible through localhost proxy unspent_url = 'https://blockchain.info/unspent?active=%s&format=json' % address unspent_response = try_get(unspent_url) r = unspent_response.json() for u in r['unspent_outputs']: tx_out = TransactionOutput( COutPoint(unhexlify(u['tx_hash']), u['tx_output_n']), CBitcoinAddress(address), CScript(unhexlify(u['script'])), int(u['value'])) unspent_outputs.append(tx_out) return unspent_outputs
def get_unspent_outputs(self, address): unspent_outputs = [] # this calls a different api not accessible through localhost proxy unspent_url = 'https://blockchain.info/unspent?active=%s&format=json' % address unspent_response = try_get(unspent_url) r = unspent_response.json() for u in r['unspent_outputs']: tx_out = TransactionOutput(COutPoint(unhexlify(u['tx_hash']), u['tx_output_n']), CBitcoinAddress(address), CScript(unhexlify(u['script'])), int(u['value'])) unspent_outputs.append(tx_out) return unspent_outputs
def create_transactions(self, wallet, revocation_address, issuing_transaction_cost, split_input_trxs): # finish tree self.tree.make_tree() op_return_value = unhexlify(self.tree.get_merkle_root()) unspent_outputs = wallet.get_unspent_outputs(self.issuing_address) last_output = unspent_outputs[-1] txouts = self.build_txouts(issuing_transaction_cost) txouts = txouts + [ trx_utils.create_transaction_output( revocation_address, issuing_transaction_cost.min_per_output) ] tx = trx_utils.create_trx(op_return_value, issuing_transaction_cost, self.issuing_address, txouts, last_output) unsigned_tx_file_name = convert_file_name( self.config.unsigned_txs_file_pattern, self.batch_id) unsent_tx_file_name = convert_file_name( self.config.signed_txs_file_pattern, self.batch_id) sent_tx_file_name = convert_file_name( self.config.sent_txs_file_pattern, self.batch_id) td = TransactionData(uid=self.batch_id, tx=tx, tx_input=last_output, op_return_value=hexlify(op_return_value), unsigned_tx_file_name=unsigned_tx_file_name, signed_tx_file_name=unsent_tx_file_name, sent_tx_file_name=sent_tx_file_name) return [td]
def create_transactions(self, revocation_address, issuing_transaction_cost): """ Create the batch Bitcoin transaction :param revocation_address: :param issuing_transaction_cost: :return: """ self.tree.make_tree() spendables = get_unspent_outputs(self.issuing_address) if not spendables: error_message = 'No money to spend at address {}'.format( self.issuing_address) logging.error(error_message) raise InsufficientFundsError(error_message) last_input = spendables[-1] op_return_value = unhexlify(self.tree.get_merkle_root()) tx_outs = self.build_recipient_tx_outs() tx_outs.append( trx_utils.create_transaction_output( revocation_address, issuing_transaction_cost.min_per_output)) transaction = trx_utils.create_trx(op_return_value, issuing_transaction_cost, self.issuing_address, tx_outs, last_input) unsigned_tx_file_name = convert_file_name( self.config.unsigned_txs_file_pattern, self.batch_id) unsent_tx_file_name = convert_file_name( self.config.signed_txs_file_pattern, self.batch_id) sent_tx_file_name = convert_file_name( self.config.sent_txs_file_pattern, self.batch_id) transaction_data = TransactionData( uid=self.batch_id, tx=transaction, tx_input=last_input, op_return_value=hexlify(op_return_value), unsigned_tx_file_name=unsigned_tx_file_name, signed_tx_file_name=unsent_tx_file_name, sent_tx_file_name=sent_tx_file_name) return [transaction_data]
def issue_on_blockchain(self): """ Issue the certificates on the Bitcoin blockchain :param revocation_address: :return: """ self.tree.make_tree() op_return_value_bytes = unhexlify(self.tree.get_merkle_root()) op_return_value = hexlify(op_return_value_bytes) spendables = self.connector.get_unspent_outputs(self.issuing_address) if not spendables: error_message = 'No money to spend at address {}'.format(self.issuing_address) logging.error(error_message) raise InsufficientFundsError(error_message) last_input = spendables[-1] tx = self.transaction_handler.create_transaction(last_input, op_return_value_bytes) hex_tx = hexlify(tx.serialize()) logging.info('Unsigned hextx=%s', hex_tx) prepared_tx = tx_utils.prepare_tx_for_signing(hex_tx, [last_input]) with FinalizableSigner(self.secure_signer) as signer: signed_tx = signer.sign_transaction(prepared_tx) # log the actual byte count tx_byte_count = tx_utils.get_byte_count(signed_tx) logging.info('The actual transaction size is %d bytes', tx_byte_count) signed_hextx = signed_tx.as_hex() logging.info('Signed hextx=%s', signed_hextx) # verify transaction before broadcasting tx_utils.verify_transaction(signed_hextx, op_return_value) # send tx and persist txid tx_id = self.connector.broadcast_tx(signed_tx) if tx_id: logging.info('Broadcast transaction with txid %s', tx_id) else: logging.warning( 'could not broadcast transaction but you can manually do it! signed hextx=%s', signed_hextx) return tx_id
def create_transactions(self, wallet, revocation_address, issuing_transaction_cost, split_input_trxs): unspent_outputs = wallet.get_unspent_outputs(self.issuing_address) current_tail = -1 txs = [] for uid, certificate_metadata in self.certificates_to_issue.items(): last_output = unspent_outputs[current_tail] with open(certificate_metadata.certificate_hash_file_name, 'r') as in_file: op_return_value = in_file.read() # send a transaction to the recipient's public key, and to a # revocation address txouts = trx_utils.create_recipient_outputs( issuing_transaction_cost.min_per_output, certificate_metadata.public_key, revocation_address) tx = trx_utils.create_trx(unhexlify(op_return_value), issuing_transaction_cost, self.issuing_address, txouts, last_output) unsigned_tx_file_name = convert_file_name( self.config.unsigned_txs_file_pattern, uid) unsent_tx_file_name = convert_file_name( self.config.signed_txs_file_pattern, uid) sent_tx_file_name = convert_file_name( self.config.sent_txs_file_pattern, uid) td = TransactionData(uid=certificate_metadata.uid, tx=tx, tx_input=last_output, op_return_value=op_return_value, unsigned_tx_file_name=unsigned_tx_file_name, signed_tx_file_name=unsent_tx_file_name, sent_tx_file_name=sent_tx_file_name) txs.append(td) if split_input_trxs: current_tail -= 1 return txs
def create_transactions(self, revocation_address): """ Create the batch Bitcoin transaction :param revocation_address: :return: """ self.tree.make_tree() spendables = self.connector.get_unspent_outputs(self.issuing_address) if not spendables: error_message = 'No money to spend at address {}'.format( self.issuing_address) logging.error(error_message) raise InsufficientFundsError(error_message) last_input = spendables[-1] op_return_value = unhexlify(self.tree.get_merkle_root()) tx_outs = self.build_recipient_tx_outs() tx_outs.append( tx_utils.create_transaction_output( revocation_address, self.tx_cost_constants.get_minimum_output_coin())) transaction = tx_utils.create_trx(op_return_value, self.total, self.issuing_address, tx_outs, last_input) transaction_data = TransactionData( uid=self.batch_id, tx=transaction, tx_input=last_input, op_return_value=hexlify(op_return_value), batch_metadata=self.batch_metadata) return [transaction_data]
def broadcast_tx(self, transaction): as_hex = transaction.as_hex() transaction = CTransaction.deserialize(unhexlify(as_hex)) tx_id = bitcoin.rpc.Proxy().sendrawtransaction(transaction) # reverse endianness for bitcoind return hexlify(bytearray(tx_id)[::-1])
def broadcast_tx(self, transaction): as_hex = transaction.as_hex() transaction = CTransaction.deserialize(unhexlify(as_hex)) tx_id = bitcoin.rpc.Proxy().sendrawtransaction(transaction) # reverse endianness for bitcoind return hexlify(bytearray(tx_id)[::-1])
def bitcoind_broadcast(hextx): tx = CTransaction.deserialize(unhexlify(hextx)) txid = bitcoin.rpc.Proxy().sendrawtransaction(tx) # reverse endianness for bitcoind return hexlify(bytearray(txid)[::-1])
def bitcoind_broadcast(hextx): tx = CTransaction.deserialize(unhexlify(hextx)) txid = bitcoin.rpc.Proxy().sendrawtransaction(tx) return hexlify(txid)
def issue_on_blockchain(self): """ Issue the certificates on the Bitcoin blockchain :param revocation_address: :return: """ self.tree.make_tree() op_return_value_bytes = unhexlify(self.tree.get_merkle_root()) op_return_value = hexlify(op_return_value_bytes) for attempt_number in range(0, self.max_retry): try: if self.prepared_inputs: inputs = self.prepared_inputs else: spendables = self.connector.get_unspent_outputs( self.secure_signer.issuing_address) if not spendables: error_message = 'No money to spend at address {}'.format( self.secure_signer.issuing_address) logging.error(error_message) raise InsufficientFundsError(error_message) cost = self.transaction_handler.estimate_cost_for_certificate_batch( ) current_total = 0 inputs = [] random.shuffle(spendables) for s in spendables: inputs.append(s) current_total += s.coin_value if current_total > cost: break tx = self.transaction_handler.create_transaction( inputs, op_return_value_bytes) hex_tx = hexlify(tx.serialize()) logging.info('Unsigned hextx=%s', hex_tx) prepared_tx = tx_utils.prepare_tx_for_signing(hex_tx, inputs) with FinalizableSigner(self.secure_signer) as signer: signed_tx = signer.sign_transaction(prepared_tx) # log the actual byte count tx_byte_count = tx_utils.get_byte_count(signed_tx) logging.info('The actual transaction size is %d bytes', tx_byte_count) signed_hextx = signed_tx.as_hex() logging.info('Signed hextx=%s', signed_hextx) # verify transaction before broadcasting tx_utils.verify_transaction(signed_hextx, op_return_value) # send tx and persist txid tx_id = self.connector.broadcast_tx(signed_tx) logging.info('Broadcast transaction with txid %s', tx_id) return tx_id except BroadcastError: logging.warning( 'Failed broadcast reattempts. Trying to recreate transaction. This is attempt number %d', attempt_number) logging.error( 'All attempts to broadcast failed. Try rerunning issuer.') raise BroadcastError( 'All attempts to broadcast failed. Try rerunning issuer.')
def bitcoind_broadcast(hextx): tx = CTransaction.deserialize(unhexlify(hextx)) txid = bitcoin.rpc.Proxy().sendrawtransaction(tx) return hexlify(txid)