Using agent-js with custom domain error

Trying to setup this custom domain:

https://openfpl.xyz/

But I get an error:

image

Followed the documentation but not sure what I’ve missed.

Hello! Glad to see that speed dial is working now :wink:

The /status endpoint that the fetchRootKey() function calls should NEVER be called on mainnet.

This should only be called when you’re running against the local DFX replica because that replica will have a different root key than the mainnet network. The mainnet root key is hardcoded in agent-js, but if fetchRootKey() is called then this hardcoded key will be replaced with whatever is returned from the /status endpoint, which could be malicious key.

So there is a service worker bug that prevents dapps on custom domains from calling the /status endpoint, but we haven’t prioritized fixing it because nobody should be doing that anyway.

So why is it being called for your canister? It’s called if the process.env.DFX_NETWORK variable !== ic, so the root cause of why can likely be found in Webpack.

1 Like

So in here?:

Yes although I don’t see anything wrong with it… I’ll clone and try debug it.

1 Like

I get this error when I try to build:

import error [M0009], file "/home/nathan/programming/OpenFPL/.dfx/local/canisters/idl/ryjl3-tyaaa-aaaaa-aaaba-cai.did" does not exist

Am I missing a step in your build process?

So currently I search //local dev and adjust some comments, my main.mo has some canister defintions, could be that.

That’s a little vague. If you can provide a clear set of steps to build the canister from a fresh clone then I’ll be happy to debug this.

Sorry mate,

I think all you will need to do is switch this definition from the live canisters to local:

@NathanosDev not sure but my first assumption is rather that the issue is not DFX_NETWORK but the host not being set to agent-js http client.

OP website works on mainnet when used with canister-id.icp0.io but, does not when use on the custom domain.

I had a look to the code but don’t know how to set the host when Actor.agentOf(... is being used.

Unfortunately I get the same error. I see there’s this candid/nns-ledger.did file being referenced in dfx.json, I copied in the ledger did file but it doesn’t match what the Motoko library is expecting. Where did you source this file from?

You’re right that setting the host to icp-api.io may help the /status endpoint succeed, but we don’t want that call being made in the first place.

Moving to the custom domain has exposed a security issue that existed on https://bgpwv-eqaaa-aaaal-qb6eq-cai.icp0.io/.

1 Like

I followed some documentation to setup my local NNS but I can’t find it!

It was on the official docs somewhere, will let you know when I’ve found it.

In webpack.config try:

new webpack.EnvironmentPlugin({
      NODE_ENV: "development", // <--- change this to NODE_ENV: process.env.NODE_ENV or remove the line maybe? 
      II_URL: internetIdentityUrl,
      DFX_NETWORK: network, // <---- add this
      ...canisterEnvVariables,
    }),

Did not debugged but, that should maybe do.

2 Likes

ahhh I think you’re right! nicely spotted

I think this is a new error!

I would say the DFX_NETWORK / fetchRootKey is solved but, now it’s the host issue I mentionned.

I have committed the latest changes.

1 Like

Since we cannot deploy locally your project, would you mind pushing your declarations file to the repo? Or can you share the content of import { OpenFPL_backend as open_fpl_backend } from '../../../../declarations/OpenFPL_backend';?

Ok I have pushed them.

1 Like

Thanks. Generally speaking, I’m one of those that think that declaration files should be pushed with the repo and not excluded as generated by dfx per default.

I think the problem is the host, however you cannot set the host the way you are using the actor currently as for example OpenFPL_backend is hardcoded within the files without host.

e.g src/declarations/OpenFPL_backend/OpenFPL_backend.did.js

export const createActor = (canisterId, options = {}) => {
  const agent = options.agent || new HttpAgent({ ...options.agentOptions });
 ...
};

export const OpenFPL_backend = createActor(canisterId);

Unfortunately you cannot modify this code because it is generated automatically - any modifications would be overwritten by next deploy.

So what you need to do is declare your own actors.

You can for example declare each canisters in your own code and do something like:

export const OpenFPL_backend = createActor(canisterId, process.env.DFX_NETWORK === "ic" ? {
  agentOptions: { host: "https://icp-api.io" }
} : {});

And then replace all reference in your code from old OpenFPL_backend to your own declaration