def _send_op_return_txt(self, cmd): tx = make_op_return_tx(cmd, self.private_key, self.bitcoind_client, fee=self.fee, format='bin') broadcast_transaction(tx, self.bitcoind_client)
def broadcast(namespace_id, private_key, blockchain_client, testset=False, tx_only=False, blockchain_broadcaster=None): if blockchain_broadcaster is None: blockchain_broadcaster = blockchain_client nulldata = build(namespace_id, testset=testset) # get inputs and from address private_key_obj, from_address, inputs = analyze_private_key( private_key, blockchain_client) # OP_RETURN outputs outputs = make_op_return_outputs(nulldata, inputs, from_address, fee=DEFAULT_OP_RETURN_FEE, format='hex') if tx_only: unsigned_tx = serialize_transaction(inputs, outputs) return {'unsigned_tx': signed_tx} else: signed_tx = tx_serialize_and_sign(inputs, outputs, private_key_obj) response = broadcast_transaction(signed_tx, blockchain_broadcaster) response.update({'data': nulldata}) return response
def broadcast_tx(tx_hex, config_path=CONFIG_PATH, tx_broadcaster=None): """ Send a signed transaction to the blockchain """ if tx_broadcaster is None: tx_broadcaster = get_tx_broadcaster(config_path=config_path) if os.environ.get("BLOCKSTACK_TEST") == "1": log.debug("Send %s" % tx_hex) resp = {} try: resp = broadcast_transaction(tx_hex, tx_broadcaster) if 'tx_hash' not in resp or 'error' in resp: log.error("Failed to send %s" % tx_hex) resp['error'] = 'Failed to broadcast transaction: %s' % tx_hex return resp except Exception, e: log.exception(e) resp['error'] = 'Failed to broadcast transaction: %s' % tx_hex if os.environ.get("BLOCKSTACK_TEST") == "1": # should NEVER happen in test mode log.error("FATAL: failed to send transaction:\n%s" % simplejson.dumps(resp, indent=4, sort_keys=True)) sys.exit(1)
def broadcast(name, private_key, blockchain_client, testset=False, blockchain_broadcaster=None, user_public_key=None, tx_only=False): # sanity check pay_fee = True if user_public_key is not None: pay_fee = False tx_only = True if user_public_key is None and private_key is None: raise Exception("Missing both public and private key") if not tx_only and private_key is None: raise Exception("Need private key for broadcasting") if blockchain_broadcaster is None: blockchain_broadcaster = blockchain_client from_address = None inputs = None private_key_obj = None if user_public_key is not None: # subsidizing pubk = BitcoinPublicKey(user_public_key) from_address = pubk.address() inputs = get_unspents(from_address, blockchain_client) elif private_key is not None: # ordering directly pubk = BitcoinPrivateKey(private_key).public_key() public_key = pubk.to_hex() private_key_obj, from_address, inputs = analyze_private_key( private_key, blockchain_client) nulldata = build(name, testset=testset) outputs = make_outputs(nulldata, inputs, from_address, pay_fee=pay_fee) if tx_only: unsigned_tx = serialize_transaction(inputs, outputs) return {'unsigned_tx': unsigned_tx} else: signed_tx = tx_serialize_and_sign(inputs, outputs, private_key_obj) response = broadcast_transaction(signed_tx, blockchain_broadcaster) response.update({'data': nulldata}) return response
def broadcast(message_hash, private_key, blockchain_client, testset=False, blockchain_broadcaster=None, user_public_key=None, tx_only=False): # sanity check pay_fee = True if user_public_key is not None: pay_fee = False tx_only = True if user_public_key is None and private_key is None: raise Exception("Missing both public and private key") if not tx_only and private_key is None: raise Exception("Need private key for broadcasting") if len(message_hash) != 40: raise Exception("Invalid message hash: not 20 bytes") if not is_hex( message_hash ): raise Exception("Invalid message hash: not hex") if blockchain_broadcaster is None: blockchain_broadcaster = blockchain_client from_address = None inputs = None private_key_obj = None if user_public_key is not None: # subsidizing pubk = BitcoinPublicKey( user_public_key ) from_address = pubk.address() inputs = get_unspents( from_address, blockchain_client ) elif private_key is not None: # ordering directly pubk = BitcoinPrivateKey( private_key ).public_key() public_key = pubk.to_hex() private_key_obj, from_address, inputs = analyze_private_key(private_key, blockchain_client) nulldata = build(message_hash, testset=testset) outputs = make_outputs( nulldata, inputs, from_address, pay_fee=pay_fee ) if tx_only: unsigned_tx = serialize_transaction( inputs, outputs ) return {'unsigned_tx': unsigned_tx} else: signed_tx = tx_serialize_and_sign( inputs, outputs, private_key_obj ) response = broadcast_transaction( signed_tx, blockchain_broadcaster ) response.update({'data': nulldata}) return response
def broadcast(name, data_hash, consensus_hash, private_key, blockchain_client, blockchain_broadcaster=None, tx_only=False, user_public_key=None, testset=False): """ Write a name update into the blockchain. Returns a JSON object with 'data' set to the nulldata and 'transaction_hash' set to the transaction hash on success. """ # sanity check pay_fee = True if user_public_key is not None: pay_fee = False tx_only = True if user_public_key is None and private_key is None: raise Exception("Missing both public and private key") if not tx_only and private_key is None: raise Exception("Need private key for broadcasting") if blockchain_broadcaster is None: blockchain_broadcaster = blockchain_client from_address = None inputs = None private_key_obj = None if user_public_key is not None: # subsidizing pubk = BitcoinPublicKey( user_public_key ) from_address = pubk.address() # get inputs from utxo provider inputs = get_unspents( from_address, blockchain_client ) elif private_key is not None: # ordering directly pubk = BitcoinPrivateKey( private_key ).public_key() public_key = pubk.to_hex() # get inputs and from address using private key private_key_obj, from_address, inputs = analyze_private_key(private_key, blockchain_client) nulldata = build(name, consensus_hash, data_hash=data_hash, testset=testset) outputs = make_outputs( nulldata, inputs, from_address, pay_fee=pay_fee ) if tx_only: unsigned_tx = serialize_transaction( inputs, outputs ) return {'unsigned_tx': unsigned_tx} else: signed_tx = tx_serialize_and_sign( inputs, outputs, private_key_obj ) response = broadcast_transaction( signed_tx, blockchain_broadcaster ) response.update({'data': nulldata}) return response
def broadcast_tx( tx_hex, config_path=CONFIG_PATH, tx_broadcaster=None ): """ Send a signed transaction to the blockchain """ if tx_broadcaster is None: tx_broadcaster = get_tx_broadcaster( config_path=config_path ) resp = broadcast_transaction( tx_hex, tx_broadcaster ) if 'tx_hash' not in resp: resp['error'] = 'Failed to broadcast transaction: %s' % tx_hex # for compatibility resp['transaction_hash'] = resp['tx_hash'] del resp['tx_hash'] return resp
def broadcast(name, private_key, blockchain_client, testset=False, blockchain_broadcaster=None, pay_fee=True, public_key=None, tx_only=False): # sanity check if public_key is None and private_key is None: raise Exception("Missing both public and private key") if not tx_only and private_key is None: raise Exception("Need private key for broadcasting") if blockchain_broadcaster is None: blockchain_broadcaster = blockchain_client from_address = None inputs = None private_key_obj = None if private_key is not None: # ordering directly pubk = BitcoinPrivateKey( private_key ).public_key() public_key = pubk.to_hex() # get inputs and from address using private key private_key_obj, from_address, inputs = analyze_private_key(private_key, blockchain_client) elif public_key is not None: # subsidizing pubk = BitcoinPublicKey( public_key ) from_address = pubk.address() # get inputs from utxo provider inputs = get_unspents( from_address, blockchain_client ) nulldata = build(name, testset=testset) outputs = make_op_return_outputs( nulldata, inputs, from_address, fee=DEFAULT_OP_RETURN_FEE, format='hex' ) if tx_only: unsigned_tx = serialize_transaction( inputs, outputs ) return {'unsigned_tx': unsigned_tx} else: signed_tx = tx_serialize_and_sign( inputs, outputs, private_key_obj ) response = broadcast_transaction( signed_tx, blockchain_broadcaster, format='hex') response.update({'data': nulldata}) return response
def broadcast_tx(tx_hex, config_path=CONFIG_PATH, tx_broadcaster=None): """ Send a signed transaction to the blockchain """ if tx_broadcaster is None: tx_broadcaster = get_tx_broadcaster(config_path=config_path) if BLOCKSTACK_TEST is not None: log.debug('Send {}'.format(tx_hex)) resp = {} try: if BLOCKSTACK_DRY_RUN: resp = { 'tx': tx_hex, 'transaction_hash': virtualchain.tx_get_hash(tx_hex), 'status': True } return resp else: resp = broadcast_transaction(tx_hex, tx_broadcaster) if 'tx_hash' not in resp or 'error' in resp: log.error('Failed to send {}'.format(tx_hex)) resp['error'] = 'Failed to broadcast transaction: {}'.format( tx_hex) return resp except Exception as e: log.exception(e) resp['error'] = 'Failed to broadcast transaction: {}'.format(tx_hex) if BLOCKSTACK_TEST is not None: # should NEVER happen in test mode msg = 'FATAL: failed to send transaction:\n{}' log.error(msg.format(json.dumps(resp, indent=4, sort_keys=True))) os.abort() # for compatibility resp['status'] = True resp['transaction_hash'] = resp.pop('tx_hash') return resp
def broadcast( namespace_id, private_key, blockchain_client, testset=False, tx_only=False, blockchain_broadcaster=None ): if blockchain_broadcaster is None: blockchain_broadcaster = blockchain_client nulldata = build( namespace_id, testset=testset ) # get inputs and from address private_key_obj, from_address, inputs = analyze_private_key(private_key, blockchain_client) # OP_RETURN outputs outputs = make_op_return_outputs( nulldata, inputs, from_address, fee=DEFAULT_OP_RETURN_FEE, format='hex' ) if tx_only: unsigned_tx = serialize_transaction( inputs, outputs ) return {'unsigned_tx': signed_tx} else: signed_tx = tx_serialize_and_sign( inputs, outputs, private_key_obj ) response = broadcast_transaction( signed_tx, blockchain_broadcaster ) response.update({'data': nulldata}) return response
def broadcast(name, data_hash, consensus_hash, private_key, blockchain_client, blockchain_broadcaster=None, tx_only=False, user_public_key=None, testset=False): """ Write a name update into the blockchain. Returns a JSON object with 'data' set to the nulldata and 'transaction_hash' set to the transaction hash on success. """ # sanity check pay_fee = True if user_public_key is not None: pay_fee = False tx_only = True if user_public_key is None and private_key is None: raise Exception("Missing both public and private key") if not tx_only and private_key is None: raise Exception("Need private key for broadcasting") if blockchain_broadcaster is None: blockchain_broadcaster = blockchain_client from_address = None inputs = None private_key_obj = None if user_public_key is not None: # subsidizing pubk = BitcoinPublicKey(user_public_key) from_address = pubk.address() # get inputs from utxo provider inputs = get_unspents(from_address, blockchain_client) elif private_key is not None: # ordering directly pubk = BitcoinPrivateKey(private_key).public_key() public_key = pubk.to_hex() # get inputs and from address using private key private_key_obj, from_address, inputs = analyze_private_key( private_key, blockchain_client) nulldata = build(name, consensus_hash, data_hash=data_hash, testset=testset) outputs = make_outputs(nulldata, inputs, from_address, pay_fee=pay_fee) if tx_only: unsigned_tx = serialize_transaction(inputs, outputs) return {'unsigned_tx': unsigned_tx} else: signed_tx = tx_serialize_and_sign(inputs, outputs, private_key_obj) response = broadcast_transaction(signed_tx, blockchain_broadcaster) response.update({'data': nulldata}) return response
from pybitcoin import BitcoinPrivateKey, BitcoindClient, broadcast_transaction from pybitcoin import make_op_return_tx version_byte_private = 111 """ Test Network requires a diffrent version byte!!!!!!!! """ class BitcoinTestnetPrivateKey(BitcoinPrivateKey): _pubkeyhash_version_byte = version_byte_private private_key_hex = 'cSi82vbDR5NQUJ2eB55C4oz1LxEsA6NePmXM7zNGNSXYbSt6Aop1' msg = "Lubu is great2" private_key = BitcoinTestnetPrivateKey(private_key_hex) print private_key.to_wif() print private_key.to_hex() print private_key.public_key().address() client = BitcoindClient("talos", "talos", port=18332, version_byte=111) tx = make_op_return_tx(msg, private_key, client, fee=10000, format='bin') broadcast_transaction(tx, client)
'error': 'Name register can only use a single private key with a P2PKH script' } try: signed_tx = name_register_tx(name, payment_privkey_info, consensus_hash, payment_address, utxo_client) except ValueError, ve: log.exception(ve) log.error("Failed to create name register tx") return {'error': 'Failed to create name register tx'} resp = {} try: resp = broadcast_transaction(signed_tx, tx_broadcaster) except Exception, e: log.exception(e) log.error("Failed to sign and broadcast tx") return { 'error': 'Failed to sign and broadcast namespace preorder transaction' } return resp def do_name_update(name, data_hash, payment_privkey_info, tx_broadcaster): owner_address = get_privkey_info_address(payment_privkey_info) # Check ownership db = get_default_db_inst()
def broadcast_transaction( tx_hex ): """ Send out a raw transaction to the mock framework. """ global utxo_client return pybitcoin.broadcast_transaction( tx_hex, utxo_client )
def broadcast_transaction(tx_hex): """ Send out a raw transaction to the mock framework. """ global utxo_client return pybitcoin.broadcast_transaction(tx_hex, utxo_client)
def write_data(client, data, private_key): tx = make_op_return_tx(data, private_key, client, fee=10000, format='bin') broadcast_transaction(tx, client)