@searchboxlabs/metalayer
v1.0.11
Published
Verifiable Onchain Metadata for 0G Storage Store, verify, and fetch file contexts directly from the blockchain.
Readme
🧱 MetaLayer SDK
Verifiable Onchain Metadata for 0G Storage
Store, verify, and fetch file contexts directly from the blockchain.
🌐 Overview
MetaLayer provides a lightweight SDK for working with onchain-aware metadata stored on the 0G Storage Network.
It enables:
- Seamless uploading of files with verifiable Merkle roots.
- Storing & retrieving contextual metadata onchain.
- Integration with decentralized indexers.
- Easy browser & backend usage.
⚙️ Installation
npm install @searchboxlabs/[email protected]or with Yarn:
yarn add @searchboxlabs/[email protected]🚀 Quick Start
import MetaLayerClient from "@metalayer/metalayer";
import { NETWORKS } from "@metalayer/metalayer/dist/network";
import { Blob, Indexer } from "@0glabs/0g-ts-sdk";
import { ethers } from "ethers";
import fs from "fs/promises";
async function main() {
const client = new MetaLayerClient();
const indexer = new Indexer(NETWORKS.testnet.indexerUrl);
const ctx = {
fileType: "text",
extension: ".txt",
dateAdded: BigInt(Date.now()),
encrypted: false,
creator: "0x330cA32b71b81Ea2b1D3a5C391C5cFB6520E0A10"
};
const buffer = await fs.readFile("./test.txt");
const file = {
size: buffer.length,
slice: (start: number, end: number) => ({
arrayBuffer: async () => buffer.slice(start, end).buffer,
}),
};
const provider = new ethers.JsonRpcProvider(NETWORKS.testnet.rpcUrl);
const signer = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);
await client.uploadWithCtx(indexer, ctx, file, NETWORKS.testnet, signer);
const blob = new Blob(file);
const [tree] = await blob.merkleTree();
const rootHash = tree?.rootHash();
const encodedCtx = await client.getOnchainAwareCtx(rootHash, NETWORKS.testnet, signer);
console.log(client.decodeOGFileCtx(encodedCtx));
}
main();🧩 Smart Contract
The onchain contract powering MetaLayer lives here: 🔗 MetaLayer.sol
It defines:
getCtx(bytes32 rootHash) view returns (bytes)storeCtx(bytes32 rootHash, bytes memory ctx)
This allows verified metadata retrieval from smart contracts or external SDKs.
🔗 Integration Example
You can see an example integration using this SDK here: 📂 MetaLayer Integration Code
🧠 Network Configuration
export const NETWORKS: Record<string, NetworkConfig> = {
testnet: {
name: 'testnet',
rpcUrl: 'https://evmrpc-testnet.0g.ai/', // <- change if you have a different RPC
indexerUrl: 'https://indexer-storage-testnet-turbo.0g.ai/', // <- change to the real indexer endpoint
explorerUrl: 'https://chainscan-galileo.0g.ai/',
chainId: 16602,
metaLayerAddress: `0x${"3Ca8B5e3fCD85c9960C3F8c09f0cbe6145ccdC27"}`
},
mainnet: {
name: 'mainnet',
rpcUrl: 'https://evmrpc.0g.ai/', // <- change if you have a different RPC
indexerUrl: 'https://indexer-storage-turbo.0g.ai/', // <- change to the real indexer endpoint
explorerUrl: 'https://chainscan.0g.ai/',
chainId: 16661,
metaLayerAddress: `0x${"0xd15f8fc862d29b19d8f614c4aceb727f67986677"}`
},
};🛠️ Development
To build from source:
git clone https://github.com/s29papi/MetaLayer.git
cd MetaLayer
npm install
npm run buildThe compiled SDK will appear under:
dist/🤝 Contributing
Pull requests and ideas are welcome! Please open an issue before submitting large changes.
🪪 License
This project is licensed under the ISC License. See LICENSE for details.
