computeMetadata method

WalrusBlobMetadata computeMetadata(
  1. int nShards,
  2. Uint8List data
)

Compute blob metadata (blob ID, root hash) without returning slivers.

Used for the upload-relay path where the relay handles actual encoding. Returns a WalrusBlobMetadata with all fields needed for on-chain registration.

Implementation

WalrusBlobMetadata computeMetadata(int nShards, Uint8List data) {
  final dataPtr = calloc<Uint8>(data.isEmpty ? 1 : data.length);
  final outBlobId = calloc<Uint8>(32);
  final outRootHash = calloc<Uint8>(32);
  final outUnencodedLength = calloc<Uint64>(1);
  final outEncodingType = calloc<Uint8>(1);

  try {
    if (data.isNotEmpty) {
      dataPtr.asTypedList(data.length).setAll(0, data);
    }

    final ret = _computeMetadata(
      nShards,
      data.isEmpty ? nullptr : dataPtr,
      data.length,
      outBlobId,
      outRootHash,
      outUnencodedLength,
      outEncodingType,
    );

    if (ret != 0) {
      throw WalrusFfiException(
        'walrus_compute_metadata failed with code $ret',
      );
    }

    return WalrusBlobMetadata(
      blobId: Uint8List.fromList(outBlobId.asTypedList(32)),
      rootHash: Uint8List.fromList(outRootHash.asTypedList(32)),
      unencodedLength: outUnencodedLength.value,
      encodingType: outEncodingType.value,
    );
  } finally {
    calloc.free(dataPtr);
    calloc.free(outBlobId);
    calloc.free(outRootHash);
    calloc.free(outUnencodedLength);
    calloc.free(outEncodingType);
  }
}