The average person would not have the knowledge, but what is the mechanism that stops a programmer capable of writing a wallet, from modifying the local bitcoin file/data to be, for example, 10 bitcoins instead of 1? Or maybe it should be - what prevents modification and then use of the data.


Bitcoin aren't stored on a machine, but they are held in a distributed public ledger known as the blockchain. The blockchain is a database which is replicated over the machines of everybody who participates by running the bitcoin software. This means that the database you hold locally on your machine is just a copy of the one which the network as a whole agrees is the correct and undisputed ledger.

The process by which all participants agree as to which chain is the true valid chain is called mining, which uses a Proof-of-Work mechanism to enable one participant to commit a new block to the chain approximately every 10 minutes. The process by which a participant is selected is pseudo-random and probabilistic, based on the amount of work they do, which generally corresponds to the amount of electricity they irreversibly consume to perform computation on the current tip of the chain.

The cost of electricity to perform work is one of the game theoretic elements of Bitcoin which keeps it secure, because it means that any participant who attempts to adjust the ledger to favor themselves will have to spend money and consume electricity, but their chance of success is extremely small, as they would need perform more than 50% of the total global work being done on the current tip of the blockchain in order to keep ahead of the honest majority. The majority of participants are honest because it can be profitable to be honest, but being dishonest is a loss-maker unless you are certain to have more than 50% of the global work being performed on Bitcoin.

The chain is secured by cryptographic hashes, so that any attempted change to any part of the ledger produces different hashes. Each block in the chain explicitly refers to the previous block header's hash, and this value is also included in the current block header. This means every block commits to the entire blockchain history, and any attempt to rewrite part of the ledger would require rewriting the entire history of the ledger from the point of modification. Rewriting the history would therefore require an amount of work in excess of the total amount of work which was performed between the block which was attempted to be modified and the current tip of the chain.

The ledger itself is a linear sequence of blocks which each contain a set of transactions, where one of the transactions in the set is a coinbase, which collects the fees of other transactions in the block, and which releases some previously unused bitcoin as part of a predetermined release schedule. Currently around 35/40 of all bitcoin have been released and the remaining 5/40 will be released gradually over the next 120 years.

The other transactions in a block are regular transactions which have inputs and outputs. The inputs of a transaction refer to the output of a previous transaction. The outputs contain a locking script, which is a set of preconditions which must be met in order for that output to become spent. Once a transaction output has been spent, it may not be spent again. This means that there exists at any moment, a set of unspent transaction outputs (UTXOs) in the ledger which are where all of the bitcoin in existence are "held".

The relationship to a wallet is where this locking script comes in. For all regular transactions, one of the preconditions to unlock the output and spend it is to provide a digital signature matching a public key which is stored in the output locking script. Only the person who has the private key corresponding to the public key is capable of producing such signature, and the message they sign is a hash of the transaction which they are attempting to spend - which ensures that nobody else can manipulate this transaction because it would invalidate the signature.

So a wallet is fundamentally a set of private keys which are used to unlock some bitcoin held in some UTXOs on the distributed public ledger. A wallet will compare the set of corresponding public keys against the entire UTXO set of the public ledger to determine which bitcoins are essentially "owned" by this wallet. The amount of bitcoin in set of UTXOs which the user can unlock correponds to their balance.

A regular bitcoin software will obtain a copy of the entire ledger and will listen for new blocks being added by other nodes - who broadcast them over a peer-to-peer network. Multiple distinct connections are used to ensure that peers do not present false or outdated information. Each time a new block is created, approximately one every 10 minutes, the wallet will download the block and look through its set of transactions for any which the wallet has the relevant private keys to unlock. Any which it can unlock are added to the balance, and this becomes newly received bitcoin.

To spend bitcoin, a user must select one or more UTXOs which they have the private key to unlock. They create a new transaction referring to these UTXOs as inputs, and they create a set of outputs which contain new locking scripts - usually by including the public key hash of the person they wish to pay, which comes from the bitcoin address which they use for that payment. The bitcoin amounts in the outputs of a transaction must be less than or equal to the amounts of the input UTXOs referred to in the transaction, with any difference being paid as the fee for the transaction. The user will then sign this transaction and broadcast it over the peer-to-peer network.

A transaction must be valid for any other node in the p2p network to accept it. This means that the participant who receives a transaction will compare the accuracy of the UTXO set against their own copy of the ledger. If you modify anything in your local copy of the ledger, it will not produce changes to the copy anybody else has. If you attempted to spend some amount that other participants do not agree that you own, then your transaction will be invalid and rejected by the network. It will never be included in a block, and the intended recipient will not receive anything.

In other words - any attempt to modify data locally can only attempt to fool yourself, but it can't fool others. Nobody accepts anything you say as truth - they verify it for themselves by verifying the historical accuracy of the ledger.

|improve this answer|||||

Mark’s answer is great and extremely comprehensive, but I think it’s worth adding a shorter version as well:

You are correct that anyone can modify the code of their local bitcoin wallet/node. If you wanted to edit wallet files to display a balance of 1 million BTC in your screen, you could do so.

However, the problem is that every single node on the network independently verifies the state of the network, based on strict rules about how coins are created, and who can spend them.

So, quite simply: every other node on the network would reject your transactions, since they would be obviously invalid according to the rules that all other nodes are following.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.