readBlobAttributes method

Future<Map<String, String>?> readBlobAttributes({
  1. required String blobObjectId,
})

Read the on-chain attributes (metadata key-value pairs) for a blob.

Returns null if the blob has no metadata attached. Returns a Map<String, String> of key-value pairs otherwise.

Uses Sui dynamic field lookup for the metadata key on the blob object.

Mirrors the TS SDK's readBlobAttributes().

Implementation

Future<Map<String, String>?> readBlobAttributes({
  required String blobObjectId,
}) async {
  try {
    // The metadata is stored as a dynamic field on the blob object
    // with key type `vector<u8>` and key value b"metadata".
    //
    // The Dart `sui` package's `getDynamicFieldObject` only accepts a
    // String for the name value, but for `vector<u8>` the Sui JSON-RPC
    // expects a JSON array of byte integers. Use a raw RPC call instead.
    final nameValue = 'metadata'.codeUnits; // UTF-8 byte array
    final transport = suiClient;
    final SuiObjectResponse response;
    if (transport is DirectGrpcClient) {
      response = await transport.getDynamicFieldByBcs(
        blobObjectId,
        'vector<u8>',
        Uint8List.fromList([nameValue.length, ...nameValue]),
      );
    } else {
      // Explicitly injected legacy clients retain their original transport.
      response = SuiObjectResponse.fromJson(
        await transport.client.request('suix_getDynamicFieldObject', [
          blobObjectId,
          {'type': 'vector<u8>', 'value': nameValue},
        ]),
      );
    }
    final content = response.data?.content;
    if (content == null) return null;

    // Parse the metadata VecMap from the dynamic field content.
    return _parseMetadataContent(content);
  } catch (e) {
    // Dynamic field not found → no metadata attached.
    if ((e is GrpcWebException && e.status == 5) ||
        e.toString().contains('DynamicFieldNotFound') ||
        e.toString().contains('not found') ||
        e.toString().contains('404')) {
      return null;
    }
    rethrow;
  }
}