Bitwasp DER Signature


Distinguished Encoding Rules (DER) signature is required when dealing with script opcodes such as OP_CHECKSIG, OP_CHECKSIGVERIFY, OP_CHECKMULTISIG & OP_CHECKMULTISIGVERIFY.

Find DER Signature

<?php 

use BitWasp\Buffertools\Buffer;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory;
use BitWasp\Bitcoin\Signature\TransactionSignature;

include_once "../libraries/vendor/autoload.php";
include_once("html_iframe_header.php");

$errmsg = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
		$sigHashType = $_POST['sighashtype'];
		
		$ecAdapter = Bitcoin::getEcAdapter();

		$privKeyFactory = new PrivateKeyFactory($ecAdapter);
		$privKey = $privKeyFactory->fromHexCompressed($_POST['privatekey']);
		
		$sig = new TransactionSignature($ecAdapter, $privKey->sign(Buffer::hex($_POST['data'])), $sigHashType);

    ?>
        <div class="table-responsive">
            <table border=0 class='table'>
                <tr><td>DER Signature Hex</td><td><?php echo $sig->getSignature()->getBuffer()->getHex() . Buffer::int($sigHashType, 1)->getHex()?></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='' method='post'>
    <div class="form-group">
        <label for="data">Data To Sign (Hex):</label>
        <input class="form-control" type='text' name='data' id='data' value='<?php echo $_POST['data']?>'>
    </div>
	 <div class="form-group">
        <label for="privatekey">Private Key Hex:</label>
        <input class="form-control" type='text' name='privatekey' id='privatekey' value='<?php echo $_POST['privatekey']?>'>
    </div>
    <div class="form-group">
        <label for="sighashtype">SigHashType:</label>
        <select name="sighashtype" class="form-control" id='sighashtype'>
			<?php
			$sighashtypes = [1=>"ALL",2=>"NONE",3=>"SINGLE",128=>"ANYONECANPAY"];
			
			foreach($sighashtypes as $sighashkey=>$sighashtype) {
				echo "<option value='{$sighashkey}'".($sighashkey == $_POST['sighashtype'] ? " selected": "").">{$sighashtype}</option>";
			}
			?>			
        </select>
    </div>
    <input type='submit' class="btn btn-success btn-block"/>
</form>
<?php
include_once("html_iframe_footer.php");		








Tutorials
About Us
Contents have been open source in GITHUB. Please give me a ⭐ if you found this helpful :)
Community
Problem? Raise me a new issue.
Support Us
Buy me a coffee. so i can spend more nights for this :)

BTCSCHOOLS would like to present you with more pratical but little theory throughout our tutorials. Pages' content are constantly keep reviewed to avoid mistakes, but we cannot warrant correctness of all contents. While using this site, you agree to accept our terms of use, cookie & privacy policy. Copyright 2019 by BTCSCHOOLS. All Rights Reserved.