org.hyperledger.fabric.sdk.exception.ChaincodeEndorsementPolicyParseException Java Examples

The following examples show how to use org.hyperledger.fabric.sdk.exception.ChaincodeEndorsementPolicyParseException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IntermediateChaincodeID.java    From fabric-net-server with Apache License 2.0 6 votes vote down vote up
/**
 * 实例化智能合约
 *
 * @param org  中继组织对象
 * @param args 初始化参数数组
 */
JSONObject instantiate(IntermediateOrg org, String[] args) throws ProposalException, InvalidArgumentException, IOException, ChaincodeEndorsementPolicyParseException, TransactionException {
    /// Send transaction proposal to all peers
    InstantiateProposalRequest instantiateProposalRequest = org.getClient().newInstantiationProposalRequest();
    instantiateProposalRequest.setChaincodeID(chaincodeID);
    instantiateProposalRequest.setProposalWaitTime(proposalWaitTime);
    instantiateProposalRequest.setArgs(args);

    ChaincodeEndorsementPolicy chaincodeEndorsementPolicy = new ChaincodeEndorsementPolicy();
    chaincodeEndorsementPolicy.fromYamlFile(new File(chaincodePolicy));
    instantiateProposalRequest.setChaincodeEndorsementPolicy(chaincodeEndorsementPolicy);

    Map<String, byte[]> tm2 = new HashMap<>();
    tm2.put("HyperLedgerFabric", "InstantiateProposalRequest:JavaSDK".getBytes(UTF_8));
    tm2.put("method", "InstantiateProposalRequest".getBytes(UTF_8));
    tm2.put("result", ":)".getBytes(UTF_8));
    instantiateProposalRequest.setTransientMap(tm2);

    long currentStart = System.currentTimeMillis();
    Collection<ProposalResponse> instantiateProposalResponses = org.getChannel().get().sendInstantiationProposal(instantiateProposalRequest, org.getChannel().get().getPeers());
    log.info("chaincode instantiate transaction proposal time = " + (System.currentTimeMillis() - currentStart));
    return toOrdererResponse(instantiateProposalResponses, org);
}
 
Example #2
Source File: IntermediateChaincodeID.java    From fabric-net-server with Apache License 2.0 6 votes vote down vote up
/**
 * 升级智能合约
 *
 * @param org  中继组织对象
 * @param args 初始化参数数组
 */
JSONObject upgrade(IntermediateOrg org, String[] args) throws ProposalException, InvalidArgumentException, IOException, ChaincodeEndorsementPolicyParseException, TransactionException {
    /// Send transaction proposal to all peers
    UpgradeProposalRequest upgradeProposalRequest = org.getClient().newUpgradeProposalRequest();
    upgradeProposalRequest.setChaincodeID(chaincodeID);
    upgradeProposalRequest.setProposalWaitTime(proposalWaitTime);
    upgradeProposalRequest.setArgs(args);

    ChaincodeEndorsementPolicy chaincodeEndorsementPolicy = new ChaincodeEndorsementPolicy();
    chaincodeEndorsementPolicy.fromYamlFile(new File(chaincodePolicy));
    upgradeProposalRequest.setChaincodeEndorsementPolicy(chaincodeEndorsementPolicy);

    Map<String, byte[]> tm2 = new HashMap<>();
    tm2.put("HyperLedgerFabric", "UpgradeProposalRequest:JavaSDK".getBytes(UTF_8));
    tm2.put("method", "UpgradeProposalRequest".getBytes(UTF_8));
    tm2.put("result", ":)".getBytes(UTF_8));
    upgradeProposalRequest.setTransientMap(tm2);

    long currentStart = System.currentTimeMillis();
    Collection<ProposalResponse> upgradeProposalResponses = org.getChannel().get().sendUpgradeProposal(upgradeProposalRequest, org.getChannel().get().getPeers());
    log.info("chaincode instantiate transaction proposal time = " + (System.currentTimeMillis() - currentStart));
    return toOrdererResponse(upgradeProposalResponses, org);
}
 
Example #3
Source File: ChaincodeEndorsementPolicy.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
/**
 * From a yaml file
 *
 * @param yamlPolicyFile File location for the chaincode endorsement policy specification.
 * @throws IOException
 * @throws ChaincodeEndorsementPolicyParseException
 * @deprecated use {@link #fromYamlFile(Path)}
 */

