栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

HTML调起钱包签名并返回校验

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HTML调起钱包签名并返回校验

1、需要导入web3链接钱包
2、需要链接钱包,前面文章里面有


//生成随机签名码(自定义)
var str = web3.utils.sha3("Some string to be hashed");

function signature() {
    //调取签名并打印
    web3.eth.personal.sign(str,wordAddress).then(function (accounts) {
        
        alert(accounts);
    })

}

下面是java校验签名代码

public final class EcRecoverUtil {

    
    public static List ecRecover(String message, String signature) {
        if (message == null || signature == null || signature.isEmpty()) {
            return Collections.emptyList();
        }
        String newMessage = "u0019Ethereum Signed Message:n" + message.length() + message;

        byte[] msgHash = Hash.sha3(newMessage.getBytes());
        byte[] signatureBytes = Numeric.hexStringToByteArray(signature);
        if (signatureBytes.length <= 64) {
            return Collections.emptyList();
        }
        byte v = signatureBytes[64];
        if (v < 27) {
            v += 27;
        }
        Sign.SignatureData sd =
            new Sign.SignatureData(v,
                Arrays.copyOfRange(signatureBytes, 0, 32),
                Arrays.copyOfRange(signatureBytes, 32, 64));

        List result = new ArrayList<>();

        // Iterate for each possible key to recover
        for (int i = 0; i < 4; i++) {
            BigInteger publicKey =
                Sign.recoverFromSignature((byte) i,
                    new ECDSASignature(new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())),
                    msgHash);

            if (publicKey != null) {
                result.add("0x" + Keys.getAddress(publicKey));
            }
        }
        return result;
    }

    
    public static boolean checkEcRecover(String message, String signature, String address) {
        if (address == null) {
            return false;
        }
        List result = ecRecover(message, signature);
        return result.stream().anyMatch(address::equalsIgnoreCase);
    }
}

依赖


    org.web3j
    core
    5.0.0


    com.squareup.okhttp3
    okhttp
    4.9.0
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/975925.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号