当前位置: 首页 > news >正文

dapp获取钱包地址,及签名

npm install ethersimport {ethers} from 'ethers'
const accounts = await ethereum.request({method: 'eth_requestAccounts'});
// 获取钱包地址
this.form.address = accounts[0]
console.log("accounts:" + this.address)const provider = new ethers.BrowserProvider(window.ethereum)
//签名
const signer = await provider.getSigner()
const signature = await signer.signMessage(this.form.address)
console.log("signature:" + signature);        

ethers.verifyMessage(nonce, signature) 在后端的实现

Java 后端项目 maven导入web3j

<dependency><groupId>org.web3j</groupId><artifactId>core</artifactId><version>4.5.16</version>
</dependency>

Java main方法示例代码

import org.web3j.crypto.ECDSASignature;
import org.web3j.crypto.Hash;
import org.web3j.crypto.Keys;
import org.web3j.crypto.Sign;
import org.web3j.utils.Numeric;import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;public class SignInWithEthereum {public static void main(String[] args) {String address = "0x4adbe2009cff6a1e9d280d28815c49e91b8ebad0";String nonce = "3618473";String signature = "0x9a38fb504315869609ef2e948b1a80f670e6ff725d16b5ae443b118eb2d108bc3c659c2417d96bbff240b44d4f1078fde73b72f83cc71e4e726640bc19a9c2a91c";String MESSAGE_PREFIX = "\u0019Ethereum Signed Message:\n";try {String digest = Hash.sha3(Numeric.toHexStringNoPrefix((MESSAGE_PREFIX + nonce.length() + nonce).getBytes(StandardCharsets.UTF_8)));byte[] signatureBytes = Numeric.hexStringToByteArray(signature);byte v = signatureBytes[64];if (v < 27) {v += 27;}byte[] r = (byte[]) Arrays.copyOfRange(signatureBytes, 0, 32);byte[] s = (byte[]) Arrays.copyOfRange(signatureBytes, 32, 64);Sign.SignatureData signatureData = new Sign.SignatureData(v, r, s);int header = 0;for (byte b : signatureData.getV()) {header = (header << 8) + (b & 0xFF);}if (header < 27 || header > 34) {System.out.println("false");}int recId = header - 27;BigInteger key = Sign.recoverFromSignature(recId,new ECDSASignature(new BigInteger(1, signatureData.getR()), new BigInteger(1, signatureData.getS())),Numeric.hexStringToByteArray(digest));if (key == null) {System.out.println("false");}String signAddress = ("0x" + Keys.getAddress(key)).trim();System.out.println("signAddress:" + signAddress);if (address.toLowerCase().equals(signAddress.toLowerCase())) {System.out.println("true");}} catch (Exception e) {System.out.println("false");}}
}

Java 封装的工具类

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;import org.web3j.crypto.ECDSASignature;
import org.web3j.crypto.Hash;
import org.web3j.crypto.Keys;
import org.web3j.crypto.Sign;
import org.web3j.crypto.Sign.SignatureData;
import org.web3j.utils.Numeric;public class EthersUtils {private static final String MESSAGE_PREFIX = "\u0019Ethereum Signed Message:\n";public static String verifyMessage(String message, String signature) {return EthersUtils.recoverAddress(EthersUtils.hashMessage(message), signature);}public static String hashMessage(String message) {return Hash.sha3(Numeric.toHexStringNoPrefix((EthersUtils.MESSAGE_PREFIX + message.length() + message).getBytes(StandardCharsets.UTF_8)));}public static String recoverAddress(String digest, String signature) {SignatureData signatureData = EthersUtils.getSignatureData(signature);int header = 0;for (byte b : signatureData.getV()) {header = (header << 8) + (b & 0xFF);}if (header < 27 || header > 34) {return null;}int recId = header - 27;BigInteger key = Sign.recoverFromSignature(recId,new ECDSASignature(new BigInteger(1, signatureData.getR()), new BigInteger(1, signatureData.getS())),Numeric.hexStringToByteArray(digest));if (key == null) {return null;}return ("0x" + Keys.getAddress(key)).trim();}private static SignatureData getSignatureData(String signature) {byte[] signatureBytes = Numeric.hexStringToByteArray(signature);byte v = signatureBytes[64];if (v < 27) {v += 27;}byte[] r = (byte[]) Arrays.copyOfRange(signatureBytes, 0, 32);byte[] s = (byte[]) Arrays.copyOfRange(signatureBytes, 32, 64);return new SignatureData(v, r, s);}
}


http://www.mrgr.cn/news/70725.html

相关文章:

  • Leetcode169. 多数元素(HOT100)
  • 2023AE软件、Adobe After Effects安装步骤分享教程
  • A045-基于spring boot的个人博客系统的设计与实现
  • 【DQ Robotics】二次规划控制
  • Flutter:AnimatedIcon图标动画,自定义Icon通过延时Interval,实现交错式动画
  • CCE-基础
  • js.零钱兑换
  • python:用 sklearn 转换器处理数据
  • 【C++ 篇】类之华章:超越固有模式,品味面向对象的璀璨光芒
  • OSG开发笔记(三十一):OSG中LOD层次细节模型介绍和使用
  • MySQL数据库的备份与还原
  • 大模型论文精华—20241111
  • 贪心算法day05(k次取反后最大数组和 田径赛马)
  • 3.keeplived配置文件
  • VideoChat:开源的数字人实时对话系统,支持自定义数字人的形象和音色
  • 二维差分矩阵 模板题
  • 李佳琦回到巅峰背后,双11成直播电商分水岭
  • 链式结构二叉树
  • 【QT常用技术讲解】任务栏图标+socket网络服务+开机自启动
  • 项目管理平台盘点:2024推荐的9款优质工具
  • jmeter基础05_第1个http请求
  • 【论文速看】DL最新进展202411011-图像超分、Transformer
  • 分布式----Ceph部署(上)
  • 软件测试中的PIE模型
  • 11个简单易用的电商购物车设计案例
  • 算法每日双题精讲——滑动窗口(长度最小的子数组,无重复字符的最长子串)