Esempio n. 1
0
def decode_signature_policy_envelope(signature_policy_envelope_bytes):
    """Decodes signature policy envelope bytes

    Args:
        signature_policy_envelope_bytes (str): Serialized signature envelope

    Returns: deserialized signature policy envelope contents.
    """
    signature_policy_envelope = {}
    proto_signature_policy_envelope = policies_pb2.SignaturePolicyEnvelope()
    proto_signature_policy_envelope.ParseFromString(
        signature_policy_envelope_bytes)
    signature_policy_envelope['version'] = \
        decode_version(proto_signature_policy_envelope.version)
    signature_policy_envelope['rule'] = \
        decode_signature_policy(proto_signature_policy_envelope.rule)
    identities = []
    proto_identities = proto_signature_policy_envelope.identities
    if proto_identities:
        for identity in proto_identities:
            msp_principal = decode_MSP_principal(identity)
            identities.append(msp_principal)
    signature_policy_envelope['identities'] = identities
    return signature_policy_envelope
    def _build_policy(self, policy, msps=None, returnProto=False):
        proto_signature_policy_envelope = \
            policies_pb2.SignaturePolicyEnvelope()

        if policy:
            self._check_policy(policy)
            proto_signature_policy_envelope.version = 0
            proto_signature_policy_envelope.rule.CopyFrom(
                self._get_policy(policy['policy']))
            proto_signature_policy_envelope.identities.extend(
                [self._build_principal(x) for x in policy['identities']])
        else:
            # TODO need to support MSPManager
            # no policy was passed in, construct a 'Signed By any member
            # of an organization by mspid' policy
            # construct a list of msp principals to select from using the
            # 'n out of' operator

            # for not making it fail with current code
            return proto_b('')

            principals = []
            signedBys = []
            index = 0

            if msps is None:
                msps = []

            for msp in msps:
                onePrn = msp_principal_pb2.MSPPrincipal()
                onePrn.principal_classification = \
                    msp_principal_pb2.MSPPrincipal.ROLE

                memberRole = msp_principal_pb2.MSPRole()
                memberRole.role = msp_principal_pb2.MSPRole.MEMBER
                memberRole.msp_identifier = msp

                onePrn.principal = memberRole.SerializeToString()

                principals.append(onePrn)

                signedBy = policies_pb2.SignaturePolicy()
                index += 1
                signedBy.signed_by = index
                signedBys.append(signedBy)

            if len(principals) == 0:
                raise Exception('Verifying MSPs not found in the'
                                ' channel object, make sure'
                                ' "initialize()" is called first.')

            oneOfAny = policies_pb2.SignaturePolicy.NOutOf()
            oneOfAny.n = 1
            oneOfAny.rules.extend(signedBys)

            noutof = policies_pb2.SignaturePolicy()
            noutof.n_out_of.CopyFrom(oneOfAny)

            proto_signature_policy_envelope.version = 0
            proto_signature_policy_envelope.rule.CopyFrom(noutof)
            proto_signature_policy_envelope.identities.extend(principals)

        if returnProto:
            return proto_signature_policy_envelope

        return proto_signature_policy_envelope.SerializeToString()