public function testAll()
 {
     $bitcoinECDSA = new BitcoinECDSA();
     $expectedRes = null;
     $sxPubKey = exec('echo -n "b7a5e63a5da3df5c04b5be15734733205a45b751259f46d89471d4a8cd120929" | sx pubkey');
     if ($sxPubKey != '03a10b6eee47ad4382915d387ee7e17ed7bd22caa9771288df24560e7a5faad880') {
         echo "Warning SX is not installed. \n";
         $expectedRes = $this->sxFallback();
     }
     for ($i = 0; $i < 100; $i++) {
         if (!$expectedRes) {
             $bitcoinECDSA->generateRandomPrivateKey();
         } else {
             $bitcoinECDSA->setPrivateKey($expectedRes[$i]->privKey);
         }
         $privKey = $bitcoinECDSA->getPrivateKey();
         //test : compressed public key
         if (!$expectedRes) {
             $sxPubKey = exec('echo -n "' . $privKey . '" | sx pubkey');
         } else {
             $sxPubKey = $expectedRes[$i]->compressedPubKey;
         }
         $bpPubKey = $bitcoinECDSA->getPubKey();
         $this->assertEquals($bpPubKey, $sxPubKey, 'Something went wrong for privateKey : ' . $bitcoinECDSA->getPrivateKey() . ', please report us the issue');
         // test : compressed address
         $addr = $bitcoinECDSA->getAddress();
         if (!$expectedRes) {
             $sxAddr = exec('echo -n "' . $privKey . '" | sx addr');
         } else {
             $sxAddr = $expectedRes[$i]->compressedAddr;
         }
         $this->assertEquals($addr, $sxAddr, 'Something went wrong for privateKey : ' . $bitcoinECDSA->getPrivateKey() . ', please report us the issue');
         $this->assertTrue($bitcoinECDSA->validateAddress($addr), 'Something went wrong while validating address : ' . $addr . ' with private key : ' . $bitcoinECDSA->getPrivateKey() . ', please report us the issue');
         $this->assertTrue($bitcoinECDSA->validateWifKey($bitcoinECDSA->getWif()), 'Something went wrong while validating Wif key : ' . $bitcoinECDSA->getWif() . ' with private key : ' . $bitcoinECDSA->getPrivateKey() . ', please report us the issue');
         //test : uncompressed public key
         if (!$expectedRes) {
             $ucSxPubKey = exec('echo -n "' . $privKey . '" | sx pubkey false');
         } else {
             $ucSxPubKey = $expectedRes[$i]->unCompressedPubKey;
         }
         $bpUcPubKey = $bitcoinECDSA->getUncompressedPubKey();
         $this->assertEquals($bpUcPubKey, $ucSxPubKey, 'Something went wrong for privateKey : ' . $bitcoinECDSA->getPrivateKey() . ', please report us the issue');
         // test : uncompressed address
         if (!$expectedRes) {
             $ucSxAddr = exec('echo -n "' . $privKey . '" | sx pubkey false | sx addr');
         } else {
             $ucSxAddr = $expectedRes[$i]->unCompressedAddr;
         }
         $ucBpAddr = $bitcoinECDSA->getUncompressedAddress();
         $this->assertEquals($ucBpAddr, $ucSxAddr, 'Something went wrong for privateKey : ' . $bitcoinECDSA->getPrivateKey() . ', please report us the issue');
         $ucPt = $bitcoinECDSA->getPubKeyPoints();
         $cDerPt = $bitcoinECDSA->getPubKeyPointsWithDerPubKey($bpPubKey);
         $ucDerPt = $bitcoinECDSA->getPubKeyPointsWithDerPubKey($bpUcPubKey);
         $this->assertEquals($ucPt['y'], $cDerPt['y'], 'decoding of compressed DER public key failed');
         $this->assertEquals($ucPt['y'], $ucDerPt['y'], 'decoding of uncompressed DER public key failed');
         // test : calculate Y with X (decompress compressed public key)
         $bitcoinECDSA->setPrivateKey('b7a5e63a5da3df5c04b5be15734733205a45b751259f46d89471d4a8cd120929');
         $bitcoinECDSA->generateRandomPrivateKey();
         $bitcoinECDSA->setPrivateKey('5d60f0259a57094daacf21dbe408f43176a2e00cc6057fad030d928df833755e');
         echo "\nprivate key : " . $bitcoinECDSA->getPrivateKey() . "\n";
         //$nonce = hash('sha256', rand(0,10000000) . microtime());
         //$nonce = '0910f9897f4e8524646d1e1caa53177d66dd5d8cfed01eabfb1b4b3810fb8f32';
         //echo "\nnonce : " . $nonce . "\n";
         $pts = $bitcoinECDSA->getSignatureHashPoints(hash('sha256', 'hello'));
         echo "Check pts signature \n";
         print_r($bitcoinECDSA->checkSignaturePoints($bitcoinECDSA->getPubKey(), $pts['R'], $pts['S'], hash('sha256', 'hello')));
         echo "\nEND";
         $signedMessage = $bitcoinECDSA->signMessage('Hello');
         echo $signedMessage . "\n";
         echo "\nSignature:\n";
         print_r($bitcoinECDSA->getSignatureHashPoints(gmp_strval(gmp_init('968236873715988614170569073515315707566766479517', 10), 16), '1'));
         echo "\n" . bin2hex(base64_decode('Gyk26Le4ER0EUvZiFGUCXhJKWVEoTtQNU449puYZPaiUmYyrcozt2LuAMgLvnEgpoF6cw8ob9Mj/CjP9ATydO1k='));
         // test : signed message is valid
         $this->assertTrue($bitcoinECDSA->checkSignatureForRawMessage($signedMessage));
         // test : DER signature
         $hash = hash('sha256', rand(0, 10000000) . microtime());
         $signature = $bitcoinECDSA->signHash($hash);
         $this->assertTrue($bitcoinECDSA->checkDerSignature($bitcoinECDSA->getPubKey(), $signature, $hash));
         die;
     }
 }
 public function testVerifySignedMessages()
 {
     $bitcoinECDSA = new BitcoinECDSA();
     $this->assertTrue($bitcoinECDSA->checkSignatureForRawMessage("-----BEGIN BITCOIN SIGNED MESSAGE-----\nThis is an example of a signed message.\n-----BEGIN SIGNATURE-----\n1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN\nG+ckNQLD26XRplYry7VqEoIS1O1U5c/qCrppiJjK8G2CtBIHZ+NDj9FLR/ojezD5Geyfv5sOSLWZAMO7y6YBmIs=\n-----END BITCOIN SIGNED MESSAGE-----"));
     $this->assertTrue($bitcoinECDSA->checkSignatureForRawMessage("-----BEGIN BITCOIN SIGNED MESSAGE-----\noho\n-----BEGIN SIGNATURE-----\n1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN\nG9NVTo1N2vbNEVNYGgobMwwuZuUb0jWvytoRd92qfckoBjhlkTbDQehADRXOWWqHGexxWcPGmub0CPdcEC+0Rbs=\n-----END BITCOIN SIGNED MESSAGE-----"));
     $this->assertTrue($bitcoinECDSA->checkSignatureForRawMessage("-----BEGIN BITCOIN SIGNED MESSAGE-----\naha\n-----BEGIN SIGNATURE-----\n1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN\nHJoQp4rhjY5wd4NyhSVUMy4EY+9npgOnXzro+l5ibkSBfA/p6JjkfUuvlnc8As6tw4eOIhtp2BN81xw/El9bpIg=\n-----END BITCOIN SIGNED MESSAGE-----"));
 }
