Thanks for your answers, I get the basic idea of Electrum. But I still have some questions.
OK. But how the online wallet(W2) knows whether the new address is produced by the offline one(W1). I could use the online wallet to send transactions even the address and private key are produced by another wallet, for example, BitCore-Qt wallet. Is there any calculation or algorithm behind the checking?
No, you couldn't use an address and private key from another wallet... Electrum HD wallets DO NOT allow you to import private keys. They can only ever hold the keys/addresses generated from your seed.
So, both the online and offline wallet use the same identical Master Public Key. This is the "starting point" for address generation, so both wallets will generate identical lists of addresses... Forever!
Let's create a scenario for it. The offline wallet(W1) and watch-only wallet(W2) have the identical addresses in the beginning and they
are marked as add1, add2 .... add20 and chadd1,....chaadd5.
And I got a payment from Bob(0.4BTC) and Alice(0.6BTC) and all the payments are received by the add1. Now I send 0.3BTC to Jacky which uses the input from Bob. After the transaction, I have 0.7BTC(0.6 in add1 and 0.1 in chaadd1).
Q1: The add1 is used, but there is still 0.6BTC in it. Is there a new add21 produced ? I guess the answer is yes.
Q2: In the online wallet(W2), since the add1 is used, a new address is generated and the address is add21, right ?
Since the new addresses is produced after the Wallet is created, and the gap limit is 20 by default. If I recover from the seed, I think only 20 addresses are produced by default. If you are correct, is that possible the new address is not in the 'address' list by default ? I have to create new addresses and wish it shows up.
The "gap limit" is simply how many consecutive "empty" addresses need to occur before the wallet stops looking for more used addresses after the wallet is restored.
So, if you have used say 14 addresses, and then wipe your computer and restore your wallet... During the restore, it'll find the first 14 addresses have been used... Then it will find 20 empty ones and that triggers the "gap limit" functionally, so it'll stop generating and checking addresses... And you wallet will now have a total of 34 addresses in it.
Also, each time you use an address, Electrum will automatically generate a new address out to the gap limit to maintain a pool of unused addresses.
You should not need to modify the gap limit unless you're constantly generating an enormous amount of addresses and receiving payments to them "out of order"... More likely in an ecommerce setup as opposed to a cold storage scenario.
I have about 80 agencies, everyday I will receive payments. So I want to generate a least 80 address. After I read the warning in the '
http://docs.electrum.org/en/latest/faq.html#how-is-the-wallet-encrypted' page, It looks like it's a better option to change the gap_limit to 80. Any better suggestion?