UNPKG

@axiom-crypto/tools

Version:

Useful data, field, and byte manipulation tools for Axiom.

148 lines (147 loc) 4.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.QuicknodeIpfsClient = void 0; const axios_1 = __importDefault(require("axios")); const form_data_1 = __importDefault(require("form-data")); const ipfsClient_1 = require("./ipfsClient"); // Maximum number of pages to get all pinned objects const MAX_PAGES = 1000; class QuicknodeIpfsClient extends ipfsClient_1.IpfsClient { constructor(apiKey, gatewayUrl) { super("Quicknode"); if (gatewayUrl[gatewayUrl.length - 1] === "/") { gatewayUrl = gatewayUrl.slice(0, -1); if (!gatewayUrl.endsWith("/ipfs")) { gatewayUrl += "/ipfs"; } } this.gatewayUrl = gatewayUrl; this.apiKey = apiKey; } async getSize(hashOrCid) { try { if (hashOrCid.startsWith("0x")) { hashOrCid = this.convertBytes32ToIpfsCid(hashOrCid); } const res = await axios_1.default.head(`${this.gatewayUrl}/${hashOrCid}`); const size = res.headers["content-length"]; if (!size) { return { status: res.status, value: null, }; } return { status: res.status, value: size, }; } catch (e) { return { status: e.response?.status ?? 500, value: e.message, }; } } async read(hashOrCid) { try { if (hashOrCid.startsWith("0x")) { hashOrCid = this.convertBytes32ToIpfsCid(hashOrCid); } const res = await axios_1.default.get(`${this.gatewayUrl}/${hashOrCid}`, { headers: { "x-api-key": this.apiKey, }, }); if (!(res.data.data || res.data)) { return { status: res.status, value: null, }; } return { status: res.status, value: res.data.data ?? res.data, }; } catch (e) { return { status: e.response?.status ?? 500, value: e.message, }; } } async pin(data) { const path = Date.now().toString(); const formdata = new form_data_1.default(); formdata.append("Body", data, path); formdata.append("Key", path); formdata.append("ContentType", "text/plain"); try { const res = await axios_1.default.post(`https://api.quicknode.com/ipfs/rest/v1/s3/put-object`, formdata, { headers: { "Content-Type": "multipart/form-data", "x-api-key": this.apiKey, }, }); const cid = res.data.pin.cid; return { status: res.status, value: this.convertIpfsCidToBytes32(cid), }; } catch (e) { return { status: e.response?.status ?? 500, value: null, }; } } async unpin(hashOrCid) { try { if (hashOrCid.startsWith("0x")) { hashOrCid = this.convertBytes32ToIpfsCid(hashOrCid); } // Get all user's pinned objects let pinnedCidRequestIds = {}; let pageNumber = 1; while (true) { const res = await axios_1.default.get(`https://api.quicknode.com/ipfs/rest/v1/pinning?` + new URLSearchParams({ pageNumber: pageNumber.toString(), perPage: "100", }), { headers: { "x-api-key": this.apiKey, }, }); res.data.data.forEach((pinnedObject) => { pinnedCidRequestIds[pinnedObject.cid] = pinnedObject.requestId; }); if (pageNumber === res.data.totalPages || pageNumber > MAX_PAGES) { break; } pageNumber++; } const requestId = pinnedCidRequestIds[hashOrCid]; const res = await axios_1.default.delete(`https://api.quicknode.com/ipfs/rest/v1/pinning/${requestId}`, { headers: { "x-api-key": this.apiKey, }, }); return { status: res.status, value: true, }; } catch (e) { return { status: e.response?.status ?? 500, value: false, }; } } } exports.QuicknodeIpfsClient = QuicknodeIpfsClient;