I am quite new to ethereum. I would like to know if its possible to write a contract in which, I can know the Owner's public key and 'Sender's public key ?

  • Out of curiosity, why do you need the public key in the contract, instead of the address? – Nick Johnson Jun 14 '16 at 15:09
  • @NickJohnson Wanted to encrypt some data with the public key and send over to the sender. – jgm Jun 14 '16 at 15:12
  • 1
    You're aware that the unencrypted data will be visible in the blockchain for anyone who cares to look? – Nick Johnson Jun 14 '16 at 15:13
  • @NickJohnson Ya. So I wanted to send a transaction using the blockchain encrypted with the Public key so that others cant read it. – jgm Jun 14 '16 at 15:16
  • That won't help, though - anyone can replay your transaction and see the data before it was encrypted. – Nick Johnson Jun 14 '16 at 15:19

This is not possible directly: The public key is not directly available to the contract. The contract only has the address of the sender, which is derived from their public key.

However, it is possible for a contract to do the derivation the other way, so if you pass it a public key as a parameter, it can find out what address it corresponds to.

Although the contract can't see the public key, it is technically possible to retrieve it from the blockchain and/or the network using code running outside the contract, provided the address has actually been used at least once. This is possible because every time you send a transaction, checking the signature involves first recovering the public key from the signature, then calculating the address and checking it matches.

So one thing you (or somebody else) could do would be to make a directory contract that keeps a list of addresses to public keys, and run a bot to query it for missing addresses, recover the public key from the blockchain, and send it to the directory contract.

If you don't want to run the bot yourself, you could modify the directory contract to manage a bounty for each missing public key. So your contract would send a small amount of ETH to the directory contract whenever it wanted a public key, and some unknown person out on the internet would look up the public key, send it to the directory contract and claim the bounty.


It seems like you are trying to make some sort of encrypted messaging dapp. Pretty cool.

Something you could do is create a user registry, and have the users "register" for the DAPP by providing their own public key as well as key-type. Then off chain in the JS layer retrieve that user metadata and do the expensive encryption computations then store the product of the encryption in a message mapping.

So you could do something like this

struct User{
  string pubKey;
  string keyType;

struct Message{
  string body;

mapping(address=>User) public users;
mapping(bytes32=>Message) public msgs;

Use some sort of unique identifier for the message mapping


For the Solidity contract language:
Whenever you send a transaction to a contract

  1. to call a function,
  2. to transfer ETH to the contract (here you use an anonymous fallback function function() {...}

you have access to a msg object that contains the senders public key: msg.sender.
This is true also in your constructor, where the sender is the owner/ creator of the contract.

  • kofier is it the public keyor the address of the User. – jgm Jun 14 '16 at 10:09
  • I see you want the public key, since the address is a hash of it you are right it's not the same. No way to know the public key directly, AFAIK. – Roland Kofler Jun 14 '16 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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