web3.eth.accounts.recover - 提取数据的签名账户

优质
小牛编辑
126浏览
2023-12-01

从给定的已签名数据中回复用来进行签名的以太坊地址。

调用:

web3.eth.accounts.recover(signatureObject);
web3.eth.accounts.recover(message, signature [, preFixed]);
web3.eth.accounts.recover(message, v, r, s [, preFixed]);

参数:

  • message|signatureObject - String|Object: 已签名消息或消息哈希,或者是如下的签名对象:
    • messageHash - String: 给定消息的哈希,已加前缀 "\x19Ethereum Signed Message:\n" + message.length + message.
    • r - String: 签名的前32字节
    • s - String: 签名的后32字节
    • v - String: 恢复值 + 27
  • signature - String: RLP编码的签名,或者2-4参数分别是 v, r, s
  • preFixed - Boolean ,可选,默认值为false。如果该值为true,则给定的消息不会自动添加前缀 "\x19Ethereum Signed Message:\n" + message.length + message

返回值:

String: 用于签名的以太坊地址

示例代码:

web3.eth.accounts.recover({
    messageHash: '0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655',
    v: '0x1c',
    r: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd',
    s: '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029'
})
> "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23"

// hash signature
web3.eth.accounts.recover('0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655', '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c');
> "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23"

// hash, v, r, s
web3.eth.accounts.recover('0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655', '0x1c', '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd', '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029');
> "0x2c7536E3605D9C16a7a3D7b1898e529396a65c23"