public function testBlock() { $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 < 10; $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->getPubKey()); 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)); echo $signedMessage; // test : DER signature $hash = hash('sha256', rand(0, 10000000) . microtime()); $signature = $bitcoinECDSA->signHash($hash); $this->assertTrue($bitcoinECDSA->checkDerSignature($bitcoinECDSA->getPubKey(), $signature, $hash)); } }