2017 © Pedro Peláez
 

library merkle-tree

Merkle Tree

image

pitsolu/merkle-tree

Merkle Tree

  • Monday, January 22, 2018
  • by pitsolu
  • Repository
  • 1 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 100 % Grown

The README.md

Merkle Tree

Usage

Leaf (Array)


require("vendor/autoload.php"); $customer = sha1("John Smith"); $retailer = sha1("John Doe"); $merchant = sha1("Amazon"); $taxman = sha1("Revenue Authority"); $courier = sha1("Shiply"); $exchange = sha1("Coinbase"); $transactions = array( "purchase"=>array( "sender"=>$customer, "recipient"=>$retailer, "amount"=>110 ), "w/h-tax"=>array( "sender"=>$retailer, "recipient"=>$taxman, "amount"=>10 ), "commission"=>array( "sender"=>$customer, "recipient"=>$merchant, "amount"=>5 ), "freight"=>array( "sender"=>$customer, "recipient"=>$courier, "amount"=>5 ), "trx-fees"=>array( "sender"=>$customer, "recipient"=>$exchange, "amount"=>1 ) ); $merkleTree = new Merkle\Tree(function($data){ return hash("sha256", hash("sha256", $data)); }); // foreach($transactions as $name=>$trx) // $tree = $merkleTree->add(new Merkle\Leaf($trx)); $tree = $merkleTree->add(new Merkle\Leaf($transactions["purchase"])); $tree = $merkleTree->add(new Merkle\Leaf($transactions["w/h-tax"])); $tree = $merkleTree->add(new Merkle\Leaf($transactions["commission"])); $tree = $merkleTree->add(new Merkle\Leaf($transactions["freight"])); $tree = $merkleTree->add(new Merkle\Leaf($transactions["trx-fees"])); // print_r($tree); //merkle tree echo(key($tree)); //merle root

Leaf Item (String)

The example below also tests consistency between two trees., (*1)

$slimLyrics = $realSlimShadyLyrics = array(

    "I'm Slim Shady, yes I'm the real Shady",
    "All you other Slim Shadys are just imitating",
    "So won't the real Slim Shady please stand up",
    "Please stand up, please stand up?"
);

$errIdx = 2;

$slimLyrics[$errIdx] = sprintf("%s ???", $slimLyrics[$errIdx]);

foreach($realSlimShadyLyrics as $idx=>$lyric){

    $tree = $this->merkleTree->add(new Merkle\LeafItem($lyric));
    $xtree = $this->merkleTree->add(new Merkle\LeafItem($slimLyrics[$idx]));   

    if(key($tree)!=key($xtree))
        throw new \Exception("Slims lyrics don't match!");        
}

The Versions

22/01 2018

dev-master

9999999-dev

Merkle Tree

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Moderator