From d097fd8d32bd1b399ff1c135ee184ed8ea428184 Mon Sep 17 00:00:00 2001 From: iteye Date: Thu, 25 Jul 2024 10:13:15 +0800 Subject: [PATCH 1/4] support putblobs --- README.md | 3 +-- spec.md | 5 ++--- src/ethstorage.js | 30 +++++++++++++++++++++++++----- src/param/abi.js | 2 +- test.js | 22 +++++++++------------- 5 files changed, 38 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index c86d28b..83bd9a3 100644 --- a/README.md +++ b/README.md @@ -94,9 +94,8 @@ const data = await ethStorage.read(key); Batch upload blob data. ```js -const count = 6; const data = Buffer.from("test data"); -await ethStorage.putBlobs(count, data); +await ethStorage.putBlobs(data); ``` diff --git a/spec.md b/spec.md index b5c5bfe..e7271b7 100644 --- a/spec.md +++ b/spec.md @@ -151,8 +151,7 @@ const data = await ethStorage.read("example.txt"); **Description**: Batch upload blob data to the EthStorage network. **Parameters** -- `number` (number): Number of blobs. -- `data` (Buffer): Blob content to be written. +- `data` (Buffer): Blob content to be written. The size not exceed 6 times the blob size. **Returns** - `status` (Promise): A Promise that resolves to the execution result. `true|false` @@ -160,7 +159,7 @@ const data = await ethStorage.read("example.txt"); **Example** ```javascript const blobData = Buffer.from("some data"); -const status = await ethStorage.putBlobs(number, blobData); +const status = await ethStorage.putBlobs(blobData); ```

