这是我们在后端需要的流程。
- 第一个用户创建未签名的提议,并将提议缓冲区返回给他。
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')
- 然后在用户签名文摘并创建签名后,我们的后端发送签名提案给背书者:
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方法参数?在我的例子中,我从字符串化的json对象创建了缓冲区,如何在Hyperledger Fabric文档中定义SignedProposal。
我看到你的代码是自定义代码发送提案。你为什么要这样做?为什么不使用fabric-network
lib这样简单的方法呢?
对于你的问题,我在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来编码您的提案。希望能有所帮助
通过移动到@hyperledger/fabric-gateway
库解决了这个问题。它工作得很好,并且有一个文档完备的API。离线事务也得到更好的支持。