Skip to main content

Blob

The Binary Large Object (Blob) extension allows storing generic data on-chain, such as images and documents. This data can be read by other Solana programs and therefore be used by them, e.g., for displaying the asset in a marketplace. While storing large amounts of data in a Solana account data is quite expensive, this extension gives the ability to create fully on-chain (FOC) assets for those who wish for them.

The extension consists of a content type and data fields:

Creating a Blob

Depending on the size of the Blob, there are different way you can create the extension. For blobs that fit in one transaction, you can use allocate, create or update instructions directly. For cases where the blob does not fit in a single transaction, it is necessary to use a combination of a single allocate and multiple write instructions.

The SDKs offer helpers to facilitate the creation of larger blobs. For example, the JS SDK offers an initialize helper that returns a list of instructions necessary to create a blob, given its data; the Rust SDK offers a mint helper that creates an asset with any number of extensions.

For on-chain use, the Nifty Asset crate offers macros that facilitate the manipulation of the extension data to avoid issues with stack/heap size: allocate_and_write. The macro will invoke allocate and any number of write CPIs to create the blob.

import { blob, initialize } from '@nifty-oss/asset';

const response = await fetch(
'https://arweave.net/Y8MBS8tqo9XJ_Z1l9V6BIMvhknWxhzP0UxSNBk1OXSs'
);
const image = new Uint8Array(await response.arrayBuffer());
const contentType = response.headers.get('content-type') ?? 'image/png';

await initialize(umi, {
asset,
payer,
extension: blob(contentType, image),
}).sendAndConfirm(umi);

Fetching a Blob

Given an asset account, it is possible to retrieve the blob of an asset. Note that not all assets might have the extension, therefore it is necessary to assert if the extension was found.

import {
ExtensionType,
fetchAsset,
getExtension
} from '@nifty-oss/asset';

const asset = await fetchAsset(umi, address);
const blob = getExtension(asset, ExtensionType.Blob);

if (blob) {
console.log("content-type=" + blob.contentType);
console.log("bytes=" + blob.data.length);
}