I am trying to sign and verify a signature in Solidity. I understood that it is possible to verify a signature using
ecrecover. However, I am not able to find any useful information on signing.
There might be implementations of signing in Solidity, but you shouldn't do it.
This is because in order to sign something, you will need a private key. Since every transaction is public on the blockchain, so will your private key that you use to sign something with.
Therefore, it doesn't make much sense to sign something in Solidity because everyone will be able to see your private key.
I do not agree with @erkt. Really not sure what that even means. Private Keys never go on the blockchain, unless you explicitly put it there. You can refer to some answers already provided here Can someone explain 'signing a transaction' and how its different from sending ether? There are reasons you would sign, but not all transactions need to be signed. Any message you send is implicitly signed and verified by the protocol (miners, verifiers). If you have any specific use case you can sign them before sending and verify using
The problem of verifying signatures have been eliminated by ERC1271. You might want to look at this https://eips.ethereum.org/EIPS/eip-1271
ERC-1271: Standard Signature Validation Method for Contracts
The idea is to create a data hash that you need to pass into txn and create a signature and sign it using your wallet. The function isValidSignature takes hash and signature and tries to verify it. If the signer is an EOA address, it will return the public key after decoding and verifying the signature. If the signer is a Smart Contract Wallet, it will return a magic value. But for your question, I think smart contract wallets are out of scope.