Last active
November 30, 2020 10:24
-
-
Save askaaqib/209d08184a187c0e1ef6be64ee2fcb5a to your computer and use it in GitHub Desktop.
Wallet Import Format (WIF) in PHP - Private to Wif
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 | |
declare(strict_types=1); | |
require "../vendor/autoload.php"; | |
echo "<pre>"; | |
$base58 = new \FurqanSiddiqui\Base58\Base58(); | |
$base16 = new \Comely\DataTypes\Buffer\Base16(); | |
$privateKey = $base16->set("0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D"); | |
echo "<h3>Private Key</h3>"; | |
var_dump($privateKey->value()); | |
echo "<h3>Extended Key</h3>"; | |
$mainPrefix = "0x80"; | |
$extendedKey = $mainPrefix . $privateKey->value(); | |
$extendedKey = substr($extendedKey, 2); | |
var_dump($extendedKey); | |
// 800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D | |
echo "<h3>SHA1</h3>"; | |
$sha1 = hash("sha256", hex2bin($extendedKey)); | |
var_dump($sha1); | |
// 8147786c4d15106333bf278d71dadaf1079ef2d2440a4dde37d747ded5403592 | |
echo "<h3>SHA2</h3>"; | |
$sha2 = hash("sha256", hex2bin($sha1)); | |
var_dump($sha2); | |
// 507a5b8dfed0fc6fe8801743720cedec06aa5c6fca72b07c49964492fb98a714 | |
echo "<h3>Takes First 4 Bytes (First 8 characters)</h3>"; | |
$finalSha = strtoupper(substr($sha2, 0, 8)); | |
var_dump($finalSha); | |
// 507A5B8D | |
echo "<h3>Final Extended Key</h3>"; | |
$finalExtended = $extendedKey . $finalSha; | |
var_dump($finalExtended); | |
// 800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D507A5B8D | |
echo "<h3>Base58 Encoding</h3>"; | |
$base58_encode = $base58::Encode(\Comely\DataTypes\BcNumber::fromBase16($base16->set($finalExtended))); | |
var_dump($base58_encode->value()); | |
// 5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment