Created
May 20, 2021 17:04
-
-
Save placecodex/f343e4700b056bf92d5a5cad2321a76b to your computer and use it in GitHub Desktop.
bitcoin transaction php testnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use BitWasp\Bitcoin\Bitcoin; | |
use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory; | |
use BitWasp\Bitcoin\Script\P2shScript; | |
use BitWasp\Bitcoin\Script\WitnessScript; | |
use BitWasp\Bitcoin\Script\ScriptFactory; | |
use BitWasp\Bitcoin\Transaction\Factory\Signer; | |
use BitWasp\Bitcoin\Transaction\Factory\TxBuilder; | |
use BitWasp\Bitcoin\Transaction\OutPoint; | |
use BitWasp\Bitcoin\Transaction\TransactionOutput; | |
use BitWasp\Buffertools\Buffer; | |
use BitWasp\Bitcoin\Network\NetworkFactory; | |
use BitWasp\Bitcoin\Address\AddressCreator; | |
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress; | |
use BitWasp\Bitcoin\Transaction\TransactionFactory; | |
use BitWasp\Bitcoin\Transaction\SignatureHash\SigHash; | |
$addrCreator = new AddressCreator(); | |
$ecAdapter = Bitcoin::getEcAdapter(); | |
$Bitcoin = new Bitcoin(); | |
$Bitcoin->setNetwork(false ? NetworkFactory::bitcoin() : NetworkFactory::bitcoinTestnet()); | |
$network = $Bitcoin->getNetwork(); | |
$privFactory = new PrivateKeyFactory(); | |
// Setup network and private key to segnet | |
$privateKey = $privFactory->fromWif('cUst4FwMABeNT8N5t5cD13gZh3aNvXRc52h56F5JdUwiuiNWQRMh'); | |
$address = new PayToPubKeyHashAddress($privateKey->getPublicKey()->getPubKeyHash()); | |
echo "[Key: " . $privateKey->toWif($network)."<br>"; | |
echo " From Address " . $address->getAddress($network) ."<br>". "]\n"; | |
$scriptPubKey = ScriptFactory::scriptPubKey()->payToPubKeyHash($privateKey->getPubKeyHash()); | |
$txOut = new TransactionOutput(100000000, $scriptPubKey); | |
//address recipient | |
$recipient = $addrCreator->fromString('n1b2a9rFvuU9wBgBaoWngNvvMxRV94ke3x'); | |
echo "[Send to: " . $recipient->getAddress($network) . " \n"; | |
echo "Generate a transaction spending the one above \n"; | |
$spendTx = TransactionFactory::build(); | |
// input | |
$spendTx->input('1f88e03624c5eb82c832b787b69efa8ad7ecf62d135c65c3978f0392289f0238', 0); | |
$spendTx->payToAddress(40000, $recipient); | |
// Make transaction | |
$transaction = $spendTx->get(); | |
echo "<br>"; | |
echo $transaction->getHex().PHP_EOL; | |
echo "Sign transaction\n"; | |
$signer = new Signer($transaction, $ecAdapter); | |
$signatureType = SigHash::ALL | SigHash::ANYONECANPAY; | |
$ScriptFactory = new ScriptFactory(); | |
$signer->sign(0, $privateKey, $txOut); | |
echo "Generate transaction: \n"; | |
$new = $signer->get(); | |
echo $new->getHex()."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment