Hello all.
BackstoryI am owner of
https://selldefi.com (
Read more). This website helps people to sell their files for bitcoin. This website script is kinda famous and have multiple clones in market but I have made its payment system by myself. Older system was on Coinpayments which requires heavy KYC so I thought to make my own custom solution. Though it is not a standard way and you can call it DIY method but it works perfectly and it is decentralized.
Accepting PaymentTo accept Bitcoin payments you need 3 steps
- Generate new Bitcoin Address for each customer
- Check balance of address
- Update database once address have enough balance and confirmation
Generate new Bitcoin Address for each customerThere are two ways to generate Bitcoin address and both require xpub. For people who do not know XPUB is Master Public Key which can generate as many addresses as you want under same Seed. Mean all addresses can be derived by yourself if you have your seed phrase. Other names of XPUB are ZPUB and YPUB which are for addresses starting with bc and 3 respectively.
You can play with seeds, addresses and master pub key here. Master public key is only used to derived addresses and if someone knows your master public key they can know all addresses that belong to you in that wallet. But they cannot spend funds from it.
How to get XPUB/YPUB/ZPUBYou can get your Master public key from Electrum wallet or any other similiar wallet. For Electrum 4.3 you can get it in Menu as Wallet > Information > Master public key. There you can have your xpub, ybup or zpub.
How to genenrate addresses from master keyWallets automatically generate addresses for us but for website we have to do it ourselves. So there are again two methods to generate addresses from Master public key.
1.
Blockchain API V22.
BitswapIf you use Blockchain v2 Api it is very helpful and easy. You get API. You provide your xpub and it will automatically generate 1 address for you and when that address will receive balance with 6 confirmations then blockchain.com will call your callback url once. In your callback url you can put code for checking received balance, confirmations and update Database. You may face address limit (20 address gap limitation is met) problem which you can read about it once you face it but its solution is to add below code in end of your api call.
&gap_limit=1000000
although setting such high limit is not feasible for you to handle as Electrum will take time to generate 1m addresses. Also if you do not know how to generate more addresses in Electrum then you can do this by these commands.
-----For Older version
wallet.create_new_address(False)
wallet.storage.put('gap_limit',1000)
-----For New version
wallet.change_gap_limit(5000)
Restart Electrum to generate addresses
BitswapYou can use Bitswap library to custom generate addresses for you
function GenerateAddress($nb){
require_once('loader.php');
$xpub = 'xpub.........';
$rem = '0/';
$path = $rem.$nb; // Receiving address path
$hd = new HD();
$hd->set_xpub($xpub);
$address = $hd->address_from_master_pub($path);
return $address;
}
This function will return new address you will pass it number of address you want to generate.
?>
Save addresses in database and check last addresses you have used and generate next address.
Check balance of addressOnce you have received bitcoins you can check with any APi to check for balance. That can be blockchain.info api like
public function CheckBalance($addr) {
$balance = file_get_contents('https://blockchain.info/q/addressbalance/'.$addr.'?confirmations=2');
$balance = $balance/100000000;
if($balance > 0 ) {
return $balance;
} else return 0;
}
You can also check address balance and confirmations by subtracting block of payment confirmation from current block height. There are hundreds of free api available to do this you have to be little creative for this.
Update database once address have enough balance and confirmationOnce you have received required balance you can update database and transactions. You can reuse addresses if you manage previous balance of each address and reuse it after a week.
I recommend to not use above 5000 addresses from each master key and change new wallets because Electrum cannot handle more than 5000 addresses with ease. It can handle any number of addresses but for a normal computer it is not friendly.
I hope you will like this and in future many more will take benefits from this. Remind me in your prayers.