I am looking at the web3js documentation to deploy a new contract, however, I see no mention of how to sign the transaction that will deploy the contract in the blockchain...document makes no mention of signing with private key...will appreciate any insight into this

here is the example code snippet from the docs (https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#eth-contract)

myContract.deploy({ data: '0x12345...', arguments: [123, 'My String'] }) .send({ from: '0x1234567890123456789012345678901234567891', gas: 1500000, gasPrice: '30000000000000' }, function(error, transactionHash){ ... }) .on('error', function(error){ ... }) .on('transactionHash', function(transactionHash){ ... }) .on('receipt', function(receipt){ console.log(receipt.contractAddress) // contains the new contract address }) .on('confirmation', function(confirmationNumber, receipt){ ... }) .then(function(newContractInstance){ console.log(newContractInstance.options.address) // instance with the new contract address });

3 Answers 3


enter image description here

This is the method which can be used to sign transactions.


Try this (using web3.js v1):

let fs = require("fs");
let Web3 = require("web3");

let web3 = new Web3(NODE_ADDRESS);

async function send(transaction) {
    let gas = await transaction.estimateGas({from: PUBLIC_KEY});
    let options = {
        to  : transaction._parent._address,
        data: transaction.encodeABI(),
        gas : gas
    let signedTransaction = await web3.eth.accounts.signTransaction(options, PRIVATE_KEY);
    return await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);

async function deploy(contractName, contractArgs) {
    let abi = fs.readFileSync(contractName + ".abi").toString();
    let bin = fs.readFileSync(contractName + ".bin").toString();
    let contract = new web3.eth.Contract(JSON.parse(abi));
    let handle = await send(contract.deploy({data: "0x" + bin, arguments: contractArgs}));
    console.log(`${contractName} contract deployed at address ${handle.contractAddress}`);
    return new web3.eth.Contract(JSON.parse(abi), handle.contractAddress);

async function run() {
    let myContract = await deploy("MyContract", [123, "My String"]);

Using Web3.js 1.0.0

 var contractInstance = new web3.eth.Contract(contractABI);
var deployData = contractInstance.deploy({
    data : contractByteCode

var tx = {
    data : deployData

web3.eth.accounts.signTransaction(tx, privateKey).then (signed => {
    web3.eth.sendSignedTransaction(signed.rawTransaction).on('receipt', console.log)

signTransaction is the function you need to sign your transaction with the private key. For that you need to construct a transaction with the required data for the contract deployment i.e. contract abi and its byte code. Now, you can sign the trasaction and send it. You will get contract address of the newly deployed contract and other information in transaction receipt.

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.