UNPKG

@iexec/web3mail

Version:

This product enables users to confidentially store data–such as mail address, documents, personal information ...

39 lines (34 loc) 952 B
import { create } from 'kubo-rpc-client'; import { IPFS_UPLOAD_URL, DEFAULT_IPFS_GATEWAY } from '../config/config.js'; interface GetOptions { ipfsGateway?: string; } interface AddOptions extends GetOptions { ipfsNode?: string; } const get = async ( cid, { ipfsGateway = DEFAULT_IPFS_GATEWAY }: GetOptions = {} ) => { const multiaddr = `/ipfs/${cid.toString()}`; const publicUrl = `${ipfsGateway}${multiaddr}`; const res = await fetch(publicUrl); if (!res.ok) { throw Error(`Failed to load content from ${publicUrl}`); } const arrayBuffer = await res.arrayBuffer(); return new Uint8Array(arrayBuffer); }; const add = async ( content, { ipfsNode = IPFS_UPLOAD_URL, ipfsGateway = DEFAULT_IPFS_GATEWAY, }: AddOptions = {} ) => { const ipfsClient = create(ipfsNode); const { cid } = await ipfsClient.add(content); await get(cid.toString(), { ipfsGateway }); return cid.toString(); }; export { add, get };