private static IndexedHashMap <string, MSPPrincipal> ParseIdentities(Dictionary <object, object> identities)
        {
            //Only Role types are excepted at this time.

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

            foreach (KeyValuePair <object, object> kp in identities)
            {
                string key = (string)kp.Key;
                object val = kp.Value;

                /*
                 * if (!(key instanceof String)) {
                 *  throw new ChaincodeEndorsementPolicyParseException(format("In identities key expected String got %s ", key == null ? "null" : key.getClass().getName()));
                 * }
                 */
                if (ret.ContainsKey(key))
                {
                    throw new ChaincodeEndorsementPolicyParseException($"In identities with key {key} is listed more than once ");
                }

                if (!(val is Dictionary <object, object> dictval))
                {
                    string str = val == null ? "null" : val.GetType().Name;
                    throw new ChaincodeEndorsementPolicyParseException($"In identities with key {key} value expected Map got {str}");
                }

                object role = dictval.ContainsKey("role") ? dictval["role"] : null;
                if (!(role is Dictionary <object, object> roleMap))
                {
                    string str = role == null ? "null" : role.GetType().Name;
                    throw new ChaincodeEndorsementPolicyParseException($"In identities with key {key} value expected Map for role got {str}");
                }

                object nameObj = roleMap.ContainsKey("name") ? roleMap["name"] : null;
                if (!(nameObj is string name))
                {
                    string str = nameObj == null ? "null" : nameObj.GetType().Name;
                    throw new ChaincodeEndorsementPolicyParseException($"In identities with key {key} name expected String in role got {str}");
                }

                name = name.Trim();
                object mspId = roleMap.ContainsKey("mspId") ? roleMap["mspId"] : null;

                if (!(mspId is string))
                {
                    string str = mspId == null ? "null" : mspId.GetType().Name;
                    throw new ChaincodeEndorsementPolicyParseException($"In identities with key {key} mspId expected String in role got {str}");
                }

                if (string.IsNullOrEmpty((string)mspId))
                {
                    throw new ChaincodeEndorsementPolicyParseException($"In identities with key {key} mspId must not be null or empty String in role");
                }

                MSPRole.Types.MSPRoleType mspRoleType;

                switch (name)
                {
                case "member":
                    mspRoleType = MSPRole.Types.MSPRoleType.Member;
                    break;

                case "admin":
                    mspRoleType = MSPRole.Types.MSPRoleType.Admin;
                    break;

                case "client":
                    mspRoleType = MSPRole.Types.MSPRoleType.Client;
                    break;

                case "peer":
                    mspRoleType = MSPRole.Types.MSPRoleType.Peer;
                    break;

                default:
                    throw new ChaincodeEndorsementPolicyParseException($"In identities with key {key} name expected member, admin, client, or peer in role got {name}");
                }

                MSPRole mspRole = new MSPRole {
                    MspIdentifier = (string)mspId, Role = mspRoleType
                };
                MSPPrincipal principal = new MSPPrincipal {
                    Principal = mspRole.ToByteString(), PrincipalClassification = MSPPrincipal.Types.Classification.Role
                };
                ret.Add(key, principal);
            }

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

            return(ret);
        }
Beispiel #2
0
        private Dictionary <string, MSPPrincipal> ParseIdentities(JArray identities)
        {
            Dictionary <string, MSPPrincipal> ret = new Dictionary <string, MSPPrincipal>();

            foreach (JToken jsonValue in identities)
            {
                if (jsonValue.Type != JTokenType.Object)
                {
                    throw new ChaincodeCollectionConfigurationException($"Expected in identities user to be Object type but got: {jsonValue.Type.ToString()}");
                }

                JObject user = jsonValue as JObject;
                if (user?.Count != 1)
                {
                    throw new ChaincodeCollectionConfigurationException("Only expected on property for user entry in identities.");
                }

                string key = user.Properties().Select(p => p.Name).First();
                JToken vv  = user[key];
                if (vv.Type != JTokenType.Object)
                {
                    throw new ChaincodeCollectionConfigurationException($"Expected in identities role to be Object type but got: {vv.Type.ToString()}");
                }

                JObject role = vv as JObject;
                if (role == null)
                {
                    throw new ChaincodeCollectionConfigurationException($"Expected a valid role");
                }
                JObject roleObj = role["role"] as JObject;
                if (roleObj == null)
                {
                    throw new ChaincodeCollectionConfigurationException($"Expected a valid role");
                }
                string roleName = roleObj["name"].Value <string>();
                string mspId    = roleObj["mspId"].Value <string>();

                MSPRole.Types.MSPRoleType mspRoleType;

                switch (roleName.ToLowerInvariant())
                {
                case "member":
                    mspRoleType = MSPRole.Types.MSPRoleType.Member;
                    break;

                case "admin":
                    mspRoleType = MSPRole.Types.MSPRoleType.Admin;
                    break;

                case "client":
                    mspRoleType = MSPRole.Types.MSPRoleType.Client;
                    break;

                case "peer":
                    mspRoleType = MSPRole.Types.MSPRoleType.Peer;
                    break;

                default:
                    throw new ChaincodeCollectionConfigurationException($"In identities with key {key} name expected member, admin, client, or peer in role got {roleName}");
                }

                MSPRole mspRole = new MSPRole();
                mspRole.Role          = mspRoleType;
                mspRole.MspIdentifier = mspId;
                MSPPrincipal principal = new MSPPrincipal();
                principal.PrincipalClassification = MSPPrincipal.Types.Classification.Role;
                principal.Principal = mspRole.ToByteString();
                ret.Add(key, principal);
            }

            return(ret);
        }