function pubKeyToAddress($pubkey)
{
    return hash160ToAddress(hash160($pubkey));
}
function updateKeys($hash160, $pubkey, $blockhash)
{
    global $db;
    $address = hash160ToAddress($hash160, "6F");
    $result = pg_fetch_assoc(pg_query_params($db, "SELECT pubkey,encode(hash160,'hex') AS hash160 FROM keys WHERE hash160=decode(\$1,'hex')", array($hash160)));
    if (!$result && !is_null($pubkey)) {
        pg_query_params($db, "INSERT INTO keys VALUES (decode(\$1,'hex'),\$2,decode(\$3,'hex'),decode(\$4,'hex'));", array($hash160, $address, $pubkey, $blockhash));
    } else {
        if (!$result) {
            pg_query_params($db, "INSERT INTO keys(hash160,address,firstseen) VALUES (decode(\$1,'hex'),\$2,decode(\$3,'hex'));", array($hash160, $address, $blockhash));
        } else {
            if ($result && !is_null($pubkey) && is_null($result["pubkey"])) {
                if ($result["hash160"] != strtolower(hash160($pubkey))) {
                    sleep(10);
                    die("Hashes don't match");
                }
                pg_query_params($db, "UPDATE keys SET pubkey = decode(\$1,'hex') WHERE hash160=decode(\$2,'hex');", array($pubkey, $hash160));
            }
        }
    }
}