1

I am trying to deploy a contract to Parity via Web3.js.

Here is my code:

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

let NODE_URL = "http://localhost:8545";
let OWNER_ADDRESS = "0x...";

let web3 = new Web3(new Web3.providers.HttpProvider(NODE_URL));

async function deploy(contractName, contractArgs) {
    let abi = fs.readFileSync(contractName + ".abi").toString();
    let bin = fs.readFileSync(contractName + ".bin").toString();
    let tx = {from: OWNER_ADDRESS, gas: 2310334, data: "0x" + bin};
    return await web3.eth.contract(JSON.parse(abi)).new(...contractArgs, tx);
}

deploy("MyContract", []);

When I execute it (with NodeJS), it pends forever.

Then, when I open Parity GUI, I see a popup window asking me to sign the transaction.

How can I sign this transaction programmatically (i.e., as part of my code)?

I think I need to use the web3.eth.signTransaction function with the tx object and my private key, but I haven't been able to figure out how.

Thank you!!!

UPDATE:

I got the general idea from this post, which suggest:

before contract.new(params) please unlock an account

It then provides the following code:

curl --data '{"method":"personal_unlockAccount","params":["0xXX","your password",null],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST localhost:8545

But I'm not really sure what exactly I'm supposed to do with this piece of code, how to run it, etc (I'm on Windows for all it matters).

UPDATE #2:

OK, I've made some progress on this.

The equivalent of the curl command above can be added to my NodeJS script as:

let http = require("http");

let data = '{"method":"personal_unlockAccount","params":["0xMyAccount","My Password",null],"id":1,"jsonrpc":"2.0"}';

let options = {
  protocol: 'http:',
  host: 'localhost',
  port: 8545,
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(data)
  }
};

let req = http.request(options, function(res) {
  // Now I should be able to deploy the contract
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

req.write(data);
req.end();

And then, in the callback function passed to http.request, I should be able to deploy the contract (i.e., execute the code at the beginning of my question).

However, I get the following response for this request:

STATUS: 200
HEADERS: {
    "content-type": "application/json",
    "transfer-encoding": "chunked",
    "date": "..."
}
BODY: {
    "jsonrpc": "2.0",
    "error": {
        "code": -32601,
        "message": "Method not found"
    },
    "id":1
}

I found the description of the personal_unlockAccount method on Parity's JSON-RPC API here, but I cannot find anything wrong in the way that I am calling via the HTTP request.

UPDATE #3:

With the help of this answer, which list Parity options nicely, I've managed to resolve this problem as well, by starting Parity with the personal API enabled:

parity --jsonrpc-apis=personal

The http response that I receive now is:

"error": {
    "code": -32023,
    "message": "Unable to unlock the account.",
    "data": "InvalidAccount"
}

I assume that I should probably start Parity also with the following flags:

--chain=../../parity/localcoin.json
--password=MyPasswordFile.txt
--engine-signer=0xMyAccountAddress
--ntp-servers=127.0.0.1:123
--reseal-on-txs=none
--force-sealing
--mode=offline
--light

Or at least with some of them...

UPDATE #4:

Using the switches above in combined with --jsonrpc-apis=personal, allows me to successfully invoke the personal_unlockAccount method, via an http request, to which the response is:

{"jsonrpc":"2.0","result":true,"id":1}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.