UPDATE.
Zaopensorsil payment gateway: github.com/Overtorment/Cashier-BTC For various reasons, existing payment gateways (such as Bitpay) may not suit you. In this article we will look at creating your own Bitcoin gateway from scratch.
It is assumed that the reader is familiar with the Bitcoin network. If not, I recommend these articles: “How the Bitcoin Protocol Really Works” and “Bitcoin: An Introduction for Developers.” Conventionally, I would divide our proposed system into 4 parts:
- Working with addresses
. Generating a pair of public and private keys (as you know, the hash of a public key is a Bitcoin address, and the corresponding private key allows them to be managed) - Receiving information from the Bitcoin network
. Transaction status, address balance. - Creating and signing transactions
. Formation of a correct transaction, signing with a key/keys, conversion to hex. The resulting hex is ready to be broadcast to the network. - Broadcast transactions
. Aka broadcast, send, push. Transmitting the hex transaction to the Bitcoin network so that miners can begin work on including the transaction in the blockchain.
To perform these operations, we will need to select a set of software solutions that will simplify our life. The most popular and used solutions can perform all or almost all of the above operations, but nothing prevents us from combining solutions, or even writing our own bicycle for certain stages.
Receiving information from the Bitcoin network
The most “heavy” point. The classic solution is to raise your own reference Bitcoin full node, also known as canonical bitcoind. This will allow us to communicate with it via JSON-RPC. With it, we will be able to both receive information from the network and push transactions. What you should pay attention to:
- After installation, node synchronization may take a long time. Only after synchronization the node can be used.
- It will take up a lot of space. Already 40+ gigabytes.
- I personally don’t know what kind of request load it can withstand.
- Any problems with the crash/update will fall on your shoulders.
An alternative is a full node implementation in Ruby+PostgreSQL, Toshi. Non-canonical, but striving for full compatibility implementation. Please note, due to additional indexes, the database will take up 220+ gigabytes of space. Again, synchronization with the network is required. There may be other full node implementations (unknown to me). Another alternative
is to use the provider's public API. Receiving information from the network and broadcasting transactions will fall on his shoulders.
Currently there are:
- chain.com
- blockchain.info/ru/api (not recommended)
- www.blockcypher.com
- chain.so/api
- coinalytics.co
- www.blocktrail.com
- coinkite.com/developers
- other
Personally, I recommend connecting several solutions with fallover.
About ASP.NET Core
A small educational program for those who have been writing applications in WinForms (or even in Java) for 15 years and have slept through everything. ASP.NET Core is the most modern web framework from Microsoft, which replaced ASP.NET MVC and ASP.NET WebAPI. Open source, cross-platform, universal. It feels great in the clouds, but works just as well locally. Thanks to the modular architecture, you can build even microservices or blogs on it. In short, any modern Dotnet developer should know it, even if he (oh, horror!) does not speak English.
And if earlier for this you had to read boring documentation from Microsoft, multiplied by machine translation, now there is a better option. Andrew Locke is the author of one of the most popular blogs covering ASP.NET Core, a Microsoft MVP and a longtime .NET developer. So if you are reading a book dedicated to ASP.NET Core in its entirety, then his candidacy as the author is perfect. And community volunteers made sure that Locke's competencies were not tainted by a poor translation.
Broadcast transactions
The result of creating and signing transactions is binary data (hex), ready to be pushed into the network. Until the network sees the transaction, consider there is no transaction. When the network sees a transaction, it is considered unconfirmed. The transaction is enough to transfer Bitcoin to one node, after which in a matter of seconds the transaction will be seen by most of the Bitcoin network. Some client nodes from the “Working with Addresses” section (through some of their own hardcoded endpoints), or any full node, can broadcast transactions. You can even broadcast a transaction manually by going to the provider’s special Bitcoin API page and entering the transaction into a special form. Canonically, a confirmed transaction is a transaction included in 6 or more consecutive blocks (or 1-3. Non-canonical, but faster). Transactions with zero (or insufficient) commission may remain unconfirmed for a long time (up to a month, in my experience). It is advisable to periodically retransmit such transactions.
General principles of payment gateway operation
Option 1
Suppose we have a unique invoice (invoice, order) presented to the client for payment, and the client will pay in bitcoins.
Let's start by converting the original account currency (USD for example) into BTC. This is a trivial task and we will not consider it. Further. The de facto standard is the generation of a new unique Bitcoin address for each order (aka invoice, aka invoice, aka order). It is expected that only our client will transfer funds to this account, only 1 time, and only a strictly specified amount (more is possible, no one will be offended, but not less). That. When funds arrive at the specified Bitcoin address in the required quantity, the order is considered paid. Briefly, the chain is like this:
- order in the system ->
- generate a unique Bitcoin address corresponding to the order ->
- show it to the client ->
- We are waiting for payment to the address ->
- the order is closed (cancellation upon expiration or receipt of BTC and counting the fact of payment)
When bitcoins arrive at an address, you have options to credit the unconfirmed or confirmed balance. There is a small chance that the transaction will be rolled back, and this could be either due to the fault of the payer (who is actually an attacker) or due to circumstances beyond his control.
If you have the opportunity to “take away” the provided product or service from the client in the event of a reversed transaction, I recommend setting off the unconfirmed balance. This will mean an almost instantaneous payment process for the client (as opposed to an hour of waiting, for example). And if some transactions are found to have been withdrawn in the end, ask the client for a repeat payment, threatening to take away the service/product.
Don’t expect that such fraud will immediately overtake you en masse; transaction rollbacks are very rare, and it is unrealistic to “manually” stimulate such a rollback (for which, by the way, the attacker has no guarantee of success) for technically unsavvy clients (as opposed to chargebacks on credit cards).
Another example when an unconfirmed balance can be counted is if it takes more than one hour to prepare a customer’s order (for example, a customer’s shopping cart is being processed and is being prepared for shipment by a courier service). There is plenty of time to double-check the balance before sending the goods.
For other cases, you can enter a certain threshold, above which you must expect a confirmed balance (for example, 0.25 BTC). For maximum reliability, make it zero.
After closing the order, you can leave the bitcoins at this address until required, or for convenience, transfer them to a single “aggregation” wallet of the merchant. Be careful, in the latter case you can compromise such a commercial indicator as “turnover”, because The payment transaction can be tracked by every paying customer. For transfers, you will need to create, sign and broadcast transactions using the private keys of the addresses.
A few words about the order lifetime.
If your product or service is strictly pegged to its fiat currency equivalent (for example USD), then the typical order lifetime is 7-15 minutes due to exchange rate volatility.
Option 2
Suitable when you do not issue invoices for payment, and the user’s account contains a certain single balance, which he replenishes and from which he spends. Here you will need to generate a Bitcoin address for the user and show it to him, asking him to top up for any amount. In this case, it is necessary to monitor the address for incoming transactions and replenish the user’s internal balance if available. In this case, I recommend counting only confirmed transactions (from 3 blocks and above).
- generating an address for the user ->
- monitoring transactions to address ->
- replenishment of the internal account in the presence of incoming transactions
Installing a full Bitcoin node with Bitcoin Core - step by step guide
05/21/2020 Andrey Asmakov
#Bitcoin Core#nodes#full nodes
Decentralization is a key characteristic of Bitcoin. Thanks to decentralization, Bitcoin does not have a single point of control and failure. Decentralization also means the need for as many full nodes as possible.
ForkLog has compiled step-by-step instructions for installing a full Bitcoin node using the most popular client of the Bitcoin Core network.
What is a full node and why is it needed?
We covered this issue in an educational card, but let us remind you: a full node is considered to be any computer connected to the blockchain and completely synchronized with it. Full nodes store all blockchain data, starting with the genesis block.
Full nodes serve the network for free, download and validate each block of transactions, guided solely by the consensus algorithm. They are completely independent. Full nodes reject blocks or individual transactions that contradict the consensus.
“Bitcoin is a sophisticated, decentralized network of trust that can support myriad financial processes. At the same time, each node in the Bitcoin network follows a few simple mathematical rules. Interaction between many nodes is what leads to sophisticated behavior, not any inherent complexity or trust in a single node. Like an ant colony, Bitcoin is a resilient network of simple nodes following simple rules, which together can do amazing things without any central coordination,” writes renowned Bitcoin evangelist Andreas Antonopoulos in his book Mastering Bitcoin.
Any user with access to a computer with sufficient characteristics and an Internet connection can deploy a full node. As of May 20, 2022, the Bitcoin network was supported by more than 10 thousand nodes, most of which were deployed in North America and Western Europe.
Data: bitnodes.io
Some may ask why install a full node when you can just use one of the many wallets available. There are several answers:
You believe in Bitcoin and want the network to grow and be successful. Each new node brings us closer to a future in which people carry out Bitcoin transactions and no government or third party can interfere with this.
You plan to carry out a large number of transactions and want to be sure that your transactions will be verified. Unless you run your own node, you trust someone else to verify your transactions. A full node provides complete control over personal finances.
Finally, let’s not forget about such an important aspect as privacy - although there is no anonymity of transactions in Bitcoin in the full sense of the word, managing a full node helps to solve this problem to a certain extent.
Minimum technical requirements
Before installing a full node, you need to make sure that your computer meets a number of minimum technical requirements. According to bitcoin.org, these are:
- Desktop computer or laptop with the latest versions of Windows, Mac OS X or Linux;
- 200 GB of free hard disk space with a minimum read/write speed of 100 MB/s; 2 GB of random access memory (RAM);
- Broadband Internet connection with an upload speed of at least 400 Kb/s. It is also important that the connection is unlimited and has high upload limits.
Ideally, full node software should run 24/7, but this may not be possible for everyone, so the recommended run time is at least six hours a day.
Installing a full node
The props were a Dell Inspiron 15 3584 laptop (Core i3-7020U (2.30 GHz), DDR4 4GB, HDD 1TB) with Windows 10. We installed Bitcoin Core - the most popular client on the network, which is being developed by a wide community of developers. As you will see below, this is a fairly simple process, most of which comes down to installing the wallet itself.
We go to https://bitcoin.org/en/download, where we are greeted by Bitcoin Core 0.19.1 - the latest version of the software, released in March of this year.
By default, a direct link to the .exe file is provided here, but you can also select a .zip archive or download versions for other operating systems.
After downloading the installation file, we proceed directly to the installation. We are one step away from perhaps the most important decision of our lives!
The next step is to select the drive on which the program will be installed. By default, the installer will prompt you to select drive C. To do this, you need 52MB of free space, which we have:
Let's start the unpacking process:
After a short time, the unboxing is complete and Bitcoin Core is ready to go. Almost.
Next comes a very important step - you need to specify the disk on which the blockchain data will be stored. And as you can see in the screenshot below, by today its full size has grown to 284 GB.
By default, the installer will prompt you to select drive C, but there may be situations when it does not have the necessary free space. This is exactly the case that awaited us. I had to choose another drive.
As you can see, the developers warn in advance that the initial synchronization process will not be easy. You also need to be prepared for the fact that it will take a fair amount of time.
In addition, a firewall may get in the way, but this can be solved quite quickly and simply.
After all these actions, we found ourselves in the client itself, which immediately begins the process of synchronizing the blockchain from the very beginning of the existence of the Bitcoin network. That is, the data of all blocks is loaded, including the genesis block created by Satoshi Nakamoto. The feeling of being part of history is difficult to put into words!
As mentioned earlier, synchronizing blockchain data will take time, and while it continues, you can back up your private keys. Methods for their storage and safety will not be discussed in this material.
Data synchronization until the second half of 2015 was completed very quickly - it took about three hours. However, further the process slowed down significantly, which is explained by the increased average block size.
Be that as it may, after almost five days of continuous operation of the laptop and nervous waiting, the synchronization process was completed!
But this is still not enough to launch a full node - at this stage, the client acts solely as a wallet, and not the most convenient in terms of speed.
Our task is to launch a full node. To do this, you need to perform several more steps. First of all, configure incoming connections via port 8333.
To do this, go to the Settings > Options tab, go to the network settings, check “allow incoming connections” and manually enter the port parameters.
In theory, this should be enough, but in order for the changes to take effect, a program reboot is still necessary. By the way, shutting down Bitcoin Core should always be done via File > Close Program.
After rebooting the client, wait about 15-20 minutes, after which we go to https://bitnodes.io/, where we find the field for checking the availability of the node.
If you do this immediately after starting the program, you will most likely be greeted with a message that the node is not responding:
Our final goal is to get the following result:
Sometimes simply allowing incoming connections on port 8333 is not enough: not all routers support such automatic configuration, in which case the configuration must be done manually.
Additional information on possible configuration problems can be found in a special section on Bitcoin.org, or seek help from specialists.
Nevertheless, with sufficient effort, the issue can be completely resolved, which means that launching a full node is not nearly as complicated a process as it might seem at first glance. And upon completion, you can safely boast that you not only own bitcoins, but that you are bitcoin! Well, or at least part of it.
Let us add that a full node can also be deployed on a virtual server, which is a topic for a separate discussion, or you can try to install it on a separate physical device, the choice of which has recently become wider.
Andrew Asmakov
Subscribe to ForkLog news in Telegram: ForkLog FEED - the entire news feed, ForkLog - the most important news and polls.
Found an error in the text? Select it and press CTRL+ENTER