如何从签名中恢复公共地址

时间:2019-05-23 13:33:40

标签: java blockchain ethereum web3-java ethereumj

我对从签名中恢复公共地址有疑问。 我下面这个教程https://www.toptal.com/ethereum/one-click-login-flows-a-metamask-tutorial。对于前端,我使用angular;对于后端Java,我使用
在前端,我将发送一个签名,该签名已使用web3js签名。

this.web3Service.getWeb3().eth.personal.sign(
      this.web3Service.getWeb3().utils.fromUtf8('I am signing my one-time nonce ' + this.currentUser.nonce),
      this.currentUser.ethereumAddress
    ).then(signature => {
      this.authenticationService.authenticate(signature, this.currentUser.ethereumAddress).subscribe();
    });

在后端我收到用户的签名和公共地址。我正在尝试用Java重新创建此代码

 const msg = `I am signing my one-time nonce: ${user.nonce}`;

// We now are in possession of msg, publicAddress and signature. We
// can perform an elliptic curve signature verification with ecrecover
const msgBuffer = ethUtil.toBuffer(msg);
const msgHash = ethUtil.hashPersonalMessage(msgBuffer);
const signatureBuffer = ethUtil.toBuffer(signature);
const signatureParams = ethUtil.fromRpcSig(signatureBuffer);
const publicKey = ethUtil.ecrecover(
  msgHash,
  signatureParams.v,
  signatureParams.r,
  signatureParams.s
);
const addressBuffer = ethUtil.publicToAddress(publicKey);
const address = ethUtil.bufferToHex(addressBuffer);

// The signature verification is successful if the address found with
// ecrecover matches the initial publicAddress
if (address.toLowerCase() === publicAddress.toLowerCase()) {
  return user;
} else {
  return res
    .status(401)
    .send({ error: 'Signature verification failed' });
}

在Java中是否有等效功能可用于从签名和消息中获取公钥?

0 个答案:

没有答案