如何读取ECDSA中的行,从程序集中的签名中恢复公钥



im正在研究如何在etherscan 中读取组装中的ECDSA恢复输出

function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError) {
// Check the signature length
// - case 65: r,s,v signature (standard)
// - case 64: r,vs signature (cf https://eips.ethereum.org/EIPS/eip-2098) _Available since v4.1._
if (signature.length == 65) {
bytes32 r;
bytes32 s;
uint8 v;
// ecrecover takes the signature parameters, and the only way to get them
// currently is to use assembly.
/// @solidity memory-safe-assembly
assembly {
r := mload(add(signature, 0x20))
s := mload(add(signature, 0x40))
v := byte(0, mload(add(signature, 0x60)))
}
return tryRecover(hash, v, r, s);
} else if (signature.length == 64) {
bytes32 r;
bytes32 vs;
// ecrecover takes the signature parameters, and the only way to get them
// currently is to use assembly.
/// @solidity memory-safe-assembly
assembly {
r := mload(add(signature, 0x20))
vs := mload(add(signature, 0x40))
}
return tryRecover(hash, r, vs);
} else {
return (address(0), RecoverError.InvalidSignatureLength);
}
}

有什么方法可以读取操作码来获得它的输出?

提前感谢

此库帮助您从ECDSA签名的消息中恢复公钥https://github.com/0xcyphered/secp256k1-solidity

示例:

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "@0xcyphered/secp256k1-solidity/contracts/SECP256K1.sol";
contract Example {
function recoverPersonalSignPublicKey(
bytes32 message,
uint8 v,
bytes32 r,
bytes32 s
) public pure returns (bytes memory) {
string memory header = 'x19Ethereum Signed Message:n32';
bytes32 _message = keccak256(abi.encodePacked(header, message));
(uint256 x, uint256 y) = SECP256K1.recover(uint256(_message), v - 27, uint256(r), uint256(s));
return abi.encodePacked(x, y);
}
}

最新更新