Blockchain Technology - Using PHP develop Blockchain technology
Welcome back to Learn Tech Tips channel, as we discuss on the first topic, This topic will using PHP language develop Blockchain technology. OK let do now
To describe a Block (transaction element) in the Blockchain (array of transactions),
we need a class like this:
Data in a Block is very simple. check below code
The most important function in each Block is the function that calculates the Hash value (encryption) of the entire Block.
Since the Hash calculation function requires a strong encryption algorithm, which is 256-bit SHA encryption, I use hash('sha256', $hash_string);
// Author: Zidane - Learn Tech Tips,
// Email: huuvi168@gmail.com
class Block
{
public $time;
public $data;
public $previous_hash;
public $hash;
public function __construct($time, $data, $previous_hash = '')
{
$this->time = $time;
$this->data = $data;
$this->previous_hash = $previous_hash;
$this->hash = $this->calc_hash();
}
public function calc_hash()
{
$data_content = "";
if(is_array($this->data))
{
$data_content = json_encode($this->data);
} else
{
$data_content = $this->data;
}
$hash_string = $this->time . $data_content . $this->previous_hash;
return hash("sha256", $hash_string);
}
}
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:We need to create a Blockchain class to hold the array of Block elements.
The add_new_block($time, $data) function is a function that will ensure 2 things:
- Calc entire hash with current block.
- Get the Hash of the last Block to store it in the HashTruocDo variable of the current Block.
=> Thus, the Blocks will be connected (chain-chained) together through the binding of Hash code.
The validate() function is a very important function that plays the heart role in the Blockchain.
- Every time any user wants to check the entire current Blockchain whether it is safe or not to continue the transaction.
- All Hashes in the string are compared.
- The integrity of each Block and the interconnection of the entire Block must be guaranteed without the wrong Hash code.
- If there is a false positive, the data has been edited by someone in the network.
// Author: Zidane - Learn Tech Tips,
// Email: huuvi168@gmail.com
class Blockchain extends Block
{
public $chain = array();
public function __construct()
{
$this->chain[] = new Block(time(), "Genesis Block");
}
public function get_latest_item()
{
return $this->chain[count($this->chain) - 1];
}
public function add_new_block($time, $data)
{
$previous_hash = $this->get_latest_item()->hash;
$this->chain[] = new Block($time, $data, $previous_hash);
}
public function validate()
{
for ($i = 1; $i < count($this->chain); $i++) {
$current_block = $this->chain[$i];
// pr ($current_block->data['Money']);
// exit;
$previous_block = $this->chain[$i - 1];
if ($current_block->hash != $current_block->calc_hash()) {
return false;
}
if ($current_block->previous_hash != $previous_block->hash) {
return false;
}
return true;
}
}
}
So the self-generated Blockchain technology is done, now we can start testing it:
$blockchain = new Blockchain();
$data = array("Send" => "Mr.A", "received" => "Mrs.B", "Money" => "300");
$blockchain->add_new_block(time(), $data);
$data = array("Send" => "Mrs.B", "received" => "Mr.C", "Money" => "200");
$blockchain->add_new_block(time(), $data);
$is_validate = $blockchain->validate();
=> The result will be show on $blockchain params with two transaction and after validate will return Correct data (because of no one change it before)
Array ( [chain] => Array ( [0] => App\Controller\Admin\Block Object ( [time] => 1640505857 [data] => Genesis Block [previous_hash] => [hash] => 8de7d3c326ac02b8cd447c87726d18eaa715c9b2647b1c2573b4eb5723686429 ) [1] => App\Controller\Admin\Block Object ( [time] => 1640505857 [data] => Array ( [Send] => Mr.A [received] => Mrs.B [Money] => 300 ) [previous_hash] => 8de7d3c326ac02b8cd447c87726d18eaa715c9b2647b1c2573b4eb5723686429 [hash] => 05fdb82a073d3e70e19c5d4148aa9165956f1bf8468ae899f4a2662c04e0526b ) [2] => App\Controller\Admin\Block Object ( [time] => 1640505857 [data] => Array ( [Send] => Mrs.B [received] => Mr.C [Money] => 200 ) [previous_hash] => 05fdb82a073d3e70e19c5d4148aa9165956f1bf8468ae899f4a2662c04e0526b [hash] => 8267a1a4404d373c5db38060470412929430039f4f4b7000a9ffc99219cfeaba ) ) [time] => [data] => [previous_hash] => [hash] => )
Validate of block chain
If anyone / hacker change the anydata on some element
$blockchain = new Blockchain();
$data = array("Send" => "Mr.A", "received" => "Mrs.B", "Money" => "300");
$blockchain->add_new_block(time(), $data);
$data = array("Send" => "Mrs.B", "received" => "Mr.C", "Money" => "200");
$blockchain->add_new_block(time(), $data);
$is_validate = $blockchain->validate();
// hacker change data here
$blockchain->chain[1]->data['Money'] = 30000;
// we go to revalidate again
$is_validate = $blockchain->validate();
$this->set(compact('blockchain', 'is_validate'));
=> The result will be
Array
(
[chain] => Array
(
[0] => App\Controller\Admin\Block Object
(
[time] => 1640506057
[data] => Genesis Block
[previous_hash] =>
[hash] => 79f9d544d7a4cf1928a53c0eca23d06fd7f3effeb2b51911fda5e4a87b525ca2
)
[1] => App\Controller\Admin\Block Object
(
[time] => 1640506057
[data] => Array
(
[Send] => Mr.A
[received] => Mrs.B
[Money] => 30000
)
[previous_hash] => 79f9d544d7a4cf1928a53c0eca23d06fd7f3effeb2b51911fda5e4a87b525ca2
[hash] => a5db4e7fdfdf79961a39a3d6254bdf5902621b10a384d15d71df08ea0718cd9f
)
[2] => App\Controller\Admin\Block Object
(
[time] => 1640506057
[data] => Array
(
[Send] => Mrs.B
[received] => Mr.C
[Money] => 200
)
[previous_hash] => a5db4e7fdfdf79961a39a3d6254bdf5902621b10a384d15d71df08ea0718cd9f
[hash] => 582c97c25491c100fc31813200f4fdadd8ec296ab8f0435c75b2fc88228f82c5
)
)
[time] =>
[data] =>
[previous_hash] =>
[hash] =>
)
Validate of block chain
=> you will see the Incorrect Chain. That a core value of Blockchain Technology
Full source code you can checkout here - BlockchainsControllers_v1.php
Related Posts:
Blockchain Part 1: What is Blockchain: https://learn-tech-tips.blogspot.com/2021/12/what-is-blockchain.html
Blockchain Part 2: Using PHP develop Blockchain Technology: https://learn-tech-tips.blogspot.com/2021/12/using-php-develop-blockchain-technology.html
Blockchain Part 3: Loophples of the current Blockchain:
https://learn-tech-tips.blogspot.com/2021/12/loophples-of-the-current-blockchain.html
Blockchain Part 4: Create virtual currency mining technology:
Blockchain Part 5: How to create own bitcoin virtual currency
Blockchain Part 6: Apply blockchain application in the life