ethstorage-sdk-ts
Version:
eip-4844 blobs upload sdk from ethstorage-sdk
37 lines (36 loc) • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Download = void 0;
const ethers_1 = require("ethers");
const contractABI = [
"function countChunks(bytes memory name) external view returns (uint256)",
"function readChunk(bytes memory name, uint256 chunkId) external view returns (bytes memory, bool)",
];
const stringToHex = (s) => ethers_1.ethers.hexlify(ethers_1.ethers.toUtf8Bytes(s));
async function readChunk(ethStorageRpc, ethStorageAddress, hexName, index) {
let result;
try {
const provider = new ethers_1.ethers.JsonRpcProvider(ethStorageRpc);
const contract = new ethers_1.Contract(ethStorageAddress, contractABI, provider);
result = await contract.readChunk(hexName, index);
}
catch (e) {
const provider = new ethers_1.ethers.JsonRpcProvider(ethStorageRpc);
const contract = new ethers_1.Contract(ethStorageAddress, contractABI, provider);
result = await contract.readChunk(hexName, index);
}
return ethers_1.ethers.getBytes(result[0]);
}
async function Download(ethStorageRpc, ethStorageAddress, fileName) {
const hexName = stringToHex(fileName);
const provider = new ethers_1.ethers.JsonRpcProvider(ethStorageRpc);
const contract = new ethers_1.Contract(ethStorageAddress, contractABI, provider);
const blobCount = await contract.countChunks(hexName);
let buff = [];
for (let i = 0; i < blobCount; i++) {
const chunk = await readChunk(ethStorageRpc, ethStorageAddress, hexName, i);
buff = [...buff, ...chunk];
}
return Buffer.from(buff);
}
exports.Download = Download;