public void fromYamlFile(File yamlPolicyFile) throws IOException, ChaincodeEndorsementPolicyParseException {
    final Yaml yaml = new Yaml(new SafeConstructor());
    final Map<?, ?> load = (Map<?, ?>) yaml.load(new FileInputStream(yamlPolicyFile));

    Map<?, ?> mp = (Map<?, ?>) load.get("policy");

    if (null == mp) {
        throw new ChaincodeEndorsementPolicyParseException("The policy file has no policy section");
    }

    IndexedHashMap<String, MSPPrincipal> identities = parseIdentities((Map<?, ?>) load.get("identities"));

    SignaturePolicy sp = parsePolicy(identities, mp);

    policyBytes = Policies.SignaturePolicyEnvelope.newBuilder()
            .setVersion(0)
            .addAllIdentities(identities.values())
            .setRule(sp)
            .build().toByteArray();
}
 
Example #4
Source File: ChaincodeEndorsementPolicy.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
public static ChaincodeEndorsementPolicy fromYamlFile(Path yamlPolicyFile) throws IOException, ChaincodeEndorsementPolicyParseException {
    final Yaml yaml = new Yaml(new SafeConstructor());
    final Map<?, ?> load = (Map<?, ?>) yaml.load(new FileInputStream(yamlPolicyFile.toFile()));

    Map<?, ?> mp = (Map<?, ?>) load.get("policy");

    if (null == mp) {
        throw new ChaincodeEndorsementPolicyParseException("The policy file has no policy section");
    }

    IndexedHashMap<String, MSPPrincipal> identities = parseIdentities((Map<?, ?>) load.get("identities"));

    SignaturePolicy sp = parsePolicy(identities, mp);

    ChaincodeEndorsementPolicy chaincodeEndorsementPolicy = new ChaincodeEndorsementPolicy();

    chaincodeEndorsementPolicy.policyBytes = Policies.SignaturePolicyEnvelope.newBuilder()
            .setVersion(0)
            .addAllIdentities(identities.values())
            .setRule(sp)
            .build().toByteArray();

    return chaincodeEndorsementPolicy;
}
 
Example #5
Source File: LifecycleChaincodeEndorsementPolicy.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
public static LifecycleChaincodeEndorsementPolicy fromSignaturePolicyYamlFile(Path yamlPolicyFile) throws IOException, ChaincodeEndorsementPolicyParseException {
    final Yaml yaml = new Yaml(new SafeConstructor());
    final Map<?, ?> load = (Map<?, ?>) yaml.load(new FileInputStream(yamlPolicyFile.toFile()));

    Map<?, ?> mp = (Map<?, ?>) load.get("policy");

    if (null == mp) {
        throw new ChaincodeEndorsementPolicyParseException("The policy file has no policy section");
    }

    IndexedHashMap<String, MSPPrincipal> identities = parseIdentities((Map<?, ?>) load.get("identities"));

    SignaturePolicy sp = parsePolicy(identities, mp);

    return new LifecycleChaincodeEndorsementPolicy(Policy.ApplicationPolicy.newBuilder()
            .setSignaturePolicy(Policies.SignaturePolicyEnvelope.newBuilder()
                    .setVersion(0)
                    .addAllIdentities(identities.values())
                    .setRule(sp)
                    .build()).build().toByteString());
}
 
Example #6
Source File: QueryBlock.java    From fabric-jdbc-connector with Apache License 2.0 5 votes vote down vote up
private ChaincodeEndorsementPolicy getChaincodeEndorsementPolicy(Endorsers endorsers) {
    ChaincodeEndorsementPolicy policy = new ChaincodeEndorsementPolicy();
    PolicyFile policyFile = endorsers.getChildType(PolicyFile.class, 0);
    String fileLoc = policyFile.getFileLocation().replace("'", "").replace("\"", "");
    try {
        policy.fromYamlFile(new File(fileLoc));
    } catch (ChaincodeEndorsementPolicyParseException | IOException e) {
        String errMsg = "QueryBlock | getChaincodeEndorsementPolicy |" + e;
        logger.error(errMsg);
        throw new BlkchnException(errMsg, e);
    }
    return policy;
}
 
