Created
August 9, 2021 15:43
-
-
Save nenadjaja/76626d4d503c649656ce75bfbe02968f to your computer and use it in GitHub Desktop.
IPFS helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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