Monday, November 25, 2024

php personal key to pockets import format (wip)

I’ve been writing a php file supposed to create a bitcoin personal key and alter it to pockets import format. The difficulty is once I strive a command in a gnome-terminal comparable to:
bitcoin-cli importprivkey ygm8fAjomMyKVcb8vK8MPBByYpMDySY1vAxcpw6DUfPBHb8kv “” false
I get an error:
error: {“code”:-5,”message”:”Invalid personal key encoding”}

From this I’ve infered that my code is flawed in creating a non-public key and altering it into pockets import format. However from what I see within the supply code it ought to do what it says to at https://en.bitcoin.it/wiki/Wallet_import_format underneath “Non-public key to WIF”

Listed below are the php recordsdata:

test1.php:

<?php
require('devRand.php');
#require('base58.php');

$a = new btcMakePrivKey();
#echo $GLOBALS['base58oper'];
perform decodeHex($hex) {
    $hexchars = "0123456789ABCDEF";

    $hex = strtoupper($hex);
    $return = "0";
    for ($i = 0; $i < strlen($hex); $i++) {
        $present = (string) strpos($hexchars, $hex[$i]);
        $return = (string) bcmul($return, "16", 0);
        $return = (string) bcadd($return, $present, 0);
    }
    return $return;
}
perform encodeBase58($hex) 
{
    $base58chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
    if (strlen($hex) % 2 != 0) {
        die("encodeBase58: uneven variety of hex characters");
    }
    $orighex = $hex;

    $hex = decodeHex($hex);
    $return = "";
    whereas (bccomp($hex, 0) == 1) {
        $dv = (string) bcdiv($hex, "58", 0);
        $rem = (integer) bcmod($hex, "58");
        $hex = $dv;
        $return = $return . $base58chars[$rem];
    }
    $return = strrev($return);

    //main zeros
    for ($i = 0; $i < strlen($orighex) && substr($orighex, $i, 2) == "00"; $i += 2) {
        $return = "1" . $return;
    }
    return $return;
}
class btcMakePrivKey
{
    //$base58oper = new base58();
    public perform __construct()
    {
        #$this->base58oper = new base58();
        $rely = 0;  $privKey = "";
        whereas($rely < 62)
        {
            $appendable = devurandom_rand(0,15);
            change($appendable)
            {
                case 10:
                $appendable="A";
                break;
                case 11:
                $appendable="B";
                break;
                case 12:
                $appendable="C";
                break;
                case 13:
                $appendable="D";
                break;
                case 14:
                $appendable="E";
                break;
                case 15:
                $appendable="F";
                break;
                default:
                break;
            }
            $rely = $rely + 1;
            $privKey = $privKey . $appendable;
        }
        $privKey = $this->addLeadingByte($privKey);
        #echo $privKey . "nn";
        $pkHashed = $this->sha256Hash($this->sha256Hash($privKey) );
        $checksum = substr($pkHashed, 0, 8);
        $privKey = $privKey . $checksum;
        #$privKey = $this->base58oper->encode($privKey);
        $privKey = encodeBase58($privKey);
        echo $privKey;// . "nn" . $pkHashed . "nn" . $checksum;
    }
    personal perform addLeadingByte($privKey)
    {
        return '80' . $privKey;
    }
    personal perform sha256Hash($privKey)
    {
        return hash('sha256', $privKey, false);
    }
}

and devRand.php:

<?php
//equiv to rand, mt_rand
//returns int in *closed* interval [$min,$max]
perform devurandom_rand($min = 0, $max = 0x7FFFFFFF) {
    $diff = $max - $min;
    if ($diff < 0 || $diff > 0x7FFFFFFF) {
    throw new RuntimeException("Dangerous vary");
    }
    $bytes = mcrypt_create_iv(4, MCRYPT_DEV_URANDOM);
    if ($bytes === false || strlen($bytes) != 4) {
        throw new RuntimeException("Unable to get 4 bytes");
    }
    $ary = unpack("Nint", $bytes);
    $val = $ary['int'] & 0x7FFFFFFF;   // 32-bit protected
    $fp = (float) $val / 2147483647.0; // convert to [0,1]
    return spherical($fp * $diff) + $min;
}

I intend my code to create a bitcoin personal key and convert it to a useable key in pockets import format, which can be utilized in bitcoin-cli importprivkey insertKeyHere “” false

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles