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 :
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
?
0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c
? – Xavier Leprêtre B9lab Jul 3 '18 at 17:03web3.eth.defaultAccount
isnull
. Perhaps put in the actual address you intend to sign with (the account you've set up in the local Ethereum node). – user19510 Jul 3 '18 at 19:210xca35b7d915458ef540ade6068dfe2f44e8fa733c
and0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c
. One has thecapitalization checksum
on, the other not. This is what your original error message was about. – Xavier Leprêtre B9lab Jul 3 '18 at 19:36