toBytes method

Uint8List toBytes()

Serializes proof to bytes for network transmission

Implementation

Uint8List toBytes() {
  final buffer = <int>[];

  // Add metadata
  buffer.addAll(MerkleProof._intToBytes(leafIndex));
  buffer.addAll(MerkleProof._intToBytes(totalLeaves));
  buffer.addAll(MerkleProof._intToBytes(hashes.length));

  // Add hashes
  for (final hash in hashes) {
    final hashBytes = hash.codeUnits;
    buffer.addAll(MerkleProof._intToBytes(hashBytes.length));
    buffer.addAll(hashBytes);
  }

  // Add directions
  for (final direction in directions) {
    buffer.add(direction ? 1 : 0);
  }

  return Uint8List.fromList(buffer);
}