示例#1
0
        private SignaturePolicyEnvelope PolicyFromMSPIDs()
        {
            List <string>          mspids     = ListOrgs().OrderByAlphaNumeric(a => a).ToList();
            List <MSPPrincipal>    principals = new List <MSPPrincipal>();
            List <SignaturePolicy> sigpolicy  = new List <SignaturePolicy>();

            for (int i = 0; i < mspids.Count; i++)
            {
                string mspid = mspids[i];
                principals.Add(new MSPPrincipal {
                    PrincipalClassification = MSPPrincipal.Types.Classification.Role, Principal = new MSPRole {
                        MspIdentifier = mspid, Role = orgs[mspid]
                    }.ToByteString()
                });
                sigpolicy.Add(StateBasedEndorsementUtils.SignedBy(i));
            }

            // create the policy: it requires exactly 1 signature from all of the principals
            SignaturePolicyEnvelope spe = new SignaturePolicyEnvelope {
                Version = 0, Rule = StateBasedEndorsementUtils.NOutOf(mspids.Count, sigpolicy)
            };

            spe.Identities.AddRange(principals);
            return(spe);
        }
        /**
         * From a yaml file
         *
         * @param yamlPolicyFile File location for the chaincode endorsement policy specification.
         * @throws IOException
         * @throws ChaincodeEndorsementPolicyParseException
         */

        public void FromYamlFile(string yamlPolicyFile)
        {
            IDeserializer nl = new DeserializerBuilder().Build();
            Dictionary <object, object> bld = (Dictionary <object, object>)nl.Deserialize(new StreamReader(File.OpenRead(yamlPolicyFile)));
            var mpy = (Dictionary <object, object>)bld.GetOrNull("policy");

            if (null == mpy)
            {
                throw new ChaincodeEndorsementPolicyParseException("The policy file has no policy section");
            }
            Dictionary <object, object> idsp = (Dictionary <object, object>)bld.GetOrNull("identities");

            if (null == idsp)
            {
                throw new ChaincodeEndorsementPolicyParseException("The policy file has no identities section");
            }
            IndexedHashMap <string, MSPPrincipal> identities = ParseIdentities(idsp);
            SignaturePolicy         sp  = ParsePolicy(identities, mpy);
            SignaturePolicyEnvelope env = new SignaturePolicyEnvelope {
                Rule = sp, Version = 0
            };

            env.Identities.AddRange(identities.Values);
            ChaincodeEndorsementPolicyAsBytes = env.ToByteArray();
        }
        /**
         * Creates a {@link SignaturePolicyEnvelope}
         * requiring 1 signature from any fabric entity, having the passed role, of the specified MSP
         *
         * @param mspId
         * @param role
         * @return
         */
        public static SignaturePolicyEnvelope SignedByFabricEntity(string mspId, MSPRole.Types.MSPRoleType role)
        {
            // specify the principal: it's a member of the msp we just found
            MSPPrincipal principal = new MSPPrincipal {
                PrincipalClassification = MSPPrincipal.Types.Classification.Role, Principal = new MSPRole {
                    MspIdentifier = mspId, Role = role
                }.ToByteString()
            };
            SignaturePolicyEnvelope spe = new SignaturePolicyEnvelope {
                Version = 0, Rule = NOutOf(1, new List <SignaturePolicy> {
                    SignedBy(0)
                })
            };

            spe.Identities.Add(principal);
            return(spe);
        }
示例#4
0
 public StateBasedEndorsement(byte[] ep)
 {
     if (ep == null)
     {
         ep = new byte[] { }
     }
     ;
     try
     {
         SignaturePolicyEnvelope spe = SignaturePolicyEnvelope.Parser.ParseFrom(ep);
         SetMSPIDsFromSP(spe);
     }
     catch (InvalidProtocolBufferException e)
     {
         throw new ArgumentException("error unmarshaling endorsement policy bytes", e);
     }
 }
