@lidofinance/lido-ethereum-sdk
Version:
<div style="display: flex;" align="center"> <h1 align="center">Lido Ethereum SDK</h1> </div>
55 lines • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchIPFSVerify = exports.calculateIPFSAddCID = exports.fetchIPFSBuffer = exports.fetchIPFS = exports.IPFS_GATEWAY = void 0;
const cid_1 = require("multiformats/cid");
const blockstore_core_1 = require("blockstore-core");
const ipfs_unixfs_importer_1 = require("ipfs-unixfs-importer");
exports.IPFS_GATEWAY = 'https://ipfs.io/ipfs';
const fetchIPFS = async (args) => {
const { cid, gateway = exports.IPFS_GATEWAY } = args;
const { json } = await (0, exports.fetchIPFSVerify)(cid, gateway);
return json;
};
exports.fetchIPFS = fetchIPFS;
const fetchIPFSBuffer = async (args) => {
const { cid, gateway = exports.IPFS_GATEWAY } = args;
const ipfsUrl = `${gateway}/${cid}`;
const response = await fetch(ipfsUrl);
if (!response.ok) {
throw new Error(`Failed to fetch IPFS content: ${response.statusText}`);
}
const buffer = await response.arrayBuffer();
return new Uint8Array(buffer);
};
exports.fetchIPFSBuffer = fetchIPFSBuffer;
const calculateIPFSAddCID = async (fileContent) => {
const blockstore = new blockstore_core_1.MemoryBlockstore();
const entries = (0, ipfs_unixfs_importer_1.importer)([{ content: fileContent }], blockstore, {
cidVersion: 0,
rawLeaves: false,
});
let lastCid = null;
for await (const entry of entries) {
lastCid = entry.cid;
}
if (!lastCid) {
throw new Error('CID calculation failed — no entries found');
}
return lastCid;
};
exports.calculateIPFSAddCID = calculateIPFSAddCID;
const fetchIPFSVerify = async (cid, gateway = exports.IPFS_GATEWAY) => {
const originalCID = cid_1.CID.parse(cid);
const fileContent = await (0, exports.fetchIPFSBuffer)({ cid, gateway });
const calculatedCID = await (0, exports.calculateIPFSAddCID)(fileContent);
if (!calculatedCID.equals(originalCID)) {
throw new Error(`❌ CID mismatch! Expected ${originalCID}, but got ${calculatedCID}`);
}
const json = JSON.parse(new TextDecoder().decode(fileContent));
return {
fileContent,
json,
};
};
exports.fetchIPFSVerify = fetchIPFSVerify;
//# sourceMappingURL=ipfs.js.map