Example #7
Source File: ChaincodeEndorsementPolicyTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for {@link ChaincodeEndorsementPolicy#fromYamlFile(File)}
 *
 * @throws IOException
 * @throws ChaincodeEndorsementPolicyParseException
 */
@Test
public void testSDKIntegrationYaml() throws IOException, ChaincodeEndorsementPolicyParseException {

    ChaincodeEndorsementPolicy itTestPolicy = new ChaincodeEndorsementPolicy();
    itTestPolicy.fromYamlFile(new File("src/test/fixture/sdkintegration/chaincodeendorsementpolicy.yaml"));

    SignaturePolicyEnvelope sigPolEnv = SignaturePolicyEnvelope.parseFrom(itTestPolicy.getChaincodeEndorsementPolicyAsBytes());
    List<MSPPrincipal> identitiesList = sigPolEnv.getIdentitiesList();
    for (MSPPrincipal ident : identitiesList) {

        MSPPrincipal mspPrincipal = MSPPrincipal.parseFrom(ident.getPrincipal());
        MSPPrincipal.Classification principalClassification = mspPrincipal.getPrincipalClassification();
        assertEquals(principalClassification.toString(), MSPPrincipal.Classification.ROLE.name());
        MspPrincipal.MSPRole mspRole = MspPrincipal.MSPRole.parseFrom(ident.getPrincipal());

        String iden = mspRole.getMspIdentifier();
        assertTrue("Org1MSP".equals(iden) || "Org2MSP".equals(iden));
        assertTrue(mspRole.getRole().getNumber() == MspPrincipal.MSPRole.MSPRoleType.ADMIN_VALUE
                || mspRole.getRole().getNumber() == MspPrincipal.MSPRole.MSPRoleType.MEMBER_VALUE);

    }

    Policies.SignaturePolicy rule = sigPolEnv.getRule();
    TypeCase typeCase = rule.getTypeCase();
    assertEquals(TypeCase.N_OUT_OF.getNumber(), typeCase.getNumber());
}
 
Example #8
Source File: ChannelClient.java    From blockchain-application-using-fabric-java-sdk with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * Instantiate chaincode.
 * 
 * @param chaincodeName
 * @param version
 * @param chaincodePath
 * @param language
 * @param functionName
 * @param functionArgs
 * @param policyPath
 * @return
 * @throws InvalidArgumentException
 * @throws ProposalException
 * @throws ChaincodeEndorsementPolicyParseException
 * @throws IOException
 */
public Collection<ProposalResponse> instantiateChainCode(String chaincodeName, String version, String chaincodePath,
		String language, String functionName, String[] functionArgs, String policyPath)
		throws InvalidArgumentException, ProposalException, ChaincodeEndorsementPolicyParseException, IOException {
	Logger.getLogger(ChannelClient.class.getName()).log(Level.INFO,
			"Instantiate proposal request " + chaincodeName + " on channel " + channel.getName()
					+ " with Fabric client " + fabClient.getInstance().getUserContext().getMspId() + " "
					+ fabClient.getInstance().getUserContext().getName());
	InstantiateProposalRequest instantiateProposalRequest = fabClient.getInstance()
			.newInstantiationProposalRequest();
	instantiateProposalRequest.setProposalWaitTime(180000);
	ChaincodeID.Builder chaincodeIDBuilder = ChaincodeID.newBuilder().setName(chaincodeName).setVersion(version)
			.setPath(chaincodePath);
	ChaincodeID ccid = chaincodeIDBuilder.build();
	Logger.getLogger(ChannelClient.class.getName()).log(Level.INFO,
			"Instantiating Chaincode ID " + chaincodeName + " on channel " + channel.getName());
	instantiateProposalRequest.setChaincodeID(ccid);
	if (language.equals(Type.GO_LANG.toString()))
		instantiateProposalRequest.setChaincodeLanguage(Type.GO_LANG);
	else
		instantiateProposalRequest.setChaincodeLanguage(Type.JAVA);

	instantiateProposalRequest.setFcn(functionName);
	instantiateProposalRequest.setArgs(functionArgs);
	Map<String, byte[]> tm = new HashMap<>();
	tm.put("HyperLedgerFabric", "InstantiateProposalRequest:JavaSDK".getBytes(UTF_8));
	tm.put("method", "InstantiateProposalRequest".getBytes(UTF_8));
	instantiateProposalRequest.setTransientMap(tm);

	if (policyPath != null) {
		ChaincodeEndorsementPolicy chaincodeEndorsementPolicy = new ChaincodeEndorsementPolicy();
		chaincodeEndorsementPolicy.fromYamlFile(new File(policyPath));
		instantiateProposalRequest.setChaincodeEndorsementPolicy(chaincodeEndorsementPolicy);
	}

	Collection<ProposalResponse> responses = channel.sendInstantiationProposal(instantiateProposalRequest);
	CompletableFuture<TransactionEvent> cf = channel.sendTransaction(responses);
	
	Logger.getLogger(ChannelClient.class.getName()).log(Level.INFO,
			"Chaincode " + chaincodeName + " on channel " + channel.getName() + " instantiation " + cf);
	return responses;
}
 
Example #9
Source File: ChaincodeEndorsementPolicy.java    From fabric-sdk-java with Apache License 2.0 4 votes vote down vote up
private static IndexedHashMap<String, MSPPrincipal> parseIdentities(Map<?, ?> identities) throws ChaincodeEndorsementPolicyParseException {
    //Only Role types are excepted at this time.

    IndexedHashMap<String, MSPPrincipal> ret = new IndexedHashMap<>();

    for (Map.Entry<?, ?> kp : identities.entrySet()) {
        Object key = kp.getKey();
        Object val = kp.getValue();

        if (!(key instanceof String)) {
            throw new ChaincodeEndorsementPolicyParseException(format("In identities key expected String got %s ", key == null ? "null" : key.getClass().getName()));
        }

        if (null != ret.get(key)) {
            throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s is listed more than once ", key));
        }

        if (!(val instanceof Map)) {
            throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s value expected Map got %s ", key, val == null ? "null" : val.getClass().getName()));
        }

        Object role = ((Map<?, ?>) val).get("role");

        if (!(role instanceof Map)) {
            throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s value expected Map for role got %s ", key, role == null ? "null" : role.getClass().getName()));
        }
        final Map<?, ?> roleMap = (Map<?, ?>) role;

        Object nameObj = (roleMap).get("name");

        if (!(nameObj instanceof String)) {
            throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s name expected String in role got %s ",
                    key, nameObj == null ? "null" : nameObj.getClass().getName()));
        }
        String name = (String) nameObj;
        name = name.trim();
        Object mspId = roleMap.get("mspId");

        if (!(mspId instanceof String)) {
            throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s mspId expected String in role got %s ", key, mspId == null ? "null" : mspId.getClass().getName()));
        }

        if (StringUtil.isNullOrEmpty((String) mspId)) {

            throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s mspId must not be null or empty String in role ", key));

        }

        MSPRole.MSPRoleType mspRoleType;

        switch (name) {
            case "member":
                mspRoleType = MSPRole.MSPRoleType.MEMBER;
                break;
            case "admin":
                mspRoleType = MSPRole.MSPRoleType.ADMIN;
                break;
            case "client":
                mspRoleType = MSPRole.MSPRoleType.CLIENT;
                break;
            case "peer":
                mspRoleType = MSPRole.MSPRoleType.PEER;
                break;
            default:
                throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s name expected member, admin, client, or peer in role got %s ", key, name));
        }

        MSPRole mspRole = MSPRole.newBuilder().setRole(mspRoleType)
                .setMspIdentifier((String) mspId).build();

        MSPPrincipal principal = MSPPrincipal.newBuilder()
                .setPrincipalClassification(MSPPrincipal.Classification.ROLE)
                .setPrincipal(mspRole.toByteString()).build();

        ret.put((String) key, principal);

    }

    if (ret.size() == 0) {
        throw new ChaincodeEndorsementPolicyParseException("No identities were found in the policy specification");
    }

    return ret;

}
 
