package network.thunder.core.communication;

import io.netty.channel.ChannelHandlerContext;
import network.thunder.core.communication.objects.subobjects.AuthenticationObject;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Node {
	private String host;
	private int port;

	private boolean connected = false;

	private ChannelHandlerContext nettyContext;

	private byte[] pubkey;
	private boolean isAuth;
	private boolean sentAuth;
	private boolean authFinished;
	private boolean isReady;
	private boolean hasOpenChannel;

	public Node (String host, int port) {
		this.host = host;
		this.port = port;
	}

	public Node(ResultSet set) throws SQLException {
		this.host = set.getString("host");
		this.port = set.getInt("port");
	}

	public Node () {

	}

	public boolean processAuthentication (AuthenticationObject authentication, byte[] secret) {
		//TODO: Check authentication based on the supplied pubkey

		isAuth = true;
		if (sentAuth) {
			authFinished = true;
		}
		return true;
	}

	public AuthenticationObject getAuthenticationObject (byte[] secret) {
		//TODO: Produce a proper authentication object..

		sentAuth = true;
		return new AuthenticationObject();
	}

	public ChannelHandlerContext getNettyContext () {
		return nettyContext;
	}

	public void setNettyContext (ChannelHandlerContext nettyContext) {
		this.nettyContext = nettyContext;
	}

	public int getPort () {
		return port;
	}

	public void setPort (int port) {
		this.port = port;
	}

	public String getHost () {
		return host;
	}

	public void setHost (String host) {
		this.host = host;
	}

	public boolean isConnected () {
		return connected;
	}

	public void setConnected (boolean connected) {
		this.connected = connected;
	}

	public boolean hasSentAuth () {
		return sentAuth;
	}

	public boolean isAuth () {
		return isAuth;
	}

	public boolean allowsAuth () {
		return !isAuth;
	}

	public void finishAuth () {
		authFinished = true;
	}

	public boolean isAuthFinished () {
		return authFinished;
	}


}

@@ -168,16 +168,193 @@
 Object;%0A
+import network.thunder.core.etc.crypto.CryptoTools;%0Aimport org.bitcoinj.core.ECKey;%0A%0Aimport java.security.NoSuchAlgorithmException;%0Aimport java.security.NoSuchProviderException;
 %0Aimport 
@@ -1027,84 +1027,507 @@
 on, 
-byte%5B%5D secret)
+ECKey pubkeyClient, ECKey pubkeyServerTemp) throws NoSuchProviderException,%0A%09%09%09NoSuchAlgorithmException
  %7B%0A
+%0A
 %09%09
-//TODO: Check authentication based on the supplied pubkey
+byte%5B%5D data = new byte%5BpubkeyClient.getPubKey().length+pubkeyServerTemp.getPubKey().length%5D;%0A%09%09System.arraycopy(pubkeyClient.getPubKey(), 0, data, 0, pubkeyClient.getPubKey().length);%0A%09%09System.arraycopy(pubkeyServerTemp.getPubKey(), 0, data, pubkeyClient.getPubKey().length, pubkeyServerTemp.getPubKey().length);%0A%0A%09%09CryptoTools.verifySignature(pubkeyClient, data, authentication.signature);
 %0A%0A%09%09
@@ -1664,75 +1664,565 @@
 ct (
-byte%5B%5D secret)
+ECKey keyServer, ECKey keyClientTemp) throws NoSuchProviderException, NoSuchAlgorithmException
  %7B%0A
+%0A
 %09%09
-//TODO: Produce a proper authentication object..
+byte%5B%5D data = new byte%5BkeyServer.getPubKey().length+keyClientTemp.getPubKey().length%5D;%0A%09%09System.arraycopy(keyServer.getPubKey(), 0, data, 0, keyServer.getPubKey().length);%0A%09%09System.arraycopy(keyClientTemp.getPubKey(), 0, data, keyServer.getPubKey().length, keyClientTemp.getPubKey().length);%0A%0A%09%09AuthenticationObject obj = new AuthenticationObject();%0A%09%09obj.pubkeyServer = keyServer.getPubKey();%0A%09%09obj.signature = CryptoTools.createSignature(keyServer, data);
 %0A%0A%09%09
@@ -2251,34 +2251,11 @@
 urn 
-new AuthenticationObject()
+obj
 ;%0A%09%7D

Implement authentication methods