fromBytes static method

MerkleDAGNode fromBytes(
  1. Uint8List bytes
)

Creates a MerkleDAGNode from its byte representation (DAG-PB). Automatically inspects inner data to determine if it's a UnixFS Directory.

Implementation

static MerkleDAGNode fromBytes(Uint8List bytes) {
  try {
    final pbNode = dag_proto.PBNode.fromBuffer(bytes);

    bool isDir = false;
    int? mtime;

    // Try parsing UnixFS Data
    if (pbNode.hasData()) {
      try {
        final unixData = unixfs_proto.Data.fromBuffer(pbNode.data);
        isDir =
            (unixData.type == unixfs_proto.Data_DataType.Directory ||
            unixData.type == unixfs_proto.Data_DataType.HAMTShard);
        if (unixData.hasMtime()) {
          mtime = unixData.mtime.toInt();
        }
      } catch (_) {
        // Not UnixFS or failed to parse, treat as raw DAG-PB
      }
    }

    return MerkleDAGNode(
      links: pbNode.links.map((link) => Link.fromProto(link)).toList(),
      data: Uint8List.fromList(pbNode.data),
      isDirectory: isDir,
      mtime: mtime,
    );
  } catch (e) {
    throw FormatException('Failed to parse MerkleDAGNode from bytes: $e');
  }
}