Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

private-data go chaincode now sets the Asset owner as client identity certificate string #374

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions asset-transfer-private-data/application-javascript/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ const mspOrg1 = 'Org1MSP';
const mspOrg2 = 'Org2MSP';
const Org1UserId = 'appUser1';
const Org2UserId = 'appUser2';
const userOrg1IdentityString = `x509::CN=${Org1UserId},OU=client+OU=org1+OU=department1::CN=ca.org1.example.com,O=org1.example.com,L=Durham,ST=North Carolina,C=US`;
const userOrg2IdentityString = `x509::CN=${Org2UserId},OU=client+OU=org2+OU=department1::CN=ca.org2.example.com,O=org2.example.com,L=Hursley,ST=Hampshire,C=UK`;

const RED = '\x1b[31m\n';
const RESET = '\x1b[0m';
Expand All @@ -42,7 +40,7 @@ function doFail(msgString) {
process.exit(1);
}

function verifyAssetData(org, resultBuffer, expectedId, color, size, owner, appraisedValue) {
function verifyAssetData(org, resultBuffer, expectedId, color, size, ownerUserId, appraisedValue) {

let asset;
if (resultBuffer) {
Expand All @@ -63,11 +61,11 @@ function verifyAssetData(org, resultBuffer, expectedId, color, size, owner, appr
if (asset.size !== size) {
doFail(`Failed size check - asset ${asset.assetID} has size of ${asset.size}, expected value ${size}`);
}
let assetsOwner = Buffer.from(asset.owner, 'base64').toString();
if (assetsOwner === owner) {
console.log(`\tasset ${asset.assetID} owner: ${assetsOwner}`);

if (asset.owner.includes(ownerUserId)) {
console.log(`\tasset ${asset.assetID} owner: ${asset.owner}`);
} else {
doFail(`Failed owner check from ${org} - asset ${asset.assetID} owned by ${assetsOwner}, expected value ${owner}`);
doFail(`Failed owner check from ${org} - asset ${asset.assetID} owned by ${asset.owner}, expected userId ${ownerUserId}`);
}
if (appraisedValue) {
if (asset.appraisedValue !== appraisedValue) {
Expand Down Expand Up @@ -243,7 +241,7 @@ async function main() {
console.log('\n--> Evaluate Transaction: ReadAsset ' + assetID1);
result = await contractOrg2.evaluateTransaction('ReadAsset', assetID1);
console.log(`<-- result: ${prettyJSONString(result.toString())}`);
verifyAssetData(mspOrg2, result, assetID1, 'green', 20, userOrg1IdentityString);
verifyAssetData(mspOrg2, result, assetID1, 'green', 20, Org1UserId);


// Org2 cannot ReadAssetPrivateDetails from Org1's private collection due to Collection policy
Expand Down Expand Up @@ -291,7 +289,7 @@ async function main() {
console.log('\n--> Evaluate Transaction: ReadAsset ' + assetID1);
result = await contractOrg1.evaluateTransaction('ReadAsset', assetID1);
console.log(`<-- result: ${prettyJSONString(result.toString())}`);
verifyAssetData(mspOrg1, result, assetID1, 'green', 20, userOrg2IdentityString);
verifyAssetData(mspOrg1, result, assetID1, 'green', 20, Org2UserId);

//Confirm that transfer removed the private details from the Org1 collection:
console.log('\n--> Evaluate Transaction: ReadAssetPrivateDetails');
Expand All @@ -304,7 +302,7 @@ async function main() {
console.log('\n--> Evaluate Transaction: ReadAsset ' + assetID2);
result = await contractOrg1.evaluateTransaction('ReadAsset', assetID2);
console.log(`<-- result: ${prettyJSONString(result.toString())}`);
verifyAssetData(mspOrg1, result, assetID2, 'blue', 35, userOrg1IdentityString);
verifyAssetData(mspOrg1, result, assetID2, 'blue', 35, Org1UserId);

console.log('\n********* Demo deleting asset **************');
let dataForDelete = { assetID: assetID2 };
Expand Down
2 changes: 1 addition & 1 deletion asset-transfer-private-data/chaincode-go/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[Using Private Data tutorial](https://hyperledger-fabric.readthedocs.io/en/latest/private_data_tutorial.html)
[Using Private Data tutorial](https://hyperledger-fabric.readthedocs.io/en/latest/private_data_tutorial.html)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no diff here

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package chaincode

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"log"
Expand Down Expand Up @@ -102,9 +103,9 @@ func (s *SmartContract) CreateAsset(ctx contractapi.TransactionContextInterface)
}

// Get ID of submitting client identity
clientID, err := ctx.GetClientIdentity().GetID()
clientID, err := submittingClientIdentity(ctx)
if err != nil {
return fmt.Errorf("failed to get verified OrgID: %v", err)
return err
}

// Verify that the client is submitting request to peer in their organization
Expand All @@ -131,7 +132,8 @@ func (s *SmartContract) CreateAsset(ctx contractapi.TransactionContextInterface)
// Save asset to private data collection
// Typical logger, logs to stdout/file in the fabric managed docker container, running this chaincode
// Look for container name like dev-peer0.org1.example.com-{chaincodename_version}-xyz
log.Printf("CreateAsset Put: collection %v, ID %v", assetCollection, assetInput.ID)
log.Printf("CreateAsset Put: collection %v, ID %v, owner %v", assetCollection, assetInput.ID, clientID)

err = ctx.GetStub().PutPrivateData(assetCollection, assetInput.ID, assetJSONasBytes)
if err != nil {
return fmt.Errorf("failed to put asset into private data collecton: %v", err)
Expand Down Expand Up @@ -170,9 +172,9 @@ func (s *SmartContract) CreateAsset(ctx contractapi.TransactionContextInterface)
func (s *SmartContract) AgreeToTransfer(ctx contractapi.TransactionContextInterface) error {

// Get ID of submitting client identity
clientID, err := ctx.GetClientIdentity().GetID()
clientID, err := submittingClientIdentity(ctx)
if err != nil {
return fmt.Errorf("failed to get verified OrgID: %v", err)
return err
}

// Value is private, therefore it gets passed in transient field
Expand Down Expand Up @@ -355,9 +357,9 @@ func (s *SmartContract) verifyAgreement(ctx contractapi.TransactionContextInterf
// Check 1: verify that the transfer is being initiatied by the owner

// Get ID of submitting client identity
clientID, err := ctx.GetClientIdentity().GetID()
clientID, err := submittingClientIdentity(ctx)
if err != nil {
return fmt.Errorf("failed to get verified OrgID: %v", err)
return err
}

if clientID != owner {
Expand Down Expand Up @@ -574,3 +576,15 @@ func verifyClientOrgMatchesPeerOrg(ctx contractapi.TransactionContextInterface)

return nil
}

func submittingClientIdentity(ctx contractapi.TransactionContextInterface) (string, error) {
b64ID, err := ctx.GetClientIdentity().GetID()
if err != nil {
return "", fmt.Errorf("Failed to read clientID: %v", err)
}
decodeID, err := base64.StdEncoding.DecodeString(b64ID)
if err != nil {
return "", fmt.Errorf("failed to base64 decode clientID: %v", err)
}
return string(decodeID), nil
}