diff --git a/src/ethstorage.js b/src/ethstorage.js index d96f5dd..0f4e4b5 100644 --- a/src/ethstorage.js +++ b/src/ethstorage.js @@ -134,21 +134,41 @@ export class EthStorage { return ethers.getBytes(data); } - async putBlobs(number, data) { + async putBlobs(data) { if (!data) { throw new Error(`EthStorage: Invalid data.`); } data = Buffer.from(data); + if (data.length < 0 || data.length > BLOB_DATA_SIZE) { + throw new Error(`EthStorage: the length of data(Buffer) should be > 0 && < ${6 * BLOB_DATA_SIZE}.`); + } + + const blobs = encodeBlobs(data); + const blobLength = blobs.length; + const blobDataSize = BLOB_DATA_SIZE; + + const keys = []; + const ids = []; + const lengths = []; + for (let i = 0; i < blobLength; i ++) { + const key = ethers.keccak256(stringToHex(Date.now() + "_" + i + "_" +this.#wallet.address)); + keys.push(key); + ids.push(i); + if (i === blobLength - 1) { + lengths.push(data.length - blobDataSize * (blobLength - 1)); + } else { + lengths.push(blobDataSize); + } + } const contract = new ethers.Contract(this.#contractAddr, EthStorageAbi, this.#wallet); try { const storageCost = await contract.upfrontPayment(); - const tx = await contract.putBlobs.populateTransaction(number, { - value: storageCost * BigInt(number), + const tx = await contract.putBlobs.populateTransaction(keys, ids, lengths, { + value: storageCost * BigInt(blobLength), }); - const blobs = encodeBlobs(data); - let txRes = await this.#blobUploader.sendTx(tx, [blobs[0]]); + let txRes = await this.#blobUploader.sendTx(tx, blobs); console.log(`EthStorage: Tx hash is ${txRes.hash}`) txRes = await txRes.wait(); return txRes.status; diff --git a/src/param/abi.js b/src/param/abi.js index 0af3e64..7f4cee0 100644 --- a/src/param/abi.js +++ b/src/param/abi.js @@ -1,6 +1,6 @@ export const EthStorageAbi = [ - 'function putBlobs(uint256 num) public payable', + 'function putBlobs(bytes32[] memory _keys, uint256[] memory _blobIdxs, uint256[] memory _lengths)', 'function putBlob(bytes32 _key, uint256 _blobIdx, uint256 _length) public payable', 'function get(bytes32 _key, uint8 _decodeType, uint256 _off, uint256 _len) public view returns (bytes memory)', 'function size(bytes32 _key) public view returns (uint256)', diff --git a/test.js b/test.js index b2fae82..07a1464 100644 --- a/test.js +++ b/test.js @@ -7,7 +7,7 @@ const dotenv = require("dotenv") dotenv.config() const privateKey = process.env.pk; -const filePath = '/Users/lmp/Downloads/dist/img123.jpeg'; +const filePath = '/Users/lmp/Downloads/dist/img122.jpeg'; const name = filePath.substring(filePath.lastIndexOf("/") + 1); const saveFile = (data) => { @@ -19,8 +19,8 @@ const saveFile = (data) => { async function EthStorageTest() { const es = await EthStorage.create({ - rpc: 'http://142.132.154.16:8545', - ethStorageRpc: 'http://65.108.230.142:9545', + rpc: 'http://65.109.20.29:8545', + ethStorageRpc: 'http://65.109.115.36:9540', privateKey }) @@ -38,14 +38,10 @@ async function EthStorageTest() { console.log(p) // put blobs - // const es = await EthStorage.create({ - // rpc: 'http://88.99.30.186:8545', - // privateKey - // }) - // const status = await es.putBlobs(3, content); - // console.log(status); + status = await es.putBlobs(content); + console.log(status); } -// EthStorageTest(); +EthStorageTest(); async function FlatDirectoryTest() { const fd = await FlatDirectory.create({ @@ -68,7 +64,7 @@ async function FlatDirectoryTest() { onFail: (err) => { console.log(err); }, - onSuccess: (info) => { + onFinish: (info) => { console.log(info); } }); @@ -88,7 +84,7 @@ async function FlatDirectoryTest() { onFail: (err) => { console.log(err); }, - onSuccess: (info) => { + onFinish: (info) => { console.log(info); } }); @@ -110,4 +106,4 @@ async function FlatDirectoryTest() { } }) } -FlatDirectoryTest(); +// FlatDirectoryTest(); From accefa0548e388c1a00d66ab28d10935f80daed5 Mon Sep 17 00:00:00 2001 From: iteye Date: Mon, 29 Jul 2024 16:19:23 +0800 Subject: [PATCH 2/4] putblobs add keys --- README.md | 5 +++-- index.d.ts | 2 +- spec.md | 8 ++++--- src/ethstorage.js | 50 +++++++++++++++++++++++-------------------- src/param/constant.js | 2 +- test.js | 4 +++- 6 files changed, 40 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 83bd9a3..65628cb 100644 --- a/README.md +++ b/README.md @@ -94,8 +94,9 @@ const data = await ethStorage.read(key); Batch upload blob data. ```js -const data = Buffer.from("test data"); -await ethStorage.putBlobs(data); +const keys = ["key1", "key2"]; +const data = [Buffer.from("some data"), Buffer.from("test data")]; +const status = await ethStorage.putBlobs(keys, data); ``` diff --git a/index.d.ts b/index.d.ts index e3adadf..fedf993 100644 --- a/index.d.ts +++ b/index.d.ts @@ -50,7 +50,7 @@ declare module 'ethstorage-sdk' { estimateCost(key: string, data: Buffer | Uint8Array): Promise; write(key: string, data: Buffer | Uint8Array): Promise; read(key: string): Promise; - putBlobs(number: number, data: Buffer | Uint8Array): Promise; + putBlobs(keys: string[], data: Buffer[] | Uint8Array[]): Promise; } export class FlatDirectory { diff --git a/spec.md b/spec.md index e7271b7..f34a64d 100644 --- a/spec.md +++ b/spec.md @@ -151,15 +151,17 @@ const data = await ethStorage.read("example.txt"); **Description**: Batch upload blob data to the EthStorage network. **Parameters** -- `data` (Buffer): Blob content to be written. The size not exceed 6 times the blob size. +- `keys` (string[]): Array of strings representing the keys for the blobs. +- `data` (Buffer[]): Array of Buffers containing the blob content to be written. Each Buffer's size must not exceed the corresponding blob size. **Returns** - `status` (Promise): A Promise that resolves to the execution result. `true|false` **Example** ```javascript -const blobData = Buffer.from("some data"); -const status = await ethStorage.putBlobs(blobData); +const keys = ["key1"]; +const data = [Buffer.from("some data")]; +const status = await ethStorage.putBlobs(keys, data); ```

diff --git a/src/ethstorage.js b/src/ethstorage.js index 0f4e4b5..8f7ff41 100644 --- a/src/ethstorage.js +++ b/src/ethstorage.js @@ -10,7 +10,8 @@ import { BLOB_DATA_SIZE, BLOB_SIZE, PaddingPer31Bytes, - EthStorageAbi + EthStorageAbi, + BLOB_COUNT_LIMIT } from "./param"; export class EthStorage { @@ -134,42 +135,45 @@ export class EthStorage { return ethers.getBytes(data); } - async putBlobs(data) { - if (!data) { - throw new Error(`EthStorage: Invalid data.`); + async putBlobs(keys, data) { + if (!keys || !data) { + throw new Error(`EthStorage: Invalid parameter.`); } - data = Buffer.from(data); - if (data.length < 0 || data.length > BLOB_DATA_SIZE) { - throw new Error(`EthStorage: the length of data(Buffer) should be > 0 && < ${6 * BLOB_DATA_SIZE}.`); + if (keys.length !== data.length) { + throw new Error(`EthStorage: The number of keys and data does not match.`); + } + if (keys.length > BLOB_COUNT_LIMIT) { + throw new Error(`EthStorage: The count exceeds the maximum blob limit.`); } - const blobs = encodeBlobs(data); - const blobLength = blobs.length; + const blobLength = keys.length; const blobDataSize = BLOB_DATA_SIZE; - const keys = []; - const ids = []; - const lengths = []; - for (let i = 0; i < blobLength; i ++) { - const key = ethers.keccak256(stringToHex(Date.now() + "_" + i + "_" +this.#wallet.address)); - keys.push(key); - ids.push(i); - if (i === blobLength - 1) { - lengths.push(data.length - blobDataSize * (blobLength - 1)); - } else { - lengths.push(blobDataSize); + const blobArr = []; + const keyArr = []; + const idArr = []; + const lengthArr = []; + for (let i = 0; i < blobLength; i++) { + const d = Buffer.from(data[i]); + if (d.length < 0 || d.length > blobDataSize) { + throw new Error(`EthStorage: the length of data(Buffer) should be > 0 && < ${blobDataSize}.`); } + const blob = encodeBlobs(d); + blobArr.push(blob[0]); + keyArr.push(ethers.keccak256(stringToHex(keys[i]))); + idArr.push(i); + lengthArr.push(d.length); } const contract = new ethers.Contract(this.#contractAddr, EthStorageAbi, this.#wallet); try { const storageCost = await contract.upfrontPayment(); - const tx = await contract.putBlobs.populateTransaction(keys, ids, lengths, { + const tx = await contract.putBlobs.populateTransaction(keyArr, idArr, lengthArr, { value: storageCost * BigInt(blobLength), }); - let txRes = await this.#blobUploader.sendTx(tx, blobs); - console.log(`EthStorage: Tx hash is ${txRes.hash}`) + let txRes = await this.#blobUploader.sendTx(tx, blobArr); + console.log(`EthStorage: Tx hash is ${txRes.hash}`); txRes = await txRes.wait(); return txRes.status; } catch (e) { diff --git a/src/param/constant.js b/src/param/constant.js index 2359366..d7197c4 100644 --- a/src/param/constant.js +++ b/src/param/constant.js @@ -21,5 +21,5 @@ export const RawData = 0; export const PaddingPer31Bytes = 1; - +export const BLOB_COUNT_LIMIT = 6; export const MAX_BLOB_COUNT = 3; diff --git a/test.js b/test.js index 07a1464..4f6352d 100644 --- a/test.js +++ b/test.js @@ -38,7 +38,9 @@ async function EthStorageTest() { console.log(p) // put blobs - status = await es.putBlobs(content); + const keys = ["key1", "key2"]; + const blobData = [Buffer.from("some data1"), Buffer.from("some data2")]; + status = await es.putBlobs(keys, blobData); console.log(status); } EthStorageTest(); From b68237074f968312cd1a3c26463abfcace310222 Mon Sep 17 00:00:00 2001 From: iteye Date: Mon, 29 Jul 2024 17:31:20 +0800 Subject: [PATCH 3/4] change name --- README.md | 4 ++-- index.d.ts | 2 +- spec.md | 8 ++++---- src/ethstorage.js | 8 ++++---- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 65628cb..e1d2c6c 100644 --- a/README.md +++ b/README.md @@ -95,8 +95,8 @@ Batch upload blob data. ```js const keys = ["key1", "key2"]; -const data = [Buffer.from("some data"), Buffer.from("test data")]; -const status = await ethStorage.putBlobs(keys, data); +const dataBlobs = [Buffer.from("some data"), Buffer.from("test data")]; +const status = await ethStorage.putBlobs(keys, dataBlobs); ``` diff --git a/index.d.ts b/index.d.ts index fedf993..c34df56 100644 --- a/index.d.ts +++ b/index.d.ts @@ -50,7 +50,7 @@ declare module 'ethstorage-sdk' { estimateCost(key: string, data: Buffer | Uint8Array): Promise; write(key: string, data: Buffer | Uint8Array): Promise; read(key: string): Promise; - putBlobs(keys: string[], data: Buffer[] | Uint8Array[]): Promise; + putBlobs(keys: string[], dataBlobs: Buffer[] | Uint8Array[]): Promise; } export class FlatDirectory { diff --git a/spec.md b/spec.md index f34a64d..e979c10 100644 --- a/spec.md +++ b/spec.md @@ -152,16 +152,16 @@ const data = await ethStorage.read("example.txt"); **Parameters** - `keys` (string[]): Array of strings representing the keys for the blobs. -- `data` (Buffer[]): Array of Buffers containing the blob content to be written. Each Buffer's size must not exceed the corresponding blob size. +- `dataBlobs` (Buffer[]): Array of Buffers containing the blob content to be written. Each Buffer's size must not exceed the corresponding blob size. **Returns** - `status` (Promise): A Promise that resolves to the execution result. `true|false` **Example** ```javascript -const keys = ["key1"]; -const data = [Buffer.from("some data")]; -const status = await ethStorage.putBlobs(keys, data); +const keys = ["key1", "key2", "key3"]; +const dataBlobs = [Buffer.from("test data 1"), Buffer.from("test data 2"), Buffer.from("test data 3")]; +const status = await ethStorage.putBlobs(keys, dataBlobs); ```

diff --git a/src/ethstorage.js b/src/ethstorage.js index 8f7ff41..3806141 100644 --- a/src/ethstorage.js +++ b/src/ethstorage.js @@ -135,11 +135,11 @@ export class EthStorage { return ethers.getBytes(data); } - async putBlobs(keys, data) { - if (!keys || !data) { + async putBlobs(keys, dataBlobs) { + if (!keys || !dataBlobs) { throw new Error(`EthStorage: Invalid parameter.`); } - if (keys.length !== data.length) { + if (keys.length !== dataBlobs.length) { throw new Error(`EthStorage: The number of keys and data does not match.`); } if (keys.length > BLOB_COUNT_LIMIT) { @@ -154,7 +154,7 @@ export class EthStorage { const idArr = []; const lengthArr = []; for (let i = 0; i < blobLength; i++) { - const d = Buffer.from(data[i]); + const d = Buffer.from(dataBlobs[i]); if (d.length < 0 || d.length > blobDataSize) { throw new Error(`EthStorage: the length of data(Buffer) should be > 0 && < ${blobDataSize}.`); } From a2d8e13db5f5910c3b00ce18fe2d47f6bd85dc9f Mon Sep 17 00:00:00 2001 From: iteye Date: Tue, 30 Jul 2024 13:50:20 +0800 Subject: [PATCH 4/4] change method name --- README.md | 4 ++-- index.d.ts | 2 +- spec.md | 8 ++++---- src/ethstorage.js | 2 +- test.js | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e1d2c6c..f230223 100644 --- a/README.md +++ b/README.md @@ -89,14 +89,14 @@ const key = "test.txt"; const data = await ethStorage.read(key); ``` -#### putBlobs +#### writeBlobs Batch upload blob data. ```js const keys = ["key1", "key2"]; const dataBlobs = [Buffer.from("some data"), Buffer.from("test data")]; -const status = await ethStorage.putBlobs(keys, dataBlobs); +const status = await ethStorage.writeBlobs(keys, dataBlobs); ``` diff --git a/index.d.ts b/index.d.ts index c34df56..d709022 100644 --- a/index.d.ts +++ b/index.d.ts @@ -50,7 +50,7 @@ declare module 'ethstorage-sdk' { estimateCost(key: string, data: Buffer | Uint8Array): Promise; write(key: string, data: Buffer | Uint8Array): Promise; read(key: string): Promise; - putBlobs(keys: string[], dataBlobs: Buffer[] | Uint8Array[]): Promise; + writeBlobs(keys: string[], dataBlobs: Buffer[] | Uint8Array[]): Promise; } export class FlatDirectory { diff --git a/spec.md b/spec.md index e979c10..2115fea 100644 --- a/spec.md +++ b/spec.md @@ -11,7 +11,7 @@ - estimateCost - read - write - - putBlobs + - writeBlobs - [4. FlatDirectory Class](#FlatDirectory) - Static Methods - create @@ -51,7 +51,7 @@ The `FlatDirectory` class is a higher-level data management tool that provides m | estimateCost | Estimate the cost of uploading data(gas cost and storage cost) | | write | Asynchronously write data | | read | Asynchronously read data | -| putBlobs | Batch upload blob data to the EthStorage network | +| writeBlobs | Batch upload blob data to the EthStorage network | ### FlatDirectory Class @@ -147,7 +147,7 @@ const status = await ethStorage.write("dataKey", Buffer.from("some data")); const data = await ethStorage.read("example.txt"); ``` -#### putBlobs +#### writeBlobs **Description**: Batch upload blob data to the EthStorage network. **Parameters** @@ -161,7 +161,7 @@ const data = await ethStorage.read("example.txt"); ```javascript const keys = ["key1", "key2", "key3"]; const dataBlobs = [Buffer.from("test data 1"), Buffer.from("test data 2"), Buffer.from("test data 3")]; -const status = await ethStorage.putBlobs(keys, dataBlobs); +const status = await ethStorage.writeBlobs(keys, dataBlobs); ```

diff --git a/src/ethstorage.js b/src/ethstorage.js index 3806141..fb2c5bf 100644 --- a/src/ethstorage.js +++ b/src/ethstorage.js @@ -135,7 +135,7 @@ export class EthStorage { return ethers.getBytes(data); } - async putBlobs(keys, dataBlobs) { + async writeBlobs(keys, dataBlobs) { if (!keys || !dataBlobs) { throw new Error(`EthStorage: Invalid parameter.`); } diff --git a/test.js b/test.js index 4f6352d..e107b70 100644 --- a/test.js +++ b/test.js @@ -40,7 +40,7 @@ async function EthStorageTest() { // put blobs const keys = ["key1", "key2"]; const blobData = [Buffer.from("some data1"), Buffer.from("some data2")]; - status = await es.putBlobs(keys, blobData); + status = await es.writeBlobs(keys, blobData); console.log(status); } EthStorageTest();