Encrypting Messages with Bitcoin Cash Keys
Recently I have been able to kill three birds with one stone. I have learned more about various blockchains and cryptocurrencies. I have learned to write code in Go. And through the existence of cryptocurrency code bounties, I have been able to make money doing it.
My latest bounty was adding private messaging to the Memocash platform. Memocash is a social network built on top of the Bitcoin Cash block chain. All actions in the social network are stored permanently in the blockchain. So essentially all the site itself does is retrieve this data from the blockchain and store it in a database so it can be displayed.
So far you can create a profile with a username, description and avatar. You can create, reply and like posts. You can create polls. And you can embed media. And this is permanent. No accounts getting closed. So I ran into a bounty to add a new private messaging feature to their protocol and I thought I’d try my hand at it. A lot of research, lines of code and a few weeks later, I accomplished the goal and learned a lot.
The actions in Memocash are added to the blockchain by sending Bitcoin Cash back to your own wallet with another outgoing transaction containing an OP_RETURN. This code marks this output as invalid, but since you can add data along with this code, it is leveraged to embed these actions on the blockchain. These type of transactions are really cheap. I created dozens of transactions testing on the live blockchain and didn’t even spend a nickel.
To encrypt a message, you use the sender’s private key and recipient’s public key. Then to decrypt the message, the recipient would then use his private key with the sender’s public key. The only thing anyone else could know is that the sender created a private message. They would not be able to read the message or know who it was intended for. Because of the size of the final encrypted message, the transactions had to be chained. I did this by adding a number to the first transaction that was a count of the remaining chunks in the messages. Each remaining chunk contained a transaction hash of the chunk before it.
You can try it out by signing up for a Memocash account. You can get started with less than a $1 in Bitcoin Cash. Here is a gist the encryption part of the algorithm that can be used to create encrypted messages in the same way using private keys and addresses. It retrieves the public keys from the Bitcoin Cash API.