【tronLink】调用合约签名底层实现
作者:互联网
var str = 'message digest'// 要签名的字符串
const ethers = window._ethers;
var message = ethers.utils.sha256(tronWeb.toHex(str));// 此处用sha256做hash,tron钱包签的东西用sha3,下方会举例sha3的使用情景
message = message.replace(/^0x/, '');
var privateKey = '';// 账户私钥
const value ={
toHexString: function() {
return '0x' + privateKey
},
value: privateKey
}
var SigningKey = ethers.utils.SigningKey;// 注意此处要先定义class再调用
var signingKey = new SigningKey(value);
var messageBytes = [
...require('ethers').utils.toUtf8Bytes('\x19TRON Signed Message:\n32'),// 带tron签名头
...tronWeb.utils.code.hexStr2byteArray(message)
];
var messageDigest = ethers.utils.keccak256(messageBytes); // 转hex格式
var signature = signingKey.signDigest(messageDigest);
var signatureHex = [
'0x',
signature.r.substring(2),
signature.s.substring(2),
Number(signature.v).toString(16)
].join('');
console.log('signatureHex------', signatureHex);
// sha3方式做hash
let res = tronWeb.utils.abi.encodeParams(['address','uint256','string'], ['TQGf58avp2oHSNQ8tPn7FTKSp16RB26TBY','100000000','e733be0c-195b-4fa0-aa8d-ff4b6d34f2ba'])// 编码参数,type和value要一一对应
var message = window.web3.utils.sha3(res)
// tronLink签名方法(message必须是hex形式)
window.tronWeb.trx.sign(message,privateKey)
标签:调用,tronLink,utils,privateKey,tronWeb,var,message,ethers,底层 来源: https://www.cnblogs.com/SSzzPP/p/15967137.html