/** * Serialize Idemix Identity */ public SerializedIdentity CreateSerializedIdentity() { OrganizationUnit ou = new OrganizationUnit(); ou.CertifiersIdentifier = ByteString.CopyFrom(ipkHash); ou.MspIdentifier = mspId; ou.OrganizationalUnitIdentifier = Ou; //Warning, this does not support multi-roleMask. //Serialize the bitmask is the correct way to support multi-roleMask in the future MSPRole role = new MSPRole(); role.Role = RoleMask.ToMSPRoleTypes().First(); role.MspIdentifier = mspId; SerializedIdemixIdentity serializedIdemixIdentity = new SerializedIdemixIdentity(); serializedIdemixIdentity.Proof = ByteString.CopyFrom(associationProof.ToProto().ToByteArray()); serializedIdemixIdentity.Ou = ByteString.CopyFrom(ou.ToByteArray()); serializedIdemixIdentity.Role = ByteString.CopyFrom(role.ToByteArray()); serializedIdemixIdentity.NymY = ByteString.CopyFrom(pseudonym.Y.ToBytes()); serializedIdemixIdentity.NymX = ByteString.CopyFrom(pseudonym.X.ToBytes()); SerializedIdentity serializedIdentity = new SerializedIdentity(); serializedIdentity.IdBytes = ByteString.CopyFrom(serializedIdemixIdentity.ToByteArray()); serializedIdentity.Mspid = mspId; return(serializedIdentity); }
/** * Create Idemix Identity from a Serialized Identity * * @param proto */ public IdemixIdentity(SerializedIdentity proto) { if (proto == null) { throw new ArgumentException("Input must not be null"); } mspId = proto.Mspid; try { logger.Trace("Fetching Idemix Proto"); SerializedIdemixIdentity idemixProto = SerializedIdemixIdentity.Parser.ParseFrom(proto.IdBytes); if (idemixProto == null) { throw new ArgumentException("The identity does not contain a serialized idemix identity"); } logger.Trace("Deserializing Nym and attribute values"); pseudonym = new ECP(BIG.FromBytes(idemixProto.NymX.ToByteArray()), BIG.FromBytes(idemixProto.NymY.ToByteArray())); OrganizationUnit ou = OrganizationUnit.Parser.ParseFrom(idemixProto.Ou); MSPRole role = MSPRole.Parser.ParseFrom(idemixProto.Role); Ou = ou.OrganizationalUnitIdentifier; RoleMask = role.Role.ToIdemixRole(); ipkHash = ou.CertifiersIdentifier.ToByteArray(); logger.Trace("Deserializing Proof"); associationProof = new IdemixSignature(Signature.Parser.ParseFrom(idemixProto.Proof.ToByteArray())); } catch (InvalidProtocolBufferException e) { throw new CryptoException("Cannot deserialize MSP ID", e); } }
public void TestSerializingAndDeserializingIdentity() { SerializedIdentity proto = signingIdentity.CreateSerializedIdentity(); Assert.IsNotNull(proto); SerializedIdemixIdentity idemixProto = null; try { idemixProto = SerializedIdemixIdentity.Parser.ParseFrom(proto.IdBytes); } catch (InvalidProtocolBufferException e) { Assert.Fail("Could not parse Idemix Serialized Identity" + e.Message); } if (idemixProto != null) { new ECP(BIG.FromBytes(idemixProto.NymX.ToByteArray()), BIG.FromBytes(idemixProto.NymY.ToByteArray())); idemixProto.Ou.ToByteArray(); idemixProto.Role.ToByteArray(); try { new IdemixSignature(Signature.Parser.ParseFrom(idemixProto.Proof.ToByteArray())); } catch (InvalidProtocolBufferException e) { Assert.Fail("Cannot deserialize proof" + e.Message); } } try { new IdemixIdentity(proto); } catch (System.Exception e) when(e is CryptoException || e is ArgumentException) { Assert.Fail("Cannot create Idemix Identity from Proto" + e.Message); } }