node -v: v20.18.1
import { ZgFile, Indexer, getFlowContract } from '@0glabs/0g-ts-sdk';
import { ethers } from 'ethers';
const main = async () => {
// 配置 RPC 地址和私钥
const evmRpc = 'https://evmrpc-testnet.0g.ai/';
const privateKey = 'xxxxxxxx'; // 替换为您的私钥
const flowAddr = "0xbD2C3F0E65eDF5582141C35969d66e34629cC768"; // Turbo(快速但费用高)
const indRpc = 'https://indexer-storage-testnet-standard.0g.ai';
// 初始化 Provider、Signer 和 Indexer
const provider = new ethers.JsonRpcProvider(evmRpc);
const signer = new ethers.Wallet(privateKey, provider);
const flowContract = getFlowContract(flowAddr, signer);
const indexer = new Indexer(indRpc);
try {
// 1. 创建文件对象并生成 Merkle 树
const filePath = './example.txt'; // 替换为本地文件路径
const file = await ZgFile.fromFilePath(filePath);
const [tree, treeErr] = await file.merkleTree();
if (treeErr) throw new Error(`Merkle Tree Error: ${treeErr}`);
console.log("File Root Hash:", tree.rootHash());
// 2. 上传文件
console.log("111111111");
const [tx, uploadErr] = await indexer.upload(file, 0, evmRpc, signer, flowAddr);
console.log("222222222");
if (uploadErr) throw new Error(`Upload Error: ${uploadErr}`);
console.log("File uploaded successfully, tx:", tx);
// 3. 下载文件
const rootHash = tree.rootHash();
const outputPath = './downloaded_example.txt'; // 下载后的文件路径
const downloadErr = await indexer.download(rootHash, outputPath, true); // 设置 with_proof = true
if (downloadErr) throw new Error(`Download Error: ${downloadErr}`);
console.log(`File downloaded successfully to ${outputPath}`);
// 关闭文件对象
await file.close();
} catch (error) {
console.error("Error:", error.message);
}
};
main();
{
"name": "js0gstorageclient",
"type": "module",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"dev": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@0glabs/0g-ts-sdk": "^0.2.2",
"ethers": "^6.13.1"
}
}
In the version of node being v20.18.1, using 0g-ts-sdk to upload files results in an error (the code and package.json are shown above). The error is as follows:
root@vmi2255799:~/Js0gStorageClient# node index.js
File Root Hash: 0x29a074e29e9392790f965951a7c782e74e2feef6a98f005820b820aa4179ce97
111111111
First selected node status : {
connectedPeers: 9,
logSyncHeight: 2188293,
logSyncBlock: '0x586da5e8000be5785efead90f456def78fd544e8f0b43e37ee61e201cd4fb88c',
nextTxSeq: 185679,
networkIdentity: {
chainId: 16600,
flowAddress: '0x0460aa47b41a66694c0a73f667a1b795a5ed3556',
p2pProtocolVersion: { major: 0, minor: 3, build: 1 }
}
}
Selected nodes: [
StorageNode {
url: 'https://rpc1-storage-testnet-standard.0g.ai',
timeout: 30000,
retry: 3
}
]
Error: url.clone is not a function
node -v: v20.18.1
In the version of node being v20.18.1, using 0g-ts-sdk to upload files results in an error (the code and package.json are shown above). The error is as follows: