go eth 踩坑 crypto.Sign 与 eth.account.signHash 或者 web3Process.web3.eth.personal.sign 签名结果不同 除了最后一位 都匹配
从 go-ethereum 实现 Ethereum personal_sign (EIP-191) 给出了与
ethers.js web3.py web3.js solidy.sgin不同的签名
原文 https://stackoverflow.com/questions/69762108/implementing-ethereum-personal-sign-eip-191-from-go-ethereum-gives-different-s
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | js 签名消息代码 const accounts = await web3Process.web3.eth.getAccounts() this .imtContract = new web3Process.web3.eth.Contract(imtAbi, paymentErc20) const decimals = await this .imtContract.methods.decimals().call({ from: accounts[0] }) const heroPrice = price * Math.pow(10, decimals) console.log(web3Process.heroAddress, tokenId, paymentErc20, heroPrice, saltNonce) let messageHash = web3Process.web3.utils.soliditySha3(web3Process.heroAddress, tokenId, paymentErc20, heroPrice, saltNonce) console.log( "messageHash" , messageHash) // messageHash = "\x19Ethereum Signed Message:\n32" + messageHash // messageHash = web3Process.web3.eth.accounts.hashMessage(messageHash) try { const signature = await web3Process.web3.eth.personal.sign(messageHash, accounts[0]) console.log( "signature" , signature) const hash = web3Process.web3.eth.accounts.hashMessage(messageHash) return [signature, hash] } catch (e) { throw (e) } |
1 2 3 4 5 6 7 8 | python 签名代码 hash_data = w3.soliditySha3(abi_types = [ "address" , "uint256" , "address" , "uint256" , "uint256" ], values = [ "address" , token_id, "address" , amount, time]) print (w3.toHex(hash_data)) sign_data = w3.eth.account.signHash(defunct_hash_message(hexstr = w3.toHex(hash_data)), private_key = "private_key" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package main import ( "fmt" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "encoding/hex" "encoding/json" "log" ) func signHash(data []byte) common.Hash { msg := fmt.Sprintf( "\x19Ethereum Signed Message:\n%d%s" , len(data), data) return crypto.Keccak256Hash([]byte(msg)) } func main() { hexPrivateKey := "8da4ef21b864d2cc526dbdb2a120bd2874c36c9d0a1fb7f8c63d7f7a8b41de8f" dataMap := map [string]string{ "data1" : "value1" , "data2" : "value2" } dataToSign, _ := json.Marshal(dataMap) privateKey, err := crypto.HexToECDSA(hexPrivateKey) if err != nil { log.Fatal(err) } dataHash := crypto.Keccak256Hash(dataToSign) //0x8d218fc37d2fd952b2d115046b786b787e44d105cccf156882a2e74ad993ee13 signHash := signHash(dataHash.Bytes()) signatureBytes, err := crypto.Sign(signHash.Bytes(), privateKey) if err != nil { log.Fatal(err) } fmt.Println( "0x" + hex.EncodeToString(signatureBytes)) } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!