How does Bitcoin verify that a user owns the bitcoins he/she spent? I'm making my own blockchain based currency for a school project and kind of hit a roadblock. I have a wallet class which initializes with zero coins. What prevents the user from just sending a transaction, if he doesn't own the spent coins. Bitcoin is anonymous, so it doesn't save the balance of each account, right? So you could just reprogram a wallet to not subtract the spent amount from your balance? How does Bitcoin prevent this? Thanks in advance.
Bitcoin doesn't really care about balances, accounts, people or ownership. It doesn't need to know about any of those things.
Read the above sentence again. It is true. It has many important implications.
Bitcoin is cash, not an account. There's no system that tells a pizza storekeeper how many $10 bills (banknotes) you have under your mattress. The pizza storekeeper doesn't care. They only care that the $10 bill you hand them is not fake.
They don't need your ID. They don't need your name. They don't care whether you don't have a bank account. They don't ask you who owns that $10 bill, because they don't care who paid for the pizza - only that it gets paid for at all.
Don't let the terminology confuse you. The bitcoin blockchain isn't a ledger that keeps track of balances. It is a transaction journal that records only half of a real-life transaction - the half where A gives B some Bitcoin money, not the half where B gives A a pizza.
The transaction has inputs and outputs. The inputs are &quot;coins&quot; that A is giving up and the outputs are typically a new &quot;coin&quot; that B is getting and a new &quot;coin&quot; that A gets back as change (since Pizzas rarely cost exactly $10).
A Bitcoin &quot;coin&quot; is really just an output amount from an earlier transaction that has not yet been spent in a subsequent transaction. It is an Unspent Transaction Output (a UTXO).
Bitcoin nodes just keep track of UTXOs. When a UTXO is spent, the U in its name no longer applies. Every node observing the spend removes that TXO from its list of UTXOs. So every node seeing a second spend of a TXO would know that TXO is now not a UTXO and reject that second spend as invalid.
If a wallet reports a false balance to its owner, no one is harmed but the owner. A fake balance does not enable you to spend money you don't have. Its like a leather wallet that has &quot;$30&quot; written on the outside but which only contains two $10 bills.
That's the difference between bank accounts and cash.