0

For my hyperledger fabric chain code, i craeted a chaincode which implements Hybrid Encryption using RSA and Symetric AES.

I have used the cliper documentation's tutorial itself.

      encryptString (plaintext,publicKey) {

    // publicEncrypt() method with its parameters
    const encrypted = crypto.publicEncrypt(
        publicKey, Buffer.from(plaintext));

    return encrypted.toString("base64");
}


encryptAsset (cipher,asset) {
    return cipher.update(JSON.stringify(asset), 'utf8', 'hex') + cipher.final('hex');
}
hybridEncryption(assetIn,id){
    var token = crypto.createHash('sha256').update(String("")).digest('base64').substr(0, 32);
    const iv = crypto.randomBytes(16).toString('hex').slice(0, 16);
    const keyPair = crypto.generateKeyPairSync('rsa', {
        modulusLength: 1024,
        publicKeyEncoding: {
            type: 'spki',
            format: 'pem'
        },
        privateKeyEncoding: {
        type: 'pkcs8',
        format: 'pem',
        cipher: 'aes-256-cbc',
        passphrase: ''
        }
    });

    //Encrypting the entry
    var cipher = crypto.createCipheriv("aes256",token,iv);
    var encryptedAsset = this.encryptAsset(cipher,assetIn);

    //Encrypting the token
    let encrypterToken = this.encryptString(JSON.stringify(token),keyPair.publicKey);

    const asset = {
        ID: id,
        token:encrypterToken,
        asset: encryptedAsset,
    }
    return asset;
}
// CreateAsset issues a new asset to the world state with given details.

async CreateAsset(ctx, id, color, size, owner, appraisedValue) {

    const exists = await this.AssetExists(ctx, id);
    if (exists) {
        throw new Error(`The asset ${id} does not exist`);
    }
    const asset = {
        ID: id,
        Color: color,
        Size: size,
        Owner: owner,
        AppraisedValue: appraisedValue,
    };
    this.hybridEncryption(asset,id)
    return await ctx.stub.putState(id, Buffer.from(JSON.stringify(this.hybridEncryption(asset,id))));
    
}

When running he benchmark, I get this error:

error: [DiscoveryHandler]: compareProposalResponseResults[undefined] - read/writes result sets do not match index=1
 2022-01-26T11:04:20.087Z - error: [Transaction]: Error: No valid responses from any peers. Errors:
    peer=undefined, status=grpc, message=Peer endorsements do not match
2022.01.26-16:34:20.087 error [caliper] [connectors/v2/FabricGateway]   Failed to perform submit transaction [CreateAsset] using arguments [0_1_3,blue,20,penguin,500],  with error: Error: No valid responses from any peers. Errors:
    peer=undefined, status=grpc, message=Peer endorsements do not match
    at newEndorsementError (/mnt/e/MIT/capstone/capstone/sdk/node_modules/fabric-network/lib/transaction.js:49:12)
    at getResponsePayload (/mnt/e/MIT/capstone/capstone/sdk/node_modules/fabric-network/lib/transaction.js:17:23)
    at Transaction.submit (/mnt/e/MIT/capstone/capstone/sdk/node_modules/fabric-network/lib/transaction.js:212:28)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async V2FabricGateway._submitOrEvaluateTransaction (/mnt/e/MIT/capstone/capstone/sdk/node_modules/@hyperledger/caliper-fabric/lib/connector-versions/v2/FabricGateway.js:376:26)
    at async V2FabricGateway._sendSingleRequest (/mnt/e/MIT/capstone/capstone/sdk/node_modules/@hyperledger/caliper-fabric/lib/connector-versions/v2/FabricGateway.js:170:16)
    at async V2FabricGateway.sendRequests (/mnt/e/MIT/capstone/capstone/sdk/node_modules/@hyperledger/caliper-core/lib/common/core/connector-base.js:78:28)
    at async MyWorkload.submitTransaction (/mnt/e/MIT/capstone/capstone/sdk/fabric3wsl/newcliperall/fabric-samples/caliper-workspace/workload/createAsset.js:28:13)

If I run just this code for the chain code, there is no problem while running it:

async CreateAsset(ctx, id, color, size, owner, appraisedValue) {
     const exists = await this.AssetExists(ctx, id);
     if (exists) {
         throw new Error(`The asset ${id} does not exist`);
     }
     const asset = {
         ID: id,
         Color: color,
         Size: size,
         Owner: owner,
         AppraisedValue: appraisedValue,
     };
     return await ctx.stub.putState(id, Buffer.from(JSON.stringify(asset)));
}

Also I check my encryption too separately. There is not problem in encryption code. Also if I do a simply replace this in my encryption based chaincode:

return await ctx.stub.putState(id, Buffer.from(JSON.stringify(this.hybridEncryption(asset,id))));

with

return await ctx.stub.putState(id, Buffer.from(JSON.stringify(asset)));

the code works just fine. I have also checked the logs of the docker images. There the transactions are showing as successful. There is no error in the logs of any docker image.

What exactly is the problem. Is it due to caliper is not waiting long enough to get a response? If it is the case can't find a way to change it.

NOTE: Its not related to the bug in the code of "await" missing.

1 Answers1

0

Chaincode must be deterministic and in your case it looks like it isn't. For chaincode to be deterministic it must produce the same result given the same inputs when run, what is happening in your case is that the chaincode executes on 2 peers simultaneously but the data being put into the world state is not the same. Looking at the code I'm guessing that this line is causing the issue

const iv = crypto.randomBytes(16).toString('hex').slice(0, 16);

as the value of iv will differ between peers

david_k
  • 5,843
  • 2
  • 9
  • 16