0

I have set a secret for my project in infura.

Here is my truffle-config.js file:

module.exports = {
  networks: {
    ...
    ropsten: {
      provider: new HDWalletProvider(mnemonic, "https://ropsten.infura.io/xxxxx"),
      network_id: 3
    }
  }
};

My question is: Where can i put secret in this configuration file. I have read a lot of documentation/turorials and there is nothing about this secret.

Thanks a lot

1
  • did you figure this out? I have the same issue, current answers don't seem to understand the question in the first place
    – Pablote
    Commented Sep 27, 2021 at 23:25

4 Answers 4

1

After some trial and error I've found a way to make this work. Not sure if this is the only way or the best, but here it goes:

const provider = new HDWalletProvider(process.env.WALLET_MNEMONIC, `https://:${process.env.PROVIDER_SECRET}@rinkeby.infura.io/v3/${process.env.PROVIDER_ID}`);
const web3 = new Web3(provider);

Notice how there's a : before the secret and a @ after. This works because Infura expects this as a basic auth with no username, only password, which is the secret.

0

You should be able to use something like a .env file to secure your project secret and keep it from being seen. Alternatively, you could take an approach similar to what this user did for their

1
  • This is not what the question asks. The question is, how do you actually use the secret so that it's sent to infura.
    – Pablote
    Commented Sep 27, 2021 at 23:26
0

Make a .env file in the same folder:

MNEMONIC="Your MNEMONICs"
INFURA_URL="Your Infura URL with API key"

Then replace the truffle-config.js code with this:

const HDWallet = require('@truffle/hdwallet-provider');
const dotenv = require('dotenv');
dotenv.config();
const mnemonic = process.env.MNEMONIC;

module.exports = {
  networks: {
    kovan: {
       provider: () => new HDWallet(mnemonic, process.env.INFURA_URL),
       network_id: process.env.NETWORK_ID,       
       gas: 5500000,        
       confirmations: 0,    
       timeoutBlocks: 200,  
       skipDryRun: true     
     },
  },
3
  • Thanks that is exactly what i have done in my real project. I have removed env var stuff for my question. but there is no infura secret in your answer...
    – Bob5421
    Commented Apr 15, 2021 at 6:11
  • Go to infura.io, after that you can signup and get the secret. Commented Apr 15, 2021 at 6:20
  • I have the secret ! What i want to do is to know how i can set this secret in truffle-config.js
    – Bob5421
    Commented Apr 15, 2021 at 8:54
0

Regarding the Project secret, this is an optional value for authenticating and securing your HTTP traffic to Infura. This option is disabled by default and not needed by truffle. Is there a particular reason why you would need to truffle to connect to infura in this way?

2
  • I would like to configure this secret in truffle for security reasons. I don’t want someone to use my infura account
    – Bob5421
    Commented Aug 29, 2021 at 13:02
  • Infura docs even say that's an additional protection you should use if you can secure the secret.
    – Pablote
    Commented Sep 27, 2021 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.