import axios, { AxiosResponse } from 'axios'; import axiosRetry from 'axios-retry'; import { Buffer } from 'buffer'; import Debug from 'debug'; import FormData from 'form-data'; import pako from 'pako'; import Hash from 'typestub-ipfs-only-hash'; export interface Headers { [key: string]: string | string[] | number | boolean | null; } const debug = Debug('cannon:builder:ipfs'); export function compress(data: string) { return pako.deflate(data); } export function uncompress(data: any) { return pako.inflate(data, { to: 'string' }); } export async function getContentCID(value: string | Buffer): Promise { return Hash.of(value); } export async function getContentUrl(content?: any): Promise { if (!content) return null; const buffer = compress(JSON.stringify(content)); const cid = await getContentCID(Buffer.from(buffer)); return `ipfs://${cid}`; } const STRICT_CID_REGEX = /^(?[a-zA-Z0-9]{46})$/; export function parseIpfsCid(cid: any) { if (typeof cid !== 'string' || !cid) return null; return cid.trim().match(STRICT_CID_REGEX)?.groups?.cid || null; } const CID_REGEX = /^(?:ipfs:\/\/)?(?[a-zA-Z0-9]{46})$/; export function getIpfsCid(str: any): string | null { if (typeof str !== 'string' || !str) return null; return str.trim().match(CID_REGEX)?.groups?.cid || null; } export function getIpfsUrl(str: any): string | null { const cid = getIpfsCid(str); return cid ? `ipfs://${cid}` : null; } export function extractValidCid(str: any): string { const cid = getIpfsCid(str); if (!cid) throw new Error(`Invalid CID ${str}`); return cid; } export async function prepareFormData(info: any) { const data = JSON.stringify(info); const buf = compress(data); const cid = await getContentCID(Buffer.from(buf)); const formData = new FormData(); // This check is needed for proper functionality in the browser, as the Buffer is not correctly concatenated // But, for node we still wanna keep using Buffer const content = typeof window !== 'undefined' && typeof Blob !== 'undefined' ? new Blob([buf]) : Buffer.from(buf); formData.append('data', content); return { cid, formData, content }; } export async function prepareRawFormData(data: Buffer) { const cid = await getContentCID(data); const formData = new FormData(); // This check is needed for proper functionality in the browser, as the Buffer is not correctly concatenated // But, for node we still wanna keep using Buffer const content = typeof window !== 'undefined' && typeof Blob !== 'undefined' ? new Blob([data]) : data; formData.append('data', content); return { cid, formData }; } let _currentRetries = 0; export function setAxiosRetries(totalRetries = 3) { if (_currentRetries === totalRetries) return; _currentRetries = totalRetries; axiosRetry(axios, { retries: totalRetries, shouldResetTimeout: true, retryCondition: () => true, onRetry: (currentRetry, error) => { debug('Failed with error:', error); debug('Retrying...'); if (currentRetry == totalRetries) { error.code = 'RETRY_ERROR'; throw error; } }, retryDelay: axiosRetry.exponentialDelay, }); } // eslint-disable-next-line export async function isIpfsGateway(ipfsUrl: string, _customHeaders?: any) { debug(`is-gateway ${ipfsUrl} false`); return false; } export async function readRawIpfs({ ipfsUrl, cid, customHeaders = {}, timeout = 1000 * 60, }: { ipfsUrl: string; cid: string; customHeaders?: Headers; timeout?: number; }) { const url = new URL(`/api/v0/cat?arg=${cid}`, ipfsUrl.replace('+ipfs', '')); const res = await axios.post( url.toString(), {}, { responseEncoding: 'application/octet-stream', responseType: 'arraybuffer', headers: customHeaders, timeout, } ); return res.data; } export async function readIpfs( ipfsUrl: string, hash: string, customHeaders: Headers = {}, isGateway: boolean, timeout: number, retries = 3 ): Promise { debug(`downloading content from ${hash}`); setAxiosRetries(retries); let result: AxiosResponse; try { if (isGateway) { const res = await axios.get(ipfsUrl + `/ipfs/${hash}`, { responseType: 'arraybuffer', responseEncoding: 'application/octet-stream', headers: customHeaders, timeout, }); result = res.data; } else { // the +ipfs extension used to indicate a gateway is not recognized by // axios even though its just regular https // so we remove it if it exists result = await readRawIpfs({ ipfsUrl, cid: hash, customHeaders, timeout, }); } } catch (err: any) { let errMsg = `could not download cannon package data from "${hash}": ${err.toString()}`; if (ipfsUrl.includes('infura')) { errMsg += '\n\nNOTE: it appears you are using infura for IPFS. Please note that infura tends to be problematic when downloading IPFS artifacts outside of infura itself. Please consider using a different IPFS service.'; } throw new Error(errMsg); } try { return JSON.parse(uncompress(result)); } catch (err: any) { throw new Error(`could not decode cannon package data: ${err.toString()}`); } } export async function writeRawIpfs({ ipfsUrl, data, customHeaders = {}, timeout = 1000 * 60 * 10, retries = 3, }: { ipfsUrl: string; data: Buffer; customHeaders?: Headers; timeout?: number; retries?: number; }): Promise { setAxiosRetries(retries); const { cid, formData } = await prepareRawFormData(data); const url = new URL(`/api/v0/add?local=true&to-files=%2F${cid}`, ipfsUrl); const res = await axios.post(url.toString(), formData, { headers: customHeaders, timeout, }); return res.data; } export async function writeIpfs( ipfsUrl: string, info: any, customHeaders: Headers = {}, isGateway: boolean, timeout: number, retries = 3 ): Promise { setAxiosRetries(retries); if (isGateway) { throw new Error( 'unable to upload to ipfs: the IPFS url you have configured is either read-only (ie a gateway), or invalid. please double check your configuration.' ); } const { cid, formData } = await prepareFormData(info); debug('upload to ipfs:', cid); let result: AxiosResponse; try { result = await axios.post(ipfsUrl.replace('+ipfs', '') + '/api/v0/add?local=true&to-files=%2F' + cid, formData, { headers: customHeaders, timeout, }); } catch (err) { throw new Error( 'Failed to upload to IPFS. Make sure you have a local IPFS daemon running and run `cannon setup` to confirm your configuration is set properly. ' + err ); } debug('upload', result.statusText, result.data.Hash); if (cid !== result.data.Hash) { throw new Error('Invalid CID generated locally'); } return cid; } export async function deleteIpfs( ipfsUrl: string, hash: string, customHeaders: Headers = {}, isGateway: boolean, timeout: number ): Promise { if (isGateway) { // cannot write to IPFS on gateway throw new Error('Cannot delete from IPFS gateway'); } debug('delete from ipfs:', hash); try { await axios.post(ipfsUrl.replace('+ipfs', '') + '/api/v0/pin/rm?arg=' + hash, {}, { headers: customHeaders, timeout }); } catch (err) { throw new Error('Failed to delete from IPFS. ' + err); } } export async function listPinsIpfs(ipfsUrl: string, customHeaders: Headers = {}, isGateway: boolean): Promise { if (isGateway) { throw new Error('Cannot list pinned IPFS files on a gateway endpoint'); } debug('list ipfs pins'); try { const result = await axios.post(ipfsUrl.replace('+ipfs', '') + '/api/v0/pin/ls', { headers: customHeaders }); return Object.keys(result.data.Keys).map((key) => 'ipfs://' + key); } catch (err) { throw new Error('Failed to list ipfs artifacts' + err); } } async function isIPFSRunningLocally(ipfsUrl: string): Promise { try { const response = await axios.post(`${ipfsUrl}/api/v0/id`, {}, { timeout: 2000 }); if (response.status === 200) return true; } catch (error) { return false; } return false; } async function getIPFSAvailabilityScoreLocally(ipfsUrl: string, cid: string): Promise { try { const response = await axios.post(`${ipfsUrl}/api/v0/dht/findprovs?arg=${cid}`, {}, { timeout: 2000 }); const lines = response.data.split('\n').filter(Boolean); let total = 0; for (const line of lines) { const obj = JSON.parse(line); if (obj.Type === 4 && Array.isArray(obj.Responses)) { total += obj.Responses.length; } } return total; } catch (error) { return 0; } } export async function fetchIPFSAvailability(ipfsUrl: string | undefined, cid: string): Promise { if (!ipfsUrl) { return undefined; } if (await isIPFSRunningLocally(ipfsUrl)) { const score = await getIPFSAvailabilityScoreLocally(ipfsUrl, cid); return score; } else { debug('local ipfs unavailable, returning null for ipfs availability. Cannot fetch availability score.'); return undefined; } }