1

Note: I updated the html / JavaScript code according to the answer and I used THIS META MASK sample code for web3 provider. However, I receive the same error. I eventually did not understand that do I need run testRPC or not ?.

I run following html JavaScript code to generate a signature after running testRPC and using Meta Mask as follows:

(1) run testRPC :

PS C:\Users\s> testrpc
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2

Available Accounts
==================
(0) 0xf4d2b942c3baebef5b81f4b909904015166b238a
(1) 0xef0d1e102d33252f232b39b31950f736ab2535aa
(2) 0x23064a138dc6b681a51a18b837a376f061fbc511
(3) 0x2dfaf5c48bc3c868f63741d6cd0489b336e74b03
(4) 0x372110011158860806d9d9b19aa67c045740727a
(5) 0x3faa74be50e1dfa86e5f6951a8c11b3e726edf65
(6) 0xaecc4c8f0b802b83a2cd84e6c98ecad6b0495c00
(7) 0x36b08ae47ec94870b98b0e0d98fca2646332ae87
(8) 0x1461cfb91f6fd2aca8d722dce9f67f7806732e49
(9) 0x1a0b2fd11e57f6473ae5f9b55e17cbfb6d8b504e

Private Keys
==================
(0) acb8b8ee76ce99fc390127ae88e246d3f8d8ac3db7
(1) 8957622705ccec21da4791f8535c70ab4726bfbe08
(2) 41181e3cf8c0294e949062a894bd96aea15e342db5
(3) c1bad63470c62b56032b034df0f0b827779e678cfd
(4) f49109620e9e56aa0bff9bdeb486d05ca75732cf1a
(5) ed1107372972a702c166924846f6d01234b9d277a7
(6) 10afce272e6a3d1d3474c385bf28be7e58de991995
(7) 0a92c9aa01497d29f01023cdeb50429954be06c949
(8) 87d0c56f9cab4c86ebc66749d0894719b647f72cea
(9) c8ca68e445b249a7d860fc3d76b210d6dd1a1c2197

HD Wallet
==================
Mnemonic:      access invest retire civil eagl
Base HD Path:  m/44'/60'/0'/0/{account_index}

(2) selecting 0xf4d2b942c3baebef5b81f4b909904015166b238a as signer address.

(3) adding 0xf4d2b942c3baebef5b81f4b909904015166b238a to Meta Mask.

(4) running following html / JavaScript code in Google Chrome :

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>EthereumJS - Browser Example</title>
</head>

<body>
  <script src="https://rawgit.com/ethereumjs/browser-builds/master/dist/ethereumjs-abi/ethereumjs-abi-0.6.5.js"></script> 
  <script src="https://rawgit.com/ethereum/web3.js/develop/dist/web3.js"></script> 
  <script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>

  <script>

window.addEventListener('load', function() {

   // Checking if Web3 has been injected by the browser (Mist/MetaMask)
  if (typeof web3 !== 'undefined') {
    // Use Mist/MetaMask's provider
    web3 = new Web3(web3.currentProvider);
  } else {
    console.log('No web3? You should consider trying MetaMask!')
    // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
  }

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

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

  var message = constructPaymentMessage("0x0f0f422477d83bcbed227b0ae2fa3ace7ea3c653", 100);
  console.log(message);

  web3.personal.sign("0x" + message.toString("hex"), "0xF4D2B942c3baebef5B81F4B909904015166b238A", function(err, signature) {
  console.log(signature);
  console.log(err);
  // Be sure to make use of the signature only here.
  // It will not be defined until this callback is invoked.
});

}) 
  </script>
</body>
</html>

And the following is the result :

enter image description here

Pleas note that

Uint8Array(32) [2, 16, 48, 216, 191, 103, 173, 178, 132, 121, 144, 241, 45, 224, 83, 239, 76, 206, 69, 151, 42, 86, 174, 144, 3, 97, 172, 171, 166, 231, 234, 10]

is the message value and result of console.log(message); and not signature value and when I use console.log(signature); the output is only undefined.

How can I see signature value ?

And the following is the error message, result of console.log(err); :

Error: Method personal_sign not supported

Error: Error: Method personal_sign not supported.
    at GethApiDouble.handleRequest (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:89858:16)
    at next (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:59378:18)
    at GethDefaults.handleRequest (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:89767:12)
    at next (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:59378:18)
    at FilterSubprovider.handleRequest (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:65832:7)
    at next (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:59378:18)
    at DelayedBlockFilter.handleRequest (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:89595:3)
    at next (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:59378:18)
    at ReactiveBlockTracker.handleRequest (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:89722:12)
    at next (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:59378:18)
    at GethApiDouble.handleRequest (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:89858:16)
    at next (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:59378:18)
    at GethDefaults.handleRequest (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:89767:12)
    at next (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:59378:18)
    at FilterSubprovider.handleRequest (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:65832:7)
    at next (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:59378:18)
    at DelayedBlockFilter.handleRequest (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:89595:3)
    at next (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:59378:18)
    at ReactiveBlockTracker.handleRequest (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:89722:12)
    at next (C:\Users\s\AppData\Roaming\npm\node_modules\ethereumjs-testrpc\build\cli.node.js:59378:18)
    at Object.InvalidResponse (https://rawgit.com/ethereum/web3.js/develop/dist/web3.js:3138:16)
    at https://rawgit.com/ethereum/web3.js/develop/dist/web3.js:6375:36
    at XMLHttpRequest.request.onreadystatechange (https://rawgit.com/ethereum/web3.js/develop/dist/web3.js:4377:7)

Related question: "UPDATED : " "VM Exception while processing transaction: revert"

Questioner
  • 2,670
  • 2
  • 33
  • 62
  • 1
    There is no reason to include "web3.js" and "web3.min.js". They are the same thing. – Shawn Tabrizi Jul 22 '18 at 20:08
  • @Shawn Tabrizi Do you mean I can remove one of them ? Thanks – Questioner Jul 22 '18 at 20:10
  • 1
    Yes. You should probably only be using the web3.min.js file unless you want to look at the source code. See Minification (programming) – Shawn Tabrizi Jul 22 '18 at 20:12
  • @Shawn Tabrizi According to comment of smarx I need to add MetaMast web3 provider in the code. Does it mean I need to replace HttpProvider("http://localhost:8545")) by something else ? Thanks – Questioner Jul 22 '18 at 20:22
  • 1
    window.web3 = new Web3(web3.currentProvider); Check out my answer to your other question – Shawn Tabrizi Jul 22 '18 at 20:30
  • @Shawn Tabrizi I update my code according to this MetaMask sample code : https://github.com/MetaMask/faq/blob/master/DEVELOPERS.md However, if i am connected to testrpc i receive the same error i.e. Method personal_sign not supported because testrpc does not support it and if i am NOT connected to testrpc i receive this error : http://localhost:8545/ 0 () (screen shot : https://ibb.co/dFRQpJ) Thanks – Questioner Jul 22 '18 at 21:00
  • fyi: since 1.0.0-beta.38, web3.eth.personal.sign no longer accepts an undefined value for the third argument (password for the account). – Cody Django Feb 27 '19 at 01:44

1 Answers1

1

The signature is sent to your callback, which is empty in the code you shared. This should work:

web3.personal.sign("0x" + message.toString("hex"), "0xF4D2B942c3baebef5B81F4B909904015166b238A", function(err, signature) {
  console.log(signature);
  // Be sure to make use of the signature only here.
  // It will not be defined until this callback is invoked.
});

As an aside, note that your current code does not use MetaMask.

user19510
  • 27,999
  • 2
  • 30
  • 48
  • @smartx Thank you, I replaced it, However, it's still undefined. Btw, in testrpc terminal I receive eth_getBlockByNumber one after another, it can be a problem ? Thanks – Questioner Jul 22 '18 at 19:33
  • Here is the testrpc output : https://ibb.co/fZHUid Thanks – Questioner Jul 22 '18 at 19:40
  • 1
    Then try printing out the err. – user19510 Jul 22 '18 at 19:42
  • Thank you, I updated my question. Now err is Error: Method personal_sign not supported. why? Thanks – Questioner Jul 22 '18 at 19:54
  • 2
    testrpc doesn't support the method personal_sign. You'll need to use geth, Parity, MetaMask, or something else that does support that method. – user19510 Jul 22 '18 at 20:04
  • Thank you, I am using MetaMask and after running testrpc I connect MetaMask to private network (localhost 8545). but without running testrpc I am not able to connect to localhost 8545 using MetaMask. How can I solve this problem ? Thanks again – Questioner Jul 22 '18 at 20:09
  • 1
    Your code needs to use the MetaMask web3 provider. You're instead connecting directly to testrpc. And yes, of course you need to run testrpc for MetaMask to be able to connect to it. – user19510 Jul 22 '18 at 20:10
  • Thank you, So do you mean I need to replace HttpProvider("http://localhost:8545")) with something else for MetaMast web3 proviver ? if so, could you please let me know what is MetaMast provider that must be inserted in HttpProvider(" ? ")) ? Thanks – Questioner Jul 22 '18 at 20:14
  • 1
    The MetaMask documentation will help. – user19510 Jul 22 '18 at 20:29
  • Thank you, I used this sample code of MetaMask from here: link is it what you mean as MetaMask documentation ? I updated my question, however, I receive the same error. Thanks – Questioner Jul 22 '18 at 20:50
  • In fact, if I am connected to testRPC I receive previous error message and if I am not connected to testRPC i receive this error : web3.js:4386 OPTIONS http://localhost:8545/ 0 () meaning that I am not connected to MetaMast provider . I am really confused ..... Thanks – Questioner Jul 22 '18 at 20:54
  • Here is screen shot of error when I am not connected to testrpc : https://ibb.co/dFRQpJ I finally did not understand that i need to run testrpc for this javascript code or not ? Thank you – Questioner Jul 22 '18 at 21:02
  • Yes, as I said, you need to run testrpc. Is it running? – user19510 Jul 22 '18 at 21:21
  • Thank you, Yes, if it's running I receive the previous error : personal_sign not supported and when I stop it, I receive another error : here is screen shot of error when testrpc is NOT running: https://ibb.co/dFRQpJ . So, in both cases i receive an error. maybe testing Ropsten would be better .... i do not know. Please check out my new code, since i modified it. Thanks – Questioner Jul 22 '18 at 21:26
  • Are you sure you're connected to MetaMask? Do you see the console message "No web3? ..."? – user19510 Jul 22 '18 at 21:37
  • Here is the steps I do: (1) running testrpc. (2) click on MetaMask icon and selecting localhost 8545 (3) select one of private keys generated by testrpc (4) selecting import account from MetaMask and paste this private key in Paste your private key string here (5) now balance of this address is 100 eth (6) paste this address (public key) in web3.personal.sign ... (7) openinghtml file by Google Chrome (8) receiving this messages : No web3? You should consider trying MetaMask! & Error: Method personal_sign not supported.screen shot: https://ibb.co/i93oRy .Thx – Questioner Jul 22 '18 at 21:58
  • is something missing in my steps ? because apparently it's not connected to MetaMask. Thanks – Questioner Jul 22 '18 at 22:24
  • This answer : (https://ethereum.stackexchange.com/a/46400/23024) says that I have to use https://mainnet.infura.io/<APIKEY>. is the problem related to to run my page locally ? (According to this answer) Thanks – Questioner Jul 22 '18 at 23:06
  • 1
    That message means you're not using MetaMask. (MetaMask didn't inject web3, so it was undefined.) If you're running your page from a file:// URL, then yes, this is the problem. You need to run from an http:// or https:// URL. Running locally is fine, but it needs to be via a web server, not the filesystem. – user19510 Jul 22 '18 at 23:52
  • Thank you, so in this case, I have to use ex. Parity to connect to ex. Ropsten and then use web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); in top of JavaScript code and running this JS file using node filename.js ? Is there any better way? Thanks – Questioner Jul 23 '18 at 09:08
  • 1
    No. You can just run testrpc and then open your web page on an http:// URL. Any web server will do. – user19510 Jul 23 '18 at 13:49
  • Comments are not for extended discussion; this conversation has been moved to chat. – Badr Bellaj Jul 23 '18 at 14:45