0

I am trying to sign a message using following JavaScript code (from HERE)

function constructPaymentMessage(contractAddress, amount) {
  return ethereumjs.ABI.soliditySHA3(
    ["address", "uint256"],
    [contractAddress, amount],
  );
}

function signMessage(message, callback) {
  web3.personal.sign("0x" + message.toString("hex"), web3.eth.defaultAccount,
    callback);
}

// contractAddress is used to prevent cross-contract replay attacks.
// amount, in wei, specifies how much ether should be sent.
function signPayment(contractAddress, amount, callback) {
    var message = constructPaymentMessage(contractAddress, amount);
    signMessage(message, callback);
}

I have tried several approaches to run this code and get its output (signed payment), however, I could not run it successfully.

HERE is the screen shot of the .js file I created and HERE is the error message I receive when I run it by node sign.js . And when I remove callback from the code, I receive another error.

What is the correct approach to run this code to get its output (i.e. signed message) ?

UPDATE: I modified the code as follows:

Web3 = require("web3")
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

var ethereumjs = require('ethereumjs-abi')

web3.eth.defaultAccount = '0xca35b7d915458ef540ade6068dfe2f44e8fa733c';


function constructPaymentMessage(contractAddress, amount) {
  return ethereumjs.soliditySHA3(
    ["address", "uint256"],
    [contractAddress, amount],
  );
}

function signMessage(message, callback) {
  //web3.eth.personal.sign("0x" + message.toString("hex"), web3.eth.defaultAccount, callback);

web3.eth.sign("0x" + message.toString("hex"),"0x135a7de83802408321b74c322f8558db1679ac20");

}

// contractAddress is used to prevent cross-contract replay attacks.
// amount, in wei, specifies how much ether should be sent.
function signPayment(contractAddress, amount, callback) {
    var message = constructPaymentMessage(contractAddress, amount);
    signMessage(message, callback);
}

signPayment('0xca35b7d915458ef540ade6068dfe2f44e8fa733c', 1, function(err, something) {});

I need to run testrpc before running this code. Now the error message is as follows:

"Error: Returned error: Error: cannot sign data; no private key"

And here is screen shot of the error message :

enter image description here

NOTE 1: I am using web3.js version 1.0.

NOTE 2: I run this JavaScript file in Ubuntu Terminal: node filename.js.

NOTE 3: Since I use web3.eth.personal.sign(dataToSign, address, password [, callback]) I do not need to use a password for this address ? > web3.eth.personal.sign("0x" + message.toString("hex"), web3.eth.defaultAccount);

NOTE 4: There is a similar problem here: similar problem but in remix.

NOTE 5: And alternatively, is there any other approach to sign such a message instead of using web3.js and ethereumjs-abi ?

  • What is you replace with 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c? – Xavier Leprêtre B9lab Jul 3 '18 at 17:03
  • @Xavier Leprêtre B9lab, I used it as an example for contractAddress input parameter. to invoke signPayment function. Thanks. – Questioner Jul 3 '18 at 17:42
  • 1
    The new error is because web3.eth.defaultAccount is null. Perhaps put in the actual address you intend to sign with (the account you've set up in the local Ethereum node). – smarx Jul 3 '18 at 19:21
  • 1
    Look carefully at the difference between 0xca35b7d915458ef540ade6068dfe2f44e8fa733c and 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c. One has the capitalization checksum on, the other not. This is what your original error message was about. – Xavier Leprêtre B9lab Jul 3 '18 at 19:36
  • 1
    Make sure the account you're using is one that exists in your Ethereum node (geth, Parity, ganache, or whatever). The error indicates that the node doesn't know the private key for that account. – smarx Jul 4 '18 at 21:27

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.