Yeah, I'm starting to no longer have fun doing this. The relationship between the inputs and outputs are about as clear as mud. Well, let me be clear, I get that each output needs an input, but hell if I know how to figure out which outputs refer to which inputs. Just a bit ago, I wanted to figure out how to convert the binary representation of a public-key to ASCII and even that it had a log of dependencies; even the 'no library dependency version' depended on some network thing called 'htonl' and on an encryption routine as well. That's a lot of code drug in just to convert some binary to ASCII.
They don't. That might be why you are confused.
The only relationship between inputs and outputs in a tx is is that the sum of the inputs must be equal to or greater than the sum of the outputs. The tx fee is the difference between the sum of the inputs and the sum of the outputs.
So some rules to get you started.
1) The inputs of all txs (except coinbase = generation) are the output of prior transactions.
2) There is a 1:1 relationship between the INPUT of a given transaction and the output of a PRIOR transaction.
3) In a given transaction there is no relationship between the inputs and outputs.
4) For a tx to be valid the sum of the inputs must be equal to or greater than the sum of the outputs.
5) The difference between inputs and outputs is the tx fee.
6) Since a tx can have n inputs and m outputs we refer to a specific output by both the tx hash AND the index.
but converting that raw date into paired input/output transactions seems to be a bit of a mystery to me so far.
It will forever remain a mystery no matter how many libraries you use until you shake that flawed model.
Lets look at a random transaction.
http://blockexplorer.com/tx/a6d9c176ecb041c2184327b8375981127f3632758a7a8e61b041343efc3bcb6eIn raw form
{
"hash":"a6d9c176ecb041c2184327b8375981127f3632758a7a8e61b041343efc3bcb6e",
"ver":1,
"vin_sz":1,
"vout_sz":2,
"lock_time":0,
"size":257,
"in":[
{
"prev_out":{
"hash":"b5045e7daad205d1a204b544414af74fe66b67052838851514146eae5423e325",
"n":0
},
"scriptSig":"304402200e3d4711092794574e9b2be11728cc7e44a63525613f75ebc71375f0a6dd080d02202ef 1123328b3ecddddb0bed77960adccac5bbe317dfb0ce149eeee76498c19b101 04a36b5d3b4caa05aec80752f2e2805e4401fbdbe21be1011dc60c358c5fc4d3bedd1e03161fb4b 3a021c3764da57fee0d73570f3570f1b3dd92a1b06aae968846"
}
],
"out":[
{
"value":"300.00000000",
"scriptPubKey":"OP_DUP OP_HASH160 0331e5256416bc11ecf9088091f8424819553a10 OP_EQUALVERIFY OP_CHECKSIG"
},
{
"value":"699.99950000",
"scriptPubKey":"OP_DUP OP_HASH160 4186719d739ae983d8c75a0cb82958e94b7ae81e OP_EQUALVERIFY OP_CHECKSIG"
}
]
}
Now the tx hash is a6d9c176ecb041c2184327b8375981127f3632758a7a8e61b041343efc3bcb6e
The # of inputs is 1 (vin_sz).
The # of outputs is 2 (vout_sz).
The single input of the transaction is referenced here:
"in":[
{
"prev_out":{
"hash":"b5045e7daad205d1a204b544414af74fe66b67052838851514146eae5423e325",
"n":0
},
"scriptSig":"304402200e3d4711092794574e9b2be11728cc7e44a63525613f75ebc71375f0a6dd080d02202ef 1123328b3ecddddb0bed77960adccac5bbe317dfb0ce149eeee76498c19b101 04a36b5d3b4caa05aec80752f2e2805e4401fbdbe21be1011dc60c358c5fc4d3bedd1e03161fb4b 3a021c3764da57fee0d73570f3570f1b3dd92a1b06aae968846"
}
The INPUT for this transaction is the OUTPUT of a PREVIOUS transaction.
Specifically it is the tx with hash b5045e7daad205d1a204b544414af74fe66b67052838851514146eae5423e325. However a tx can have multiple outputs so how do we know which one of those otuptuts this input refers to? Simple the tx output index is provided.
Inputs of transactions are outputs of prior transactions.In this case the input we are looking for is is specifically the output #0 of transaction hash b5045e7daad205d1a204b544414af74fe66b67052838851514146eae5423e325
So lets look at that transaction. The output #0 of this transaction is 1,000 BTC.
http://blockexplorer.com/tx/b5045e7daad205d1a204b544414af74fe66b67052838851514146eae5423e325So back to our current transaction (hash ending e325). There are only 1 input. The value of the transaction is the sum of the inputs. In this case a single input of 1,000 BTC.
Now looking at the output section we can see we the two outputs
"out":[
{
"value":"300.00000000",
"scriptPubKey":"OP_DUP OP_HASH160 0331e5256416bc11ecf9088091f8424819553a10 OP_EQUALVERIFY OP_CHECKSIG"
},
{
"value":"699.99950000",
"scriptPubKey":"OP_DUP OP_HASH160 4186719d739ae983d8c75a0cb82958e94b7ae81e OP_EQUALVERIFY OP_CHECKSIG"
}
Output#0 is 300 BTC and output #1 is 699.9995 BTC.
The sum of the outputs is 999.9995 BTC
The sum of the inputs is 1,000.0000 BTC
The tx fee to miners is the difference or 0.0005 BTC.
If you want to decode further which addresses a transaction is sent to well you will need more code.
Lets look at the second output.
OP_DUP OP_HASH160 4186719d739ae983d8c75a0cb82958e94b7ae81e OP_EQUALVERIFY OP_CHECKSIG
So what is 4186719d739ae983d8c75a0cb82958e94b7ae81e? It is the RIPEMD160 hash of the public key. However Bitcoin addresses include a checksum and are converted to Base58.
BTW the public key hash 4186719d739ae983d8c75a0cb82958e94b7ae81e is Bitcoin address 16yTynjmSe5bsRGykDaaCL5bm2pxiEfcqP.