[Ethereum] “Updated :” How to see “output” of “web3.personal.sign” in “browser”? (“Method personal_sign not supported”)

javascriptmetamasksignaturetestrpcweb3js

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"

Best Answer

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.

Related Topic