Example #10
Source File: LifecycleChaincodeEndorsementPolicy.java    From fabric-sdk-java with Apache License 2.0 4 votes vote down vote up
private static IndexedHashMap<String, MSPPrincipal> parseIdentities(Map<?, ?> identities) throws ChaincodeEndorsementPolicyParseException {
    //Only Role types are excepted at this time.

    IndexedHashMap<String, MSPPrincipal> ret = new IndexedHashMap<>();

    for (Map.Entry<?, ?> kp : identities.entrySet()) {
        Object key = kp.getKey();
        Object val = kp.getValue();

        if (!(key instanceof String)) {
            throw new ChaincodeEndorsementPolicyParseException(format("In identities key expected String got %s ", key == null ? "null" : key.getClass().getName()));
        }

        if (null != ret.get(key)) {
            throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s is listed more than once ", key));
        }

        if (!(val instanceof Map)) {
            throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s value expected Map got %s ", key, val == null ? "null" : val.getClass().getName()));
        }

        Object role = ((Map<?, ?>) val).get("role");

        if (!(role instanceof Map)) {
            throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s value expected Map for role got %s ", key, role == null ? "null" : role.getClass().getName()));
        }
        final Map<?, ?> roleMap = (Map<?, ?>) role;

        Object nameObj = (roleMap).get("name");

        if (!(nameObj instanceof String)) {
            throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s name expected String in role got %s ",
                    key, nameObj == null ? "null" : nameObj.getClass().getName()));
        }
        String name = (String) nameObj;
        name = name.trim();
        Object mspId = roleMap.get("mspId");

        if (!(mspId instanceof String)) {
            throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s mspId expected String in role got %s ", key, mspId == null ? "null" : mspId.getClass().getName()));
        }

        if (StringUtil.isNullOrEmpty((String) mspId)) {

            throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s mspId must not be null or empty String in role ", key));

        }

        MSPRole.MSPRoleType mspRoleType;

        switch (name) {
            case "member":
                mspRoleType = MSPRole.MSPRoleType.MEMBER;
                break;
            case "admin":
                mspRoleType = MSPRole.MSPRoleType.ADMIN;
                break;
            case "client":
                mspRoleType = MSPRole.MSPRoleType.CLIENT;
                break;
            case "peer":
                mspRoleType = MSPRole.MSPRoleType.PEER;
                break;
            default:
                throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s name expected member, admin, client, or peer in role got %s ", key, name));
        }

        MSPRole mspRole = MSPRole.newBuilder().setRole(mspRoleType)
                .setMspIdentifier((String) mspId).build();

        MSPPrincipal principal = MSPPrincipal.newBuilder()
                .setPrincipalClassification(MSPPrincipal.Classification.ROLE)
                .setPrincipal(mspRole.toByteString()).build();

        ret.put((String) key, principal);

    }

    if (ret.size() == 0) {
        throw new ChaincodeEndorsementPolicyParseException("No identities were found in the policy specification");
    }

    return ret;

}
 
