/** * Private Keys To Wallet * * This function accepts $wallet - a reference to an array containing * wallet info, indexed by hash160 of expected address. * It will attempt to add each key to this wallet, as well as all the * details that could be needed later on: public key, uncompressed key, * address, an indicator for address compression. Type is always set * to pubkeyhash for private key entries in the wallet. * * @param array $wallet * @param array $wifs * @param string $magic_byte */ public static function private_keys_to_wallet(&$wallet, array $wifs, $magic_byte = '00') { if (count($wifs) > 0) { foreach ($wifs as $wif) { $key = BitcoinLib::WIF_to_private_key($wif); $pubkey = BitcoinLib::private_key_to_public_key($key['key'], $key['is_compressed']); $pk_hash = BitcoinLib::hash160($pubkey); if ($key['is_compressed'] == TRUE) { $uncompressed_key = BitcoinLib::decompress_public_key($pubkey); $uncompressed_key = $uncompressed_key['public_key']; } else { $uncompressed_key = $pubkey; } $wallet[$pk_hash] = array('type' => 'pubkeyhash', 'private_key' => $key['key'], 'public_key' => $pubkey, 'uncompressed_key' => $uncompressed_key, 'is_compressed' => $key['is_compressed'], 'address' => BitcoinLib::hash160_to_address($pk_hash, $magic_byte)); } } }
/** * CKD * * This recursive function accepts $master, a parent extended key, * and an array of address bytes (the $address_definition tuple). It * pop's the next value from the $address_definition tuple and * generates the desired key. If the $address_definition tuple is * empty, then it returns the key. If not, then it calls itself again * with the new key and the tuple with the remaining key indexes to * generate, but will terminate with an array containing the desired * key at index 0, and it's human readable definition in the second. * * @param string $master * @param array $address_definition * @return array */ public static function CKD($master, $address_definition, $generated = array()) { $previous = self::import($master); if ($previous['type'] == 'private') { $private_key = $previous['key']; $public_key = BitcoinLib::private_key_to_public_key($private_key, TRUE); } else { if ($previous['type'] == 'public') { $public_key = $previous['key']; } else { // Exception here? return FALSE; } } $fingerprint = substr(hash('ripemd160', hash('sha256', pack("H*", $public_key), TRUE)), 0, 8); $i = array_pop($address_definition); $is_prime = self::check_is_prime_hex($i); if ($is_prime == 1) { if ($previous['type'] == 'public') { return FALSE; } // Cannot derive private from public key - Exception here? $data = '00' . $private_key . $i; } else { if ($is_prime == 0) { $data = $public_key . $i; } } if (!isset($data)) { return FALSE; } $I = hash_hmac('sha512', pack("H*", $data), pack("H*", $previous['chain_code'])); $I_l = substr($I, 0, 64); $I_r = substr($I, 64, 64); if (self::check_valid_hmac_key($I_l) == FALSE) { // Check the key is in a valid range. // calculate the next i in the sequence, and start over with that. $new_i = self::calc_address_bytes(self::get_address_number($i) + 1, $is_prime); array_push($address_definition, $new_i); return self::CKD($master, $address_definition, $generated); } // Keep a record of the address being built. Done after error // checking so only valid keys get to this point. if (count($generated) == 0 && $previous['depth'] == 0) { array_push($generated, $previous['type'] == 'private' ? 'm' : 'M'); } array_push($generated, self::get_address_number($i, $is_prime) . ($is_prime == 1 ? "'" : NULL)); $g = \SECcurve::generator_secp256k1(); $n = $g->getOrder(); if ($previous['type'] == 'private') { // (Il + kpar) mod n $key = str_pad(gmp_strval(\gmp_Utils::gmp_mod2(gmp_add(gmp_init($I_l, 16), gmp_init($private_key, 16)), $n), 16), 64, '0', STR_PAD_LEFT); } else { if ($previous['type'] == 'public') { // newPoint + parentPubkeyPoint $decompressed = BitcoinLib::decompress_public_key($public_key); // Can return FALSE. Throw exception? $curve = \SECcurve::curve_secp256k1(); // Prepare offset, by multiplying Il by g, and adding this to the previous public key point. // Create a new point by adding the two. $new_point = \Point::add(\Point::mul(gmp_init($I_l, 16), $g), $decompressed['point']); $new_x = str_pad(gmp_strval($new_point->getX(), 16), 64, '0', STR_PAD_LEFT); $new_y = str_pad(gmp_strval($new_point->getY(), 16), 64, '0', STR_PAD_LEFT); $key = BitcoinLib::compress_public_key('04' . $new_x . $new_y); } } if (!isset($key)) { return FALSE; } $data = array('network' => $previous['network'], 'testnet' => $previous['testnet'], 'magic_bytes' => $previous['magic_bytes'], 'type' => $previous['type'], 'depth' => $previous['depth'] + 1, 'fingerprint' => $fingerprint, 'i' => $i, 'address_number' => self::get_address_number($i), 'chain_code' => $I_r, 'key' => $key); return count($address_definition) > 0 ? self::CKD(self::encode($data), $address_definition, $generated) : array(self::encode($data), implode('/', $generated)); }