All files / fabric-shim/lib/utils statebased.js

100% Statements 49/49
100% Branches 6/6
100% Functions 11/11
100% Lines 49/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152            1x   1x 1x                                     13x 13x 2x 2x                 4x 4x                     11x   8x 8x   1x 1x   2x     9x 18x                   2x 3x                 4x                   2x     5x 5x 5x                       4x 4x   4x   4x 4x 4x 11x 11x 11x   11x 11x 11x   11x   11x 11x 11x         4x 4x 4x   4x 4x   4x 4x   4x       1x  
/*
# Copyright Zhao Chaoyi. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
*/
 
const {msp, common} = require('@hyperledger/fabric-protos');
 
const ROLE_TYPE_MEMBER = 'MEMBER';
const ROLE_TYPE_PEER = 'PEER';
 
 
/**
 * KeyEndorsementPolicy is used to help set endorsement policies and decode them
 * into validation parameter byte arrays, the shim provides convenience functions
 * that allow the chaincode developer to deal with endorsement policies in terms
 * of the MSP identifiers of organizations.
 * For more informations, please read the [documents]{@link https://hyperledger-fabric.readthedocs.io/en/latest/endorsement-policies.html#setting-key-level-endorsement-policies}
 *
 * @class
 */
class KeyEndorsementPolicy {
    /**
     * The construcotr accepts an optional argument `policy`.
     * The argument `policy` will be parsed if provided.
     * @param {Buffer} policy the endorsement policy retrieved for a key
     */
    constructor(policy) {
        this.orgs = {};
        if (policy) {
            const spe = common.SignaturePolicyEnvelope.deserializeBinary(policy);
            this._setMspIdsFromSPE(spe);
        }
    }
 
    /**
     * returns the endorsement policy as bytes
     * @returns {Buffer} the endorsement policy
     */
    getPolicy() {
        const spe = this._getPolicyFromMspId();
        return spe.serializeBinary();
    }
 
    /**
     * adds the specified orgs to the list of orgs that are required
     * to endorse
     * @param {string} role the role of the new org(s). i.e., MEMBER or PEER
     * @param  {...string} neworgs the new org(s) to be added to the endorsement policy
     */
    addOrgs(role, ...neworgs) {
        let mspRole;
        switch (role) {
            case ROLE_TYPE_MEMBER:
                mspRole = msp.MSPRole.MSPRoleType.MEMBER;
                break;
            case ROLE_TYPE_PEER:
                mspRole = msp.MSPRole.MSPRoleType.PEER;
                break;
            default:
                throw new Error(`role type ${role} does not exist`);
        }
 
        neworgs.forEach(neworg => {
            this.orgs[neworg] = mspRole;
        });
    }
 
    /**
     * delete the specified channel orgs from the existing key-level endorsement
     * policy for this KVS key.
     * @param  {...string} delorgs the org(s) to be deleted
     */
    delOrgs(...delorgs) {
        delorgs.forEach(delorg => {
            delete this.orgs[delorg];
        });
    }
 
    /**
     * listOrgs returns an array of channel orgs that are required to endorse changes
     * @return {string[]} the list of channel orgs that are required to endorse changes
     */
    listOrgs() {
        return Object.keys(this.orgs);
    }
 
    /**
     * Internal used only, set the orgs map from a signature policy envelope
     * @param {_policiesProto.SignaturePolicyEnvelope} signaturePolicyEnvelope the signaturePolicyEnvelope
     *  decoded from the endorsement policy.
     */
    _setMspIdsFromSPE(signaturePolicyEnvelope) {
        // iterate over the identities in this envelope
        signaturePolicyEnvelope.getIdentitiesList().forEach(identity => {
            // this implementation only supports the ROLE type
            /* istanbul ignore else */
            if (identity.getPrincipalClassification() === msp.MSPPrincipal.Classification.ROLE) {
                const msprole = msp.MSPRole.deserializeBinary(identity.getPrincipal());
                this.orgs[msprole.getMspIdentifier()] = msprole.getRole();
            }
        });
    }
 
    /**
     * Internal used only. construct the policy from all orgs' mspIds.
     * the policy requires exactly 1 signature from all of the principals.
     * @returns {_policiesProto.SignaturePolicyEnvelope} return the SignaturePolicyEnvelope instance
     */
    _getPolicyFromMspId() {
 
        const spe = new common.SignaturePolicyEnvelope();
        spe.setVersion(0);
 
        const mspIds = this.listOrgs();
 
        const principals = [];
        const sigsPolicies = [];
        mspIds.forEach((mspId, i) => {
            const mspRole = new msp.MSPRole();
            mspRole.setMspIdentifier(mspId);
            mspRole.setRole(this.orgs[mspId]);
 
            const principal = new msp.MSPPrincipal();
            principal.setPrincipalClassification(msp.MSPPrincipal.Classification.ROLE);
            principal.setPrincipal(mspRole.serializeBinary());
 
            principals.push(principal);
 
            const signedBy = new common.SignaturePolicy();
            signedBy.setSignedBy(i);
            sigsPolicies.push(signedBy);
        });
 
 
        // 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);
 
        return spe;
    }
}
 
module.exports = KeyEndorsementPolicy;