Example #11
Source File: FabricManager.java    From fabric-net-server with Apache License 2.0 2 votes vote down vote up
/**
 * 实例化智能合约
 *
 * @param args 初始化参数数组
 */
public JSONObject instantiate(String[] args) throws ProposalException, InvalidArgumentException, IOException, ChaincodeEndorsementPolicyParseException, TransactionException {
    return org.getChainCode().instantiate(org, args);
}
 
Example #12
Source File: FabricManager.java    From fabric-net-server with Apache License 2.0 2 votes vote down vote up
/**
 * 升级智能合约
 *
 * @param args 初始化参数数组
 */
public JSONObject upgrade(String[] args) throws ProposalException, InvalidArgumentException, IOException, ChaincodeEndorsementPolicyParseException, TransactionException {
    return org.getChainCode().upgrade(org, args);
}
 
Example #13
Source File: ChaincodeEndorsementPolicy.java    From fabric-sdk-java with Apache License 2.0 2 votes vote down vote up
private static SignaturePolicy parsePolicy(IndexedHashMap<String, MSPPrincipal> identities, Map<?, ?> mp) throws ChaincodeEndorsementPolicyParseException {

        if (mp == null) {
            throw new ChaincodeEndorsementPolicyParseException("No policy section was found in the document.");
        }
        if (!(mp instanceof Map)) {
            throw new ChaincodeEndorsementPolicyParseException("Policy expected object section was not found in the document.");

        }

        for (Map.Entry<?, ?> ks : mp.entrySet()) {
            Object ko = ks.getKey();
            Object vo = ks.getValue();
            final String key = (String) ko;
            // String val = (String) vo;

            if ("signed-by".equals(key)) {

                if (!(vo instanceof String)) {
                    throw new ChaincodeEndorsementPolicyParseException("signed-by expecting a string value");
                }

                MSPPrincipal mspPrincipal = identities.get(vo);
                if (null == mspPrincipal) {
                    throw new ChaincodeEndorsementPolicyParseException(format("No identity found by name %s in signed-by.", vo));
                }

                return SignaturePolicy.newBuilder()
                        .setSignedBy(identities.getKeysIndex((String) vo))
                        .build();

            } else {

                Matcher match = noofPattern.matcher(key);

                if (match.matches() && match.groupCount() == 1) {

                    String matchStingNo = match.group(1).trim();
                    int matchNo = Integer.parseInt(matchStingNo);

                    if (!(vo instanceof List)) {
                        throw new ChaincodeEndorsementPolicyParseException(format("%s expected to have list but found %s.", key, String.valueOf(vo)));
                    }

                    @SuppressWarnings ("unchecked") final List<Map<?, ?>> voList = (List<Map<?, ?>>) vo;

                    if (voList.size() < matchNo) {

                        throw new ChaincodeEndorsementPolicyParseException(format("%s expected to have at least %d items to match but only found %d.", key, matchNo, voList.size()));
                    }

                    SignaturePolicy.NOutOf.Builder spBuilder = SignaturePolicy.NOutOf.newBuilder()
                            .setN(matchNo);

                    for (Map<?, ?> nlo : voList) {

                        SignaturePolicy sp = parsePolicy(identities, nlo);
                        spBuilder.addRules(sp);

                    }

                    return SignaturePolicy.newBuilder().setNOutOf(spBuilder.build()).build();

                } else {

                    throw new ChaincodeEndorsementPolicyParseException(format("Unsupported policy type %s", key));
                }

            }

        }
        throw new ChaincodeEndorsementPolicyParseException("No values found for policy");

    }
 
