Author

Topic: Createrawtransaction: How does this actually work? (Read 1197 times)

donator
Activity: 1218
Merit: 1079
Gerald Davis
No just going from memory (probably not a good idea).  I use the bitcoind API everyday just not manually.
legendary
Activity: 3472
Merit: 4794
One final question: How do I tell the "index" of my address PubKey from any given txn? Only way is to just decoderawtransaction always?

decoderawtransaction is one option.  If the keys for the tx are in your wallet another option is listunspentoutputs which returns a list of all the unspent outputs for which your wallet has the keys necessary for signing.  The wallet is already checking in realtime all blocks and transactions for outputs which it can spend, the listunspentoutputs returns that list.

Been a while since you worked with raw transactions in the reference client?

The command you're looking for is:

listunspent

(not listunspentoutputs)
donator
Activity: 1218
Merit: 1079
Gerald Davis
One final question: How do I tell the "index" of my address PubKey from any given txn? Only way is to just decoderawtransaction always?

decoderawtransaction is one option.  If the keys for the tx are in your wallet another option is listunspentoutputs which returns a list of all the unspent outputs for which your wallet has the keys necessary for signing.  The wallet is already checking in realtime all blocks and transactions for outputs which it can spend, the listunspentoutputs returns that list.
cp1
hero member
Activity: 616
Merit: 500
Stop using branwallets
It's the n value, you need some way of reading the raw transaction, such as you suggested.
sr. member
Activity: 448
Merit: 250
Ok thanks.

Let me repeat what I've learned, to make sure I know what I'm doing, and also for posterity in case anyone else has this question.

createrawtransaction command looks like this:

bitcoind createrawtransaction [\{\"txid\":\"sometxid\",\"vout\":index\}, ...] {\"receivingaddr1\":amount, ...}

obviously you can put multiple inputs and outputs to each txn.

The first list represents the source of your expenditures. The sometxid represents a transaction where you received BTC, while index represents the index of the receiving address from that transaction which you're sending from. The balance of the sending addr doesn't matter, just the total amnt it received from the txns with the txids you selected, so you might need to put multiple txs from one address as the inputs for any given txn.

And if the inputs are greater than the outputs, that amount is the transaction fee.

One final question: How do I tell the "index" of my address from any given txn? Only way is to just decoderawtransaction always?
legendary
Activity: 3472
Merit: 4794
Use testnet.

Let me repeat that for you...

USE TESTNET!

Learning rawtransactions is financially dangerous.  It is EXTREMELY easy to make a very small mistake and lose ALL of your bitcoins.

Do not try to learn rawtransactions on the bitcoin network.
hero member
Activity: 793
Merit: 1026
Every txid is basically just a unique hash generated from the components of the tx.  So a txid points to a transaction.  Then since a transaction can have multiple outputs, the vout points to which output in the tx to use.  Remember, bitcoins are always just chained together from being minted in a block then to address to address to address-- so when you are sending them, all you are doing is making a tx saying "hey i own this address with bitcoins and i'd like them to go to that address" and then you sign that message with your private key.  So what txid and vout do is give you an input.  A single vout from a tx is a singlular specific amount of bitcoins from a single address, and so that's that first part of the message saying "see here, this address has these bitcoins from this tx and they aren't spent yet", and then the rest of the tx is the explanation of how to spend them, and then the signature is from the private key of the address of that vout, as proof that you OWN the address that you are trying to spend from.  The txid and vout form a single input for the transaction.  And you can use multiple txid/vout combos to have more than one input so you have a total that's enough to spend for your transaction.
cp1
hero member
Activity: 616
Merit: 500
Stop using branwallets
Each {} is a list, so you can have as many inputs and outputs as you want.
donator
Activity: 1218
Merit: 1079
Gerald Davis
A txid alone doesn't represent an input.

An input is a reference to an unspent output of a previous transaction.  A transaction can have multiple outputs so a tx id alone is insufficient.   You can uniquely identify any output in the blockchain by a TXID (gets you to the tx in question) AND an index or vout which get identifies which output of the tx is being referenced.  

There is no such thing as balances.  Bitcoin works on inputs and outputs.  In your example you have two unspent outputs worth 0.5 BTC ea.  So you would have two inputs (each w/ a txid and vout).  The tx now has 1 BTC in input.  If you want to spend the whole 1 BTC then you would just have 1 BTC - desired fee in a single output.  If you wanted to spend less (say 0.8 BTC w/ 0.05 BTC fee) then you would have two outputs one for 0.8 BTC and one for 0.15 BTC ("change").  The difference between inputs and outputs is the fee sent to miners.  Don't get this part wrong it is a quick way to lose a small fortune.   Say you select for your input an output worth 50 BTC and your tx only has a single output worth 1 BTC.   How much did you just pay the miners in fees?  Yup 49 BTC.
legendary
Activity: 4494
Merit: 3178
Vile Vixen and Miss Bitcointalk 2021-2023
An input is one of the outputs of a transaction you have previously received. The output must not been previously spent (used as an input for another transaction) and you must have the private key(s) necessary to spend it. The listunspent command will give a list of transaction outputs which satisfy these criteria. A transaction can (and often does) have multiple inputs.

Each input specified will be spent completely. It is not possible to partially spend an input. If you wish to send an amount smaller than the inputs, you must create a change address and send the remainder there (as a second output).

The transaction fee is simply the difference between the sum of the transaction's inputs and the sum of the transaction's outputs. If you forget to make change, or calculate it incorrectly, you will pay an unexpectedly large transaction fee. I forget whether Bitcoin Core gives a warning when this happens; I think it just assumes anyone using raw transactions knows what they're doing.

Do not create raw transactions unless you know what you're doing.

EDIT: Typo
sr. member
Activity: 448
Merit: 250
I understand the createrawtransaction command looks like this:

bitcoind createrawtransaction [\{\"txid\":\"sometxid\",\"vout\":1\}, ...] {\"receivingaddr1\":amount, ...}

I understand that first bit represents inputs, and the second part represents the outputs. How does a TXID represent an input, though? What do I do, find the last txid sent to the addresses I want to send from? What if I got 2 transactions for 0.5 each, leaving my address with a balance of 1, and now I want to send that whole 1 BTC all at once. What do I do?

Also, how do I set the TX fee?
Jump to: