Exemple #1
0
function check_crypt_balance_range($public_key, $block_start = 0, $block_end = 0)
{
    set_decrypt_mode();
    // Figure out which decrypt method can be best used
    //Initialize objects for Internal RSA decrypt
    if ($GLOBALS['decrypt_mode'] == 2) {
        require_once 'RSA.php';
        $rsa = new Crypt_RSA();
        $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
    }
    if ($block_start == 0 && $block_end == 0) {
        // Find every Time Koin sent to this public Key
        $sql = "SELECT public_key_from, public_key_to, crypt_data3, attribute FROM `transaction_history` WHERE `public_key_from` = '{$public_key}' OR `public_key_to` = '{$public_key}' ";
    } else {
        // Find every TimeKoin sent to and from this public Key in a certain time range.
        // Covert block to time.
        $start_time_range = TRANSACTION_EPOCH + $block_start * 300;
        $end_time_range = TRANSACTION_EPOCH + $block_end * 300;
        $sql = "SELECT public_key_from, public_key_to, crypt_data3, attribute FROM `transaction_history` WHERE (`public_key_from` = '{$public_key}' AND `timestamp` >= '{$start_time_range}' AND `timestamp` < '{$end_time_range}')\n\t\tOR (`public_key_to` = '{$public_key}' AND `timestamp` >= '{$start_time_range}' AND `timestamp` < '{$end_time_range}')";
    }
    $sql_result = mysql_query($sql);
    $sql_num_results = mysql_num_rows($sql_result);
    $crypto_balance = 0;
    $transaction_info;
    for ($i = 0; $i < $sql_num_results; $i++) {
        $sql_row = mysql_fetch_row($sql_result);
        $public_key_from = $sql_row[0];
        $public_key_to = $sql_row[1];
        $crypt3 = $sql_row[2];
        $attribute = $sql_row[3];
        if ($attribute == "G" && $public_key_from == $public_key_to) {
            // Currency Generation
            // Decrypt transaction information
            if ($GLOBALS['decrypt_mode'] == 2) {
                $rsa->loadKey($public_key_from);
                $transaction_info = $rsa->decrypt(base64_decode($crypt3));
            } else {
                $transaction_info = tk_decrypt($public_key_from, base64_decode($crypt3), TRUE);
            }
            $transaction_amount_sent = find_string("AMOUNT=", "---TIME", $transaction_info);
            $crypto_balance += $transaction_amount_sent;
        }
        if ($attribute == "T" && $public_key_to == $public_key) {
            // Decrypt transaction information
            if ($GLOBALS['decrypt_mode'] == 2) {
                $rsa->loadKey($public_key_from);
                $transaction_info = $rsa->decrypt(base64_decode($crypt3));
            } else {
                $transaction_info = tk_decrypt($public_key_from, base64_decode($crypt3), TRUE);
            }
            $transaction_amount_sent = find_string("AMOUNT=", "---TIME", $transaction_info);
            $crypto_balance += $transaction_amount_sent;
        }
        if ($attribute == "T" && $public_key_from == $public_key) {
            // Decrypt transaction information
            $transaction_info = tk_decrypt($public_key_from, base64_decode($crypt3));
            if ($GLOBALS['decrypt_mode'] == 2) {
                $rsa->loadKey($public_key_from);
                $transaction_info = $rsa->decrypt(base64_decode($crypt3));
            } else {
                $transaction_info = tk_decrypt($public_key_from, base64_decode($crypt3), TRUE);
            }
            $transaction_amount_sent = find_string("AMOUNT=", "---TIME", $transaction_info);
            $crypto_balance -= $transaction_amount_sent;
        }
    }
    //
    // Unset variable to free up RAM
    unset($sql_result);
    return $crypto_balance;
}
Exemple #2
0
 $transaction_hash = find_string("-----hash{$tc}=", "-----attribute{$tc}", $poll_peer);
 $transaction_attribute = find_string("-----attribute{$tc}=", "-----end{$tc}", $poll_peer);
 if (empty($transaction_public_key_from) == TRUE && empty($transaction_public_key_to) == TRUE) {
     // No more data, break while loop
     break;
 }
 $transaction_public_key_from = filter_sql(base64_decode($transaction_public_key_from));
 $transaction_public_key_to = filter_sql(base64_decode($transaction_public_key_to));
 // Check for valid attribute
 if ($transaction_attribute == "G" || $transaction_attribute == "T" || $transaction_attribute == "H") {
     if ($transaction_attribute == "G" || $transaction_attribute == "T") {
         // Check that verification hash for transaction data matches
         $crypt_hash_check = hash('sha256', $transaction_crypt1 . $transaction_crypt2 . $transaction_crypt3);
         // Find destination public key
         $public_key_to_1 = tk_decrypt($transaction_public_key_from, base64_decode($transaction_crypt1));
         $public_key_to_2 = tk_decrypt($transaction_public_key_from, base64_decode($transaction_crypt2));
         $internal_public_key_to = $public_key_to_1 . $public_key_to_2;
         if ($transaction_hash == $crypt_hash_check && strlen($transaction_public_key_from) > 300 && strlen($transaction_public_key_to) > 300 && $internal_public_key_to == $transaction_public_key_to) {
             // Continue with duplicate record test
             $found_duplicate = mysql_result(mysql_query("SELECT timestamp FROM `transaction_history` WHERE `timestamp` = '{$transaction_timestamp}' AND `hash` = '{$transaction_hash}' LIMIT 1"), 0, 0);
         } else {
             // Use duplicate test to fail this transaction data
             $found_duplicate = "INVALID";
         }
     } else {
         // Transaction Cycle Hash, continue duplicate record test
         $found_duplicate = mysql_result(mysql_query("SELECT timestamp FROM `transaction_history` WHERE `timestamp` = '{$transaction_timestamp}' AND `hash` = '{$transaction_hash}' LIMIT 1"), 0, 0);
     }
     if (empty($found_duplicate) == TRUE) {
         $norm_record_insert_counter++;
         // How many records are spooling up
Exemple #3
0
 $transaction_amount_sent_test = intval($transaction_amount_sent);
 if ($transaction_amount_sent_test == $transaction_amount_sent) {
     // Is a valid integer
     $amount_valid = TRUE;
 } else {
     // Is NOT a valid integer
     $amount_valid = FALSE;
 }
 // Validate transaction against known public key balance
 if (check_crypt_balance($public_key) >= $transaction_amount_sent && $transaction_amount_sent > 0 && $amount_valid == TRUE) {
     // Balance checks out
     // Check hash value for tampering of crypt1, crypt2, or crypt3 fields
     if (hash('sha256', $crypt1 . $crypt2 . $crypt3) == $hash_check) {
         // Find destination public key
         $public_key_to_1 = tk_decrypt($public_key, base64_decode($crypt1));
         $public_key_to_2 = tk_decrypt($public_key, base64_decode($crypt2));
         $public_key_to = $public_key_to_1 . $public_key_to_2;
         if (strlen($public_key) > 300 && strlen($public_key_to) > 300 && $public_key !== $public_key_to) {
             // Public key not found, insert into final transaction history
             $sql = "INSERT INTO `transaction_history` (`timestamp` ,`public_key_from` , `public_key_to` , `crypt_data1` ,`crypt_data2` ,`crypt_data3` ,`hash` ,`attribute`)\n\t\t\t\t\t\t\t\tVALUES ({$time_created}, '{$public_key}', '{$public_key_to}' , '{$crypt1}', '{$crypt2}', '{$crypt3}', '{$hash_check}', 'T');";
             if (mysql_query($sql) == FALSE) {
                 //Something didn't work
                 write_log("Transaction Database Insert Failed for this Key:" . base64_encode($public_key), "T");
             }
         } else {
             // Invalid or blank Public Key(s)
             write_log("Transaction Public Key Error for this Key:" . base64_encode($public_key), "T");
             $safe_delete_transaction = TRUE;
         }
     } else {
         // Hash check failed
Exemple #4
0
        $sql_result = mysql_query($sql);
        $sql_num_results = mysql_num_rows($sql_result);
        $body_string = '<strong><font color="blue">( ' . number_format($sql_num_results) . ' )</font> Network Transactions Waiting for Processing</strong><br><br><div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" ><tr><th>Date</th>
			<th>Sent From</th><th>Sent To</th><th>Amount</th></tr>';
        for ($i = 0; $i < $sql_num_results; $i++) {
            $sql_row = mysql_fetch_array($sql_result);
            $crypt1 = $sql_row["crypt_data1"];
            $crypt2 = $sql_row["crypt_data2"];
            $crypt3 = $sql_row["crypt_data3"];
            $public_key_trans = $sql_row["public_key"];
            // Decode the public key this transaction is being sent to
            $public_key_to_1 = tk_decrypt($public_key_trans, base64_decode($crypt1));
            $public_key_to_2 = tk_decrypt($public_key_trans, base64_decode($crypt2));
            $public_key_trans_to = $public_key_to_1 . $public_key_to_2;
            // Decode Amount
            $transaction_info = tk_decrypt($public_key_trans, base64_decode($crypt3));
            $transaction_amount = find_string("AMOUNT=", "---TIME", $transaction_info);
            if ($public_key_trans == $my_public_key) {
                if ($public_key_trans_to == $my_public_key) {
                    // Currency Generation
                    $public_key_from = '<td class="style2"><font color="blue">Currency Generation</font>';
                    $public_key_to = '<td class="style2"><font color="green">Self</font>';
                } else {
                    // Self Generated to someone else
                    $public_key_from = '<td class="style2"><font color="blue">Self Generated Transaction</font>';
                    $public_key_to = '<td class="style1"><p style="word-wrap:break-word; width:215px; font-size:' . $default_public_key_font . 'px;">' . base64_encode($public_key_trans_to) . '</p>';
                }
            } else {
                // Everyone else
                if ($sql_row["attribute"] == "G") {
                    $public_key_to = '<td class="style2"><font color="green">Currency Generation</font>';
Exemple #5
0
 if ($transaction_info == $crypt2) {
     // Check the IP/Domain field and poll the IP to see if
     // there is a valid Timekoin server at the address.
     $crypt3_data = tk_decrypt($public_key, base64_decode($crypt3));
     $peer_ip = find_string("---ip=", "---domain", $crypt3_data);
     $peer_domain = find_string("---domain=", "---subfolder", $crypt3_data);
     $peer_subfolder = find_string("---subfolder=", "---port", $crypt3_data);
     $peer_port_number = find_string("---port=", "---end", $crypt3_data);
     $delete_request = find_string("---end=", "---end2", $crypt3_data);
     // Check if IP is already in the generation peer list
     $IP_exist1 = mysql_result(mysql_query("SELECT * FROM `generating_peer_list` WHERE `IP_Address` = '{$peer_ip}' LIMIT 1"), 0, 1);
     // Calculate public key half-crypt-hash
     $arr1 = str_split($public_key, 181);
     // Poll the address that was encrypted to check for valid Timekoin server
     $gen_key_crypt = base64_decode(poll_peer($peer_ip, $peer_domain, $peer_subfolder, $peer_port_number, 256, "genpeer.php?action=gen_key_crypt"));
     $gen_key_crypt = tk_decrypt($public_key, $gen_key_crypt);
     $domain_fail = FALSE;
     // Reset Variable
     if (empty($peer_domain) == FALSE) {
         // Check if the hostname and IP fields actually match
         // and not made up or unrelated.
         $dns_ip = gethostbyname($peer_domain);
         if ($dns_ip != $peer_ip) {
             // No match between Domain IP and Encoded IP
             $domain_fail = TRUE;
         } else {
             $domain_fail = FALSE;
         }
     }
     // Does the public key half match what is encrypted in the 3rd crypt field from
     // the same peer?
Exemple #6
0
        $sql_num_results = mysql_num_rows($sql_result);
        $crypto_balance = 0;
        $transaction_info;
        for ($i = 0; $i < $sql_num_results; $i++) {
            $sql_row = mysql_fetch_row($sql_result);
            $public_key_from = $sql_row[0];
            $public_key_to = $sql_row[1];
            $crypt3 = $sql_row[2];
            $attribute = $sql_row[3];
            if ($attribute == "G" && $public_key_from == $public_key_to) {
                // Decrypt transaction information
                if ($GLOBALS['decrypt_mode'] == 2) {
                    $rsa->loadKey($public_key_from);
                    $transaction_info = $rsa->decrypt(base64_decode($crypt3));
                } else {
                    $transaction_info = tk_decrypt($public_key_from, base64_decode($crypt3), TRUE);
                }
                $transaction_amount_sent = find_string("AMOUNT=", "---TIME", $transaction_info);
                $crypto_balance += $transaction_amount_sent;
            }
        }
        echo $crypto_balance;
    }
    // End Permission Check
    // Log inbound IP activity
    log_ip("AP", scale_trigger(100));
    exit;
}
//***********************************************************************************
//***********************************************************************************
// Log IP even when not using any functions, just in case
Exemple #7
0
     write_log("Queue Hash Data MisMatch for Public Key: " . $transaction_public_key, "QC");
     $transaction_attribute = "mismatch";
     $mismatch_error_count++;
 }
 $transaction_public_key = filter_sql(base64_decode($transaction_public_key));
 if ($transaction_attribute == "R") {
     // Check to make sure this public key isn't forged or made up to win the list
     $inside_transaction_hash = tk_decrypt($transaction_public_key, base64_decode($transaction_crypt1));
     $final_hash_compare = $transaction_crypt2;
     $crypt_hash_check = $transaction_hash;
 } else {
     if ($transaction_attribute == "T" || $transaction_attribute == "G") {
         // Decrypt transaction information for regular transaction data
         // and check to make sure the public key that is being sent to
         // has not been tampered with.
         $transaction_info = tk_decrypt($transaction_public_key, base64_decode($transaction_crypt3));
         $inside_transaction_hash = find_string("HASH=", "", $transaction_info, TRUE);
         // Check if a message is encoded in this data as well
         if (strlen($inside_transaction_hash) != 64) {
             // A message is also encoded
             $inside_transaction_hash = find_string("HASH=", "---MSG", $transaction_info);
         }
         // Check Hash against 3 crypt fields
         $crypt_hash_check = hash('sha256', $transaction_crypt1 . $transaction_crypt2 . $transaction_crypt3);
     }
     $final_hash_compare = hash('sha256', $transaction_crypt1 . $transaction_crypt2);
 }
 // Check to make sure this transaction is even valid (hash check, length check, & timestamp)
 if ($transaction_hash == $crypt_hash_check && $inside_transaction_hash == $final_hash_compare && strlen($transaction_public_key) > 300 && $transaction_timestamp >= $current_generation_cycle && $transaction_timestamp < $next_generation_cycle) {
     // Check for 100 public key limit in the transaction queue
     $sql = "SELECT * FROM `transaction_queue` WHERE `public_key` = '{$transaction_public_key}'";
        $key_create_micro_time = microtime(TRUE);
        require_once '../RSA.php';
        $rsa = new Crypt_RSA();
        extract($rsa->createKey($bits_level));
        $key_create_micro_time_done = microtime(TRUE);
        if (empty($privatekey) == FALSE && empty($publickey) == FALSE) {
            $symbols = array("\r");
            $new_publickey = str_replace($symbols, "", $publickey);
            $new_privatekey = str_replace($symbols, "", $privatekey);
            $encrypt_create_micro_time = microtime(TRUE);
            // Encrypt New Data
            $encrypt_data_new = tk_encrypt($new_privatekey, $encrypt_data);
            $encrypt_create_micro_time_done = microtime(TRUE);
            $decrypt_create_micro_time = microtime(TRUE);
            // Now Decrypt the same Data
            $decrypted_data = tk_decrypt($new_publickey, $encrypt_data_new);
            $decrypt_create_micro_time_done = microtime(TRUE);
            if (empty($decrypted_data) == TRUE) {
                $decrypted_data = '***DATA STRING TOO LONG FOR BITS ENTERED***';
            }
            $micro_time_variance = "Key Pair Creation [<strong>" . round(($key_create_micro_time_done - $key_create_micro_time) * 1000) . "</strong>] ms<br>\n\t\t\t\tData Encryption [<strong>" . round(($encrypt_create_micro_time_done - $encrypt_create_micro_time) * 1000) . "</strong>] ms<br>\n\t\t\t\tData Decryption [<strong>" . round(($decrypt_create_micro_time_done - $decrypt_create_micro_time) * 1000) . "</strong>] ms<br>\n\t\t\t\tTotal Time [<strong>" . round((microtime(TRUE) - $key_create_micro_time) * 1000) . "</strong>] ms";
            $text_bar .= " {$micro_time_variance} for <strong>{$bits_level}</strong> bit RSA Encryption";
        } else {
            // Key Pair Creation Error
            $text_bar = 'Key Creation Failed';
        }
    }
    // Main Body Text
    $body_string = '<FORM ACTION="cryptobenchmark.php?action=crypt" METHOD="post">
	Choose bits: <input type="text" size="20" name="crypt_bits" value="' . $bits_level . '" /><br><br>
	Data to Encrypt: <textarea name="encrypt_me" rows="6" cols="75">' . $encrypt_data . '</textarea><hr>
Exemple #9
0
function transaction_queue()
{
    $next_transaction_cycle = transaction_cycle(1);
    $current_transaction_cycle = transaction_cycle(0);
    $results;
    // Wipe transaction queue of all old transaction from current to previous cycle
    if (rand(1, 2) == 2) {
        mysql_query("DELETE QUICK FROM `transaction_queue` WHERE `transaction_queue`.`timestamp` < {$current_transaction_cycle}");
    }
    // Create a hash of my own transaction queue
    $transaction_queue_hash = queue_hash();
    // How does my transaction queue compare to others?
    // Ask all of my active peers
    ini_set('user_agent', 'Timekoin Client (Queueclerk) v' . TIMEKOIN_VERSION);
    ini_set('default_socket_timeout', 2);
    // Timeout for request in seconds
    $transaction_queue_hash_match = 0;
    $transaction_queue_hash_different = 0;
    $hash_different = array();
    $sql = "SELECT * FROM `active_peer_list` ORDER BY RAND()";
    $sql_result = mysql_query($sql);
    $sql_num_results = mysql_num_rows($sql_result);
    $transaction_queue_hash_match = 0;
    $transaction_queue_hash_different = 0;
    if ($sql_num_results > 0) {
        $hash_different = array();
        for ($i = 0; $i < $sql_num_results; $i++) {
            $sql_row = mysql_fetch_array($sql_result);
            $ip_address = $sql_row["IP_Address"];
            $domain = $sql_row["domain"];
            $subfolder = $sql_row["subfolder"];
            $port_number = $sql_row["port_number"];
            $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 40, "queueclerk.php?action=trans_hash&client=api");
            if ($transaction_queue_hash === $poll_peer) {
                $transaction_queue_hash_match++;
            } else {
                if (empty($poll_peer) == FALSE) {
                    $transaction_queue_hash_different++;
                    $hash_different["ip_address{$transaction_queue_hash_different}"] = $ip_address;
                    $hash_different["domain{$transaction_queue_hash_different}"] = $domain;
                    $hash_different["subfolder{$transaction_queue_hash_different}"] = $subfolder;
                    $hash_different["port_number{$transaction_queue_hash_different}"] = $port_number;
                }
            }
        }
        // End for Loop
    }
    // End number of results check
    // Compare tallies
    if ($transaction_queue_hash_different > 0) {
        // Transaction Queue still not in sync with all peers
        $hash_array = array();
        $transaction_counter = 0;
        for ($i = 1; $i < $transaction_queue_hash_different + 1; $i++) {
            $ip_address = $hash_different["ip_address{$i}"];
            $domain = $hash_different["domain{$i}"];
            $subfolder = $hash_different["subfolder{$i}"];
            $port_number = $hash_different["port_number{$i}"];
            $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 8200, "queueclerk.php?action=queue&client=api");
            // Bring up first match (if any) to compare agaist our database
            $match_number = 1;
            $current_hash = find_string("---queue{$match_number}=", "---end{$match_number}", $poll_peer);
            $transaction_counter = 0;
            $peer_transaction_limit = 100;
            $mismatch_error_count = 0;
            $mismatch_error_limit = 10;
            while (empty($current_hash) == FALSE) {
                // Count transactions coming from this peer
                $transaction_counter++;
                if ($transaction_counter > $peer_transaction_limit) {
                    break;
                }
                if ($mismatch_error_count > $mismatch_error_limit) {
                    break;
                }
                //Check if this transaction is already in our queue
                $hash_match = mysql_result(mysql_query("SELECT hash FROM `transaction_queue` WHERE `hash` = '{$current_hash}' LIMIT 1"), 0, 0);
                if (empty($hash_match) == TRUE) {
                    // This peer has a different transaction, ask for the full details of it
                    $poll_hash = poll_peer($ip_address, $domain, $subfolder, $port_number, 1500, "queueclerk.php?action=transaction&number={$current_hash}&client=api");
                    $transaction_timestamp = filter_sql(find_string("-----timestamp=", "-----public_key", $poll_hash));
                    $transaction_public_key = find_string("-----public_key=", "-----crypt1", $poll_hash);
                    $transaction_crypt1 = filter_sql(find_string("-----crypt1=", "-----crypt2", $poll_hash));
                    $transaction_crypt2 = filter_sql(find_string("-----crypt2=", "-----crypt3", $poll_hash));
                    $transaction_crypt3 = filter_sql(find_string("-----crypt3=", "-----hash", $poll_hash));
                    $transaction_hash = filter_sql(find_string("-----hash=", "-----attribute", $poll_hash));
                    $transaction_attribute = find_string("-----attribute=", "-----end", $poll_hash);
                    $transaction_qhash = find_string("---qhash=", "---endqhash", $poll_hash);
                    // If a qhash is included, use this to verify the data
                    if (empty($transaction_qhash) == FALSE) {
                        $qhash = $transaction_timestamp . $transaction_public_key . $transaction_crypt1 . $transaction_crypt2 . $transaction_crypt3 . $transaction_hash . $transaction_attribute;
                        $qhash = hash('md5', $qhash);
                        // Compare hashes to make sure data is intact
                        if ($transaction_qhash != $qhash) {
                            $transaction_attribute = "mismatch";
                            $mismatch_error_count++;
                        }
                    } else {
                        // Qhash is required to match hash now
                        $transaction_attribute = "mismatch";
                        $mismatch_error_count++;
                    }
                    $transaction_public_key = filter_sql(base64_decode($transaction_public_key));
                    if ($transaction_attribute == "T" || $transaction_attribute == "G") {
                        // Decrypt transaction information for regular transaction data
                        // and check to make sure the public key that is being sent to
                        // has not been tampered with.
                        $transaction_info = tk_decrypt($transaction_public_key, base64_decode($transaction_crypt3));
                        $inside_transaction_hash = find_string("HASH=", "", $transaction_info, TRUE);
                        // Check if a message is encoded in this data as well
                        if (strlen($inside_transaction_hash) != 64) {
                            // A message is also encoded
                            $inside_transaction_hash = find_string("HASH=", "---MSG", $transaction_info);
                        }
                        // Check Hash against 3 crypt fields
                        $crypt_hash_check = hash('sha256', $transaction_crypt1 . $transaction_crypt2 . $transaction_crypt3);
                    }
                    $final_hash_compare = hash('sha256', $transaction_crypt1 . $transaction_crypt2);
                    // Check to make sure this transaction is even valid (hash check, length check, & timestamp)
                    if ($transaction_hash == $crypt_hash_check && $inside_transaction_hash == $final_hash_compare && strlen($transaction_public_key) > 300 && $transaction_timestamp >= $current_transaction_cycle && $transaction_timestamp < $next_transaction_cycle) {
                        // Check for 100 public key limit in the transaction queue
                        $sql = "SELECT timestamp FROM `transaction_queue` WHERE `public_key` = '{$transaction_public_key}'";
                        $sql_result = mysql_query($sql);
                        $sql_num_results = mysql_num_rows($sql_result);
                        if ($sql_num_results < 100) {
                            // Transaction hash and real hash match
                            $sql = "INSERT INTO `transaction_queue` (`timestamp`,`public_key`,`crypt_data1`,`crypt_data2`,`crypt_data3`, `hash`, `attribute`)\n\t\t\t\t\t\t\tVALUES ('{$transaction_timestamp}', '{$transaction_public_key}', '{$transaction_crypt1}', '{$transaction_crypt2}' , '{$transaction_crypt3}', '{$transaction_hash}' , '{$transaction_attribute}')";
                            mysql_query($sql);
                        }
                    }
                }
                // End Empty Hash Check
                $match_number++;
                $current_hash = find_string("---queue{$match_number}=", "---end{$match_number}", $poll_peer);
            }
            // End While Loop
        }
        // End For Loop
    }
    // End Compare Tallies
    return;
}
Exemple #10
0
function transaction_queue()
{
    $next_transaction_cycle = transaction_cycle(1);
    $current_transaction_cycle = transaction_cycle(0);
    $results;
    // Wipe transaction queue of all old transaction from current to previous cycle
    if (rand(1, 2) == 2) {
        mysql_query("DELETE QUICK FROM `transaction_queue` WHERE `transaction_queue`.`timestamp` < {$current_transaction_cycle}");
    }
    // Create a hash of my own transaction queue
    $transaction_queue_hash = queue_hash();
    // How does my transaction queue compare to others?
    // Ask all of my active peers
    ini_set('user_agent', 'Timekoin Client (Queueclerk) v' . TIMEKOIN_VERSION);
    ini_set('default_socket_timeout', 2);
    // Timeout for request in seconds
    $transaction_queue_hash_match = 0;
    $transaction_queue_hash_different = 0;
    $hash_different = array();
    $sql = "SELECT * FROM `active_peer_list` ORDER BY RAND()";
    $sql_result = mysql_query($sql);
    $sql_num_results = mysql_num_rows($sql_result);
    $transaction_queue_hash_match = 0;
    $transaction_queue_hash_different = 0;
    if ($sql_num_results > 0) {
        $hash_different = array();
        for ($i = 0; $i < $sql_num_results; $i++) {
            $sql_row = mysql_fetch_array($sql_result);
            $ip_address = $sql_row["IP_Address"];
            $domain = $sql_row["domain"];
            $subfolder = $sql_row["subfolder"];
            $port_number = $sql_row["port_number"];
            $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 32, "queueclerk.php?action=trans_hash&client=api");
            if ($transaction_queue_hash === $poll_peer) {
                $transaction_queue_hash_match++;
            } else {
                if (empty($poll_peer) == FALSE) {
                    $transaction_queue_hash_different++;
                    $hash_different["ip_address{$transaction_queue_hash_different}"] = $ip_address;
                    $hash_different["domain{$transaction_queue_hash_different}"] = $domain;
                    $hash_different["subfolder{$transaction_queue_hash_different}"] = $subfolder;
                    $hash_different["port_number{$transaction_queue_hash_different}"] = $port_number;
                }
            }
        }
        // End for Loop
    }
    // End number of results check
    // Compare tallies
    if ($transaction_queue_hash_different > 0) {
        // Transaction Queue still not in sync with all peers
        $hash_array = array();
        $transaction_counter = 0;
        for ($i = 1; $i < $transaction_queue_hash_different + 1; $i++) {
            $ip_address = $hash_different["ip_address{$i}"];
            $domain = $hash_different["domain{$i}"];
            $subfolder = $hash_different["subfolder{$i}"];
            $port_number = $hash_different["port_number{$i}"];
            $poll_peer = filter_sql(poll_peer($ip_address, $domain, $subfolder, $port_number, 83000, "queueclerk.php?action=queue&client=api"));
            // Bring up first match (if any) to compare agaist our database
            $match_number = 1;
            $current_hash = find_string("---queue{$match_number}=", "---end{$match_number}", $poll_peer);
            $transaction_counter = 0;
            $peer_transaction_limit = 1000;
            $mismatch_error_count = 0;
            $mismatch_error_limit = 10;
            while (empty($current_hash) == FALSE) {
                // Count transactions coming from this peer
                $transaction_counter++;
                if ($transaction_counter > $peer_transaction_limit) {
                    break;
                }
                if ($mismatch_error_count > $mismatch_error_limit) {
                    break;
                }
                if (strlen($current_hash) >= 64) {
                    // Old Queue System Check
                    //Check if this transaction is already in our queue
                    $hash_match = mysql_result(mysql_query("SELECT timestamp FROM `transaction_queue` WHERE `hash` = '{$current_hash}' LIMIT 1"), 0, 0);
                } else {
                    // New Queue System Check
                    $sql2 = "SELECT * FROM `transaction_queue`";
                    $sql_result2 = mysql_query($sql2);
                    $sql_num_results2 = mysql_num_rows($sql_result2);
                    $queue_hash_test = NULL;
                    $hash_match = NULL;
                    if ($sql_num_results2 > 0) {
                        for ($i2 = 0; $i2 < $sql_num_results2; $i2++) {
                            $sql_row2 = mysql_fetch_array($sql_result2);
                            $queue_hash_test .= $sql_row2["timestamp"] . $sql_row2["public_key"] . $sql_row2["crypt_data1"] . $sql_row2["crypt_data2"] . $sql_row2["crypt_data3"] . $sql_row2["hash"] . $sql_row2["attribute"];
                            if (hash('md5', $queue_hash_test) == $current_hash) {
                                // This Transaction Already Exist in the Queue
                                $hash_match = TRUE;
                                break;
                            } else {
                                // No match, continue searching
                                $hash_match = NULL;
                            }
                            // No match, move on to next record
                            $queue_hash_test = NULL;
                        }
                    }
                }
                if (empty($hash_match) == TRUE) {
                    // This peer has a different transaction, ask for the full details of it
                    $poll_hash = poll_peer($ip_address, $domain, $subfolder, $port_number, 1500, "queueclerk.php?action=transaction&number={$current_hash}&client=api");
                    $transaction_timestamp = find_string("-----timestamp=", "-----public_key", $poll_hash);
                    $transaction_public_key = find_string("-----public_key=", "-----crypt1", $poll_hash);
                    $transaction_crypt1 = find_string("-----crypt1=", "-----crypt2", $poll_hash);
                    $transaction_crypt2 = find_string("-----crypt2=", "-----crypt3", $poll_hash);
                    $transaction_crypt3 = find_string("-----crypt3=", "-----hash", $poll_hash);
                    $transaction_hash = find_string("-----hash=", "-----attribute", $poll_hash);
                    $transaction_attribute = find_string("-----attribute=", "-----end", $poll_hash);
                    $transaction_qhash = find_string("---qhash=", "---endqhash", $poll_hash);
                    // If a qhash is included, use this to verify the data
                    if (empty($transaction_qhash) == FALSE) {
                        $qhash = $transaction_timestamp . $transaction_public_key . $transaction_crypt1 . $transaction_crypt2 . $transaction_crypt3 . $transaction_hash . $transaction_attribute;
                        $qhash = hash('md5', $qhash);
                        // Compare hashes to make sure data is intact
                        if ($transaction_qhash != $qhash) {
                            $transaction_attribute = "mismatch";
                            $mismatch_error_count++;
                        } else {
                            // Make sure hash is actually valid and not made up to stop other transactions
                            $crypt_hash_check = hash('sha256', $transaction_crypt1 . $transaction_crypt2 . $transaction_crypt3);
                            if ($crypt_hash_check != $transaction_hash) {
                                // Ok, something is wrong here...
                                $transaction_attribute = "mismatch";
                                $mismatch_error_count++;
                            }
                        }
                    } else {
                        // Qhash is required to match hash
                        $transaction_attribute = "mismatch";
                        $mismatch_error_count++;
                    }
                    $transaction_public_key = filter_sql(base64_decode($transaction_public_key));
                    if ($transaction_attribute == "R") {
                        // Check to make sure this public key isn't forged or made up to win the list
                        $inside_transaction_hash = tk_decrypt($transaction_public_key, base64_decode($transaction_crypt1));
                        $final_hash_compare = $transaction_crypt2;
                        $crypt_hash_check = $transaction_hash;
                        $valid_amount = TRUE;
                        // No amount, but needs this to pass amount test
                        $public_key_to = $transaction_public_key;
                        // None is used, but needs this to pass the key length test
                    } else {
                        if ($transaction_attribute == "T" || $transaction_attribute == "G") {
                            // Decrypt transaction information for regular transaction data
                            // and check to make sure the public key that is being sent to
                            // has not been tampered with.
                            $transaction_info = tk_decrypt($transaction_public_key, base64_decode($transaction_crypt3));
                            // Find destination public key
                            $public_key_to_1 = tk_decrypt($transaction_public_key, base64_decode($transaction_crypt1));
                            $public_key_to_2 = tk_decrypt($transaction_public_key, base64_decode($transaction_crypt2));
                            $public_key_to = $public_key_to_1 . $public_key_to_2;
                            $transaction_amount_sent = find_string("AMOUNT=", "---TIME", $transaction_info);
                            $transaction_amount_sent_test = intval($transaction_amount_sent);
                            if ($transaction_amount_sent_test == $transaction_amount_sent) {
                                // Is a valid integer, amount greater than zero?
                                if ($transaction_amount_sent > 0) {
                                    $valid_amount = TRUE;
                                } else {
                                    $valid_amount = FALSE;
                                }
                            } else {
                                // Is NOT a valid integer, fail check
                                $valid_amount = FALSE;
                            }
                            if ($transaction_attribute == "G") {
                                if ($transaction_amount_sent_test > 10) {
                                    // Filter silly generation amounts :p
                                    $valid_amount = FALSE;
                                }
                            }
                            $inside_transaction_hash = find_string("HASH=", "", $transaction_info, TRUE);
                            // Check if a message is encoded in this data as well
                            if (strlen($inside_transaction_hash) != 64) {
                                // A message is also encoded
                                $inside_transaction_hash = find_string("HASH=", "---MSG", $transaction_info);
                            }
                            // Check Hash against 3 crypt fields
                            $crypt_hash_check = hash('sha256', $transaction_crypt1 . $transaction_crypt2 . $transaction_crypt3);
                        }
                        $final_hash_compare = hash('sha256', $transaction_crypt1 . $transaction_crypt2);
                    }
                    // Check to make sure this transaction is even valid (hash check, length check, & timestamp)
                    if ($transaction_hash == $crypt_hash_check && $inside_transaction_hash == $final_hash_compare && strlen($transaction_public_key) > 300 && strlen($public_key_to) > 300 && $transaction_timestamp >= $current_transaction_cycle && $transaction_timestamp < $next_transaction_cycle && $valid_amount == TRUE) {
                        // Check for 100 public key limit in the transaction queue
                        $sql = "SELECT timestamp FROM `transaction_queue` WHERE `public_key` = '{$transaction_public_key}'";
                        $sql_result = mysql_query($sql);
                        $sql_num_results = mysql_num_rows($sql_result);
                        if ($sql_num_results < 100) {
                            // Transaction hash and real hash match.
                            mysql_query("INSERT INTO `transaction_queue` (`timestamp`,`public_key`,`crypt_data1`,`crypt_data2`,`crypt_data3`, `hash`, `attribute`)\n\t\t\t\t\t\t\tVALUES ('{$transaction_timestamp}', '{$transaction_public_key}', '{$transaction_crypt1}', '{$transaction_crypt2}' , '{$transaction_crypt3}', '{$transaction_hash}' , '{$transaction_attribute}')");
                        }
                    }
                }
                // End Empty Hash Check
                $match_number++;
                $current_hash = find_string("---queue{$match_number}=", "---end{$match_number}", $poll_peer);
            }
            // End While Loop
        }
        // End For Loop
    }
    // End Compare Tallies
    return;
}