Skip to content

Commit

Permalink
Fix the handling of the key endorsement policy (#377)
Browse files Browse the repository at this point in the history
The nOutOf was missing.

Signed-off-by: Matthew B White <whitemat@uk.ibm.com>

Signed-off-by: Matthew B White <whitemat@uk.ibm.com>
  • Loading branch information
mbwhite authored Jan 24, 2023
1 parent 982edda commit 653b724
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
6 changes: 5 additions & 1 deletion libraries/fabric-shim/lib/utils/statebased.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,15 @@ class KeyEndorsementPolicy {
sigsPolicies.push(signedBy);
});

const policy = new common.SignaturePolicy();

// Need to say that we want all of the mspIDs from the list.
const nOutOf = new common.SignaturePolicy.NOutOf();
nOutOf.setN(mspIds.length);
nOutOf.setRulesList(sigsPolicies);

const policy = new common.SignaturePolicy();
policy.setNOutOf(nOutOf);

spe.setIdentitiesList(principals);
spe.setRule(policy);

Expand Down
36 changes: 30 additions & 6 deletions libraries/fabric-shim/test/unit/utils/statebased.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
# SPDX-License-Identifier: Apache-2.0
*/


// const rewire = require('rewire');

const { msp, common } = require('@hyperledger/fabric-protos');
const { SignedProposal } = require('@hyperledger/fabric-protos/lib/peer');
const chai = require('chai');
const expect = chai.expect;
const KeyEndorsementPolicy = require('../../../lib/utils/statebased');
Expand Down Expand Up @@ -106,9 +105,34 @@ describe('KeyEndorsementPolicy', () => {
it('should successfully get policy', () => {
const policy = ep.getPolicy();
const anotherEp = new KeyEndorsementPolicy(policy);
expect(anotherEp.orgs).to.haveOwnProperty('Org1MSP');
expect(anotherEp.orgs).to.haveOwnProperty('Org2MSP');
expect(anotherEp.orgs).to.haveOwnProperty('Org3MSP');

const spe = common.SignaturePolicyEnvelope.deserializeBinary(policy);
const speClone = common.SignaturePolicyEnvelope.deserializeBinary(anotherEp.getPolicy());
expect(spe.toObject()).to.deep.equals(speClone.toObject());
});


it('should get policy that is semantically valid', () => {
const policy = ep.getPolicy();
const spe = common.SignaturePolicyEnvelope.deserializeBinary(policy);

// create a blank object and expand all the protobufs into it
const speObject = spe.toObject();

speObject.identitiesList = spe.getIdentitiesList().map(principal => {
let mapped = { principalClassification: 0 };
mapped.principal = msp.MSPRole.deserializeBinary(principal.getPrincipal_asU8()).toObject();
return mapped;
});

speObject.rule.nOutOf.rulesList = spe.getRule().getNOutOf().getRulesList().map(sigRule =>{
return {signedBy: sigRule.getSignedBy()}
});

const expectedPolicy={"version":0,"rule":{"signedBy":0,"nOutOf":{"n":3,"rulesList":[{"signedBy":0},{"signedBy":1},{"signedBy":2}]}},"identitiesList":[{"principalClassification":0,"principal":{"mspIdentifier":"Org1MSP","role":0}},{"principalClassification":0,"principal":{"mspIdentifier":"Org2MSP","role":0}},{"principalClassification":0,"principal":{"mspIdentifier":"Org3MSP","role":0}}]}
expect(speObject).to.deep.equals(expectedPolicy);
});
});
});


0 comments on commit 653b724

Please sign in to comment.