Search code examples
hyperledger-fabrichyperledgerhyperledger-chaincodechaincode

How could I use public/private key of a peer to encrypt/decrypt data in a chaincode?


I'd like to use public/private keys of a peer to encrypt/decrypt data in Hyperledger Fabric chaincode. Is there a way to retrieve them through the chaincode?


Solution

  • You can encrypt/decrypt the data using a single random key generated before the invocation of the chaincode.

    Otherwise there will be conflict to share the keys to other endorsing peers.They need the private key to decrypt the data and endorse the transaction.

    Or you can store the key in the transient field of the peers which is not added to the chain and other endorsing peers can use it to decrypt the content and endorse the transaction.

    You can take a look at this official fabric repository where they talked about the encryption and decryption. The chaincode and go packages to encrypt the chaincode transaction data is also mentioned there.

    https://github.com/hyperledger/fabric/tree/release-1.4/examples/chaincode/go/enccc_example