- snip -
And now I want to send 0.022 BTCs, so it will take these 4 previous transaction to reach the 0.022 I want to send, but the total of the 4 transactions is 0.02269937. Now you take that amount (0.02269937), you substrat 0.022 I want to send plus 0.0005 for the fees and the change is 0.00019937 which comes back to my wallet, on the same address or a different one according to the wallet I use. Tadaaa ?
You got it! Well explained.
Bitcoin doesn't actually store any "coins" anywhere. The blockchain that every full node has a copy of has a list of every confirmed transaction that has ever occurred. Each transaction is a reference to one or more "outputs" from previous transactions that are being used as "inputs" and one or more values that are being assigned to addresses as new "outputs". There is no way to indicate a fraction of a previous output. The transaction simply references the previous output, and anyone/everyone running the correct protocol recognizes this reference as being an indication that the entire output is being "spent".
The protocol checks to make sure that the total of the inputs is greater than or equal to the outputs (so you can't send more than you have). If the total of the outputs is less than the total of the inputs, that difference is what the miners take as fees when the transaction is added to a block. So if you don't want all that extra value to all be taken by a miner as fees, then you need to send some of it back to yourself as a new output (either to a new address or an existing one).
Using a new address for the change every time increases both anonymity and security a bit.
Improved security:There is currently no known way to determine someone's private key if you know their public key, but there is no guarantee that a weakness won't be discovered in the ECDSA algorithm at some time in the future. When you
receive bitcoins, you don't receive them at a public key, you receive them at a hash of a hash of the public key. The public key is first hashed with SHA-256, and that result is hashed with RIPEMD-160 to create a bitcoin address. Since there is currently no known way to "reverse" either of those hashes, there is no way to determine what the public key is from a bitcoin address. This means that serious weaknesses would have to be discovered almost simultaneously in ECDSA, SHA-256, and RIPEMD-160 for someone to be able to determine your private key from the bitcoin address that is publicly used in the output of a transaction and permanently stored in the blockchain.
When you send bitcoins, your wallet has to provide a signature for each of the previous outputs that are referenced as inputs. This signature is intended to prove that you know the private key associated with the address that the previous output was set to, and that you therefor have the right to re-assign that value to some other address. If that was all that was provided, then nobody could verify the signature since they don't know the public key. Therefore, as part of the requirements of the signature script in the bitcoin protocol, along with the signature your wallet also includes the public key associated with each address that inputs are associated with. This means that the first time that you send bitcoins that were received at an address, that address becomes slightly less secure. Since the public key is then permanently stored in the public blockchain, any other transactions that are received at that address are no longer protected by the SHA-256 and RIPEMD-160 hash. It would only require a weakness to be discovered in ECDSA to steal your bitcoins. If every output always goes to an address that has never previous been used, then you will never have bitcoins at an address that is not protected by SHA-256 and RIPEMD-160. This is why it is recommended to use a new address for every output you receive (regardless of whether it is "change" or otherwise).
Improved anonymity:Lets say Alfred sends me 10 bitcoins, and Bob sends me 20. If I used a new address for each of those transactions, then Alfred knows I have at least 10 BTC (since he sent them), and Bob knows I have at least 20 (send he sent them), but neither Alfred nor Bob can tell that I have 30 BTC no matter how hard they look at the blockchain.
Now lets say I'm going to send 13 bitcoins to someone.
If my wallet creates a transaction that spends those 30 BTC and sends the 17 BTC of "change" back to one of the two existing addresses, then depending on which address the wallet uses, either Alfred or Bob can tell that I now have 17 BTC, and they can tell what address I just sent 13 BTC to.
If my wallet creates a transaction that spends those 30 BTC and sends the 17 BTC of "change" to a brand new address, then it is impossible to tell if I have 13 BTC sent as change, or 17 BTC sent as change, or no change at all because the full amount was used to send BTC to two other people.