/** * 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 null $magic_p2sh_byte */ public static function redeem_scripts_to_wallet(&$wallet, array $redeem_scripts = array(), $magic_p2sh_byte = null) { $magic_p2sh_byte = BitcoinLib::magicP2SHByte($magic_p2sh_byte); 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_p2sh_byte), 'public_keys' => $decode['keys'], 'keys' => $keys); } } }
<?php use BitWasp\BitcoinLib\BitcoinLib; use BitWasp\BitcoinLib\RawTransaction; require_once __DIR__ . '/../vendor/autoload.php'; $m = 2; $public_keys = array('0379ddc228d8c44a85ae30c877a6b037ec3d627e0507f223a0412790a83a46cd5f', '024d1cf2ca917f4d679fc02df2a39c0a8110a1b6935b27ae6762a0ceeec7752801', '0258f70f6400aa6f60ff0d21c3aaf1ca236d177877d2b9ad9d2c55280e375ab2d2'); // Create redeem script $redeem_script = RawTransaction::create_redeem_script($m, $public_keys); // Obtain 20-byte hash of script $hash160 = BitcoinLib::hash160($redeem_script); // Convert to address with version 0x05. $address = BitcoinLib::hash160_to_address($hash160, '05'); // Display data $c = 0; echo "Public Keys\n"; for ($i = 0; $i < count($public_keys); $i++) { echo "{$i} : " . $public_keys[$i] . "\n"; } echo "\nRedeem Script\n"; echo "{$redeem_script}\n\n"; echo "Hash160\n"; echo "{$hash160}\n\n"; echo "Address\n"; echo "{$address}\n\n";
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); } }
//$address = BitcoinLib::hash160_to_address($script_hash, '05'); //$raw_transaction = '0100000001c723ef78f22d8563b2d1d61aee1904ef12e28524b87122db87c00d6ebc057c9600000000d40047304402203432bff2b897fe10291ec8a66449cd09d850729434d75207de6b4be843a6596b02201aeeb571d03165974215e50f8fca0dc05d35febbbc95d312cab3cf5ebc337085014c8952210217e9f0793d77e4af65e7b3c158459d51edc54011d24b3d57b9945ac4a3d377852103f868e850aed9be2513b7194cf14421ad8b6ec98f65dbdb8264d8be63d8f6ff17410400a0702140404c2e90b3a50bdcb47efb80afa8f4f3a5ae3226bf9027c3c644ce839aaad770ca287615d123e962afd1a7266974cb8da5c5468ffe3aa41c41f03453aeffffffff02d8270000000000001976a914b24490dbbd5e0f02e7891786991aadd869b5e75b88ac584d0000000000001976a914163f9c401b613a2bc7d1e331972788da0d66f6cc88ac00000000'; //$decoded_transaction = RawTransaction::decode($raw_transaction); //$json_str = ''; //$seed = '6f45ad7f901d7421a6d20d9e797bb73b'; while (!isset($redeem_script)) { echo "Enter redeem script: "; $line = trim(fgets(STDIN)); $decode_redeem_script = RawTransaction::decode_redeem_script($line); if ($decode_redeem_script == FALSE) { echo "Not a valid script!\n\n"; unset($decode_redeem_script); } else { $redeem_script = $line; $script_hash = BitcoinLib::hash160($redeem_script); $address = BitcoinLib::hash160_to_address($script_hash, '05'); echo "Learned about {$decode_redeem_script['m']} of {$decode_redeem_script['n']} address: " . $address . "\n\n"; } } // Prompt for raw transaction. // Sets $raw_transaction, and $decoded_transaction while (!isset($raw_transaction)) { echo "Enter a raw transaction to sign: "; $line = trim(fgets(STDIN)); $multi = explode(" ", $line); $decoded_transaction = RawTransaction::decode($multi[0]); if ($decoded_transaction !== FALSE) { $raw_transaction = $multi[0]; } else { echo "Not a valid raw transaction, or unable to decode.\n\n"; unset($decoded_transaction);