javascripthyperledger-fabrichyperledgerhyperledger-chaincodeibm-blockchain

Can putState() and putPrivateData() be used in the same chaincode?


I understand that using a putState() in a transaction would be logged in the ledger, where as putPrivateData() would result in a hash being stored on the ledger. What happens if both are used in one transaction? Would the private data be viewable on the public ledger? Example code:

async SignContract(ctx, collectionName) {

  await ctx.stub.putState(key, value);
  await ctx.stub.putPrivateData(collectionName, key, privateValue);
}

Solution

  • State written to the public ledger will be public whereas state written to a private data collection will be private to the organizations that can view that private data collection. You can read and write to multiple keys in multiple collections within a single transaction, and the visibility of that data is still dictated by the visibility of the collections in which the data is stored.

    This example from the Fabric samples demonstrates a combination of writing public data, private data and state-based endorsement policies within a single transaction function:

    https://github.com/hyperledger/fabric-samples/blob/6875049c8de49dec3335363aabc0e2920cd81235/asset-transfer-secured-agreement/chaincode-go/asset_transfer.go#L46