Send a message to another user, on chain.
TL;DR
- Encrypt message to receiver using their public key
- Create a transaction on main chain that emits an event with the receiver’s address and the encrypted message
- Create a subgraph and index these events
- Display the encrypted messages
- When the user opens the message, decrypt and display message
Encrypt using public key
For this you need the public key of the user. You might not always have it.
To get the public key, you need to have access to atleast 1 transaction made by this user.
To register to receive encrypted messages, a user must make a register
transaction to your smart contract on chain. Then, using this transaction, extract the public key and store it on chain.
The typed message should be encrypted using Metamask.
Refer :
https://docs.metamask.io/guide/rpc-api.html#encrypting
Emit message
If public key is available, encrypt it. Else send an un-encrypted message.
To send a message put the encrypted or unencrypted message on a blob in IPFS.
Make a transaction to your smart contract that emits an event with the to address
and ipfs hash
.
https://docs.soliditylang.org/en/v0.8.11/contracts.html#events
Create a subgraph
Index these events using TheGraph protocol.
Make it query-able “get all messages for a given receiver”.
https://thegraph.com/docs/en/developer/create-subgraph-hosted/
List messages
When a user opens their inbox show the list of all the messages they’ve received by querying TheGraph
Show only sender name (ens) or address and send time.
https://thegraph.com/docs/en/developer/graphql-api/
Open message
When the user taps on the message, fetch the file from ipfs. If it is encrypted decrypt using Metamask.
https://docs.metamask.io/guide/rpc-api.html#eth-decrypt
Bounty!
If you build this on or before 10th January 2022, I pay 1 ETH.