- snip -
What I'd like to ask about is the logical concept of a "wallet", that holds a collection of Bitcoins across these various products.
Is a wallet merely a collection of private/public key pairs that can be used to send/receive Bitcoins?
In general, yes. A wallet is simply a system of securely storing private keys. This is why a piece of paper with a private key on it is often called a "paper wallet", and a memorized private key is often called a "brain wallet". Most wallet
software also provides other services, such as:
- Scanning the blockchain for confirmed transactions
- Monitoring the Bitcoin network for relayed unconfirmed transactions
- Assisting you in creating transactions
- Assisting you in signing transactions
- Broadcasting your transactions to peers on the Bitcoin network
- Generating new private key (and the associated address) for you
- Recording a history of transactions that you've sent and received
- Maintaining a list of the unspent outputs that the wallet has the ability to spend
But it looks like you're most interested in discussing the private key storage aspect of the wallets.
How are these addresses generated?
That's up to the creator of the wallet. Most well written wallets will either generate every private keys completely randomly, or will generate a single random seed, and then use a formula to create a hierarchy of private keys from that seed.
Is there a maximum number of addresses that can be generated per wallet?
Addresses are simply the numeric result of a RIPEMD160 hash function that has a version number prepended to it and a checksum appended to it. That hash function is calculated on the result of a SHA256 hash of the public key. Assuming that you are only asking about version 1 addresses, there result of the RIPEMD160 hash is between 0 and 2
160. This puts an upper bound on the number of addresses at 1.462 X 10
48. Realistically, if the wallet software is going to give you information about the transactions that are sent to those addresses, then it needs to be able to scan through them all. As the number of addresses the wallet stores gets VERY large, the amount of time it takes for the wallet to find and send transactions gets VERY large. Eventually it becomes so slow that you would become frustrated trying to use it. How large that number is will depend a lot on how well written the software is.
How does the client know if an address is in my wallet?
Your software client reads a file that stores your private keys (and typically the RIPEMD160 hash associated with those keys). It is possible to re-generate the public key and hashes every time from the private keys, but it is faster to just store the RIPEMD160 hash along with the private key.
How is a HD wallet (used in Mycelium) different from an ordinary wallet?
Bitcoin Core generates every new private key completely randomly. There is no connection between the private keys at all. If you lose a private key that you haven't backed up somewhere, it is impossible to know what that key was.
HD wallets generate a single seed value completely randomly. Then every private key is calculated from that initial seed using a formula. As long as you have the seed somewhere, you can re-use the same formula to regenerate all the private keys that the wallet was using. If you lose access to the seed along with the private keys, then it becomes impossible to know what the seed was and therefore impossible to regenerate the list of private keys.
Also, I have a related question: according to the official Bitcoin website, an address should only be used to receive coins once.
That is very good advice. Many people seem to think of a bitcoin address as an account number, used to keep track of all the funds that they have access to. It really should be thought of more like an invoice number, used to keep track of a single payment. Your wallet software can then be thought of as keeping track of all the invoices (both paid and unpaid) and assisting you in keeping track of it all.
I want to put a donation link on my website. How can I generate a new address for each donation?
Generate a HD wallet seed and store it securely somewhere. Then the website software can be given a public seed value (xpub) and with the appropriate formula can generate a sequence of addresses. When you want to spend any of those funds received, the private keys for that sequence of addresses can be generated separately on a computer that is far more secure than a web server.