package org.irmacard.api.common;

import org.irmacard.api.common.util.GsonUtil;
import org.irmacard.credentials.idemix.IdemixSystemParameters;
import org.irmacard.credentials.idemix.util.Crypto;
import org.irmacard.credentials.info.CredentialIdentifier;
import org.irmacard.credentials.info.IssuerIdentifier;

import java.io.Serializable;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.HashSet;

@SuppressWarnings("unused")
public abstract class SessionRequest implements Serializable {
	private static final long serialVersionUID = -1765381227944439300L;

	protected BigInteger nonce;
	protected BigInteger context;

	public SessionRequest(BigInteger nonce, BigInteger context) {
		this.nonce = nonce;
		this.context = context;
	}

	public abstract HashSet<CredentialIdentifier> getCredentialList();

	public abstract HashMap<IssuerIdentifier, Integer> getPublicKeyList();

	public abstract IdemixSystemParameters getLargestParameters();

	public abstract boolean isEmpty();

	public HashSet<IssuerIdentifier> getIssuerList() {
		HashSet<IssuerIdentifier> issuers = new HashSet<>();

		for (CredentialIdentifier credential : getCredentialList())
			issuers.add(credential.getIssuerIdentifier());

		return issuers;
	}

	public BigInteger getNonce() {
		return nonce;
	}

	public void setNonce(BigInteger nonce) {
		this.nonce = nonce;
	}

	public BigInteger getContext() {
		return context;
	}

	public void setContext(BigInteger context) {
		this.context = context;
	}

	public static BigInteger generateNonce(IdemixSystemParameters params) {
		return new BigInteger(params.get_l_statzk(), new SecureRandom());
	}

	public BigInteger generateContext() {
		return Crypto.sha256Hash(toString(false).getBytes());
	}

	public void setNonceAndContext() {
		this.nonce = generateNonce(getLargestParameters());
		this.context = generateContext();
	}

	@Override
	public String toString() {
		return toString(true);
	}

	public String toString(boolean includeContext) {
		BigInteger context = this.context;
		BigInteger nonce = this.nonce;
		if (!includeContext) {
			this.context = null;
			this.nonce = null;
		}

		String val = GsonUtil.getGson().toJson(this);

		this.context = context;
		this.nonce = nonce;

		return val;
	}
}

@@ -1269,24 +1269,335 @@
 ssuers;%0A%09%7D%0A%0A
+%09/**%0A%09 * Get the scheme manager of the first credential in this session request.%0A%09 * NOTE: we implicitly assume that all credentials are from the same manager, but we%0A%09 * do not check that here.%0A%09 */%0A%09public String getSchemeManager() %7B%0A%09%09return getCredentialList().iterator().next().getSchemeManagerName();%0A%09%7D%0A%0A
 %09public BigI

Add method for getting the scheme manager of a sessionrequest