0

this is the code i got,how i can generated new random address using 1 pubkey,everytime page refresh;i try to use timestamp but din't work.

<?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');
}
?>
0

The step you're missing is actually generating the public keys, since addresses are just hashes of a public key. Mycelium uses BIP39 with BIP32 to produce deterministic public keys, using the BIP44 scheme AFAICT. The following example does the same in PHP using the composer package: bitwasp/bitcoin.

Disclaimer: I maintain the library in question.

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";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.