You can import the ECDSA.sol from OpenZeppelin to verify signature.
In solidity:
function _recoverSigner(bytes32 hash, bytes memory _signature) internal returns(address) {
return ECDSA.recover(
keccak256(
abi.encodePacked("\x19Ethereum Signed Message:\n32", hash)
),
_signature
);
}
UPDATE
I think I misunderstood your question. The solution I propose above is a way to "Verify" signature1.
Why do you want to generate signature in solidity? In my opinion, we should not sign anything on solidity! Here is reason why: link
The things that many projects on Ethereum do is to let the user sign a transaction (like the one that you created using web3 - "signature1"), then use that "signature1" and verify it on the smart contract:
In client's dapp
const privateKey = //'address user'
const hash = web3.utils.soliditySha3(address);
const signature1 = web3.eth.accounts.sign(hash, privateKey);
In solidity - verify it:
function verifySignature(address _signer, bytes32 hash, bytes calldata _signature) public returns (bool){
address signer = _recoverSigner(hash, _signature);
return _signer == signer;
}
Here is an article about signing & validating: Medium