美文网首页
以太坊eip191的签名以及验签

以太坊eip191的签名以及验签

作者: 95加不满 | 来源:发表于2025-04-08 11:12 被阅读0次

以下代码为一个示例:

import { ethers,verifyMessage} from "ethers";

// 私钥示例,请勿在实际项目中使用此私钥
const privateKey = '0x129597626c595705cf4f34cf1d2c76eb25c1c4c7f89b0c1';
const wallet = new ethers.Wallet(privateKey);
console.log("address:",wallet.address); 

const message = "0x00000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000022843ee1db00e9d4abc4d9f34e93357d881843d772f748d511fb4fbf61685d574000000000000000000000000bc1f42383f9567b43c219200d83c71cf144c2146000000000000000000000000bc1f42383f9567b43c219200d83c71cf144cc146000000000000000000000000000000000000000000000000000000000000000a";
// 内部会自动hash(注:以太坊hash-eip-191)
wallet.signMessage(message).then((signature) => {
        console.log("签名结果:", signature);
    }).catch((error) => {
        console.error("签名时出错:", error);
    });
const _signature="0x6c1cc6c98d265463dedf47660204f713e47d37c3a97e3d7ed07b9e92d6f28f2978d42adf3f2d9680ccb107779e40e63a00bcde3c34d026b7a03d1809ec527f331b";
const adddress = verifyMessage(message,_signature);
console.log("sign account:",adddress);
console.log(wallet.address==adddress);


另:以下为非eip191标准的,常规签名:

async function sign() {
    if(typeof(message) === "string"){
        console.log("string");
    }else{
        console.log("non string");
    }
    const digest = keccak256(message)
    console.log("消息摘要:", digest);

    const signature = wallet.signingKey.sign(digest).serialized;
    console.log("签名结果:", signature);
}
sign();

相关文章

网友评论

      本文标题:以太坊eip191的签名以及验签

      本文链接:https://www.haomeiwen.com/subject/pdhtbjtx.html