package leigh.chain.pki;

import leigh.chain.LeighChain;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.WalletUtils;

import java.io.IOException;

/**
 * This class provides convenience functions for interacting with the PKI on the LeighChain.
 *
 * @author Alex Leigh
 */
public class PKI {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(PKI.class);

    /**
     * Submit a name claim to the LeighChain.
     *
     * <code>validityDays</code> must be valid. If it is not, an exception will be thrown.
     */
    public static String submitNameClaim(String gethUrl, String wallet, String walletPw,
                                         String name, String serviceUrl, int validityDays) throws CipherException, IOException {
        // This will throw if the validity period is illegal
        NameClaim claim = new NameClaim(name, serviceUrl, validityDays);

        logger.info("Is name claim addr valid: {}",
                WalletUtils.isValidAddress(PKIAddresses.OP_NAME_CLAIM));

        return LeighChain.submit(gethUrl, wallet, walletPw, PKIAddresses.OP_NAME_CLAIM, claim.toJSON());
    }
}