Example #14
Source File: LifecycleChaincodeEndorsementPolicy.java    From fabric-sdk-java with Apache License 2.0 2 votes vote down vote up
private static SignaturePolicy parsePolicy(IndexedHashMap<String, MSPPrincipal> identities, Map<?, ?> mp) throws ChaincodeEndorsementPolicyParseException {

        if (mp == null) {
            throw new ChaincodeEndorsementPolicyParseException("No policy section was found in the document.");
        }
        if (!(mp instanceof Map)) {
            throw new ChaincodeEndorsementPolicyParseException("Policy expected object section was not found in the document.");

        }

        for (Map.Entry<?, ?> ks : mp.entrySet()) {
            Object ko = ks.getKey();
            Object vo = ks.getValue();
            final String key = (String) ko;
            // String val = (String) vo;

            if ("signed-by".equals(key)) {

                if (!(vo instanceof String)) {
                    throw new ChaincodeEndorsementPolicyParseException("signed-by expecting a string value");
                }

                MSPPrincipal mspPrincipal = identities.get(vo);
                if (null == mspPrincipal) {
                    throw new ChaincodeEndorsementPolicyParseException(format("No identity found by name %s in signed-by.", vo));
                }

                return SignaturePolicy.newBuilder()
                        .setSignedBy(identities.getKeysIndex((String) vo))
                        .build();

            } else {

                Matcher match = noofPattern.matcher(key);

                if (match.matches() && match.groupCount() == 1) {

                    String matchStingNo = match.group(1).trim();
                    int matchNo = Integer.parseInt(matchStingNo);

                    if (!(vo instanceof List)) {
                        throw new ChaincodeEndorsementPolicyParseException(format("%s expected to have list but found %s.", key, String.valueOf(vo)));
                    }

                    @SuppressWarnings ("unchecked") final List<Map<?, ?>> voList = (List<Map<?, ?>>) vo;

                    if (voList.size() < matchNo) {

                        throw new ChaincodeEndorsementPolicyParseException(format("%s expected to have at least %d items to match but only found %d.", key, matchNo, voList.size()));
                    }

                    SignaturePolicy.NOutOf.Builder spBuilder = SignaturePolicy.NOutOf.newBuilder()
                            .setN(matchNo);

                    for (Map<?, ?> nlo : voList) {

                        SignaturePolicy sp = parsePolicy(identities, nlo);
                        spBuilder.addRules(sp);

                    }

                    return SignaturePolicy.newBuilder().setNOutOf(spBuilder.build()).build();

                } else {

                    throw new ChaincodeEndorsementPolicyParseException(format("Unsupported policy type %s", key));
                }

            }

        }
        throw new ChaincodeEndorsementPolicyParseException("No values found for policy");

    }
 
Example #15
Source File: LifecycleChaincodeEndorsementPolicy.java    From fabric-sdk-java with Apache License 2.0 2 votes vote down vote up
public static LifecycleChaincodeEndorsementPolicy fromPolicyReference(String channelConfigPolicyReference) throws IOException, ChaincodeEndorsementPolicyParseException {

        return new LifecycleChaincodeEndorsementPolicy(Policy.ApplicationPolicy.newBuilder()
                .setChannelConfigPolicyReference(channelConfigPolicyReference).build().toByteString());

    }