function createDonation($amount, $name, $email)
{
    $ret = array();
    $resp = array();
    $db = new dbclass();
    $amount = db_input($amount);
    $name = db_input($name);
    $email = db_input($email);
    $db->close();
    $bitcoinECDSA = new BitcoinECDSA();
    $privkey = bin2hex(openssl_random_pseudo_bytes(32));
    $bitcoinECDSA->setPrivateKey($privkey);
    $ret['pubKey'] = $bitcoinECDSA->getAddress();
    $ret['privWIF'] = privtoWIF($bitcoinECDSA->getPrivateKey() . '01');
    $ret['amount'] = $amount;
    $ret['name'] = $name;
    $ret['email'] = $email;
    $ret['success'] = 1;
    $connection = Tivoka\Client::connect('http://' . RPC_USERNAME . ':' . RPC_PASSWORD . '@' . RPC_HOST . ':' . RPC_PORT);
    try {
        $request = $connection->sendRequest('sendtoaddress', array($ret['pubKey'], floatval($amount)));
        $resp = json_decode($request->response, true);
        if (isset($resp['error'])) {
            $ret['success'] = 0;
            return $ret;
        } else {
            $ret['txid'] = $resp['result'];
            $dbret = -1;
            $data_array = array('id' => '', 'pubkey' => $ret['pubKey'], 'privkey' => $ret['privWIF'], 'amount' => $ret['amount'], 'txid' => $ret['txid'], 'name' => $ret['name'], 'email' => $ret['email'], 'dateadded' => 'now()', 'lastMod' => 'now()');
            $dbret = db_perform('donations', $data_array);
        }
    } catch (Exception $e) {
        $ret['success'] = 0;
    }
    return $ret;
}