Php

我如何使用 php 腳本製作像菌絲體齒輪這樣的比特幣地址生成器

  • March 13, 2016

這是我得到的程式碼,我如何使用 1 個公鑰生成新的隨機地址,每次頁面刷新;我嘗試使用時間戳但不起作用。

<?php

// step 1

$publickey='0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6';

$step1=hexStringToByteString($publickey);

echo "step1 ".$publickey."<br>";

// step 2

$step2=hash("sha256",$step1);
echo "step2 ".$step2."<br>";

// step 3

$step3=hash('ripemd160',hexStringToByteString($step2));
echo "step3 ".$step3."<br>";

// step 4

$step4="00".$step3;
echo "step4 ".$step4."<br>";

// step 5

$step5=hash("sha256",hexStringToByteString($step4));
echo "step5 ".$step5."<br>";

// step 6

$step6=hash("sha256",hexStringToByteString($step5));
echo "step6 ".$step6."<br>";

// step 7

$checksum=substr($step6,0,8);
echo "step7 ".$checksum."<br>";

// step 8

$step8=$step4.$checksum;
echo "step8 ".$step8."<br>";

// step 9
// base conversion is from hex to base58 via decimal. 
// Leading hex zero converts to 1 in base58 but it is dropped
// in the intermediate decimal stage.  Simply added back manually.

$step9="1".bc_base58_encode(bc_hexdec($step8));
echo "step9 ".$step9."<br><br>";


function hexStringToByteString($hexString){
   $len=strlen($hexString);

   $byteString="";
   for ($i=0;$i<$len;$i=$i+2){
       $charnum=hexdec(substr($hexString,$i,2));
       $byteString.=chr($charnum);
   }

return $byteString;
}

// BCmath version for huge numbers
function bc_arb_encode($num, $basestr) {
   if( ! function_exists('bcadd') ) {
       Throw new Exception('You need the BCmath extension.');
   }

   $base = strlen($basestr);
   $rep = '';

   while( true ){
       if( strlen($num) < 2 ) {
           if( intval($num) <= 0 ) {
               break;
           }
       }
       $rem = bcmod($num, $base);
       $rep = $basestr[intval($rem)] . $rep;
       $num = bcdiv(bcsub($num, $rem), $base);
   }
   return $rep;
}

function bc_arb_decode($num, $basestr) {
   if( ! function_exists('bcadd') ) {
       Throw new Exception('You need the BCmath extension.');
   }

   $base = strlen($basestr);
   $dec = '0';

   $num_arr = str_split((string)$num);
   $cnt = strlen($num);
   for($i=0; $i < $cnt; $i++) {
       $pos = strpos($basestr, $num_arr[$i]);
       if( $pos === false ) {
           Throw new Exception(sprintf('Unknown character %s at offset %d', $num_arr[$i], $i));
       }
       $dec = bcadd(bcmul($dec, $base), $pos);
   }
   return $dec;
}


// base 58 alias
function bc_base58_encode($num) {   
   return bc_arb_encode($num, '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz');
}
function bc_base58_decode($num) {
   return bc_arb_decode($num, '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz');
}

//hexdec with BCmath
function bc_hexdec($num) {
   return bc_arb_decode(strtolower($num), '0123456789abcdef');
}
function bc_dechex($num) {
   return bc_arb_encode($num, '0123456789abcdef');
}
?>

您缺少的步驟實際上是生成公鑰,因為地址只是公鑰的雜湊值。Mycelium 使用 BIP39 和 BIP32 生成確定性公鑰,使用 BIP44 方案 AFAICT。以下範例在 PHP 中使用 composer 包執行相同的操作:bitwasp/bitcoin。

免責聲明:我維護有問題的圖書館。

use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Mnemonic\Bip39;
use BitWasp\Bitcoin\Mnemonic\MnemonicFactory;
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeySequence;
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeyFactory;

$generate = true;
$math = Bitcoin::getMath();
$bip39 = MnemonicFactory::bip39();

if ($generate) {
   $mnemonic = $bip39->create();
} else {
   $mnemonic = 'glad car usual air stomach again ecology champion ranch radar meadow wolf shrug film over glue chalk derive inform always ivory anchor jaguar umbrella floor topple click polar grid economy hint raccoon canal nose organ prepare differ escape utility major dirt scan soul shiver mention raw smoke rhythm';
}

// Produce HD root key from mnemonic
$entropy = $bip39->mnemonicToEntropy($mnemonic);
$hdRoot = HierarchicalKeyFactory::fromEntropy($entropy);

// Lets derive a branch: 
$sequence = new HierarchicalKeySequence($math);
// purpose' / coin_type' / account' / change / ...
$branch = $hdRoot->deriveFromList($sequence->decodePath('44h/0h/0/0'));

// Now lets print the first 5
for ($i = 0; $i < 5; $i++) {
   $child = $branch->deriveChild($i);
   echo $child->getPublicKey()->getAddress()->getAddress() . "\n";
}

引用自:https://bitcoin.stackexchange.com/questions/38999