Observed since 20 Oct, 22:11 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
@0glabs/0g-ts-sdk
JS SDK for 0G Storage
This is the JavaScript SDK for 0g-storage. Features include:
npm install @0glabs/0g-ts-sdk ethers
ethers is a peer dependency of this project.
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-testnet.0g.ai';
const privateKey = ''; // with balance to pay for gas
const indRpc = 'https://indexer-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)
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
To use the SDK with Vite, set up polyfills in your vite.config.ts:
import { defineConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
export default defineConfig({
plugins: [
...
nodePolyfills({
include: ['crypto', 'buffer', 'stream', 'util', 'events'],
}),
],
});
Now, you can import SDK files with the /browser suffix:
import { Indexer, Blob } from '@0glabs/0g-ts-sdk/browser';
Check codes in examples for more details.
This project uses pnpm as package manager. After cloning the project, run pnpm install to install dependencies.
Make sure 0g-storage-contracts is in project sibling directory.
pnpm gen-contract-type-flow
pnpm gen-contract-type-market
Download counts and popularity will appear here as the collector receives them.
| Package / target | Requirement | Relationship |
|---|---|---|
@ethersproject/bytesEnvironment{
"local_name": "@ethersproject/bytes",
"alias": false,
"peer_metadata": null
} | ^5.7.0 | runtime |
open-jsonrpc-providerEnvironment{
"local_name": "open-jsonrpc-provider",
"alias": false,
"peer_metadata": null
} | ^0.2.1 | runtime |
@ethersproject/keccak256Environment{
"local_name": "@ethersproject/keccak256",
"alias": false,
"peer_metadata": null
} | ^5.7.0 | runtime |
jestEnvironment{
"local_name": "jest",
"alias": false,
"peer_metadata": null
} | ^29.6.4 | development |
tslibEnvironment{
"local_name": "tslib",
"alias": false,
"peer_metadata": null
} | ^2.6.2 | development |
rollupEnvironment{
"local_name": "rollup",
"alias": false,
"peer_metadata": null
} | ^3.28.1 | development |
js-sha3Environment{
"local_name": "js-sha3",
"alias": false,
"peer_metadata": null
} | ^0.9.1 | development |
ts-jestEnvironment{
"local_name": "ts-jest",
"alias": false,
"peer_metadata": null
} | ^29.1.1 | development |
ts-nodeEnvironment{
"local_name": "ts-node",
"alias": false,
"peer_metadata": null
} | ^10.9.1 | development |
typechainEnvironment{
"local_name": "typechain",
"alias": false,
"peer_metadata": null
} | ^8.3.1 | development |
uglify-jsEnvironment{
"local_name": "uglify-js",
"alias": false,
"peer_metadata": null
} | ^3.17.4 | development |
typescriptEnvironment{
"local_name": "typescript",
"alias": false,
"peer_metadata": null
} | ^5.2.2 | development |
@types/jestEnvironment{
"local_name": "@types/jest",
"alias": false,
"peer_metadata": null
} | ^29.5.4 | development |
@types/nodeEnvironment{
"local_name": "@types/node",
"alias": false,
"peer_metadata": null
} | ^20.5.9 | development |
@typechain/ethers-v6Environment{
"local_name": "@typechain/ethers-v6",
"alias": false,
"peer_metadata": null
} | ^0.5.0 | development |
@rollup/plugin-commonjsEnvironment{
"local_name": "@rollup/plugin-commonjs",
"alias": false,
"peer_metadata": null
} | ^25.0.4 | development |
@rollup/plugin-node-resolveEnvironment{
"local_name": "@rollup/plugin-node-resolve",
"alias": false,
"peer_metadata": null
} | ^15.2.1 | development |
rollup-plugin-polyfill-nodeEnvironment{
"local_name": "rollup-plugin-polyfill-node",
"alias": false,
"peer_metadata": null
} | ^0.12.0 | development |
ethersEnvironment{
"local_name": "ethers",
"alias": false,
"peer_metadata": null
} | 6.13.1 | peer |
Source distribution
{
"shasum": "c44e2bafed965b98f2cc70c3de8a8cbe8d8ce637",
"tarball": "https://registry.npmjs.org/@0glabs/0g-ts-sdk/-/0g-ts-sdk-0.2.4.tgz",
"fileCount": 573,
"integrity": "sha512-PvVw9dzfVfEvqPlwrlkUtM/5ByX1zeUttwRnkJmW3C0ArvZe09O+YHusKwCz+HyvY+CfN8fJ2VUFIJRYysiIYA==",
"signatures": [
{
"sig": "MEUCIH86igfVZW2EZoogDenLA3PvP+XvdlDvnPU9088+hMW9AiEApsYAhzsUAYwGgoK1cGoaPZLUdpK9UCDmna/WH/5g4Vo=",
"keyid": "SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"
}
],
"unpackedSize": 4565045
}Observed since 20 Oct, 22:11 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package_version:8fbb7b7df9e25be35af5b5769c83206d1bd2a3d94fffcf11aa740d8f781a05ce