$line = trim(fgets(STDIN));
    $decode_redeem_script = RawTransaction::decode_redeem_script($line);
    if ($decode_redeem_script == FALSE) {
        echo "[ERROR]- Not a valid script!\n";
        unset($decode_redeem_script);
    } else {
        $redeem_script = $line;
        echo "Learned about {$decode_redeem_script['m']} of {$decode_redeem_script['n']} address: " . BitcoinLib::public_key_to_address($redeem_script, '05') . "\n";
    }
}
echo "Enter WIF encoded private keys: \n (1): ";
$private_keys = array();
while ("\n" != ($line = fgets(STDIN))) {
    $line = trim($line);
    $t = BitcoinLib::validate_WIF($line, '80');
    var_dump($t);
    if (BitcoinLib::validate_WIF($line, '80') == TRUE) {
        $private_keys[] = $line;
    } else {
        echo "Not a valid private key.\n";
    }
    echo " (" . (count($private_keys) + 1) . "): ";
}
// Initialize wallet
$wallet = array();
RawTransaction::private_keys_to_wallet($wallet, $private_keys, '00');
RawTransaction::redeem_scripts_to_wallet($wallet, array($redeem_script), '05');
$raw_transaction = '01000000018240b84b90a3ae326e13219dc8a2781661fa28e23129b26fea848dd8e01a0c520000000000ffffffff02d8270000000000001976a914b7119dfb9b5c8aa7157fec48fbe640ea347dc92b88ac584d0000000000001976a914592fb6dc8cf6cd561ec86fd5fbc2a140e5ac7bc988ac00000000';
$json = '[{"txid":"520c1ae0d88d84ea6fb22931e228fa611678a2c89d21136e32aea3904bb84082","vout":0,"scriptPubKey":"a914fb56f0d4845487cc9da00c3e91e63503245f151787","redeemScript":"52210385fae44cb9f0cf858e0d404baecf78d026fae4cc9dd4343b8562059473c2af7b2102f34a1b64155db258d3a910625bd80fae6adf67d7e5b5f3de03265a4208b552d841040fa9a86f3237237423dd8331dc481c0a949fe11594c5dfa0b54bdc105daa319f9de6547d97c22296d4211073e7cffa71c8d6cd4da639607ca64fca2705e562a353ae"}]';
$sign = RawTransaction::sign($wallet, $raw_transaction, $json);
print_r($sign);
$inputs = array(array('txid' => '6737e1355be0566c583eecd48bf8a5e1fcdf2d9f51cc7be82d4393ac9555611c', 'vout' => 0, 'value' => 0.0002, 'scriptPubKey' => '76a9147e3f939e8ded8c0d93695310d6d481ae5da3961688ac'));
// sum up the total amount of coins we're spending
$inputsTotal = 0;
foreach ($inputs as $input) {
    $inputsTotal += $input['value'];
}
// fixed fee
$fee = 0.0001;
// information of who we're sending coins to and how much
$to = '1PGa6cMAzzrBpTtfvQTzX5PmUxsDiFzKyW';
$send = 5.0E-5;
// calculate change
$change = $inputsTotal - $send - $fee;
// this is our own address
$changeAddress = "1CWYJZ4vSoemSCrfBvXreqEtojEeCUeKw3";
// create ouputs, one to recipient and one to change
$outputs = array($to => BitcoinLib::toSatoshi($send), $changeAddress => BitcoinLib::toSatoshi($change));
// import private key
$wallet = array();
RawTransaction::private_keys_to_wallet($wallet, array('L2V4QgXVUyWVoMGejTj7PrRUUCEi9D9Y1AhUM8E6f5yJm7gemgN6'), '00');
// crate unsigned raw transaction
$raw_transaction = RawTransaction::create($inputs, $outputs);
// sign the transaction
// to broadcast transaction take this value and `bitcoin-cli sendrawtransaction <hex>`
$sign = RawTransaction::sign($wallet, $raw_transaction, json_encode($inputs));
print_r($sign);
echo "\n";
// set the transaction hash from the raw transaction
$txid = RawTransaction::txid_from_raw($sign['hex']);
print_r($txid);
echo "\n";
$redeem_script = "522103c0b1fd07752ebdd43c75c0a60d67958eeac8d4f5245884477eae094c4361418d2102ab1fae8dacd465460ad8e0c08cb9c25871782aa539a58b65f9bf1264c355d0982102dc43b58ee5313d1969b939718d2c8104a3365d45f12f91753bfc950d16d3e82e53ae";
$inputs = array(array("txid" => "83c5c88e94d9c518f314e30ca0529ab3f8e5e4f14a8936db4a32070005e3b61f", "vout" => 0, "scriptPubKey" => "a9145fe34588f475c5251ff994eafb691a5ce197d18b87", "redeemScript" => $redeem_script));
$outputs = array("n3P94USXs7LzfF4BKJVyGv2uCfBQRbvMZJ" => BitcoinLib::toSatoshi(0.0001));
$raw_transaction = RawTransaction::create($inputs, $outputs);
/*
 * sign with first key
 */
