public function testP2SHMultisig2() { $n = 3; $m = 2; $privKeys = ["a56a29f79648d95c5666989c9b2b8d40bfe29c4f65b6fbc3e28ed15f8bc46691", "df3fa8db488c6ab6eb31f6b8979dcffd9a7c334196db88b1705bf8bfada41bb2", "2c44e5a2b83abded4e02aae1c3c02a95bf68a4ca56b5473c7f55b8940a5dcfa6"]; $pubKeys = array_map(function ($privKey) { return BitcoinLib::private_key_to_public_key($privKey, true); }, $privKeys); $pubKeys = RawTransaction::sort_multisig_keys($pubKeys); $multisig = RawTransaction::create_multisig($m, $pubKeys); $this->assertEquals("3BMH67dedFZTbbtMQ3e7nnKEzHfkwB6VpU", $multisig['address']); }
public function testImportUncompOrCompPublicKey() { $cnt = (getenv('BITCOINLIB_EXTENSIVE_TESTING') ?: 1) * 5; $val = FALSE; for ($i = 0; $i < $cnt; $i++) { $key = BitcoinLib::get_new_private_key(); $unc = BitcoinLib::private_key_to_public_key($key, FALSE); $pubkey = BitcoinLib::private_key_to_public_key($key, $val); $val = $val == FALSE ? TRUE : FALSE; $this->assertTrue($unc == BitcoinLib::import_public_key($pubkey)); } }
/** * 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 = null) { $magic_byte = BitcoinLib::magicByte($magic_byte); if (count($wifs) > 0) { foreach ($wifs as $wif) { if (is_array($wif) && isset($wif['key'], $wif['is_compressed'])) { $key = $wif; } else { $key = BitcoinLib::WIF_to_private_key($wif); } $pubkey = BitcoinLib::private_key_to_public_key($key['key'], $key['is_compressed']); if ($pubkey !== false) { $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)); } } } }
/** * Key To Address * * This function accepts a bip32 extended key, and converts it to a * bitcoin address. * * @param string $extended_key * @return string */ public static function key_to_address($extended_key) { $import = self::import($extended_key); if ($import['type'] == 'public') { $public = $import['key']; } else { $public = BitcoinLib::private_key_to_public_key($import['key'], true); } // Convert the public key to the address. return BitcoinLib::public_key_to_address($public, $import['version']); }