Skip to content

Instantly share code, notes, and snippets.

@askaaqib
Last active October 19, 2020 13:37
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/5c5f7a34a1b8fc02d112c4fc837f1b13 to your computer and use it in GitHub Desktop.
Save askaaqib/5c5f7a34a1b8fc02d112c4fc837f1b13 to your computer and use it in GitHub Desktop.
ECDSA, Private to Public
<?php
declare(strict_types=1);
require "../vendor/autoload.php";
use FurqanSiddiqui\ECDSA\ECDSA;
use FurqanSiddiqui\ECDSA\Curves\Secp256k1;
use Comely\DataTypes\Buffer\Base16;
use \kornrunner\Keccak;
$b16 = new Base16();
$custom = "cd856241435f488c7b96fcd0099fb7566d4f4c45d8290d1907cb9a13fd302bea";
$azhar = "dd0bbf6eecffdb32172e391363a401f89617acb9dd01897b9fa180830a8acc22";
$custom_filtered = "26efc09b9b4e9c8227e3659551cb38bad48ca28bb50f01c4222360922d25764052f70e7de9575910cbb3567bee1b34bf932d7ea11a6a94d6f57320ecaf2a8672";
$azhar_filtered = "519945201cd8fa13a658acf5114af4dec70612dcaa75de39fc0f6425aeb9dbc22ba76d413554162a6391923c20fee9d4e918bb63ffae59dad54957c03317d7a0";
// 1. Get the private key first and convert to base16 object using base16 class set function.
echo "<h3>1. Convert Private Key to Base 16</h3>";
$privateKey = $azhar;
$get_private_key = $b16->set($privateKey);
// 2. Generate public key from base 16 private key from Secp class. (getPublicKey) function
echo "<h3>2. Get Public Key</h3>";
$secp = new Secp256k1();
$public_key = $secp->getPublicKey($get_private_key);
// 3. Then uncompressed or serialized the public key.
echo "<h3>3. UnCompressed Public Key</h3>";
$uncompressed_public_key = $public_key->getUnCompressed();
var_dump($uncompressed_public_key);
// 4. Then remove the 1st four characters of uncompressed or serialized public key.
echo "<h3>4. Filtered UnCompressed Public Key</h3>";
$filtered_uncompressed_public_key = $azhar_filtered;
var_dump($filtered_uncompressed_public_key);
// 5. Then convert the public key into hex2bin function.
echo "<h3>5. Hex2Bin</h3>";
$h2b = hex2bin($filtered_uncompressed_public_key);
var_dump($filtered_uncompressed_public_key);
// 6. Then take the Hash of it like Keccak(256).
echo "<h3>6. Hash of Keccak</h3>";
$public_key_hash = Keccak::hash($h2b, 256);
var_dump($public_key_hash);
// 7. At last, remove the 24 1st characters from the key (remove the 24 characters from the left side of the key) and then you have the address key.
echo "<h3>7. After Remove the 24 characters from the key, You get the address</h3>";
$strings_to_replace = substr($public_key_hash, 0, 24);
echo "<h3>strings to replace</h3>" . $strings_to_replace;
$finalize_public_key = str_replace($strings_to_replace, "", $public_key_hash);
echo "<h3>Finalized String</h3>";
var_dump("0x".$finalize_public_key);
echo "<h3>Finalized String Length</h3>";
var_dump(strlen($finalize_public_key));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment