Is there a way to sign some data right out from a smart contract? Let's say I want to create a smart contract, that will represent my Digital Identity and I want it to have sign() method. Maybe something like this:

pragma solidity ^0.4.24;

contract DID {

address owner;

modifier onlyOwner {
    require(msg.sender == owner);

constructor() public {
    owner = msg.sender;

function sign(string _msgToBeSigned) public onlyOwner returns (bytes32) {
    // here to sign the message
    // and here to return the signed message

function verify(bytes32 _signedMessage) public view returns (bool) {
    // here to verify if owner == ecrecover(..., v, r, s)
    // here to return true or false


So the question is: Is it possible to perform these two actions from within a smart contract without use of client code like web3.eth.sign()?

Thank you.

1 Answer 1


ecrecover works roughly as you'd expect, but sign doesn't really make sense. All data on the blockchain is public, so a contract can't keep any sort of private key secret.

If you could elaborate on what you're trying to do (e.g. "I want to prove X to Y."), people might be able to suggest other ideas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.