# How are public and private keys related to the wallet?

I understand that all the Bitcoin protocol is based on an asymmetric cryptography.

A user gets 2 keys (public and private). How do these keys connect to the Bitcoins?

How are the keys are related to the wallet? How can I decide how many Bitcoins to transfer when using my private key?

## 2 Answers

Thomas' answer is correct, but I think an easy version may be appreciated as well.

I suppose you know the concept of public key cryptography? If you don't, here is a very short explanation (or read the wikipedia page): Public key cryptography (as used in Bitcoin), allows you to hand people a public key and use the corresponding private key to prove the ownership. So you can create any random private key (keep it secret) and calculate the corresponding public key (give it out to public for verification). Using this private key, you can sign a message and other people can verify that you own the private key by using your public key.

So, applied to Bitcoin.

A Bitcoin address is just a shorthand notation for a public key. When someone makes a transaction to an address, he states that "I give the right to spend this money to the person who owns the private key corresponding to this address". The person who has received this transaction will in turn be able to spend the transaction by signing the transaction using his private key. With this signature he can prove that he owns the key, without disclosing it. Others can verify the signature using the public key.

As for the second part of your question, it gets a little more advanced. An easy way of viewing it is that every transaction takes a number of inputs and generates a number of outputs. Inputs in this case are outputs from previous transactions. For each input, the user will have to provide verification he is allowed to spend that output by adding a signature.

A simple Bitcoin wallet consists on one of more pairs of public and private keys (I'm saying simple here as some wallet structure allow for deterministic public key generations and private keys that can only spend part of the wallet, but the principle with public/private keys remains the same). The "link" to Bitcoin initially happens during block generation where a certain amount of Bitcoin is generated and sent to the miner's public address; this is merely a record in the blockchain. Then these coins can be sent to other public addresses using transactions.

The most simple transaction has one input and one output; i.e. it spends coins from one source address to one destination address. To be valid, the amount of output coins must not exceed the amount of input coins, and to be verified the output address hash is signed by the input address' private key.

In real world though, wallets balances become the sum of all inputs to that wallet, so when you make a transaction the software will select the desired number of inputs to get a sum equal of greater to the desired transaction output + fee. If the amount exceeds what you want to spend, the remainder can be send back to one of the input address or even a new address of your wallet, so you basically send yourself the exceeding amount of Bitcoins. Any unspent amount in the transaction is considered fee and is collected by the miner generating the block.

Transactions can also be much more complex; Bitcoin allows for a very powerful scripting system that can make transactions spendable in many ways. You can read more about bitcoin transactions and scripting system here:

https://en.bitcoin.it/wiki/Transactions

https://en.bitcoin.it/wiki/Script

When you send out a transaction, it is relayed to the network for inclusion in a block if it passes common rules for transactions (validity, fee, size, etc.). A miner will eventually pick your transaction and include it a block (the transactions are actually included in a merkle tree whose root hash is in in the block header - the block header hash is the proof of work that has to match the difficulty requirement for the block).