4

I want to deploy my contract on live testnet ( rinkeby) using private key instead of mnemonic of my account and truffle. I have some configurations in truffle.js but I am getting error on migrate :

require('dotenv').config();
const Web3 = require("web3");
const web3 = new Web3();
const WalletProvider = require("truffle-wallet-provider");
const Wallet = require('ethereumjs-wallet');

var rinkebyPrivateKey = new Buffer(process.env["RINKEBY_PRIVATE_KEY"], "hex");
var rinkebyWallet = Wallet.fromPrivateKey(rinkebyPrivateKey);
var rinkebyProvider = new WalletProvider(rinkebyWallet, "https://rinkeby.infura.io/dLqHiBy4LivAhq6bHTiS");


module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8545,
      network_id: "*" // Match any network id
    },
    rinkeby: {
      provider: rinkebyProvider,
      network_id: 4
    },
  },
  solc: {
    optimizer: {
      enabled: true,
      runs: 200
    }
  }
};

I am getting follow error :

/home/rails/Desktop/DeveloperCrowdsale/node_modules/web3/lib/web3/errors.js:35
        return new Error(message);
               ^
Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (/home/rails/Desktop/DeveloperCrowdsale/node_modules/web3/lib/web3/errors.js:35:16)
    at XMLHttpRequest.request.onreadystatechange (/home/rails/Desktop/DeveloperCrowdsale/node_modules/web3/lib/web3/httpprovider.js:115:32)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/rails/Desktop/DeveloperCrowdsale/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/home/rails/Desktop/DeveloperCrowdsale/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpRequestError (/home/rails/Desktop/DeveloperCrowdsale/node_modules/xhr2/lib/xhr2.js:544:12)
    at ClientRequest.<anonymous> (/home/rails/Desktop/DeveloperCrowdsale/node_modules/xhr2/lib/xhr2.js:414:24)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at TLSSocket.socketErrorListener (_http_client.js:387:9)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

Is there any another way to deploy your contract using private key of your account ? or any update needed in my truffle.js file .

3 Answers 3

6

You can take a look at the script with which I deploy contracts to Rinkeby. Usually I use mnemonic, but I modified it to take private key instead, tested it and it works.

You will need to install "truffle-privatekey-provider" with npm for it to work.

const Web3 = require('web3');
//Factory.json file contains my compiled Factory.sol file
const compiledFactory = require('./build/Factory.json');
const PrivateKeyProvider = require("truffle-privatekey-provider");

const privateKey = "acb2f16e4******";

const provider =  new PrivateKeyProvider(privateKey, 'https://rinkeby.infura.io/******');

const web3 = new Web3(provider);

const deploy = async () => {
    const accounts = await web3.eth.getAccounts();

    console.log('Attempting to deploy from account: ', accounts[0]);

    const result = await new web3.eth.Contract(JSON.parse(compiledFactory.interface))
    .deploy({ data: compiledFactory.bytecode })
    .send({ gas: '1000000', from: accounts[0]});

    //This will display the address to which your contract was deployed
    console.log('Contract deployed to: ', result.options.address);
};
deploy();
8

I have a somehwat simpler structure (depending on your taste):

Prerequisite:

npm install truffle-privatekey-provider

or

npm install truffle-hdwallet-provider-privkey

then

const yargs = require('yargs');
var provider, address;
//const HDWalletProvider = require("truffle-hdwallet-provider-privkey");
const privKeyrinkeby = "xxx"
const PrivateKeyProvider = require("truffle-privatekey-provider");

module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8545,
      gas: 6712388,
      // gasPrice: 2000000000, // 1 gwei
      network_id: "*"
    },
    rinkeby: {
      //provider: () => new HDWalletProvider(privKeyrinkeby, "https://rinkeby.infura.io/" + process.env.INFURA_API_KEY),
      provider: () => new PrivateKeyProvider(privKeyrinkeby, "https://rinkeby.infura.io/" + process.env.INFURA_API_KEY),
      gasPrice: 50000000000, // 50 gwei,
      network_id: 3,
    },
    mainnet: {
      // gas: 5000000,
      host: "localhost",
      gasPrice: 1000000000, // 1 gwei
      port: 8545,
      // provider:provider,
      // from: "0x4b3A4F3F42BA61141A4F7101F77dC141AE15c59A",
      from: "0x4b3a4f3f42ba61141a4f7101f77dc141ae15c59a",
      network_id: 1
    }
  },
  mocha: {
    reporter: 'eth-gas-reporter',
    reporterOptions : {
      currency: 'USD',
      gasPrice: 1
    }
  }
};
2

I think it is better for you to use the official HDWalletProvider instead of the PrivateKeyProvider.

For that, you need to include it: const HDWalletProvider = require('@truffle/hdwallet-provider');

And in the networks, configure it like this:

goerli: {
      provider: new HDWalletProvider({
        privateKeys: [process.env.SECRET],
        providerOrUrl: process.env.INFURA_URL
      }),
      network_id: "5"
    },
1
  • This is the correct answer now based on the latest release by truffle.
    – Ravi Ojha
    Commented May 18, 2023 at 17:07

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.