Skip to content

Instantly share code, notes, and snippets.

@nenadjaja
Created August 9, 2021 15:43
Show Gist options
  • Save nenadjaja/76626d4d503c649656ce75bfbe02968f to your computer and use it in GitHub Desktop.
Save nenadjaja/76626d4d503c649656ce75bfbe02968f to your computer and use it in GitHub Desktop.
IPFS helpers
import base from 'base-x'
const ipfsClient = require('ipfs-http-client')
const ENDPOINT = new URL('api/v0', 'http://ipfs.network.thegraph.com/').href
const ipfs = new ipfsClient(ENDPOINT)
// convert ipfsHash to Hex string
export const ipfsHexHash = (ipfsHash: string) => {
const base58 = base('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz')
return '0x' + base58.decode(ipfsHash).slice(2).toString('hex')
}
// upload files to IPFS
export const uploadToIpfs = async (data: any): Promise<string> => {
let result
try {
result = await ipfs.add(data)
} catch (e) {
console.error(`Failed to upload to IPFS: ${e}`)
return
}
return result.path
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment