如何获得以太坊编码的函数签名

  • 当编写代码以原始方式调用智能合约的函数或从多重签名钱包调用合约函数时,需要函数签名。
  • 要获得函数签名,您需要像functionName(type1,type2,…)Keccak256 一样对函数的原型字符串进行哈希处理。然后提取前 4 个字节。
  • 例如,如果你想得到函数的编码函数签名,用 Keccak256sendMessage(string message, address to)对函数的原型字符串进行哈希处理。sendMessage(string,address)然后提取前 4 个字节“0xc48d6d5e”。

使用 Web3.js 获取编码的函数签名

在 Web3.js 1.0.0 中,可以通过实用函数获得编码的函数签名。

let encodedFunctionSignature = web3.eth.abi.encodeFunctionSignature('sendMessage(string,address)'); 
console.log(encodedFunctionSignature); 
// => 0xc48d6d5e

在线计算

https://piyolab.github.io/playground/ethereum/getEncodedFunctionSignature/

相关文章

参考

https://web3js.readthedocs.io/en/1.0/web3-eth-abi.html#encodefunctionsignature

英文原文:https://piyopiyo.medium.com/how-to-get-ethereum-encoded-function-signatures-1449e171c840

,

© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发
区块链技术的头像-区块链开发网

昵称

取消
昵称表情代码图片