web3.eth.accounts.sign
对任意数据进行签名。数据应当是utf-8并且16进制解码的,封装如下: "\x19Ethereum Signed Message:\n" + message.length + message
调用:
web3.eth.accounts.sign(data, privateKey);
参数:
- data - String: 要签名的数据
- privateKey - String: 用来签名的私钥
返回值:
String|Object: RLP编码的签名:
- message - String: 指定的消息
- messageHash - String: 消息的哈希
- r - String: 签名的前32字节
- s - String: 签名的后32字节
- v - String: 恢复值 + 27
示例代码:
web3.eth.accounts.sign('Some data', '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318');
> {
message: 'Some data',
messageHash: '0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655',
v: '0x1c',
r: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd',
s: '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029',
signature: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c'
}