在Hyperledger Fabric 2.2 NodeJS客户端中,发送以“ChaincodeId is nill”错误结尾的带有签名的建议

y53ybaqx  于 2022-12-18  发布在  Node.js
关注(0)|答案(2)|浏览(72)

这是我们在后端所需要的流。
1.第一个用户创建未签名的建议,并将建议缓冲区返回给他。

const proposal = new Endorsement(this.config.chaincodeId, this.channel)

        const user = User.createUser(
            enrollmentId,
            enrollmentId,
            this.config.userMspId,
            certificate
        )

        const identityContext = new IdentityContext(user, this.channel.client)

        const proposalBuffer = proposal.build(identityContext, {
            fcn,
            args,
        })

        const digest = createHash('sha256').update(proposalBuffer).digest('hex')

1.然后,在用户签署摘要并创建签名后,我们的后端将签署的提案发送给背书人:

const signedProposal = {
            signature: Buffer.from(signature, 'base64'),
            proposal_bytes: proposalBuffer,
        }

        const endorser = this.channel.getEndorsers(this.config.userMspId)[0]

        const response = await endorser.sendProposal(
            Buffer.from(JSON.stringify( signedProposal ))
        )

sendProposal方法引发ChaincodeId is nil错误。
有谁知道我们怎样才能实现这一权利?
我们如何为sendProposal方法参数创建Buffer对象?
在我的例子中,我从字符串化的json对象创建缓冲区,SignedProposal是如何在Hyperledger Fabric文档中定义的。

ndh0cuux

ndh0cuux1#

我看到你的代码是发送建议的自定义代码。为什么要这样做?为什么不使用fabric-network库来做简单的方法?
对于您的问题,我在fabric-network中找到了一些代码:

// This is the object that will centralize this endorsement activities
// with the fabric network
const endorsement = channel.newEndorsement(this.contract.chaincodeId);
const proposalBuildRequest = this.newBuildProposalRequest(args);
logger.debug('%s - build and send the endorsement', method);
// build the outbound request along with getting a new transactionId
// from the identity context
endorsement.build(this.identityContext, proposalBuildRequest);
endorsement.sign(this.identityContext);
...
...
else if (this.endorsingOrgs) {
   logger.debug('%s - user has assigned endorsing orgs %s', method, this.endorsingOrgs);
   const flatten = (accumulator, value) => {
        accumulator.push(...value);
        return accumulator;
   };
   proposalSendRequest.targets = this.endorsingOrgs.map((mspid) => channel.getEndorsers(mspid)).reduce(flatten, []);
}
...
...
// by now we should have targets or a discovery handler to be used
// by the send() of the proposal instance
const proposalResponse = await endorsement.send(proposalSendRequest);
...

send方法中:

...
const signedEnvelope = this.getSignedProposal();
...
peer.sendProposal(signedEnvelope, requestTimeout)
...

查看getSignedProposal方法内部:

...
const fabproto6 = require('fabric-protos');
...
/*
* return a signed proposal from the signature and the payload as bytes
*
* This method is not intended for use by an application. It will be used
* by the send method of the super class.
* @returns {object} An object with the signature and the payload bytes
*/
getSignedProposal() {
    const method = `getSignedProposal[${this.type}:${this.name}]`;
    logger.debug('%s - start', method);

    this._checkPayloadAndSignature();

    const signedProposal = fabproto6.protos.SignedProposal.create({
        signature: this._signature,
        proposal_bytes: this._payload
    });

    // const signedProposal = {
    //  signature: this._signature,
    //  proposalBytes: this._payload
    // };

    return signedProposal;
}


因此,尝试使用fabric-protos lib对您的建议进行编码。

vltsax25

vltsax252#

通过转移到@hyperledger/fabric-gateway库解决了这个问题。它工作正常,有一个文档齐全的API。脱机事务也得到了更好的支持。

相关问题