$wallet = array();
RawTransaction::private_keys_to_wallet($wallet, array("cV2BRcdtWoZMSovYCpoY9gyvjiVK5xufpAwdAFk1jdonhGZq1cCm"));
RawTransaction::redeem_scripts_to_wallet($wallet, array($redeem_script));
$sign = RawTransaction::sign($wallet, $raw_transaction, json_encode($inputs));
print_r($sign);
var_dump(2 == $sign['req_sigs'], 1 == $sign['sign_count'], 'false' === $sign['complete']);
/*
 * sign with second key
 */
$wallet = array();
RawTransaction::private_keys_to_wallet($wallet, array("cMps8Dg4Z1ThcwvPiPpshR6cbosYoTrgUwgLcFasBSxsdLHwzoUK"));
RawTransaction::redeem_scripts_to_wallet($wallet, array($redeem_script));
$sign = RawTransaction::sign($wallet, $sign['hex'], json_encode($inputs));
print_r($sign);
var_dump(2 == $sign['req_sigs'], 2 == $sign['sign_count'], 'true' === $sign['complete']);
/*
 * sign with third key
 */
$wallet = array();
RawTransaction::private_keys_to_wallet($wallet, array("cNn72iUvQhuzZCWg3TC31fvyNDYttL8emHgMcFJzhF4xnFo8LYCk"));
RawTransaction::redeem_scripts_to_wallet($wallet, array($redeem_script));
$sign = RawTransaction::sign($wallet, $sign['hex'], json_encode($inputs));
print_r($sign);
var_dump(2 == $sign['req_sigs'], 3 == $sign['sign_count'], 'true' === $sign['complete']);
 public function testSign()
 {
     // 1 loop takes ~0.22s
     $cnt = (getenv('BITCOINLIB_EXTENSIVE_TESTING') ?: 1) * 5;
     $inputs = array(array('txid' => '6737e1355be0566c583eecd48bf8a5e1fcdf2d9f51cc7be82d4393ac9555611c', 'vout' => 0));
     $outputs = array('1PGa6cMAzzrBpTtfvQTzX5PmUxsDiFzKyW' => BitcoinLib::toSatoshi(0.00015));
     $json_inputs = json_encode(array(array('txid' => '6737e1355be0566c583eecd48bf8a5e1fcdf2d9f51cc7be82d4393ac9555611c', 'vout' => 0, 'scriptPubKey' => '76a914' . '7e3f939e8ded8c0d93695310d6d481ae5da39616' . '88ac')));
     $wallet = array();
     RawTransaction::private_keys_to_wallet($wallet, array('L2V4QgXVUyWVoMGejTj7PrRUUCEi9D9Y1AhUM8E6f5yJm7gemgN6'), '00');
     $raw_transaction = RawTransaction::create($inputs, $outputs);
     for ($i = 0; $i < $cnt; $i++) {
         $sign = RawTransaction::sign($wallet, $raw_transaction, $json_inputs);
         $this->assertTrue(RawTransaction::validate_signed_transaction($sign['hex'], $json_inputs));
     }
 }
 /**
  * BIP32 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       $wallet
  * @param array $keys
  * @param null  $magic_byte
  */
 public static function bip32_keys_to_wallet(&$wallet, array $keys, $magic_byte = null)
 {
     $magic_byte = BitcoinLib::magicByte($magic_byte);
     RawTransaction::private_keys_to_wallet($wallet, array_map(function ($key) {
         return BIP32::import($key[0]);
     }, $keys), $magic_byte);
 }