public function testBase58CheckEncodeP2SH() { $cnt = (getenv('BITCOINLIB_EXTENSIVE_TESTING') ?: 1) * 50; for ($i = 0; $i < $cnt; $i++) { // random, 20-byte string. $hex = (string) bin2hex(mcrypt_create_iv(20, \MCRYPT_DEV_URANDOM)); // 'manually' create address $encode = BitcoinLib::base58_encode_checksum($this->p2shAddressVersion . $hex); $decode = BitcoinLib::base58_decode_checksum($encode); // validate 'manually' created address $this->assertTrue(BitcoinLib::validate_address($encode, $this->addressVersion, $this->p2shAddressVersion)); // validate 'manually' created address without specifying the address version // relying on the defaults $this->assertTrue(BitcoinLib::validate_address($encode)); // validate 'manually' created address // disable address version and P2S address version specifically $this->assertTrue(BitcoinLib::validate_address($encode, false, null)); $this->assertFalse(BitcoinLib::validate_address($encode, null, false)); // validate 'manually' $this->assertTrue($hex == $decode); // create address $check2 = BitcoinLib::hash160_to_address($hex, $this->p2shAddressVersion); // validate created address $this->assertTrue(BitcoinLib::validate_address($check2, $this->addressVersion, $this->p2shAddressVersion)); // validate created address without specifying the address version // relying on the defaults $this->assertTrue(BitcoinLib::validate_address($check2)); // validate created address // disable address version and P2S address version specifically $this->assertTrue(BitcoinLib::validate_address($check2, false, null)); $this->assertFalse(BitcoinLib::validate_address($check2, null, false)); // validate 'manually' $this->assertTrue($check2 == $encode); // create address, without specifying the address version // relying on the defaults $check3 = BitcoinLib::hash160_to_address($hex, 'p2sh'); // validate created address $this->assertTrue(BitcoinLib::validate_address($check3, $this->addressVersion, $this->p2shAddressVersion)); // validate created address without specifying the address version // relying on the defaults $this->assertTrue(BitcoinLib::validate_address($check3)); // validate created address // disable address version and P2S address version specifically $this->assertTrue(BitcoinLib::validate_address($check3, false, null)); $this->assertFalse(BitcoinLib::validate_address($check3, null, false)); // validate 'manually' $this->assertTrue($check3 == $encode); } }
/** * Encode * * This function accepts an array of information describing the * extended key. It will determine the magic bytes depending on the * network, testnet, and type indexes. The fingerprint is accepted * as-is, because the CKD() and master_key() functions work that out * themselves. The child number is fixed at '00000000'. Private key's * are padded with \x00 to ensure they are 33 bytes. This information * is concatenated and converted to base58check encoding. * The input array has the same indexes as the output from the import() * function to ensure compatibility. * * @param array $data * @return string */ public static function encode($data) { // Magic Byte - 4 bytes / 8 characters - left out for now $magic_byte_var = strtolower($data['network']) . "_" . ($data['testnet'] == true ? 'testnet' : 'mainnet') . "_{$data['type']}"; $magic_byte = self::${$magic_byte_var}; $fingerprint = $data['fingerprint']; $child_number = $data['i']; $depth = BitcoinLib::hex_encode($data['depth']); $chain_code = $data['chain_code']; $key_data = $data['type'] == 'public' ? $data['key'] : '00' . $data['key']; $string = $magic_byte . $depth . $fingerprint . $child_number . $chain_code . $key_data; return BitcoinLib::base58_encode_checksum($string); }