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.

Murch
  • 75,206
  • 34
  • 186
  • 622
Arsenius
  • 239
  • 1
  • 10
  • 1
    Please be careful, I have seen many people accidently mess up with raw transactions. Such as sending hundreads of BTC as a miners fee or the likes. – MaxSan Sep 16 '13 at 14:44
  • I will plan to test dipply with small amounts first. – Arsenius Sep 16 '13 at 15:28
  • There is also the "testnet" which is made for testing – RentFree Nov 17 '13 at 05:36
  • Look this, you have a example: https://bitcoin.stackexchange.com/questions/68820/createrawtransaction-and-fundrawtransaction –  Jan 17 '18 at 18:38
  • Please check rpc documentation before use it (https://developer.bitcoin.org/reference/rpc/). – Peter Lai Aug 30 '23 at 08:56

3 Answers3

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.

ManreeRist
  • 907
  • 2
  • 8
  • 12
  • Coinbin seem to require internet/blockchain access in order to create a raw transaction. Is this true with all methods of creating raw transactions? – Alex Millar May 29 '15 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.

Mani T
  • 187
  • 8
  • 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 '23 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.

Mani T
  • 187
  • 8
  • 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 '23 at 13:55