1

My frontend code:

html (using cdn, couldn't make the npm run...):

<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>

javascript:

web3.eth.personal.sign(nonce, from[0], "test password!")
                    .then(signature => {
                        console.log('signature', signature);    
                        sendSignatureToServer(signature);
                    });

In the backend:

const util = require('ethereumjs-util');

function checkSignature(nonce:string, signature:string) {

    nonce = "\x19Ethereum Signed Message:\n" + nonce.length + nonce;
    nonce = util.keccak(Buffer.from(nonce));
    const sig = signature;
    const {v, r, s} = util.fromRpcSig(sig);
    const pubKey  = util.ecrecover(util.toBuffer(nonce), v, r, s);
    const addrBuf = util.pubToAddress(pubKey);
    const addr    = util.bufferToHex(addrBuf);
    return addr;
  
  }

let possibleOwner = checkSignature("Hello world",req.body.signature);
    if(possibleOwner === myKnownAccountAddr){
        res.json({yes: 'YES!', sender: req.body.owner, signature: req.body.signature});
    }else{
        res.json({yes: 'NO!', sender: req.body.owner, signature: req.body.signature});
    }

It is always "NO!" - What am I missing here?

I know I could use:

web3.eth.personal.ecRecover("Hello World!", signature).then(console.log);

which is working well in the browser with metamask, but 1. I could not make web3.js run without a provider (metamask?) int the backend.

I tried anyways with web3 on node.js side:

import Web3 from 'web3';


 let web3 = new Web3("https://cloudflare-eth.com");
    let hash = web3.utils.sha3("Hello world");
    let possibleOwner;
    if(hash){
        possibleOwner = await web3.eth.personal.ecRecover(hash, req.body.signature)
    }

and get this error:

web3-shh package will be deprecated in version 1.3.5 and will no longer be supported. web3-bzz package will be deprecated in version 1.3.5 and will no longer be supported. (node:59630) UnhandledPromiseRejectionWarning: Error: Returned error: Method not found

so what is the right way to do this in 2021?

2

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.