web3.eth.sign(address, hash); // generates signatureconst ethutils = require('ethereumjs-util');
const extractAddress = function(signature, hash){ //signature and hash from the client side
const o = ethutils.fromRpcSig(signature);
const hashBuffer = ethutils.toBuffer(hash);
const publicKeyBuffer = ethutils.ecrecover(hashBuffer, o.v, o.r, o.s);
const publicKeyHex = ethutils.bufferToHex(publicKeyBuffer);
const addressBuffer = ethutils.pubToAddress(publicKeyHex);
const addressHex = ethutils.bufferToHex(addressBuffer);
return addressHex;
}地址不匹配。这段代码在几个月前就起作用了。
发布于 2018-04-01 09:03:32
老问题,但这对我有用。我使用的是web3@0.20.3。
web3.personal.sign(web3.fromUtf8("dinosaur"), web3.eth.coinbase, console.log);const msg = 'dinosaur';
const msgBuffer = ethereumJsUtil.toBuffer(msg);
const msgHash = ethereumJsUtil.hashPersonalMessage(msgBuffer);
const signatureBuffer = ethereumJsUtil.toBuffer(signature);
const signatureParams = ethereumJsUtil.fromRpcSig(signatureBuffer);
const publicKey = ethereumJsUtil.ecrecover(
msgHash,
signatureParams.v,
signatureParams.r,
signatureParams.s
);
const addressBuffer = ethereumJsUtil.publicToAddress(publicKey);
const address = ethereumJsUtil.bufferToHex(addressBuffer);
console.log(address); // Prints my initial web3.eth.coinbaseweb3.eth.sign(web3.eth.coinbase, web3.sha3("dinosaur"), console.log);const msg = 'dinosaur';
const msgHash = ethereumJsUtil.sha3(msg);
// The rest is the same as above
const signatureBuffer = ethereumJsUtil.toBuffer(signature);
const signatureParams = ethereumJsUtil.fromRpcSig(signatureBuffer);
const publicKey = ethereumJsUtil.ecrecover(
msgHash,
signatureParams.v,
signatureParams.r,
signatureParams.s
);
const addressBuffer = ethereumJsUtil.publicToAddress(publicKey);
const address = ethereumJsUtil.bufferToHex(addressBuffer);
console.log(address); // Prints my initial web3.eth.coinbasehttps://ethereum.stackexchange.com/questions/12033
复制相似问题