<?php

require "../vendor/autoload.php";
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Key\PrivateKeyFactory;
use BitWasp\Bitcoin\Script\ScriptFactory;
use BitWasp\Bitcoin\Transaction\Transaction;
use BitWasp\Bitcoin\Transaction\TransactionInput;
use BitWasp\Bitcoin\Transaction\TransactionOutput;
use BitWasp\Bitcoin\Transaction\TransactionBuilder;
use BitWasp\Bitcoin\Transaction\TransactionFactory;
$ecAdapter = Bitcoin::getEcAdapter();
// Two users independently create private keys.
$pk1 = PrivateKeyFactory::fromHex('421c76d77563afa1914846b010bd164f395bd34c2102e5e99e0cb9cf173c1d87');
$pk2 = PrivateKeyFactory::fromHex('f7225388c1d69d57e6251c9fda50cbbf9e05131e5adb81e5aa0422402f048162');
// They exchange public keys, and a multisignature address is made.
$redeemScript = ScriptFactory::multisig(2, [$pk1->getPublicKey(), $pk2->getPublicKey()]);
$outputScript = $redeemScript->getOutputScript();
// The address is funded with a transaction (fake, for the purposes of this script).
// You would do getrawtransaction normally
$spendTx = new Transaction();
$spendTx->getInputs()->addInput(new TransactionInput('4141414141414141414141414141414141414141414141414141414141414141', 0));
$spendTx->getOutputs()->addOutput(new TransactionOutput(50, $outputScript));
// One party wants to spend funds. He creates a transaction spending the funding tx to his address.
$builder = new TransactionBuilder($ecAdapter);
$builder->spendOutput($spendTx, 0)->payToAddress($pk1->getAddress(), 50)->signInputWithKey($pk1, $outputScript, 0, $redeemScript)->signInputWithKey($pk2, $outputScript, 0, $redeemScript);
$rawTx = $builder->getTransaction()->getHex();
echo "Fully signed transaction: " . $builder->getTransaction()->getHex() . "\n";
// 3.- Get the hex tx for the selected UTXO:
// https://live.blockcypher.com/btc-testnet/tx/e7f034f4a56999d04d8a3a8f07dca10e87cd4a7fd2a779e9ecd41c57afec84f8/
// 4.- Copy Tx Hex and paste here:
$txHex = '0100000001b674eafd9c1a79402661e7e7b37746145869260c29263f213f6f120ea8a95574010000006b483045022100ac406c14ef2da774d64d5504340fd278a827a99941cf0bef26fdff17c191ffa4022016a69a281ec1fcdf22c34543ff97a5c332ed12856fb44e525cf8ba3aea7c2d4101210274cb62e999bdf96c9b4ef8a2b44c1ac54d9de879e2ee666fdbbf0e1a03090cdfffffffff02e8030000000000001976a914a93806b8ae200fffca565f7cf9ef3ab17d4ffe8888ac204e0000000000001976a914edeed3ce7f485e44bc33969af08ec9250510f83f88ac00000000';
$myTx = TransactionFactory::fromHex($txHex);
$spendOutput = 0;
$recipient = AddressFactory::fromString('mvwhcFDFjmbDWCwVJ73b8DcG6bso3CZXDj');
if ($debug) {
    echo "[Send to: " . $recipient->getAddress($network) . "]\n";
}
$builder = new TransactionBuilder($ecAdapter);
$builder->spendOutput($myTx, $spendOutput)->payToAddress($recipient, 1000);
if ($debug) {
    echo "Setup stage\n";
    //print_r($builder);
    echo "Signing\n";
}
// This line throws a warning but it works:
// https://github.com/mdanter/phpecc/issues/90
error_reporting(E_ERROR | E_PARSE);
$builder->signInputWithKey($privateKey, $myTx->getOutputs()->getOutput($spendOutput)->getScript(), 0);
if ($debug) {
    //print_r($builder);
    echo "Generate transaction: \n";
}
$new = $builder->getTransaction();
$hexRawTx = $new->getHex();
if ($debug) {
    echo $hexRawTx . "\n";
}
return $hexRawTx;