- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将我的 Metamask 钱包连接到我的 Java Spring-Boot 后端。我试图按照示例 here .我能够自动生成随机数并毫无问题地接收钱包 ID。我正在尝试验证来自服务器上钱包的已签名随机数,以确保发件人确实是他们所说的人。但是,我无法在 Web3J 上找到任何文档来执行此操作。
web3j 不是用于此的正确包吗?该示例展示了如何基于 javascript 在 NodeJS 上进行验证,但我没有找到任何关于如何在 Java 上执行此操作的示例。
我的理解是,公钥是钱包 ID 本身,消息是由钱包私钥签名的随机数,出于明显的原因不共享。据此,我需要使用公钥“解密”消息,并查看解密后的消息是否与后端发送到 Metamask 进行签名的随机数相同。这是正确的吗?
这是我创建随机数并将其发送到 UI 的代码:
public User findUserByPublicAddress(String publicWalletId) {
User u = userRepository.findByPublicWalletId(publicWalletId);
if(u == null) {
u = new User("", "", "", null, publicWalletId, "");
String nonce = StringUtil.generateRandomAlphaNumericString();
u.setNonce(nonce);
userRepository.saveAndFlush(u);
}
return u;
}
在这里,我查看用户是否已经在我的系统中,如果不在,那么我只是创建一个临时用户,其中生成并保存在数据库中的随机随机数。这个 nonce 被发送到 UI 以供 Metamask 签名。但是,我不确定如何进行验证部分。
最佳答案
我终于弄明白了。我最初的理解是错误的。我不应该尝试解密消息来检索随机数。相反,我需要使用随机数来查看是否可以检索用于签署消息的私钥的公钥,并查看检索到的公钥是否与钱包 ID 匹配。
这是 UI(JavaScript 和 HTML)的示例代码:
web3.eth.sign(
web3.utils.sha3(nonce),
window.userWalletAddress)
.then((message) => {
console.log(message)
data['message'] = message // BODY
var xmlReq = new XMLHttpRequest();
xmlReq.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
response = this.responseText
console.log(response)
}
};
xmlReq.open("POST", "/api/users/login", true)
xmlReq.setRequestHeader('Content-Type', 'application/json')
xmlReq.send(JSON.stringify(data))
})
web3.eth.sign() 获取要签名的消息并获取对其进行签名的钱包 ID。然后将其发送到后端。在后端:
public User signin(UserLoginDTO loginDetails, HttpServletResponse response) {
try {
// Get the wallet ID and signed message from the body stored in the DTO
String publicWalletId = loginDetails.getPublicWalletId();
String message = loginDetails.getMessage();
// Find the nonce from the DB that was used to sign this message
User user = userRepository.findByPublicWalletId(publicWalletId);
String nonce = user.getNonce();
// Generate the HASH of the Nonce
byte[] nonceHash = Hash.sha3(nonce.getBytes()) // org.web3j.crypto.Hash
// Generate the Signature Data
byte[] signatureBytes = Numeric.hexStringToByteArray(message); // org.web3j.utils.Numeric
byte v = (byte) ((signatureBytes[64] < 27) ? (signatureBytes[64] + 27) : signatureBytes[64]);
byte[] r = Arrays.copyOfRange(signatureBytes, 0, 32);
byte[] s = Arrays.copyOfRange(signatureBytes, 32, 64);
SignatureData signatureData = new SignatureData(v, r, s); // org.web3j.crypto.Sign.SignatureData
// Generate the 4 possible Public Keys
List<String> recoveredKeys = new ArrayList<>();
for(int i = 0; i < 4; i++) {
BigInteger r = new BigInteger(1, signatureData.getR());
BigInteger s = new BigInteger(1, signatureData.getS());
ECDSASignature ecdsaSignature = new ECDSASignature(r, s);
BigInteger recoveredKey = Sign.recoverFromSignature((byte)i, ecdsaSignature, nonceHash);
if(recoveredKey != null) {
recoveredKeys.add("0x" + Keys.getAddressFromKey(recoveredKey)); // org.web3j.crypto.Keys
}
}
// Check if one of the generated Keys match the public wallet ID.
for(String recoveredKey : recoveredKeys) {
if(recoveredKey.equalsIgnoreCase(publicWalletId)) {
// Add Code here to create the JWT and add that to your HttpServletResponse. Not shown here.
return user;
}
}
throw new CustomException("Message Sign Invalid", HttpStatus.UNAUTHORIZED);
}
catch (Exception ex) {
// Custom Error Handling.
}
}
关于java - 如何将 Metamask 连接到 Web3J (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71630833/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!