3

I'm using bitcoind and would like to understand how to create transactions that send inputs from many specifically selected addresses (from my wallet) to one destination address. I want to do manually what the sendtoaddress command does automatically.

If this operation requires me to create a raw transaction could you please provide a working example? I already read bitcoin.it:raw_transactions, but it is hard to understand without an example and unfortunately the wiki article doesn't provide any.

I would appreciate help greatly.

5

3 Answers 3

3

When you have a bitcoin balance, that balance is actually unspent outputs from transactions your addresses have already received.

So when you want to send a raw transaction, what you include in that is the transactions and unspent outputs from those transactions... these can be from any address in your wallet.

The basic idea is;

createrawtransaction signrawtransaction sendrawtransaction

If you google around, you'll find plenty of examples of their use.

1
  • Coinbin seem to require internet/blockchain access in order to create a raw transaction. Is this true with all methods of creating raw transactions? May 29, 2015 at 21:51
0
If this operation requires me to create a raw transaction, here's a working example assuming you have UTXOs:

UTXO 1: "txid1", Index 0, Amount 0.01 BTC
UTXO 2: "txid2", Index 1, Amount 0.02 BTC
Destination: "your_destination_address"

# Calculate transaction fee estimate (adjust the fee rate as needed)
TRANSACTION_FEE=$(bitcoin-cli estimatesmartfee 6 "CONSERVATIVE")

# Calculate total input amount from UTXOs
TOTAL_INPUT=0.03

# Calculate the desired output amount
OUTPUT_AMOUNT=0.02

# Calculate the change amount
CHANGE_AMOUNT=$(echo "$TOTAL_INPUT - $OUTPUT_AMOUNT - $TRANSACTION_FEE" | bc)

# Create the raw transaction with change output
RAW_TRANSACTION_HEX=$(bitcoin-cli createrawtransaction '[
   {"txid":"txid1", "vout":0},
   {"txid":"txid2", "vout":1}
]' '{
   "your_destination_address": '$OUTPUT_AMOUNT',
   "your_change_address": '$CHANGE_AMOUNT'
}')

# Sign the raw transaction
SIGNED_TRANSACTION_HEX=$(bitcoin-cli signrawtransactionwithwallet "$RAW_TRANSACTION_HEX")

# Broadcast the transaction
bitcoin-cli sendrawtransaction "$SIGNED_TRANSACTION_HEX"

Please correct me if I was wrong.

1
  • This might work, because estimatesmartfee returns a feerate in sat/kvB, and your transaction likely is less than 1 kvB. You are however deducting a feerate from an amount, and for a larger transaction that might be too little to achieve a confirmation. Why did you post a second answer instead of editing your first?
    – Murch
    Sep 12 at 20:34
-2

Assuming you have UTXOs:

  • UTXO 1: "txid1", Index 0, Amount 0.01 BTC
  • UTXO 2: "txid2", Index 1, Amount 0.02 BTC
  • Destination: "your_destination_address"
# Create raw transaction
raw_tx=$(bitcoin-cli createrawtransaction '[{"txid":"txid1","vout":0}, {"txid":"txid2","vout":1}]' '{"your_destination_address": 0.03}')

# Sign raw transaction
signed_tx=$(bitcoin-cli signrawtransactionwithwallet "$raw_tx")

# Broadcast signed transaction
bitcoin-cli sendrawtransaction "${signed_tx["hex"]}"

Replace "your_destination_address" with your actual destination address. Run these commands in your terminal with your Bitcoin Core node running.

Maybe test on Bitcoin testnet before using real funds.

1
  • 1
    Hi Mani, this is on the way to be a much better answer than the one from ten years ago, but could you please expand on two issues: 1) createrawtransaction does not automatically create a change output. Could you please include a warning to that effect and add some instructions on how to send change back to yourself. 2) The transaction in your example does not pay any fees, so it will not propagate on the network or get mined. Do you have an idea how to fix that? :)
    – Murch
    Aug 28 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.