require_once __DIR__ . "/../vendor/autoload.php"; use BitWasp\Bitcoin\Bitcoin; use BitWasp\Bitcoin\Transaction\TransactionFactory; use BitWasp\Bitcoin\Address\AddressFactory; use BitWasp\Bitcoin\Key\PrivateKeyFactory; use BitWasp\Bitcoin\Key\PublicKeyFactory; use BitWasp\Bitcoin\Rpc\RpcFactory; use BitWasp\Bitcoin\Transaction\TransactionBuilder; Bitcoin::setNetwork(\BitWasp\Bitcoin\Network\NetworkFactory::bitcoinTestnet()); $network = Bitcoin::getNetwork(); $ecAdapter = Bitcoin::getEcAdapter(); $privateKey = PrivateKeyFactory::fromHex('17a2209250b59f07a25b560aa09cb395a183eb260797c0396b82904f918518d5'); echo "[Key: " . $privateKey->toWif($network) . " Address " . $privateKey->getAddress()->getAddress($network) . "]\n"; $txHex = '010000000114a2856f5a2992a4ca0814be16a0ae79e2f88a6f53a20fcbcad5249165f56ee7010000006a47304402201e733603ac36239010e05ad229b4a18411d5507950f696db0771a5b7fe8e051202203c46da7e970e89cbbdfb4ee62fa775597a32e5029ab1d2a94f786999df2c2fd201210271127f11b833239aefd400b11d576e7cc48c6969c8e5f8e30b0f5ec0a514edf7feffffff02801a0600000000001976a914c4126d1b70f5667e492e3301c3aa8bf1031e21a888ac75a29d1d000000001976a9141ef8d6913c289890a5e9ec249fedde4440877d0288ac88540500'; $myTx = TransactionFactory::fromHex($txHex); $spendOutput = 0; $recipient = AddressFactory::fromString('n1b2a9rFvuU9wBgBaoWngNvvMxRV94ke3x'); echo "[Send to: " . $recipient->getAddress($network) . " \n"; $builder = new TransactionBuilder($ecAdapter); $builder->spendOutput($myTx, $spendOutput)->payToAddress($recipient, 40000); echo "setup stage\n"; print_r($builder); echo "signing\n"; $builder->signInputWithKey($privateKey, $myTx->getOutputs()->getOutput($spendOutput)->getScript(), 0); print_r($builder); echo "Generate transaction: \n"; $new = $builder->getTransaction(); print_r($new); echo $new->getHex() . "\n"; print_r($new);
<?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";
// In order to run the sample you will need: // 1.- Use the faucet to fund source address // Faucet https://accounts.blockcypher.com/testnet-faucet?a=n3D2YXwvpoPg8FhcWpzJiS3SvKKGD8AXZ4 // 2.- Get unspent transaction outputs (UTXOs) and select one: // https://api.blockcypher.com/v1/btc/test3/addrs/n3D2YXwvpoPg8FhcWpzJiS3SvKKGD8AXZ4?unspentOnly=true // 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();