Skip to content

0glabs/0g-storage-ts-starter-kit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

0G Storage TypeScript SDK Starter Kit

This repository demonstrates how to integrate and use the 0G Storage TypeScript SDK in your applications. It provides implementation examples using the 0G decentralized storage network.

Repository Branches

1. Master Branch (Current)

REST API implementation using Express framework with Swagger documentation.

git checkout master
  • Features:
    • RESTful endpoints for upload/download
    • Swagger UI for API testing and documentation
    • TypeScript implementation for type safety

2. CLI Version Branch (Coming Soon)

Command-line interface implementation will be available in the cli-version branch.

git checkout cli-version
  • Features:
    • Direct file upload/download via CLI
    • Command-line arguments for configuration
    • TypeScript implementation

SDK Implementation (Master Branch)

Storage Client Setup

import { Indexer } from '@0glabs/0g-ts-sdk';

const indexer = new Indexer(
  INDEXER_RPC,           // Indexer service endpoint
  RPC_URL,               // EVM RPC endpoint
  PRIVATE_KEY,           // For signing transactions
  FLOW_CONTRACT_STANDARD // Flow contract address
);

File Operations

Upload Implementation

The upload process involves both API handling and SDK operations:

// Create ZgFile from file path
const zgFile = await ZgFile.fromFilePath(filePath);
const [tree, treeErr] = await zgFile.merkleTree();
const [tx, uploadErr] = await indexer.upload(zgFile);

// Get file identifier and transaction hash
const rootHash = tree?.rootHash();
const transactionHash = tx;

What happens during upload:

  • File is received via multipart form upload
  • SDK creates a ZgFile instance from the uploaded file
  • Merkle tree is generated for the file
  • File is uploaded to the 0G Storage network
  • Root hash and transaction hash are returned

Download Implementation

The download process retrieves files using their root hash:

const err = await indexer.download(rootHash, outputPath, true);

What happens during download:

  • Root hash is used to locate the file in the network
  • File is downloaded to a local path
  • Downloaded file is verified
  • File is streamed to the client

Usage

  1. Clone the repository:
git clone https://github.com/0glabs/0g-storage-ts-starter-kit
  1. Navigate to the project directory:
cd 0g-storage-ts-starter-kit
  1. Install dependencies:
npm install
  1. Copy the .env.example file to .env and set your private key:
cp .env.example .env
  1. Start the server:
npm start
  1. Access Swagger UI: http://localhost:3000/api-docs

  2. Available Endpoints:

    • POST /upload - Upload a file
      • Request: multipart/form-data with 'file' field
      • Response: JSON with rootHash and transactionHash
    • GET /download/{rootHash} - Download a file
      • Request: rootHash in URL path
      • Response: File content stream

Network Configuration

The application uses the following default network configuration which can be overridden through environment variables:

const RPC_URL = 'https://evmrpc-testnet.0g.ai/';
const FLOW_CONTRACT_STANDARD = '0x0460aA47b41a66694c0a73f667a1b795A5ED3556';
const INDEXER_RPC = 'https://indexer-storage-testnet-standard.0g.ai';

Best Practices

  1. Error Handling:

    • Proper error handling for file operations
    • Validation of uploaded files
    • Comprehensive error messages for debugging
  2. Security:

    • Environment variables for sensitive data
    • Private key protection
    • Input validation
  3. Resource Management:

    • Proper file cleanup after operations
    • Closing file handles
    • Memory efficient file processing

Next Steps

Explore advanced SDK features in the 0G Storage TypeScript SDK documentation. Learn more about the 0G Storage Network.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published