Download the PHP package danc0/dmerkle without Composer
On this page you can find all versions of the php package danc0/dmerkle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download danc0/dmerkle
More information about danc0/dmerkle
Files in danc0/dmerkle
Informations about the package dmerkle
dmerkle
Description
Install with: composer require danc0/dmerkle
A PHP implementation of a Merkle Hash tree. Completed hashes are returned in blocks and proof of inclusion can be run on blocks to check if transactions are included in that block.
Usage
You should do the following store your transactions in a database with a block_id
field and root_hash
field. Process your transactions via a first in first out queue. You will also need a table to store block data to make it possible to validate transactions.
You will need to pass these transactions into the hashing function in groups based on the block size you set (defaults to 100 transactions per block). For example if your block size was 2 you would pass something similar to this:
Data passed into the hashing function should be an array of arrays no larger than the max block size, a DMerkle_Exception
is thrown if this size is surpassed.
Using the DMerkle.php file
After building your queue and storing your transactions you can begin building data to pass to the hashing method.
The createBlockData
method will return the structured data of the block, including the complete hash tree. You will need to store this block information to validate blocks! It will also return the block hash for you to use in your next block. This will allow you to validate blocks and should also be saved with your block data! Passing the previous blocks hash into your current block means that if anything changes in any block prior to the current one your current block will not have the same block hash. This will allow us to validate a block is unchanged.
Example block data:
This data should be stored and the block_id
and root_hash
should be saved to each transaction used to create this block.
Validate a transaction
To validate a transaction you need to pass the transaction data used when creating blocks and the root_hash
of that block. You will also need to load the complete block data returned from the hashing method into DMerkle_Block::class
.
The transactionIsPartOfBlock
method will return true | false
and will validate the transaction from the base level up to the root hash by hashing it and the siblings it was hashed with up to the root hash level. This means if the hash is changed at the base level there is still no way the root hash would be the same unless the entire block was compromised and hashed again.
Validate a block
It is possible to validate a block is unchanged by either recreaing or rehashing that block and using the DMerkle_Block
class to validation the hash. To do this the previous block hash should always be passed into the current block when hashing transactions into a block. This technique means that any change in a block prior to the current one would create a different hash for the current block.
Since the old block hash is hashed as part of the new block if that old hash is ever changed it would cause the current blocks hash to be different thant what is stored.