Getting your head around the basic structure of Bitcoin and the way it works is best done by following a transaction through the network.
The key is to understand that a user’s balance is the total of Unspent Transactions Outputs – or UXTO’s – registered to their address or addresses.
The UXTO database is a list of all unspent transaction for all users and is kept on full nodes.
If a user receives some Bitcoin then this transaction is stored in the blockchain – i.e. when it was transferred, from whom and how much – and is also stored in the UXTO. A wallet aggregates all these unspent transaction belonging to the public keys and addresses, and hidden private keys, and then sums up a balance for the user.
A UXTO is indivisible when it is created. That is to say the Bitcoin protocol can only spend the full UXTO in a transaction – this is done for multiple reasons but primarily for security and computational efficiency. Put another way each transaction is its own “note” and can only be fully spent in each transaction.
This means that if the price of a particular item is 0.1BTC and you have one 0.2BTC UXTO and one 0.5 BTC UXTO then the transaction to pay will use the 0.5 BTC and spend 0.5BTC of the 0.2BTC generating 0.15BTC in change – or you could use the 0.2BTC and generate change of 0.1BTC. Generally most transactions will not be exactly equal to a UXTO you own and as such most transactions will generate change – this means the UXTO is continually expanding and is one of the threats to the system in terms of its size. Wallets that allow you to choose which transactions are said to give coin control – for most users the function of which transactions are used when is done under the hood.
Each Bitcoin transaction creates outputs (apart from OP_RETURN transactions – see meta coins for why). These UXTO’s are then available for the user to spend, but in order to do so they need to be unlocked and this is done via the signature or private key of the address. The term unlocking is simply a way of describing the process of proving you are the owner of those bitcoins and have the right to send them to another address. A more in depth view of this and all the mathematics can be seen in our elliptic curve guides and digital signature guides for Bitcoin and crypto currencies.
So in simple terms a transaction output sits in the UXTO pool and a transaction points to a transaction output in the UXTO and says to the network via mathematical proof “I have the right to spend this transaction output”.
Bitcoin uses two scripts to perform these processes – the scriptpubkey and the scriptsig. The scriptpubkey is the locking script that determines the conditions that need to be met in order for a transaction to be spent. The scriptsig function is the unlocking script that solves the conditions placed on the transaction by the scriptpubkey.