Technical Payments Process Walkthrough
-
Is there a document somewhere that describes the steps required to be executed in order to process and add a payment/transaction to the block chain?
For example. . .if one didn’t know any better, they would think that a partial payment from a source address would leave the remaining balance in the originating source address, but in reality, a new address is created and the balance transferred to that address leaving a zero balance in the source.
Is there something that describes this (and any other little gotchas along the way) for someone trying to create a new means of making payments?
Thanks in advance.
-
There’s plenty of wiki pages on Transactions.
But I can give you a crash course if you like.
Transactions must spend the entire output. ALWAYS. Without fail. Any unspent portion is given to the miners.
There’s no law saying change has to go to a new address. You can always send it back to the originating address, it will just form a new input that you can spend later.
The steps to create a transaction are as follows:
Identify the transactions that contain the outputs you want to spend. This is actually a critical step: You MUST spend outputs in an existing transaction. The address alone isn’t sufficient, you also need the txid, and the ‘vout’, which is the numerical index of the output within the transaction that you want to spend. This forms your list of inputs.
Generate your outputs. This is a list of addresses, and amounts to send to. If the amount of your inputs is greater than the amount of your outputs, you’ll need to generate an additional output for the change. This can be one of your input addresses if you like, or a new address.
Sign the list of inputs. For every input, you must generate a signature proving you own the private key, and sign it using the address, TXid, and vout, and all the outputs. This makes the signature unique for this spend, so it can’t be reused for another spend, because the TXid would be different.
Broadcast the tx to the network.
Now there’s more exotic transactions, like pay-to-hash transactions, or multi-sig transactions, but that’s beyond the scope of this post.
Take a look at this code I wrote:
[code]
addresses = […] #the addresses to send to
outs = {} #list of outs, address:value style
for x in addresses
outs[ x ] = 0.00000001 #send 1 satoshi to each address
client.listUnspent 0, (err, unspent) -> #get a list of unspent outputs
useable = undefined
for tx in unspent
useable = tx if parseFloat(tx.amount.toFixed(8)) >= total && (!useable? || parseFloat(useable.amount.toFixed(8)) > parseFloat(tx.amount.toFixed(8))) #find a usable output that we can spend that’s got enough
return callback(“No unspent”) unless useable?
outs[useable.address] = useable.amount - total if useable.amount > total #our change goes back to the output we’re spending
client.createRawTransaction [useable], outs, (err, rawtx)-> #create the raw transaction
client.signRawTransaction rawtx, [useable], (error, decoded) -> #sign it
client.sendRawTransaction decoded.hex, (error, result) -> #send it
callback(result) if callback?
[/code] -
Thanks Kev!
Your reply along with the following article helps out a lot.
[url=http://bitcoinmagazine.com/9249/developers-introduction-bitcoin/]http://bitcoinmagazine.com/9249/developers-introduction-bitcoin/[/url]
Going to try and tool with the c# library bitcoinsharp some to see what I can reuse/leverage for FTC. Library looks to be a couple of years old, but it may help.
-
Quick follow-up question (and sorry to resurrect an old thread. . .just seemed more appropriate to the question).
Is there any way to configure the QT Wallet application to stop creating new addresses for “change” and instead just reroute the change to the original wallet (or even a predefined one)? I hate the fact I can’t see the new addresses it creates in my address book. Doing all the debug window stuff is a pain!
Thanks in advance.
-
[quote name=“Mathco” post=“58738” timestamp=“1392229099”]
Quick follow-up question (and sorry to resurrect an old thread. . .just seemed more appropriate to the question).Is there any way to configure the QT Wallet application to stop creating new addresses for “change” and instead just reroute the change to the original wallet (or even a predefined one)? I hate the fact I can’t see the new addresses it creates in my address book. Doing all the debug window stuff is a pain!
Thanks in advance.
[/quote]What you’re describing is called ‘Coin Control’, and it’s available in a few wallets. But the problem is that if you do this by default, you destroy anonymity by default because all transactions suddenly become linked together. When you send to a change address, you’re creating deniability: “Yes, that was my address, but I sent it on. See? They’re not at that address any more.” so future transactions don’t necessarily become tied to past transactions. If you do that by default, all your transactions would be from one address, linking your transactions together.
It’s also ill-defined, because transactions can have multiple inputs.
The correct compromise is HD wallets, because all change addresses are generated from the master address, so the client can recover change even in the case of lost change addresses.
-
Got it, and I understand the desire for deniability.
I guess my bigger complaint then is the fact it is all “hidden” (“transparent” is probably the marketing term for it ;-) from the user who owns the wallet. No way to know what key-pairs are storing your coins unless you go fiddling with the chain and debug window. Doesn’t seem very user friendly for more advanced users.