MerkleTree constructor

MerkleTree({
  1. required List<Uint8List> leaves,
  2. required HashAlgo hashAlgo,
  3. bool isBitcoinTree = false,
})

Constructs a Merkle Tree. All nodes and leaves are stored as Uint8List. Lonely leaf nodes are promoted to the next level up without being hashed again.

leaves is a list of hashed leaves. Each leaf must be a Uint8List. hashAlgo is a HashAlgo function used for hashing leaves and nodes defined. isBitcoinTree decides whether to construct the MerkleTree using the Bitcoin Merkle Tree implementation. Enable it when you need to replicate Bitcoin constructed Merkle Trees. In Bitcoin Merkle Trees, single nodes are combined with themselves, and each output hash is hashed again.

import 'dart:typed_data';

import 'package:convert/convert.dart';
import 'package:merkletree/hypi_merkletree.dart';
import 'package:pointycastle/pointycastle.dart';

Uint8List sha3(Uint8List data) {
  final sha3 = Digest("SHA-3/256");
  return sha3.process(data);
}

List<Uint8List> leaves = ['a', 'b', 'c'].map((x) => Uint8List.fromList(x.codeUnits)).map((x) => sha3(x)).toList();
final tree = MerkleTree(leaves: leaves, hashAlgo: sha256);

Implementation

MerkleTree({
  required List<Uint8List> leaves,
  required this.hashAlgo,
  this.isBitcoinTree = false,
}) : _layers = [leaves] {
  _createHashes(leaves);
}