/0g-ts-sdk

Primary LanguageTypeScript

0g-ts-sdk

This is the JavaScript SDK for 0g-storage. Features include:

  • File Merkle Tree Class
  • Flow Contract Types
  • RPC methods support
  • File upload
  • Support browser environment
  • Tests for different environments
  • File download

Install

npm install @0glabs/0g-ts-sdk ethers

ethers is a peer dependency of this project.

Usage

Node.js environment ESM example:

Use ZgFile to create a file object, then call merkleTree method to get the merkle tree of the file.

import { Indexer, ZgFile } from '@0glabs/0g-ts-sdk';
import { ethers } from 'ethers';
import { exit } from 'process';

const file = await ZgFile.fromFilePath(<file_path>);
var [tree, err] = await file.merkleTree();
if (err === null) {
  console.log("File Root Hash: ", tree.rootHash());
} else {
  exit(1);
}
await file.close();

Upload file to 0g-storage:

import { getFlowContract } from '@0glabs/0g-ts-sdk';
const evmRpc = 'https://evmrpc-test-us.0g.ai';
const privateKey = ''; // with balance to pay for gas
const indRpc = 'https://rpc-storage-testnet-turbo.0g.ai'; // indexer rpc

const provider = new ethers.JsonRpcProvider(evmRpc);
const signer = new ethers.Wallet(privateKey, provider);

const indexer = new Indexer(indRpc);
// need to pay fees to store data in storage nodes
var [tx, err] = await indexer.upload(file, evmRpc, signer);
if (err === null) {
  console.log("File uploaded successfully, tx: ", tx);
} else {
  console.log("Error uploading file: ", err);
}

Download file from 0g-storage

err = await indexer.download(<root_hash>, <output_file>, <with_proof>);
if (err !== null) {
  console.log("Error downloading file: ", err);
}

Upload data to 0g-kv:

var [nodes, err] = await indexer.selectNodes(1);
if (err !== null) {
    console.log("Error selecting nodes: ", err);
    stop();
}

const batcher = new Batcher(1, nodes, flowContract, evmRpc);

const key1 = Uint8Array.from(Buffer.from("TESTKEY0", 'utf-8'));
const val1 = Uint8Array.from(Buffer.from("TESTVALUE0", 'utf-8'));
const key2 = Uint8Array.from(Buffer.from("TESTKEY1", 'utf-8'));
const val2 = Uint8Array.from(Buffer.from("TESTVALUE1", 'utf-8'));
batcher.streamDataBuilder.set("0x...", key1, val1);
batcher.streamDataBuilder.set("0x...", key2, val2);

var [tx, err] = await batcher.exec();

if (err === null) {
    console.log("Batcher executed successfully, tx: ", tx);
} else {
    console.log("Error executing batcher: ", err);
}

Download data from 0g-kv

const KvClientAddr = "http://3.101.147.150:6789"

const streamId = "0x..."
const kvClient = new KvClient(KvClientAddr)

let val = await kvClient.getValue(streamId, ethers.encodeBase64(key1));
console.log(val)

Browser environment example:

Import zgstorage.esm.js in your html file:

<script type="module">
  import { Blob, Indexer } from "./dist/zgstorage.esm.js";
  // Your code here...
</script>

Create file object from blob:

const file = new Blob(blob);
const [tree, err] = await file.merkleTree();
if (err === null) {
  console.log("File Root Hash: ", tree.rootHash());
}

File upload is same with node.js environment with the following provider change

import { BrowserProvider } from 'ethers';  // or from ethers.js url

let provider = new BrowserProvider(window.ethereum) // metamask need to be installed

Check codes in examples for more details.

Contribute

This project uses pnpm as package manager. After cloning the project, run pnpm install to install dependencies.

Generate Contract Flow Types

Make sure 0g-storage-contracts is in project sibling directory.

pnpm gen-contract-type-flow
pnpm gen-contract-type-market