Created
November 14, 2019 05:50
-
-
Save atlaschiew/44b4cc20f6644977e95d7cdaf9f56cb5 to your computer and use it in GitHub Desktop.
bitwasp_script_p2pkh_address.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress; | |
use BitWasp\Bitcoin\Bitcoin; | |
use BitWasp\Bitcoin\Crypto\Random\Random; | |
use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory; | |
use BitWasp\Buffertools\Buffer; | |
use BitWasp\Bitcoin\Network\NetworkFactory; | |
use BitWasp\Bitcoin\Script\Opcodes; | |
$errmsg = ''; | |
if ($_GET['tab'] == 'form1_tabitem1' AND $_SERVER['REQUEST_METHOD'] == 'POST') { | |
try { | |
$networkClass = $_POST['network']; | |
Bitcoin::setNetwork(NetworkFactory::$networkClass()); | |
$network = Bitcoin::getNetwork(); | |
$privKeyFactory = new PrivateKeyFactory(); | |
if (!$_POST['input'] OR ctype_xdigit($_POST['input'])) | |
{ | |
if (!$_POST['input']) | |
{ | |
$rbg = new Random(); | |
if ($_POST['compression'] == 'y') { | |
$privateKey = $privKeyFactory->generateCompressed($rbg); | |
} else { | |
$privateKey = $privKeyFactory->generateUncompressed($rbg); | |
} | |
} else { | |
if ($_POST['compression'] == 'y') { | |
$privateKey = $privKeyFactory->fromHexCompressed($_POST['input']); | |
} else { | |
$privateKey = $privKeyFactory->fromHexUncompressed($_POST['input']); | |
} | |
} | |
} else { | |
$privateKey = $privKeyFactory->fromWIF($_POST['input'], $network); | |
} | |
$publicKey = $privateKey->getPublicKey(); | |
$pubKeyHash = $publicKey->getPubKeyHash(); | |
$p2pkh = new PayToPubKeyHashAddress($pubKeyHash); | |
?> | |
<div class="table-responsive"> | |
<table border=0 class='table'> | |
<tr style='background-color:#f0f0f0'><td>Base58 Address</td><td><?php echo $p2pkh->getAddress()?></td></tr> | |
<tr><td>Private Key Hex</td><td><?php echo $privateKey->getHex()?></td></tr> | |
<tr><td>Private Key Wif</td><td><?php echo $privateKey->toWif()?></td></tr> | |
<tr><td>Has Compression?</td><td><?php echo $privateKey->isCompressed() ? "Yes" : "No"?></td></tr> | |
<tr style='background-color:#f0f0f0'><td>Public Key Hex</td><td><?php echo $publicKey->getHex()?></td></tr> | |
<tr style='background-color:#f0f0f0'><td>Has Compression?</td><td><?php echo $publicKey->isCompressed()? "Yes" : "No"?></td></tr> | |
<tr style='background-color:#f0f0f0'><td>Public Key Hash Hex</td><td><?php echo $pubKeyHash->getHex()?></td></tr> | |
<tr><td>ScriptPubKey Hex </td><td><?php echo $p2pkh->getScriptPubKey()->getHex()?></td></tr> | |
<tr><td>ScriptPubKey Asm</td> | |
<td> | |
<?php | |
$opcodes = $p2pkh->getScriptPubKey()->getOpcodes(); | |
foreach( $p2pkh->getScriptPubKey()->getScriptParser()->decode() as $operation ) { | |
if ($operation->isPush()) { | |
echo htmlentities("<{$operation->getData()->getHex()}> "); | |
} else { | |
echo $opcodes->getOp($operation->getOp()) . " " ; | |
} | |
} | |
?> | |
</td> | |
</tr> | |
</table> | |
</div> | |
<?php | |
} catch (Exception $e) { | |
$errmsg .= "Problem found. " . $e->getMessage(); | |
} | |
} | |
if ($errmsg) { | |
?> | |
<div class="alert alert-danger"> | |
<strong>Error!</strong> <?php echo $errmsg?> | |
</div> | |
<?php | |
} | |
?> | |
<form action='?tab=form1_tabitem1#hashtag1' method='post'> | |
<div class="form-group"> | |
<label for="network">Network:</label> | |
<select id="network" name="network" class="form-control" > | |
<?php | |
$networks = get_class_methods(new NetworkFactory()); | |
foreach($networks as $network) { | |
echo "<option value='{$network}'".($network == $_POST['network'] ? " selected": "").">{$network}</option>"; | |
} | |
?> | |
</select> | |
</div> | |
<div class="form-group"> | |
<label for="input">Private Key (Hex) / WIF:</label> | |
<input class="form-control" type='text' name='input' id='input' value='<?php echo $_POST['input']?>'> | |
* Put empty if you want system assign you a random private key. | |
</div> | |
<div class="form-group"> | |
<label for="compression" >Compression:</label> | |
<select name="compression" class="form-control" id='compression'> | |
<?php | |
$yesno = array("y"=>"Yes", "n"=>"No"); | |
foreach($yesno as $yesno_k=>$yesno_v) { | |
echo "<option value='{$yesno_k}'".($yesno_k == $_POST['compression'] ? " selected": "").">{$yesno_v}</option>"; | |
} | |
?> | |
</select> | |
</div> | |
<input type='submit' class="btn btn-success btn-block"/> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment