1- import * as fs from 'fs'
2- import pinataSDK from '@pinata/sdk'
31import { create } from 'ipfs-http-client'
42
5- const filePath = '/tmp/upload.txt'
6-
7- export async function saveToIPFS ( file : string | Buffer , name : string ) : Promise < string > {
8- if ( ! process . env . PINATA_KEY || ! process . env . PINATA_SECRET ) {
9- throw new Error ( 'Pinata key missing' )
3+ export function getInfuraNode ( ) {
4+ if ( ! process . env . INFURA_IPFS_USER || ! process . env . INFURA_IPFS_KEY ) {
5+ throw new Error ( 'Infura key missing' )
106 }
117
8+ const infuraAuthKey = Buffer . from (
9+ `${ process . env . INFURA_IPFS_USER } :${ process . env . INFURA_IPFS_KEY } `
10+ ) . toString ( 'base64' )
11+ const infuraNode = create ( {
12+ host : 'ipfs.infura.io' ,
13+ port : 5001 ,
14+ protocol : 'https' ,
15+ headers : {
16+ Authorization : 'Basic ' + infuraAuthKey ,
17+ } ,
18+ } )
19+ return infuraNode
20+ }
21+
22+ export async function saveToIPFS ( file : string | Buffer ) : Promise < string > {
1223 let failedUpload : string | null = null
1324 const failHandler = ( name : string ) => ( ) => {
1425 if ( failedUpload ) {
@@ -18,28 +29,17 @@ export async function saveToIPFS(file: string | Buffer, name: string): Promise<s
1829 return { IpfsHash : null , path : null }
1930 }
2031
21- const pinata = pinataSDK ( process . env . PINATA_KEY , process . env . PINATA_SECRET )
22- fs . writeFileSync ( filePath , file )
23- const pinataPromise = pinata
24- . pinFromFS ( filePath , {
25- pinataMetadata : {
26- name,
27- // @ts -ignore
28- keyvalues : {
29- type : 'module' ,
30- } ,
31- } ,
32- } )
33- . catch ( failHandler ( 'pinata' ) )
32+ const infuraNode = getInfuraNode ( )
33+ const infuraPromise = infuraNode . add ( file )
3434
3535 const graphNode = create ( 'https://api.thegraph.com/ipfs/api/v0' as any )
3636 const graphPromise = graphNode . add ( file )
3737
3838 const csNode = create ( 'https://ipfs.cryptostats.community' as any )
3939 const csPromise = csNode . add ( file ) . catch ( failHandler ( 'CryptoStats' ) )
4040
41- const [ pinataResult , graphResult , csResult ] = await Promise . all ( [
42- pinataPromise ,
41+ const [ infuraResult , graphResult , csResult ] = await Promise . all ( [
42+ infuraPromise ,
4343 graphPromise ,
4444 csPromise ,
4545 ] )
@@ -48,12 +48,12 @@ export async function saveToIPFS(file: string | Buffer, name: string): Promise<s
4848 console . warn ( `2 out of 3 uploads successful, upload to ${ failedUpload } failed` )
4949 }
5050
51- if ( pinataResult . IpfsHash && graphResult . path && pinataResult . IpfsHash !== graphResult . path ) {
52- throw new Error ( `Mismatched CIDs: ${ pinataResult . IpfsHash } & ${ graphResult . path } ` )
51+ if ( infuraResult . path && graphResult . path && infuraResult . path !== graphResult . path ) {
52+ throw new Error ( `Mismatched CIDs: ${ infuraResult . path } & ${ graphResult . path } ` )
5353 }
54- if ( pinataResult . IpfsHash && csResult . path && pinataResult . IpfsHash !== csResult . path ) {
55- throw new Error ( `Mismatched CIDs: ${ pinataResult . IpfsHash } & ${ csResult . path } ` )
54+ if ( infuraResult . path && csResult . path && infuraResult . path !== csResult . path ) {
55+ throw new Error ( `Mismatched CIDs: ${ infuraResult . path } & ${ csResult . path } ` )
5656 }
5757
58- return pinataResult . IpfsHash || graphResult . path !
58+ return infuraResult . path || graphResult . path !
5959}
0 commit comments