verifyMessage

验证字符串的签名

用法

tronWeb.trx.verifyMessage(hexMsg, signedMsg, address)

参数

ParameterDescriptionData Type
hexMsgHex格式的待签名stringString
signedMsg待验证的签名String
address签名的地址(base58格式 或 hex格式)String

返回值

bool - 验证通过返回true;验证不通过返回错误信息

示例

// sign a string message

var str = "helloworld"; 
// convert to hex format and remove the beginning "0x"
var hexStrWithout0x = tronWeb.toHex(str).replace(/^0x/, '');
// conert hex string to byte array
var byteArray = tronWeb.utils.code.hexStr2byteArray(hexStrWithout0x)
// keccak256 computing, then remove "0x" 
var strHash= tronWeb.sha3(byteArray).replace(/^0x/, '');
// sign 
var signedStr = await tronWeb.trx.sign(strHash).replace(/^0x/, '');
var tail = signedStr.substring(128, 130);
if(tail == '01')
{
    signedStr = signedStr.substring(0,128)+'1c';
}
else if(tail == '00')
{
    signedStr = signedStr.substring(0,128)+'1b';
}
  

// verify the signature
var res = await tronWeb.trx.verifyMessage(strHash,signedStr,'TPNcZ1j55FrGpsaw6K6rVjuL4HfT8ZbBf7')
console.log(res);
>true