Skip to main content

0G Storage SDKs


0G offers two Software (SDKs) to seamlessly integrate decentralized storage into your applications:

  • Go SDK: Ideal for backend systems and applications built with the Go programming language.
  • TypeScript SDK: Perfect for frontend development and JavaScript-based projects.

Both SDKs provide a streamlined interface to interact with the 0G Storage network, enabling you to:

  • Upload and Download Files: Securely store and retrieve data of various sizes and formats. Note: you can also use the explorers to do so.
  • Manage Data: List uploaded files, check their status, and control access permissions.
  • Leverage Decentralization: Benefit from the 0G network's distributed architecture for enhanced data availability, immutability, and censorship resistance.

Overview

The 0G Go SDK enables seamless interaction with the 0G decentralized storage network. This guide will walk you through the installation, setup, and usage of the SDK, including examples of key functionalities.

Installation

To install the 0G Storage Client library:

go get github.com/0glabs/0g-storage-client

First, import the necessary packages:

import (
"context"
"github.com/0glabs/0g-storage-client/common/blockchain"
"github.com/0glabs/0g-storage-client/indexer"
"github.com/0glabs/0g-storage-client/transfer"
)

Key Functionalities

Initialization

Create the necessary clients to interact with the network:

// Create Web3 client for blockchain interactions
w3client := blockchain.MustNewWeb3(evmRpc, privateKey)
defer w3client.Close()

// Create indexer client for node management
indexerClient, err := indexer.NewClient(indRpc)
if err != nil {
// Handle error
}

Parameters:

  • evmRpc: Ethereum RPC URL
  • privateKey: Your Ethereum private key for signing transactions
  • indRpc: Indexer RPC endpoint

Node Selection

Select storage nodes before performing file operations:

nodes, err := indexerClient.SelectNodes(ctx, segmentNumber, expectedReplicas, excludedNodes)
if err != nil {
// Handle error
}

Parameters:

  • ctx: Context for operation management
  • segmentNumber: Identifies which storage segment to use
  • expectedReplicas: Number of file copies to maintain (minimum 1)
  • excludedNodes: List of nodes to exclude from selection

File Upload

Upload files to the network:

uploader, err := transfer.NewUploader(ctx, w3client, nodes)
if err != nil {
// Handle error
}

txHash, err := uploader.UploadFile(ctx, filePath)
if err != nil {
// Handle error
}

Parameters:

  • ctx: Context for upload operation
  • w3client: Web3 client instance
  • nodes: Selected storage nodes
  • filePath: Path to the file being uploaded

File Hash Calculation

Calculate a file's Merkle root hash before upload, this will be used for identify file from 0G storage:

rootHash, err := core.MerkleRoot(filePath)
if err != nil {
// Handle error
}
fmt.Printf("File hash: %s\n", rootHash.String())

Parameters:

  • filePath: Path to the file you want to hash

Returns:

  • rootHash: A unique identifier for the file based on its content
    • Used for file verification
    • Required for downloading files

File Download

Download files from the network:

downloader, err := transfer.NewDownloader(nodes)
if err != nil {
// Handle error
}

err = downloader.Download(ctx, rootHash, outputPath, withProof)
if err != nil {
// Handle error
}

Parameters:

  • ctx: Context for download operation
  • rootHash: File's unique identifier (Merkle root hash)
  • outputPath: Where to save the downloaded file
  • withProof: Enable/disable Merkle proof verification
    • true: Performs verification
    • false: Skips verification

Best Practices

  1. Error Handling: Implement proper error handling and cleanup
  2. Context Management: Use contexts for operation timeouts and cancellation
  3. Resource Cleanup: Always close clients when done using defer client.Close()
  4. Verification: Enable proof verification for sensitive files
  5. Monitoring: Track transaction status for important uploads

Conclusion

The 0G Go SDK provides a robust way to interact with the 0G Storage network, enabling decentralized file storage, data integrity verification, and efficient transaction management. For more detailed information, refer to the official GitHub repository.