embarklabs / embark

Framework for serverless Decentralized Applications using Ethereum, IPFS and other platforms

Home Page:https://framework.embarklabs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Embark.Storage don't works with infura when hosted in different domain due CORS restrction

3esmit opened this issue · comments

Bug Report

Summary

I am trying to use Embark.Storage.uploadFile, however whenever I try to use it with Infura I get this error on Browser JS Console :

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://ipfs.infura.io:5001/api/v0/version?stream-channels=true. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
Could not connect to a storage provider using any of the dappConnections in the storage config storage.js:107
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://ipfs.infura.io:5001/api/v0/version?stream-channels=true. (Reason: CORS request did not succeed).

How to reproduce

in storage.js use this config:

module.exports = {
  default: {
    enabled: true,
    ipfs_bin: "ipfs",
    available_providers: ["ipfs"],
    upload: {
      provider: "ipfs",
      host: "ipfs.infura.io",
      port: 5001
    },
    dappConnection: [
      {
        provider:"ipfs",
        host: "ipfs.infura.io",
        port: 5001,
        getUrl: "https://ipfs.infura.io/ipfs/"
      }
    ]
  }
};

And try to use Embark.Storage.uploadFile(input) or Embark.Storage.addText(string)

Expected behavior

No CORS error, such as when using this snippet I am currently using as workaround:

async function ipfsAdd(data) {
    const formData  = new FormData();
    formData.append("", data);
    const response = await fetch("https://ipfs.infura.io:5001/api/v0/add", {
        method: 'post',
        body: formData
    });
    if (response.status === 200) {
        return (await response.json()).Hash;
    } else {
        throw new Error("Bad response status from ipfs.infura.io: " + response.status)
    }
}

Please provide additional information about your system

OS: Linux
Embark Version: 4.1.1
Node Version: 10.16
NPM Version: 6.12

Sometimes issues are related to Embark's installation. Can you provide information on how Embark was installed?

As a project dependency. (I'm using npx embark run)