public void completeUProveProtocolTest()
    {
      byte[] attributeEncoding = new byte[] { 0x0, 0x1 };
      IssuerSetupParameters isp = this.getIssuerSetupParameters(attributeEncoding, "foobar");

      // generate the serializable IssuerKeyAndParameters
      IssuerKeyAndParameters ikap = null;
      Assert.DoesNotThrow(delegate { ikap = isp.Generate(true); });
      Assert.DoesNotThrow(delegate { ikap.IssuerParameters.Verify(); });

      byte[] issuerPrivateKey = new byte[] { 187, 133, 215, 21, 39, 178, 240, 67, 170, 4, 148, 139, 213, 36, 164, 114, 146, 228, 243, 140, 61, 76,
                                            174, 136, 137, 65, 238, 59, 7, 198, 34, 129 };



      int numberOfTokens = 10;
      string[] attributesString = new string[] { "foo", "bar" };
      int numberOfAttributes = attributesString.Length;
      byte[][] attributes = new byte[numberOfAttributes][];
      for (int i = 0; i < numberOfAttributes; i++)
      {
        attributes[i] = encoding.GetBytes(attributesString[i]);
      }
      BigInteger bi = new BigInteger(1, issuerPrivateKey);
      IssuerKeyAndParameters ikapFristMessage = new IssuerKeyAndParameters(bi, ikap.IssuerParameters);

 

      GroupElement hdG = ikap.IssuerParameters.Gq.CreateGroupElement(defines.hd);
      Issuer issuer = new Issuer(ikapFristMessage, numberOfTokens, attributes, null, hdG);

      FirstIssuanceMessage fi = issuer.GenerateFirstMessage();

      byte[] attributes2nd = (byte[])attributes.Clone();
      byte[] tokenInformation = new byte[] { };
      byte[] proverInformation = new byte[] { };
      Prover prover = new Prover(ip, numberOfTokens, attributes, tokenInformation, proverInformation, sessionDB[sessionID].deviceManager.GetDevice());


        /*
        * issue steps. creds.               
       *  setupIssuerParams.                
       *                                    
       * setIssuerPrivateKey. {byte[32]} : 		

       * 
       *  getFirstMessage - 		[0]	"42595520544300663591556673075677003532579993719172074290116620403700505383419"	string
   

       * 
       * getSecondMessage - with outputfrom getFristMessage.
       * 
       * getThirdMessage - with output from getSecondMessage.
       * 
       * generateTokens - with output from getThirdMessage
       * 
       * proveToken - commitedIndices 0x00000001 , 
       * messageParms "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<abc:Message xmlns:abc=\"http://abc4trust.eu/wp2/abcschemav1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://abc4trust.eu/wp2/abcschemav1.0 ../../../../../../../../../abc4trust-xml/src/main/resources/xsd/schema.xsd\">\n<abc:Nonce>r9MQ57udpiWRwA==</abc:Nonce>\n</abc:Message>\n"
       * Token, 
       * -		tokenPrivateKeyParam	{byte[0x00000020]}	byte[]
    [0x00000000]	0x37	byte
    [0x00000001]	0xaa	byte
    [0x00000002]	0xb6	byte
    [0x00000003]	0xa9	byte
    [0x00000004]	0xd5	byte
    [0x00000005]	0x56	byte
    [0x00000006]	0xd8	byte
    [0x00000007]	0x21	byte
    [0x00000008]	0x77	byte
    [0x00000009]	0xe1	byte
    [0x0000000a]	0x7b	byte
    [0x0000000b]	0x5a	byte
    [0x0000000c]	0xf3	byte
    [0x0000000d]	0xad	byte
    [0x0000000e]	0xf7	byte
    [0x0000000f]	0x83	byte
    [0x00000010]	0x3c	byte
    [0x00000011]	0x2d	byte
    [0x00000012]	0xcf	byte
    [0x00000013]	0xdd	byte
    [0x00000014]	0x8a	byte
    [0x00000015]	0xbf	byte
    [0x00000016]	0x62	byte
    [0x00000017]	0xae	byte
    [0x00000018]	0xeb	byte
    [0x00000019]	0x44	byte
    [0x0000001a]	0x59	byte
    [0x0000001b]	0x6f	byte
    [0x0000001c]	0x1b	byte
    [0x0000001d]	0x09	byte
    [0x0000001e]	0x6d	byte
    [0x0000001f]	0xf0	byte

       * 
       * verifyTokenProof - proff from proveToken, otherwise same values.
       * 
       * 
       * 
       * */


    }
    // issuerPrivateKey must be set using setIssuerPrivateKey() before calling this method
    public FirstIssuanceMessageComposite getFirstMessage(string[] attributesParam, IssuerParametersComposite ipc, int numberOfTokensParam, string sessionID, byte[] hd)
    {
      /*
       *  token issuance - generate first message
       */

      cOut.write("Issuing U-Prove tokens - generate first message, issuer side");

      VerifySessionId(sessionID);
      try
      {

        // specify the attribute values agreed to by the Issuer and Prover
        int numberOfAttributes = attributesParam.Length;
        byte[][] attributes = new byte[numberOfAttributes][];
        for (int i = 0; i < numberOfAttributes; i++)
        {
          attributes[i] = encoding.GetBytes(attributesParam[i]);
        }

        IssuerParameters ip = ConvertUtils.convertIssuerParametersComposite(ipc, sessionDB[sessionID]);
        byte[] issuerPrivateKey = sessionDB[sessionID].privateKey;
        if (issuerPrivateKey == null)
        {
          cOut.write("Issuer side, issuerPrivateKey is null. Did you forget to add the issuer private key for the given sessionKey?");
          return null;
        }
        BigInteger bi = new BigInteger(1, issuerPrivateKey);
        IssuerKeyAndParameters ikap = new IssuerKeyAndParameters(bi, ip);

        // setup the issuer and generate the first issuance message

        GroupElement hdG = ip.Gq.CreateGroupElement(hd);

        Issuer issuer = new Issuer(ikap, numberOfTokensParam, attributes, null, hdG);

        // Store the issuer in issuersDictionary using the sessionKey as key
        sessionDB[sessionID].issuer = issuer;

        FirstIssuanceMessage fi = issuer.GenerateFirstMessage();

        // Convert FirstIssuanceMessage members to serializable FirstIssuanceMessageComposite
        FirstIssuanceMessageComposite fic = ConvertUtils.convertFirstIssuanceMessage(fi);

        // Add the sessionKey to FirstIssuanceMessageComposite
        fic.SessionKey = sessionID;

        return fic;
      }
      catch (Exception e)
      {
        cOut.write(e.ToString());
        DebugUtils.DebugPrint(e.StackTrace.ToString());
      }

      return null;
    }
    public ThirdIssuanceMessageInfo ThirdMessage(ThirdIssuanceMessageSpec spec)
    {
      // first we will try to lookup the cache value of the issuer object.
      Issuer issuer = null;
      if (spec.IssuerInstanceID != null)
      {
        IssuerInstanceData issuerInstance;
        bool ok = issuerInstanceDB.TryGetValue(spec.IssuerInstanceID, out issuerInstance);
        if (ok)
        {
          issuer = issuerInstance.Issuer;
        }
        else
        {
          //XXX add log about Issuer not found in cache.
        }
      }

      if (issuer != null)
      {
        return HandleThirdMessageInfo(issuer, spec.SecondMessage);
      }

      if (spec.FistMessageState != null && spec.IssuerKeyAndParameter != null)
      {
        issuer = new Issuer(spec.IssuerKeyAndParameter, spec.FistMessageState);
        issuerInstanceDB.TryAdd(spec.IssuerInstanceID, new IssuerInstanceData(issuer));
        return HandleThirdMessageInfo(issuer, spec.SecondMessage);

      }

      ApiArgumentFault fault = new ApiArgumentFault();
      fault.Details = "Issuer with unique ID was found or FirstMessageState and IssuerKeyAndParameter not provided.";
      fault.Argument = "ThirdIssuanceMessageSpec.IssuerInstanceID/ThirdIssuanceMessageSpec.FirstMessageState/ThirdIssuanceMessageSpec.IssuerKeyAndParameter";
      fault.ArgumentValue = spec.IssuerInstanceID.ToString();
      throw new FaultException<ApiArgumentFault>(fault);

    }
    private ThirdIssuanceMessageInfo HandleThirdMessageInfo(Issuer issuer, SecondIssuanceMessage secondMessage)
    {
      ThirdIssuanceMessageInfo thirdMessageInfo = new ThirdIssuanceMessageInfo();
      thirdMessageInfo.ThirdMessage = issuer.GenerateThirdMessage(secondMessage);
      return thirdMessageInfo;

    }
        public void TestIssuerStateExport()
        {
            IssuerKeyAndParameters ikap;
            IssuerProtocolParameters ipp;
            ProverProtocolParameters ppp;
            StaticTestHelpers.GenerateTestIssuanceParameters("test issuer params", "test spec", 3, true, 6, out ikap, out ipp, out ppp);

            // generate test issuer parameters
            Issuer issuer = ipp.CreateIssuer();
            IssuerParameters ip = ikap.IssuerParameters;
            Prover prover = ppp.CreateProver();

            FirstIssuanceMessage msg1 = null;
            SecondIssuanceMessage msg2 = null;
            ThirdIssuanceMessage msg3 = null;

            msg1 = issuer.GenerateFirstMessage();
            PostFirstMessageState state = issuer.ExportPostFirstMessageState();
            string serializedState = ikap.IssuerParameters.Serialize(state);
            msg2 = prover.GenerateSecondMessage(msg1);
            
            // make sure the original issuer is unusable
            try
            {
                issuer.GenerateThirdMessage(msg2);
                Assert.Fail();
            }
            catch (Exception)
            {
                // expected
            }

            // complete the issuance with a new issuer instance
            Issuer issuer2 = new Issuer(ikap, ikap.IssuerParameters.Deserialize<PostFirstMessageState>(serializedState));
            msg3 = issuer2.GenerateThirdMessage(msg2);
            // make sure tokens are valid by completing the issuance
            prover.GenerateTokens(msg3);
        }
 internal IssuerInstanceData(Issuer issuer)
 {
   this.LastAccessed = DateTime.Now;
   _issuer = issuer;
 }