1

I understand that i will need to be able to unlock my account in web3 to be able to deploy smart contracts. It's all good except i'm using Infura's hosted network instead which prevents me from interacting with accounts. I use keythereum and other methods to create user wallet generating the privatekey and public addresses.

My real questions are:

1. How can i compile my solidity contract code with solc(Nodejs package) and then "sign" it or deploy it to the Ethereum network?

2. Is there a method for me to pass in the privatekey and use that account to deploy the smart contract?

2 Answers 2

1

You can use different methods:

  1. Sign the tx yourself and use sendSignedTransaction function. Described here: https://medium.com/@yukkuri.sinai/sign-the-transaction-on-your-own-with-web3-js-for-infura-io-175860c50ab7.

    They don't deploy any contracts in the article, but it's simple enough to build a 'deploy contract' tx yourself. Just send to address 0x0 and put the compiled code in the data. Example: https://github.com/ethereumjs/ethereumjs-tx/blob/master/examples/transactions.js

  2. Use a provider engine as described here: How to login with web3 without MetaMask?

    Basically what you do is to have a wallet provider which will sign the transaction locally and a HTTP provider (infura) to send it to the network.

3
  • Received the following error setting the "to:" parameter in web3.eth.accounts.signTransaction to "0x0"; Error: Provided address "0x0" is invalid, the capitalization checksum test failed, or its an indrect IBAN address which can't be converted.
    – Zinxer
    Sep 1, 2018 at 22:13
  • Try putting this as address: 0x0000000000000000000000000000000000000000
    – lunr
    Sep 1, 2018 at 22:21
  • I don't think there is a need to include the above in the "to" parameter. Both method don't seems to work. I've stated the findings i had and that seemed to answer my question. Nevertheless, i did manage to dig deeper after reading your answer. Thanks lunr!
    – Zinxer
    Sep 3, 2018 at 2:53
1

This is eventually what i did to deploy smart contract programmatically without setting up my own node (using infura):

    var appRoot = process.cwd()
    let source = lib.fs.readFileSync(appRoot + '/contracts/forward.sol', 'utf8');
    let compiledContract = lib.solc.compile(source);

    //Retrieve ABI and byteCode
    for (let contractName in compiledContract.contracts) {
        var byteCode = compiledContract.contracts[contractName].bytecode;
        var abi = JSON.parse(compiledContract.contracts[contractName].interface);
    }

    //Instantiate contract and set deploy object.
    let contractInstance = new lib.web3.eth.Contract(abi);
    let deploy = contractInstance.deploy({
        data: '0x' + byteCode
    }).encodeABI();

I then later convert it to HEX and include it in the data param using web3.eth.accounts.signTransaction shown here:

lib.web3.eth.accounts.signTransaction({
                        from: creator,
                        gas: lib.web3.utils.hexToNumberString(result[2]),
                        gasPrice: result[1],
                        data: lib.web3.utils.toHex(deploy),
                        nonce: nonce
                    }, privateKey);

Voila!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.