示例#5
0
        private SignaturePolicyEnvelope ParseSignaturePolicyEnvelope(JObject scf)
        {
            JObject signaturePolicyEnvelope = scf["SignaturePolicyEnvelope"] as JObject;

            if (signaturePolicyEnvelope == null)
            {
                throw new ChaincodeCollectionConfigurationException($"Expected SignaturePolicyEnvelope to be Object type but got: {scf.Type.ToString()}");
            }
            JArray ids = signaturePolicyEnvelope["identities"] as JArray;
            Dictionary <string, MSPPrincipal> identities = ParseIdentities(ids);
            SignaturePolicy         sp  = ParsePolicy(identities, signaturePolicyEnvelope["policy"] as JObject);
            SignaturePolicyEnvelope env = new SignaturePolicyEnvelope();

            env.Identities.Add(identities.Values);
            env.Rule = sp;
//        env.Version = signaturePolicyEnvelope["identities"].ToObject<int>();
            return(env);
        }
        public void TestLoadFromConfigFileYamlBasic()
        {
            ChaincodeCollectionConfiguration config = ChaincodeCollectionConfiguration.FromYamlFile("Fixture/collectionProperties/testCollection.yaml".Locate());

            Assert.IsNotNull(config);
            byte[] configAsBytes = config.GetAsBytes();
            Assert.IsNotNull(configAsBytes);
            Assert.AreEqual(configAsBytes.Length, 137);
            CollectionConfigPackage collectionConfigPackage = CollectionConfigPackage.Parser.ParseFrom(configAsBytes);

            Assert.AreEqual(collectionConfigPackage.Config.Count, 1);
            CollectionConfig colConfig = collectionConfigPackage.Config.FirstOrDefault();

            Assert.IsNotNull(colConfig);
            StaticCollectionConfig staticCollectionConfig = colConfig.StaticCollectionConfig;

            Assert.IsNotNull(staticCollectionConfig);
            Assert.AreEqual(staticCollectionConfig.BlockToLive, (ulong)3);
            Assert.AreEqual(staticCollectionConfig.Name, "rick");
            Assert.AreEqual(staticCollectionConfig.MaximumPeerCount, 9);
            Assert.AreEqual(staticCollectionConfig.RequiredPeerCount, 7);
            CollectionPolicyConfig memberOrgsPolicy = staticCollectionConfig.MemberOrgsPolicy;

            Assert.IsNotNull(memberOrgsPolicy);
            SignaturePolicyEnvelope signaturePolicy = memberOrgsPolicy.SignaturePolicy;

            Assert.IsNotNull(signaturePolicy);
            Assert.AreEqual(signaturePolicy.Version, 0);
            SignaturePolicy rule = signaturePolicy.Rule;

            Assert.IsNotNull(rule);
            Assert.AreEqual(rule.TypeCase, SignaturePolicy.TypeOneofCase.NOutOf);
            SignaturePolicy.Types.NOutOf nOutOf = rule.NOutOf;
            Assert.IsNotNull(nOutOf);
            Assert.AreEqual(2, nOutOf.N);
            List <MSPPrincipal> identitiesList = signaturePolicy.Identities?.ToList();

            Assert.IsNotNull(identitiesList);
            Assert.AreEqual(3, identitiesList.Count);
        }
        public void TestSDKIntegrationYaml()
        {
            ChaincodeEndorsementPolicy itTestPolicy = new ChaincodeEndorsementPolicy();

            itTestPolicy.FromYamlFile("Fixture/sdkintegration/chaincodeendorsementpolicy.yaml".Locate());
            SignaturePolicyEnvelope sigPolEnv      = SignaturePolicyEnvelope.Parser.ParseFrom(itTestPolicy.ChaincodeEndorsementPolicyAsBytes);
            List <MSPPrincipal>     identitiesList = sigPolEnv.Identities.ToList();

            foreach (MSPPrincipal ident in identitiesList)
            {
                MSPPrincipal mspPrincipal = MSPPrincipal.Parser.ParseFrom(ident.Principal);
                MSPPrincipal.Types.Classification principalClassification = mspPrincipal.PrincipalClassification;
                Assert.AreEqual(principalClassification.ToString(), MSPPrincipal.Types.Classification.Role.ToString());
                MSPRole mspRole = MSPRole.Parser.ParseFrom(ident.Principal);
                string  iden    = mspRole.MspIdentifier;
                Assert.IsTrue("Org1MSP".Equals(iden) || "Org2MSP".Equals(iden));
                Assert.IsTrue(mspRole.Role == MSPRole.Types.MSPRoleType.Admin || mspRole.Role == MSPRole.Types.MSPRoleType.Member);
            }

            SignaturePolicy rule = sigPolEnv.Rule;

            SignaturePolicy.TypeOneofCase typeCase = rule.TypeCase;
            Assert.AreEqual(SignaturePolicy.TypeOneofCase.NOutOf, typeCase);
        }
示例#8
0
 private void SetMSPIDsFromSP(SignaturePolicyEnvelope spe)
 {
     spe.Identities.Where(a => a.PrincipalClassification == MSPPrincipal.Types.Classification.Role).ToList().ForEach(AddOrg);
 }
示例#9
0
        public byte[] Policy()
        {
            SignaturePolicyEnvelope spe = PolicyFromMSPIDs();

            return(spe.ToByteArray());
        }