/** * Decode Mnemonic * * This function decodes a string of 12 words to convert to the electrum * seed. This is an implementation of http://tools.ietf.org/html/rfc1751, * which is how electrum generates a 128-bit key from 12 words. * * @param string $words * @return string */ public static function decode_mnemonic($words) { $words = explode(" ", $words); $out = ''; $n = 1626; for ($i = 0; $i < count($words) / 3; $i++) { $a = 3 * $i; list($word1, $word2, $word3) = array($words[$a], $words[$a + 1], $words[$a + 2]); $index_w1 = array_search($word1, self::$words); $index_w2 = array_search($word2, self::$words) % $n; $index_w3 = array_search($word3, self::$words) % $n; $x = $index_w1 + $n * \gmp_Utils::gmp_mod2($index_w2 - $index_w1, $n) + $n * $n * \gmp_Utils::gmp_mod2($index_w3 - $index_w2, $n); $out .= BitcoinLib::hex_encode($x); } return $out; }
/** * Redeem Scripts To Wallet * * This function extends on whatever data is in the $wallet array, by * adding script hash addresses to the wallet, and linking keys in the * multisignature address with keys in the wallet. * Adds each redeemScript to the referenced $wallet. * * @param array $wallet * @param array $redeem_scripts * @param string $magic_byte */ public static function redeem_scripts_to_wallet(&$wallet, array $redeem_scripts = array(), $magic_byte = '05') { if (count($redeem_scripts) > 0) { foreach ($redeem_scripts as $script) { $decode = self::decode_redeem_script($script); if ($decode == FALSE) { continue; } $scripthash = BitcoinLib::hash160($script); $keys = array(); foreach ($decode['keys'] as $key) { $keyhash = BitcoinLib::hash160($key); if (isset($wallet[$keyhash])) { $keys[] = $wallet[$keyhash]; } } $wallet[$scripthash] = array('type' => 'scripthash', 'script' => $script, 'required_signature_count' => $decode['m'], 'address' => BitcoinLib::hash160_to_address($scripthash, $magic_byte), 'public_keys' => $decode['keys'], 'keys' => $keys); } } }
/** * Key To Address * * This function accepts a bip32 extended key, and converts it to a * bitcoin address. * * @param string $extended_key * @param string $address_version * return string/FALSE */ public static function key_to_address($extended_key) { $import = self::import($extended_key); if ($import['type'] == 'public') { $public = $import['key']; } else { if ($import['type'] == 'private') { $public = BitcoinLib::private_key_to_public_key($import['key'], TRUE); } else { return FALSE; } } // Convert the public key to the address. return BitcoinLib::public_key_to_address($public, $import['version']); }