Beispiel #1
1
 /**
  * @param \ParagonIE\AsgardClient\Structures\MerkleTree $tree
  * @param string $prevhash
  * @param string $nexthash
  */
 public function __construct(MerkleTree $tree, $prevhash = null, $nexthash = null)
 {
     $this->merkleTree = $tree;
     $this->currentHash = $tree->getRoot();
     $this->previousHash = $prevhash;
     $this->nextHash = $nexthash;
     if (empty($prevhash)) {
         $this->tailHash = $this->currentHash;
     } else {
         $this->tailHash = \Sodium::crypto_generichash(\bin2hex($this->previousHash) . \bin2hex($this->currentHash));
     }
 }
Beispiel #2
0
 public function testPersonalizedHash()
 {
     $treeA = new MerkleTree(new Node('a'), new Node('b'), new Node('c'), new Node('d'), new Node('e'));
     $this->assertSame('6781891a87aa476454b74dc635c5cdebfc8f887438829ce2e81423f54906c058', $treeA->getRoot());
     $treeA->setPersonalizationString('Halite unit test framework');
     $this->assertSame('e912ee25c680b0e3ee30b52eec0f0d79b502e15c9091c19cec7afc3115260b78', $treeA->getRoot());
 }
 public function testCompat()
 {
     $treeA = new MerkleTree(new Node('a'), new Node('b'), new Node('c'), new Node('d'));
     $treeB = new TrimmedMerkleTree(new Node('a'), new Node('b'), new Node('c'), new Node('d'));
     $this->assertSame($treeA->getRoot(), $treeB->getRoot());
     $personal = \random_bytes(32);
     $treeA->setPersonalizationString($personal);
     $treeB->setPersonalizationString($personal);
     $this->assertSame($treeA->getRoot(), $treeB->getRoot());
 }
 public function testExpectedBehavior()
 {
     $treeA = new MerkleTree(new Node('a'), new Node('b'), new Node('c'), new Node('d'), new Node('e'));
     $this->assertEquals('6781891a87aa476454b74dc635c5cdebfc8f887438829ce2e81423f54906c058', $treeA->getRoot());
     $treeB = new MerkleTree(new Node('a'), new Node('b'), new Node('c'), new Node('d'), new Node('e'), new Node('e'), new Node('e'), new Node('e'));
     $this->assertEquals($treeA->getRoot(), $treeB->getRoot());
     return;
     $treeC = $treeA->getExpandedTree(new Node('e'), new Node('e'), new Node('e'));
     $this->assertEquals($treeA->getRoot(), $treeC->getRoot());
     $treeD = $treeA->getExpandedTree(new Node('f'), new Node('e'), new Node('e'));
     $this->assertNotEquals($treeA->getRoot(), $treeD->getRoot());
 }
Beispiel #5
0
 /**
  * Get key updates from the channel
  *
  * @param MerkleTree $tree
  * @return Node[]
  */
 protected function getKeyUpdates(MerkleTree $tree) : array
 {
     $newNodes = [];
     foreach ($this->getChannelUpdates($tree->getRoot()) as $new) {
         $newNode = new Node($new['data']);
         $tree = $tree->getExpandedTree($newNode);
         // Verify that we've calculated the same Merkle root for each new leaf:
         if (\hash_equals($new['root'], $tree->getRoot())) {
             // Attempt to store the update (and create/revoke copies of the public keys):
             if ($this->storeUpdate($new)) {
                 $newNodes[] = $newNode;
             }
         }
     }
     if (\count($newNodes) > 0) {
         $this->notifyPeersOfNewUpdate();
     }
     return $newNodes;
 }