package ethereumAddress_test

import "seekia/internal/cryptocurrency/ethereumAddress"

import "seekia/internal/encoding"
import "seekia/internal/identity"

import "testing"

func TestEthereumAddressFunctions(t *testing.T) {

	{
		testIdentityHashString := "wgkxplfvju7yhpoadvxju4kmfdr"

		testIdentityHash, _, err := identity.ReadIdentityHashString(testIdentityHashString)
		if (err != nil){
			t.Fatal("Failed to read testIdentityHashString: " + err.Error())
		}

		identityAddress, err := ethereumAddress.GetIdentityScoreEthereumAddressFromIdentityHash(testIdentityHash)
		if (err != nil){
			t.Fatal("Failed to get ethereum identity score address: " + err.Error())
		}

		if (identityAddress != "0x74cFc329DBEe18c791f15869019b9B3954Db1869"){
			t.Fatal("GetIdentityScoreEthereumAddressFromIdentityHash not producing expected identity address: " + identityAddress)
		}
	}

	{
		testMemoHash := "db61e89bb23f5c9964393eaa0510491c5ea5ac14b26bcc33cdfed1b6a45900b0"
		
		testMemoHashBytes, err := encoding.DecodeHexStringToBytes(testMemoHash)
		if (err != nil){
			t.Fatal("Failed to decode testMemoHash: Not Hex: " + testMemoHash)
		}

		if (len(testMemoHashBytes) != 32){
			t.Fatal("testMemoHashBytes is invalid: Invalid length.")
		}

		testMemoHashArray := [32]byte(testMemoHashBytes)

		memoAddress, err := ethereumAddress.GetMemoEthereumAddressFromMemoHash(testMemoHashArray)
		if (err != nil){
			t.Fatal("Failed to get ethereum account address: " + err.Error())
		}

		if (memoAddress != "0x666b01036557dF68866E7Ab852897A4D6499ea07"){
			t.Fatal("Ethereum memo address does not match expected value: " + memoAddress)
		}
	}

	{
		testTreeRoot := "382f53db5084214c9c6b59ea431205ca7d31e890654c3220edd83edeebc32090"
		
		testTreeRootBytes, err := encoding.DecodeHexStringToBytes(testTreeRoot)
		if (err != nil){
			t.Fatal("Failed to decode testTreeRoot: Not Hex: " + testTreeRoot)
		}

		if (len(testTreeRootBytes) != 32){
			t.Fatal("testTreeRootBytes is invalid: Invalid length.")
		}

		testTreeRootArray := [32]byte(testTreeRootBytes)

		treeRootAddress, err := ethereumAddress.GetPaymentProofTreeRootEthereumAddress(testTreeRootArray)
		if (err != nil){
			t.Fatal("Failed to get ethereum account address: " + err.Error())
		}

		if (treeRootAddress != "0x755c94FD1C45319bD5a9e7b77901b95631EDB6D3"){
			t.Fatal("Ethereum tree root address does not match expected value: " + treeRootAddress)
		}
	}

	for i:=0; i < 1000; i++{

		newAddress, err := ethereumAddress.GetNewRandomEthereumAddress()
		if (err != nil){
			t.Fatal("Failed to get random Ethereum address: " + err.Error())
		}

		isValid := ethereumAddress.VerifyEthereumAddress(newAddress)
		if (isValid == false){
			t.Fatal("Randomly generated Ethereum address is invalid.")
		}
	}
}



