[Ethereum] Deploy contract on live testnet using private key + truffle

go-ethereumsoliditytruffle

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 .

Best Answer

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();
Related Topic