Skip to content

Instantly share code, notes, and snippets.

@askaaqib
Created December 1, 2020 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save askaaqib/14ffe563c61aec55582c82f952bf2a22 to your computer and use it in GitHub Desktop.
Save askaaqib/14ffe563c61aec55582c82f952bf2a22 to your computer and use it in GitHub Desktop.
WIF (Complete Implementation of WIF, Included three steps)
<?php
declare(strict_types=1);
require "../vendor/autoload.php";
echo "<pre>";
$base58 = new \FurqanSiddiqui\Base58\Base58();
$base16 = new \Comely\DataTypes\Buffer\Base16();
$base58check = new \FurqanSiddiqui\Base58\Base58Check();
/*********** PRIVATE TO WIF CONVERSION ***********/
echo "<h1>Private To WIF Conversion</h1>";
/** @var $privateKey */
$privateKey = $base16->set("0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D");
echo "<h4>Private Key</h4>";
var_dump($privateKey->value());
echo "<h4>Extended Key</h4>";
$mainPrefix = "0x80";
$extendedKey = $mainPrefix . $privateKey->value();
$extendedKey = substr($extendedKey, 2);
var_dump($extendedKey);
// 800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D
echo "<h4>SHA1</h4>";
$sha1 = hash("sha256", hex2bin($extendedKey));
var_dump($sha1);
// 8147786c4d15106333bf278d71dadaf1079ef2d2440a4dde37d747ded5403592
echo "<h4>SHA2</h4>";
$sha2 = hash("sha256", hex2bin($sha1));
var_dump($sha2);
// 507a5b8dfed0fc6fe8801743720cedec06aa5c6fca72b07c49964492fb98a714
echo "<h4>Takes First 4 Bytes (First 8 characters)</h4>";
$finalSha = strtoupper(substr($sha2, 0, 8));
var_dump($finalSha);
// 507A5B8D
echo "<h4>Final Extended Key</h4>";
$finalExtended = $extendedKey . $finalSha;
var_dump($finalExtended);
// 800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D507A5B8D
echo "<h4>Base58 Encoding</h4>";
$base58_encode = $base58::Encode(\Comely\DataTypes\BcNumber::fromBase16($base16->set($finalExtended)));
$finalWif = $base58_encode->value();
var_dump($finalWif);
// 5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ
/*********** WIF TO PRIVATE CONVERSION ***********/
echo "<h1>WIF To Private Conversion</h1>";
echo "<h4>Decode Base58 to Byte String</h4>";
$base58decode = $base58check->decode($base58_encode);
$base58decode = $base58decode->value();
print_r($base58decode);
echo "<h4>Final Private Key</h4>";
$base58decode = substr($base58decode, 2);
print_r(strtoupper($base58decode));
/*********** WIF CHECKSUM CHECKING ***********/
echo "<h1>WIF checksum checking</h1>";
echo "<h4>Base58 Decode</h4>";
$base58decode = $base58check->decode($base58_encode);
print_r($base58decode);
echo "<h4>Checksum Checking</h4>";
$checksumChecking = $base58check->decode($base58_encode)->value();
print_r(strtoupper($checksumChecking));
echo "<h4>SHA1</h4>";
$shaOne = hash("sha256", hex2bin($checksumChecking));
print_r($shaOne);
echo "<h4>SHA2</h4>";
$shaTwo = hash("sha256", hex2bin($shaOne));
print_r($shaTwo);
echo "<h4>Checksum Value</h4>";
$checksumVal = substr($shaTwo, 0, 8);
print_r($checksumVal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment