9

I got an error at sendSignedTransaction is : Project ID is required. Can you please let me know that what is the actual problem?

This is my code:

try {
    const ethereumUri = 'https://ropsten.infura.io/API_KEY'; //process.env.API_URL

    var web3 = new Web3(new Web3.providers.HttpProvider(ethereumUri));

    const keystore = web3.eth.accounts.encrypt(req.body.privateKey, 'Drc@1234');

    const privateKey = new Buffer.from(req.body.privateKey.substr(2), 'hex')

    const decryptedAccount = web3.eth.accounts.decrypt(keystore, 'Drc@1234');

    var amount = web3.utils.toWei(req.body.transaction_value.toString(), "ether");

    var fee_eth = req.body.transaction_fee * 1000000000;
    var convertTOGwei = fee_eth / 21000;
    var decimalLimit = convertTOGwei.toFixed(2);
    var fee = web3.utils.toWei(decimalLimit.toString(),'gwei'); 

    var sender = req.body.from_address;
    var receiver = req.body.to_address;

    /*var rawTransaction = {
        "from": sender,
        "to": receiver,
        "gasPrice": web3.utils.toHex(fee),
        "value": web3.utils.toHex(amount),
        "gas": 21000,
        "chainId": 3
    };*/

    const txCount = await web3.eth.getTransactionCount(sender)

    // Transaction
     const txObject = {
        nonce:    web3.utils.toHex(txCount),
        to:       receiver,
        value:    web3.utils.toHex(amount),
        gasLimit: web3.utils.toHex(21000),
        gasPrice: web3.utils.toHex(fee)
     }

    // Sign the transaction
    const tx = new Tx(txObject, { chain: 'ropsten', hardfork: 'istanbul' })
    const pk = Buffer.from(privateKey, 'hex')
    tx.sign(pk)

    const serializedTx = tx.serialize()
    const raw = '0x' + serializedTx.toString('hex')
     // Broadcast the transaction
    const receipt = await web3.eth.sendSignedTransaction(raw)
    return receipt;
} catch (e) {
    console.error(`try/catch Error:(${e})`);
}
6
  • We need more context. What's your code for example ? Do you send your transaction directly to a node or to an API that has a node for you (infura ? ) ? Commented Jan 22, 2020 at 12:28
  • @FlorianCastelain, I do a code in node js. I have edited my question and paste my code here Commented Jan 22, 2020 at 12:34
  • Infura requires a project ID (provided to you when you open an account there). So your API_KEY up there looks rather suspicious. Commented Jan 22, 2020 at 12:42
  • INdeed. Read this: infura.io/docs/gettingStarted/makeRequests Your "APIKEY" shoud be the project id, like so: "mainnet.infura.io/v3/YOUR-PROJECT-ID" Commented Jan 22, 2020 at 12:48
  • project id will get from Infura? Is it free? Commented Jan 22, 2020 at 12:49

4 Answers 4

11

You need to:

  1. Sign up with Infura.io
  2. Login into Infura.io
  3. Click CREATE NEW PROJECT button and enter project name

Then in project details screen you will see your endpoint URL. Something like this: mainnet.infura.io/v3/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx. This URL you should use in your code. You may also configure additional security settings for your project, either to require project secret (good for backend applications) or to whitelist origins (good for frontends). Origin is basically where you frontend app is deployed. If you are just developing locally, origin like localhost:3000 could be a good choice.

2
3

No, until today it was possible to connect Infura as ETH service provider by this endpoint mainnet.infura.io/PROJECT_SECRET

But today smth went wrong and it trows an exception with message "project ID is required"

Setting mainnet.infura.io/v3/PROJECT_ID as an endpoint throws "401 Unauthorized"

1

The problem is solved!!!

In the infura site:

Step 1. Create IPFS project with card info

Step 2. replace the following code with your old code

const projectId = '2E8Kps3Xij.................';   //(Step 3. Place the project id from your infura project)
const projectSecret = 'b222afc94.....................';  //(Step 4. Place the project_secrect from your infura project)

const auth = 'Basic ' + Buffer.from(projectId + ':' + projectSecret).toString('base64');


const ipfsClient = require('ipfs-http-client')
const ipfs = ipfsClient.create({ 
  host: "ipfs.infura.io",
   port: 5001, 
   protocol: "https",
   apiPath: "/api/v0",
   headers: {
       authorization: auth,
   }, 
  });

Step 5. Save and npm start


https://github.com/SaifulJnU/IFPS_Template/blob/master/IPFS%20infura%20unauthorized%20problem.txt

0

login to your https://infura.io/ panel then you can get your project secret and Id

https://infura.io/

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.