Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the usage documentation of ethstorage-sdk. #28

Merged
merged 10 commits into from
Aug 20, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

In this tutorial, we will demonstrate how to upload and download files using the [**ethstorage-sdk**](https://github.com/ethstorage/ethstorage-sdk) tool.

The `ethstorage-sdk` provides APIs for file upload, download, and management, which are built on `c-kzg` and, therefore, are not compatible with a browser environment.
The `ethstorage-sdk` provides APIs for file upload, download, and management.

## Step 1: Install ethstorage-sdk

Expand All @@ -15,80 +15,85 @@ You can install `ethstorage-sdk` by the following command:
npm i ethstorage-sdk
```

iteyelmp marked this conversation as resolved.
Show resolved Hide resolved
## Step 2: Upload Files
## Step 2: Manage Files

### 2.1 Create Flat Directory

In this section, you will create a `FlatDirectory` contract for managing files.

```js
const {BlobUploader, EncodeBlobs} = require("ethstorage-sdk");
const {EthStorage} = require("ethstorage-sdk");

const upload = async (filePath) => {
const create = async () => {
const ETH_STORAGE_ADDRESS = "0x804C520d3c084C805E37A35E90057Ac32831F96f";
const rpc = "https://rpc.sepolia.org";
const privateKey = "0x...";
const tx = {
value: cost,
to: to,
data: data,
};

const content = fs.readFileSync(filePath);
const blobs = EncodeBlobs(content);
const blobUploader = new BlobUploader(rpc, privateKey);
const hash = await blobUploader.sendTx(tx, [blobs[0], blobs[1]...) // max is 6
// hash : 0x37df32c7a3c30d3...52453dadacc838461d8629016
}
```

## Step 3: Manage Files

const ethStorage = new EthStorage(rpc, privateKey);
await ethStorage.deploy(ETH_STORAGE_ADDRESS);
// FlatDirectory address: 0x37df32c7a3c30d352453dadacc838461d8629016

### 3.1 Create Flat Directory
or
iteyelmp marked this conversation as resolved.
Show resolved Hide resolved

In this section, you will create a `FlatDirectory` contract for managing files.
// If you are on the Sepola network, you can do the following:
await ethStorage.deploySepolia();
}
```

If you already have a `FlatDirectory` contract, you can set it directly.

```js
const {EthStorage} = require("ethstorage-sdk");

const create = async () => {
const ETH_STORAGE_ADDRESS = "0x804C520d3c084C805E37A35E90057Ac32831F96f";
const rpc = "https://rpc.sepolia.org";
const privateKey = "0x...";
const flatDirectory = "0x37df32c7a3c30d352453dadacc838461d8629016";

const ethStorage = new EthStorage(rpc, privateKey);
await ethStorage.deployBlobDirectory(ETH_STORAGE_ADDRESS);
// flatDirectory address: 0x37df32c7a3c30d352453dadacc838461d8629016
const ethStorage = new EthStorage(rpc, privateKey, flatDirectory);
}
```

### 3.2: Upload Files To Flat Directory

### 2.2: Upload Files To Flat Directory

In this section, you will upload some files into the `FlatDirectory` that you just created.

```js
const {EthStorage} = require("ethstorage-sdk");

const upload = async (filePath) => {
const rpc = "https://rpc.sepolia.org";
const privateKey = "0x...";

const ethStorage = new EthStorage(rpc, privateKey);
await ethStorage.upload(filePath);
// You can set the path of a file or folder. If using a browser, you can specify the file object selected by the 'input'.
iteyelmp marked this conversation as resolved.
Show resolved Hide resolved
await ethStorage.upload(pathOrFile);
iteyelmp marked this conversation as resolved.
Show resolved Hide resolved
iteyelmp marked this conversation as resolved.
Show resolved Hide resolved
// hash: 0x753...45836

or
iteyelmp marked this conversation as resolved.
Show resolved Hide resolved

// If you want to upload data, use 'uploadData'.
const data = fs.readFileSync(filePath);
await ethStorage.uploadData(fileName, data);
}
```

### 3.3: Download Files From Flat Directory

### 2.3: Download Files From Flat Directory

In this section, you will download files from the ethstorage network.

```js
const {DownloadFile} = require("ethstorage-sdk");
const {EthStorage} = require("ethstorage-sdk");

const download = async (fileName) => {
const flatDirectory = "0x37df32c7a3c30d352453dadacc838461d8629016";
// Note: This should be ethstorage rpc, not eth rpc
const rpc = "https://ethstorage.rpc.io";

const data = await DownloadFile(rpc, flatDirectory, fileName);
const ethStorageRpc = "https://ethstorage.rpc.io";
let data = await ethStorage.download(fileName, ethStorageRpc);

or

const { Download } = require("ethstorage-sdk")
iteyelmp marked this conversation as resolved.
Show resolved Hide resolved
const flatDirectory = "0x37df32c7a3c30d352453dadacc838461d8629016";
data = await Download(ethstorageRpc, flatDirectory, fileName);

// You can save the data as a file
}
```
Expand Down