Search code examples
javascriptsolidityweb3jstruffle

How to add private keys to deploy a smart contract to Roptsen?


I am trying to deploy a smart contract to the Ropsten test net.

I have tried to add the private keys mnemonic to a .secret file but get the following error below when running in the terminal truffle migrate --network ropsten

Error: Private key does not satisfy the curve requirements (ie. it is invalid)

The infura api key works by importing it with dot env.

The mnemonic private key is from a metamask wallet.

This is what is in the truffle.config file now:

require('babel-polyfill');
require('dotenv').config();
const HDWalletProvider = require('truffle-hdwallet-provider-privkey');
const MNEMONIC = './.secret';
const infuraKey = process.env.INFURA_API_KEY


module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*" // Match any network id
    },

    ropsten: {
      provider: () => new HDWalletProvider(MNEMONIC, `https://ropsten.infura.io/v3/${infuraKey}`),
      network_id: 3,       // Ropsten's id
      gas: 5500000,        // Ropsten has a lower block limit than mainnet
      confirmations: 2,    // # of confs to wait between deployments. (default: 0)
      timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
      skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
    }
  },
  contracts_directory: './src/contracts/',
  contracts_build_directory: './src/abis/',
  compilers: {
    solc: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  }
}

Solution

  • Someone else encountered this issue here

    There was a suggestion to use Buffer.from, i.e. Buffer.from('<PRIVATE_KEY>', 'hex') to convert your private key to a Buffer first.

    Could try using below:

    const privateKey = Buffer.from('PRIVATEKEYFROMMETAMASK', 'hex');
    const provider = new HDWalletProvider(privateKey, "https://ropsten.infura.io/v3/INFURATOKEN");
    const web3 = new Web3(provider);