web3.js + web3j でmetamaskの署名検証

web3.js と web3jの組み合わせは地雷が多い。

ここにいろいろまとめられているが、要するにfrontとbackend間にprefixやエンコードなどのルールに差があることでうまくいかない。
https://lyhistory.com/docs/blockchain/ethereum/eth_web3.html

解決方法

この記事のコメントに解決方法がかかれていた。
https://www.toptal.com/ethereum/one-click-login-flows-a-metamask-tutorial


曰く

Sebestyén Csorba • 3 years ago
Thx for the article! Side note: I had a hard time with the web3j backend integration (the java library), until I found out, that

web3.personal.sign(web3.fromUtf8("aaaa"), web3.eth.coinbase, console.log);

means singing the original message prefixed with a constant and the length of the message, like this:

web3.eth.sign(web3.eth.coinbase, web3.sha3("\x19Ethereum Signed Message:\n4aaaa"), console.log)

frontは↑のやり方で署名する。
で、backendは下のやり方で検証する。
https://www.jianshu.com/p/f781cf3e8fc7

この組み合わせでうまくいった。