UNPKG

@axiom-crypto/tools

Version:

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

142 lines (141 loc) 4.57 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PinataIpfsClient = void 0; const axios_1 = __importDefault(require("axios")); const ipfsClient_1 = require("./ipfsClient"); class PinataIpfsClient extends ipfsClient_1.IpfsClient { constructor(pinataJwt, dedicatedGatewayUrl) { super("Pinata"); if (!pinataJwt) { throw new Error("Pinata JWT is required"); } this.pinataJwt = pinataJwt; if (dedicatedGatewayUrl) { if (dedicatedGatewayUrl[dedicatedGatewayUrl.length - 1] === "/") { dedicatedGatewayUrl = dedicatedGatewayUrl.slice(0, -1); if (!dedicatedGatewayUrl.endsWith("/ipfs")) { dedicatedGatewayUrl += "/ipfs"; } this.dedicatedGatewayUrl = dedicatedGatewayUrl; } } } getUrl() { if (this.dedicatedGatewayUrl) { return `${this.dedicatedGatewayUrl}`; } return `https://gateway.pinata.cloud/ipfs`; } async getSize(hashOrCid) { try { if (hashOrCid.startsWith("0x")) { hashOrCid = this.convertBytes32ToIpfsCid(hashOrCid); } // Pinata gateway will not return data size on a head request, so we need to use // the public ipfs.io gateway instead. const res = await axios_1.default.head(`https://ipfs.io/ipfs/${hashOrCid}`); const size = res.headers["x-ipfs-datasize"]; 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 url = this.getUrl(); const res = await axios_1.default.get(`${url}/${hashOrCid}`, { headers: { Authorization: `Bearer ${this.pinataJwt}`, } }); 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 dataObj = { "pinataContent": { "data": data, }, "pinataOptions": { "cidVersion": 0, "wrapWithDirectory": false, } }; try { const res = await axios_1.default.post("https://api.pinata.cloud/pinning/pinJSONToIPFS", dataObj, { headers: { 'Content-Type': "application/json", Authorization: `Bearer ${this.pinataJwt}`, } }); const cid = res.data.IpfsHash; 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); } const res = await axios_1.default.delete(`https://api.pinata.cloud/pinning/unpin/${hashOrCid}`, { headers: { Authorization: `Bearer ${this.pinataJwt}`, } }); return { status: res.status, value: true, }; } catch (e) { return { status: e.response?.status ?? 500, value: false, }; } } } exports.PinataIpfsClient = PinataIpfsClient;