Payment automation, payment management, data processing with your own bitcoin node and PHP

Greetings to the forum, in this article you will discover how to set up your own bitcoin node and communicate with it via its API with PHP and bitcoin-cli to do different tasks, such as wallet creation, sending bitcoin, …
To give you some examples, this method is used by darknet market to manage the wallet transaction/payment of their users or for the automation of ransom payment used by ransomware

Summary
0. Prerequisites
1. Installation & Configuration of the Bitcoin node
2. Preparation of the web server & Discovery of JSON-RPC calls
3. Creation of wallet, transaction etc…
4. Conclusion

0. Prerequisites
– A web server with PHP to install
– An FTP service (if you are on a server)
– Desktop or VPS hardware running recent versions of Windows, Mac OS X, or Linux.
– 7GB of free disk space, accessible at a minimum read/write speed of 100 MB/s.
– 2GB of RAM
more info: https://bitcoin.org/en/full-node#minimum-requirements

1. Installation & Configuration of the Bitcoin node
In this section we will install and configure our bitcoin node, nothing too complicated

1.1 Linux installation
Download the archive from the official website (https://bitcoincore.org/en/download/), you can use the wget command (ex: wget https://bitcoincore.org/bin/bitcoin-core-0.21.0/bitcoin-0.21.0-x86_64-linux-gnu.tar.gz)
Search it (ex: tar xzf bitcoin-0.21.0-x86_64-linux-gnu.tar.gz)
We will now place the content of our bitcoin-0.21.0/bin/ folder in /usr/local/bin to be able to access it more easily and quickly (ex: sudo install -m 0755 -o root -g root -t /usr/local/bin bitcoin-0.21.0/bin/*)
Start the bitcoin daemon with this command bitcoind -daemon
If you want to stop it do the command bitcoin-cli stop

1.2 Windows installation
Download the executable file on this url https://bitcoincore.org/en/download/, at the launching, it could be that windows blocks its execution to overcome this, launch a terminal with administrator rights and then relaunch it
Make the setup with your preferences…
Go to its installation folder and then to the daemon folder (default directory: C:\Program Files\Bitcoin\daemon\)
Run bitcoind.exe
If you want to stop it do a Ctrl+C

1.3 Linux configuration
Go to the .bitcoin folder located in the root of your user
Create a file named bitcoin.conf and put this in it:

rpcuser=username
rpcpassword=password

putting your login of course, for more info on the options you can add, please check this link: https://github.com/bitcoin/bitcoin/blob/master/share/examples/bitcoin.conf
If your bitcoin daemon is already running, restart it or start it

1.4. Windows configuration
Go to the %APPDATA%\Bitcoin folder
Create a file named bitcoin.conf and put this in it:

rpcuser=username
rpcpassword=password

putting your login of course, for more info on the options you can add, please see this link: https://github.com/bitcoin/bitcoin/blob/master/share/examples/bitcoin.conf
If your bitcoin daemon is already running, restart it or start it

2. Preparing the web server & Discovering RPC calls
To communicate with the JSON-RPC API of our bitcoin node we will need a library https://github.com/aceat64/EasyBitcoin-PHP, put it on your web server
You don’t have to use this specific library, you can replace it by another file managing JSON-RPC calls or use your own JSON-RPC communication system, so you have to put it in your web server folder

Now that this is done, let’s see the available calls to understand a little bit how it works
The references related to the RPC API can be found here or here
For what we are going to do, we will focus on the functions present in the Wallet category, you can explore on your side if you want

2.1 Initialization
To start we will pass directly via PHP, if you want you can make all the calls via bitcoin-cli that will make the same result
Create a new php file enter the code below:

require_once ‘easybitcoin.php’;

$bitcoin = new Bitcoin(“username”, “password”, “localhost”, 8332);

As I said above we will use the easybitcoin.php file which helps us to communicate with our API, replace username & password with your login that you put in the bitcoin.conf file, and localhost if your node
is on the same server as your website, port 8332 is the default port of the node, you can change it with the option rpcport={port}

The Bitcoin constructor initializes the connection to our node and the $bitcoin variable will be used to call the functions you have seen above

for the article I put my node in testnet, if you also want to put it in testnet, add the option testnet=1 in your bitcoin.conf file and the port of your node is now 18332

3. Creating a wallet, transaction etc…
This section will show you how to create a wallet, send bitcoin, and show some other functions to get information like the balance of a wallet, the amount received on a label or address etc…

3.1 Creating a wallet and an address
To create a wallet, we use the function createwallet => https://bitcoincore.org/en/doc/0.21.0/rpc/wallet/createwallet/ the function takes several I let you know but only one is necessary
here is the code in php:

$bitcoin->createwallet(“exploit”, null, null, null, null, null, true);

or via bitcoin-cli:

bitcoin-cli -named createwallet wallet_name=”exploit” load_on_startup=true

Читайте также:  Carding Forum

The last parameter allows us to load it at node startup, if your node has to restart it will reload the wallet automatically, otherwise you will have to load it manually with loadwallet => https://bitcoincore.org/en/doc/0.21.0/rpc/wallet/loadwallet/

Here we have created a wallet named exploit, where is it?
if you are not in testnet: %APPDATA%\Bitcoin\wallets || .bitcoin/wallets

if you are in testnet: %APPDATA%Bitcoin/testnet3/wallets || .bitcoin/testnet3/wallets

Now that we have created a wallet named exploit, we will create an address in the exploit-label for the exploit wallet, we use the function getnewaddress => https://bitcoincore.org/en/doc/0.21.0/rpc/wallet/getnewaddress/
here is the code in php:

$info = $bitcoin->getnewaddress(“exploit-label”);
var_dump($info);

or via bitcoin-cli:

bitcoin-cli getnewaddress “exploit-label”

the BTC address will be returned, you can link as many as you want

To be able to see the wallet addresses linked to a label, we use the function getaddressesbylabel => https://bitcoincore.org/en/doc/0.21.0/rpc/wallet/getaddressesbylabel/
Here is the code in php:

$info = $bitcoin->getaddressesbylabel(“exploit-label”);
var_dump($info);

or via bitcoin-cli:

bitcoin-cli getaddressesbylabel “exploit-label”

I hope you have understood how RPC calls work now with the few examples I have shown above, I advise you to explore, you can call listwallets => https://bitcoincore.org/en/doc/0.21.0/rpc/wallet/listwallets/ it will list all the wallet names loaded

3.2. Transaction
In this section we will see how to get fake bitcoin, send bitcoin to an address, and manage multiple wallets, to get fake bitcoin I will use this service https://coinfaucet.eu/en/btc-testnet/, it allows to get bitcoin in testnet mode
First, get the address of the wallet (see section 3.1), then go to the site and send yourself some bitcoin, you will receive about 0.01
To see the balance of a bitcoin wallet we use the function getbalances => https://bitcoincore.org/en/doc/0.21.0/rpc/wallet/getbalances/
here is the code in php:

$wallet = new Bitcoin(“username”, “password”, “localhost”, 18332, “wallet/exploit”);
$info = $wallet->getbalances();
if ($bitcoin->error)
var_dump($bitcoin->error);
var_dump($info);

or via bitcoin-cli:

bitcoin-cli -rpcwallet=exploit getbalances

Explanation of the first line, as soon as your node has several wallets load this method is to be used for all functions related to an action concerning a wallet, it will perform the actions with this wallet

Now we will make a transaction for this, create a second wallet and create an address in the unam4n-label linked to the unam4n wallet, WARNING now that you already have a wallet loaded you will have to use the MultiWallet RPC (what I explained to you just above)
here is the code in php:

$bitcoin = new Bitcoin(“username”, “password”, “localhost”, 18332);
$bitcoin->createwallet(“unam4n”, null, null, null, null, null, true);
$wallet2 = new Bitcoin(“username”, “password”, “localhost”, 18332, “wallet/unam4n”);
$btc_address = $wallet2->getnewaddress(“unam4n-label”);
if ($wallet->error)
var_dump($wallet->error);
var_dump($btc_address);

or via bitcoin-cli:

bitcoin-cli createwallet “unam4n”
bitcoin-cli -rpcwallet=unam4n getnewaddress “unam4n-label”

To send bitcoin to an address we use the sendtoaddress => https://bitcoincore.org/en/doc/0.21.0/rpc/wallet/sendtoaddress/
I have 0.01325222 on the wallet exploit for this example I will send 0.003 to the wallet unam4n
If the function succeeds it returns the transaction id, you can go on this site https://blockstream.info/ and go in testnet if your node is in test
I advise you to go and see more in depth the sendtoaddress function to know all its parameters
here is the code in php:

$wallet = new Bitcoin(“username”, “password”, “localhost”, 18332, “wallet/exploit”);
$info = $wallet->sendtoaddress(““, 0.003, null, null, null, null, null, null, null, 25);
if ($wallet->error)
var_dump($wallet->error);
var_dump($info);

or via bitcoin-cli:

bitcoin-cli -rpcwallet=exploit -named sendtoaddress address=”” amount=0.003 fee_rate=25

As you are in the test environment, no amount will be received or debited, only the functions giving information about the transactions like the amount of bitcoin received will be updated

You can see how much bitcoin has been received on a specific label or address with these functions getreceivedbylabel => https://bitcoincore.org/en/doc/0.21.0/rpc/wallet/getreceivedbylabel/, getreceivedbyaddress => https://bitcoincore.org/en/doc/0.21.0/rpc/wallet/getreceivedbyaddress/
here is the code in php:

$wallet = new Bitcoin(“username”, “password”, “localhost”, 18332, “wallet/unam4n”);
$info = $wallet->getreceivedbylabel(“unam4n-label”);
if ($wallet->error)
var_dump($wallet->error);
var_dump($info);

or via bitcoin-cli:

bitcoin-cli -rpcwallet=unam4n getreceivedbylabel “unam4n-label”

So for example with this information you can then automate some action/task if the address contains the desired bitcoin amount

4. Conclusion
Here is the conclusion, you now know how to setup a bitcoin node and use its API, we have seen the most useful functions, I let you consult them and discover by yourself
I’ll explain some terms again to make sure you understand them.

What is really a label, when you create an address with the getnewaddress function, you specify the wallet for which you want to generate the address and a label, think of the label as a kind of address group for example
for the wallet exploit, I can create several addresses with the label donation, which can make us understand that these addresses are intended for the donors, which can be useful if we want to know how much bitcoin we have received in donation
bitcoin we have received in donation.

Multiwallet RPC, you have to know that as soon as you have created or loaded several wallets it is imperative to use this, otherwise you will have a big problem with the tasks, it simply allows to specify a wallet for an action, see section 3.2 where I use this