In this recipe, we share how to handle Spore and Cluster Data using the Spore SDK.
Spore enables you to upload, retrieve, and interact with various types of content, represented as SporeData, and organized into Clusters with ClusterData.
We'll cover these fundamental operations that are key to interacting with on-chain Spore and Cluster data:
@spore-sdk/core provides powerful helper functions for a simple and efficient way to work with the SporeData and ClusterData structures, which would otherwise be a complex task.
1. Encoding and Decoding ClusterData
Ingredients:
ClusterData - Cell.data of the on-chain cluster encoded by molecule
bytes from @ckb-lumos/codec
Methods:
ClusterData.pack
ClusterData.unpack
Encoding ClusterData with ClusterData.pack
import { ClusterData } from '@spore-sdk/core';
import { bytes } from '@ckb-lumos/codec';
const encodedClusterData = ClusterData.pack({
name: bytes.bytifyRawString('cluster name'),
description: bytes.bytifyRawString('description of the cluster'),
});
console.log(encodedClusterData); // Uint8Array [ ... ]
console.log(bytes.hexify(encodedClusterData)); // 0x3a0000000c0000001c0000000c000000636c7573746572206e616d651a0000006465736372697074696f6e206f662074686520636c7573746572
The SporeData.contentType property specifies the MIME Type of the spore's content. It's typically a string composed of a MediaType (like image/jpeg) and Parameters (like a=1;b=2).
For example, the structure of a typical SporeData.contentType looks like:
Type: "image"
Subtype: "jpeg"
MediaType: "image/jpeg"
Parameters: { a: "1", b: "2" }
When working with SporeData.contentType, you can utilize existing libraries to manipulate MIME types or manually handle the string depending on your specific requirements.
Ingredients:
SporeData.contentType
Methods:
encodeContentType
decodeContentType
setContentTypeParameters
Encoding SporeData.contentType with encodeContentType
Now that you’ve explored how to handle Spore and Cluster Data using the Spore SDK. This knowledge forms the foundation for for working with on-chain Spore and Cluster data. You can focus on building and implementing the functionality of your applications, leveraging the provided helper functions without getting caught up in complex encoding and decoding details.