I want to use blockchain API for my store but the wallet only monitoring last 20 generated address.
Quote from https://blockchain.info/api/api_receive:
As defined in BIP 44, wallet software will not scan past 20 unused addresses. Given enough requests from this API that don't have a matching payment, you could generate addresses past this horizon, which would make spending funds paid to those addresses quite difficult. For this reason, this API will return an error and refuse to generate new addresses if it detects it would create a gap of over 20 unused addresses. If you encounter this error, you will either need to switch to a new xPub (within the same wallet is fine), or receive a payment to one of the previous 20 created addresses
I googled about this problem and found some solutions such as:
Solutions that I found
- Check gap with blockchain checkgap API and don't generate new address until they have been paid.
- Increase gap limit on the receive api.
- Send money to the last unused address.
Problems with these Solutions
- If I have 20 unpaid address and they never been paid, solution #1 can not solve the problem. it just say I have 20 unpaid address.
- Solution #2 can not solve the main problem. Even I set the gap limit to the 100, How about the time that 101 curious users only want to see the checkout page and close it. I will get 100 unused address again.
- Solution #3 is not a nice solution to me. It seems like a bad practice to me and this is a solution that solved by spending money.
My Solution
If I get it right, when Blockchain generating the address, It will be there until it's getting paid. Correct me if I'm wrong, Blockchain will not callback until the user pay the amount. so I do this to solve the problem:
- Check the gap by API. If gap>19 goto #3 else #2.
- Generate the address and store it inside
unpaid_address
table based on the "index number" that blockchain returns with address. set expiration time for 30 minutes and go to the #4. - Get the first expired unpaid address from
unpaid_address
table. If there isn't anything, show sorry server is busy, else goto #4. - show the address to the user and say you have 15 minutes to pay to this address. (15 minutes less than expire time to avoid problems).
- If blockchain callback my URL and It has been paid before the expire time, I remove the address from
unpaid_address
else the address has been there for the next request by other user. I will give that address to the next user for paying.
Question
This solution give me the ability to never exceed gap limit. But I don't know if it's safe to give the address to a user and if he didn't pay on 30 minutes, give it to another user.
What do you think? Is it safe? or is there better solution?
About giving the address to multiple users
- First user see the checkout page in 8:20AM and send the amount to the address. but there is no confirmation in 15 minutes. the confirmation come in 11:00AM.
- Second user see the checkout page in 9:00AM and send the amount to the same address. there is confirmation in 9:10AM so I will return callback result as paid.
What about the first user confirmation on 11:00AM?