I have user specific data in a table on my smart contract that I don't want others to see. Can I encrypt that data with a public key of the user? If possible then how to encrypt that data with the public key? Then how will the user decrypt that data with their private key?
2 Answers
Yes, but you would encrypt and decrypt off-chain while only storing the encrypted data and other necessary data in the blockchain state.
With eosjs
, you can use the following set of commands to encrypt and decrypt data:
// The message that will be encrypted
let message = "my message"
// For example purposes, this is the keypar of the recipient. Note I removed
// EOS from the public key prefix
let someonesPrivateKey="5JCgCpHE9ZjXYUaCXSZ39HwaFEBvQE9DmU7R79Dha8roARasmZw"
let someonesPublicKey="76xGfmsKEYDdgCjd4tLXRBXBNf8hTEKxPfTc7Tt7q878gDFzEG"
// Create another keypair for this encryption.
// The recipient needs to know the public key
let myPrivate=ecc.PrivateKey.fromSeed('my')
let myPublic=myPrivate.toPublic()
// Encrypt the message using both my private key and the other party's public key
let encryptedMessage = ecc.Aes.encrypt(myPrivate, someonesPublicKey, message)
// Decrypt the message using th other party's private key and your public key
console.log(ecc.Aes.decrypt(someonesPrivateKey, myPublic,
encryptedMessage.nonce, encryptedMessage.message, encryptedMessage.checksum))
Much of the code is from here
Thus, you can store encryptedMessage.nonce
, encryptedMessage.message
, and encryptedMessage.checksum
in the RAM, log, or IPFS, and only the party who knows the private key corresponding to the public key you used for encryption can decrypt the message (they'd need to know the public key used for encryption as well, which can be stored in the state/log/ipfs/etc.)
The diagram below from this blogpost illustrates the process performed.
Some sexy dude made this library https://github.com/dtrades/dtrades-communication
Let's you encrypt a string with a public key, decrypt with the corresponding private.
import { encrypt, decrypt } from 'eos-communication-lib';
const public_key = "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV";
const private_key = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3";
const message = "Private Message, shhhh!";
const encrypted = encrypt(private_key, public_key, message);
// => TO DECRYPT: eos-communication
// .1167451677...23460624..862584768Q+h1AeLQbjfzZJD1Nsx6kk3U/jSNStwoWstz9uNCadw=
const decrypted = decrypt(private_key, public_key, encrypted);
// => Private Message, shhhh!
//@abi table tb i64 struct table { uint64_t id; uin8_t num; unit64_t primary_key() const { return id ;}};
. I want to hide the num variable so that others cant get this using cleos get table command line .Also this piece of code is in private sction of my contract line .So any way to prevent others from getting the value of num variable?//@abi table tb i64 struct table { uint64_t id; uin8_t num; unit64_t primary_key() const { return id ;}};
. I want to hide the num variable so that others cant get this using cleos get table command line .Also this piece of code is in private part of my contract class .So any way to prevent others from getting the value of num variable?