def create_stock_tx(user=PRIMARY, use_smallest_spendables=True, extra_payables=[]): address = get_address_for_key(user) spendables = spendables_for_address(address) if use_smallest_spendables: spendables = trim_spendables(spendables) tx = create_tx(spendables, [address] + extra_payables, fee=10000) return tx
def write_opreturn(bitcoin_address, bitcoin_private_key, raw_message, fee=5000, push=False): message = hexlify(raw_message.encode()).decode('utf8') spendables = spendables_for_address(bitcoin_address) spendables = [s for s in spendables] bitcoin_sum = sum([spendable.coin_value for spendable in spendables]) inputs = [spendable.tx_in() for spendable in spendables] outputs = [] if (bitcoin_sum > fee): change_output_script = standard_tx_out_script(bitcoin_address) print change_output_script outputs.append(TxOut(bitcoin_sum - fee, change_output_script)) ## Build the OP_RETURN output with our message op_return_output_script = script.tools.compile("OP_RETURN %s" % message) outputs.append(TxOut(0, op_return_output_script)) ## Create the transaction and sign it with the private key tx = Tx(version=1, txs_in=inputs, txs_out=outputs) tx.set_unspents(spendables) sign_tx(tx, wifs=[bitcoin_private_key]) print tx.as_hex() if not push: return tx.as_hex() else: pushtx(tx.as_hex()) else: print "INADEQUATE FUNDS"
def write_transfer(sender, sender_priv, recipient, message, fee=m.default_fee, avoid_inputs=[]): message = hexlify(message.encode()).decode('utf8') spendables = spendables_for_address(sender) spendables = [s for s in spendables if not spendable_to_legible(s.tx_in()) in avoid_inputs] bitcoin_sum = sum([spendable.coin_value for spendable in spendables]) inputs = [spendable.tx_in() for spendable in spendables] outputs = [] if bitcoin_sum > fee + m.dust*2: remaining = bitcoin_sum - fee - m.dust*2 dest_output_script = standard_tx_out_script(recipient) change_output_script = standard_tx_out_script(sender) btc_change_output_script = standard_tx_out_script(sender) op_return_output_script = script.tools.compile("OP_RETURN %s" % message) outputs.append(TxOut(m.dust, dest_output_script)) outputs.append(TxOut(m.dust, change_output_script)) outputs.append(TxOut(remaining, btc_change_output_script)) outputs.append(TxOut(0, op_return_output_script)) tx = Tx(version=1, txs_in=inputs, txs_out=outputs) tx.set_unspents(spendables) sign_tx(tx, wifs=[sender_priv]) print tx.as_hex() return tx.as_hex() else: print "INADEQUATE FUNDS"
def create_stock_tx(user=PRIMARY, use_smallest_spendables=True, extra_payables=[]): address = get_address_for_key(user) spendables = spendables_for_address(address) if use_smallest_spendables: spendables = trim_spendables(spendables) tx = create_tx(spendables, [address] + extra_payables, fee=10000) return tx
def send_op_return_tx(key, message, fee=10000): """ Send an transaction with an OP_RETURN output. Args: key: the Bitcoin Key to send the transaction from. message: the message to include in OP_RETURN. fee: the miner fee that should be paid in Satoshis. Returns: The broadcasted Tx. """ address = key.address() if len(message) > 80: raise ValueError("message must not be longer than 80 bytes") message = hexlify(message).decode() spendables = spendables_for_address(address) bitcoin_sum = sum(spendable.coin_value for spendable in spendables) if bitcoin_sum < fee: raise Exception("not enough bitcoin to cover fee") inputs = [spendable.tx_in() for spendable in spendables] outputs = [] if bitcoin_sum > fee: change_output_script = standard_tx_out_script(address) outputs.append(TxOut(bitcoin_sum - fee, change_output_script)) op_return_output_script = script.tools.compile('OP_RETURN %s' % message) outputs.append(TxOut(0, op_return_output_script)) tx = Tx(version=1, txs_in=inputs, txs_out=outputs) tx.set_unspents(spendables) sign_tx(tx, wifs=[key.wif()]) broadcast_tx(tx) return tx
bitcoin_address = "ADDRESS" bitcoin_private_key = "PRIVATE_KEY" ## The fee that will be given to the miner in bitcoin bitcoin_fee = 10000 # In satoshis ## Get the message if(len(argv) is not 2): exit("usage: python3 send-op-return.py \"MESSAGE\"") raw_message = argv[1] if(len(raw_message) > 80): exit("Message must be 80 characters or less") message = hexlify(raw_message.encode()).decode('utf8') ## Get the spendable outputs we are going to use to pay the fee spendables = spendables_for_address(bitcoin_address) bitcoin_sum = sum(spendable.coin_value for spendable in spendables) if(bitcoin_sum < bitcoin_fee): exit("Not enough satoshis to cover the fee. found: {sum} need: {fee}" .format(sum=bitcoin_sum,fee=bitcoin_fee)) ## Create the inputs we are going to use inputs = [spendable.tx_in() for spendable in spendables] ## If we will have change left over create an output to send it back outputs = [] if (bitcoin_sum > bitcoin_fee): change_output_script = standard_tx_out_script(bitcoin_address) outputs.append(TxOut(bitcoin_sum - bitcoin_fee, change_output_script)) ## Build the OP_RETURN output with our message