0

对于我的超级账本结构链代码,我创建了一个使用 RSA 和对称 AES 实现混合加密的链代码。

我已经使用了剪辑器文档的教程本身。

      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))));
    
}

运行他的基准测试时,我收到此错误:

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)

如果我只为链码运行这段代码,运行它就没有问题:

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)));
}

我也单独检查我的加密。加密代码没有问题。另外,如果我在基于加密的链代码中简单地替换它:

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

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

代码工作得很好。我还检查了 docker 映像的日志。那里的交易显示为成功。任何 docker 镜像的日志中都没有错误。

究竟是什么问题。是因为 caliper 没有等待足够长的时间来得到响应吗?如果是这样就找不到办法改变它。

注意:它与缺少“等待”的代码中的错误无关。

4

1 回答 1

0

链码必须是确定性的,在您的情况下,它看起来不是。要使链码具有确定性,它必须在运行时在给定相同输入的情况下产生相同的结果,在您的情况下发生的情况是链码同时在 2 个对等点上执行,但放入世界状态的数据并不相同。查看代码我猜是这条线导致了这个问题

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

因为 iv 的值在同行之间会有所不同

于 2022-01-26T13:15:50.993 回答