Exemplo n.º 1
0
<?php

require "../vendor/autoload.php";
use BitWasp\Buffertools\Buffer;
use BitWasp\Bitcoin\Key\PrivateKeyFactory;
use BitWasp\Bitcoin\Script\ScriptFactory;
use BitWasp\Bitcoin\Transaction\OutPoint;
use BitWasp\Bitcoin\Transaction\TransactionFactory;
use BitWasp\Bitcoin\Script\WitnessProgram;
use BitWasp\Bitcoin\Bitcoin;
$wif = 'QP3p9tRpTGTefG4a8jKoktSWC7Um8qzvt8wGKMxwWyW3KTNxMxN7';
$s = \BitWasp\Bitcoin\Network\NetworkFactory::bitcoinSegnet();
Bitcoin::setNetwork($s);
$ec = \BitWasp\Bitcoin\Bitcoin::getEcAdapter();
$key = PrivateKeyFactory::fromWif($wif);
echo $key->getPublicKey()->getAddress()->getAddress() . PHP_EOL;
$outpoint = new OutPoint(Buffer::hex('703f50920bff10e1622117af81b622d8bbd625460e61909cc3f8b8ee78a59c0d', 32), 0);
$scriptPubKey = ScriptFactory::scriptPubKey()->payToPubKeyHash($key->getPublicKey());
$value = 100000000;
$txOut = new \BitWasp\Bitcoin\Transaction\TransactionOutput($value, $scriptPubKey);
$destination = new WitnessProgram(0, $key->getPubKeyHash());
$p2sh = new \BitWasp\Bitcoin\Script\P2shScript($destination->getScript());
$tx = TransactionFactory::build()->spendOutPoint($outpoint)->output(95590000, $p2sh->getOutputScript())->get();
$signed = new \BitWasp\Bitcoin\Transaction\Factory\Signer($tx, $ec);
$signed->sign(0, $key, $txOut);
$ss = $signed->get();
echo $ss->getHex() . PHP_EOL;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Address\AddressFactory;
use BitWasp\Bitcoin\Transaction\TransactionFactory;
use BitWasp\Bitcoin\Script\ScriptFactory;
use BitWasp\Bitcoin\Key\PrivateKeyFactory;
use BitWasp\Bitcoin\Rpc\RpcFactory;
$network = Bitcoin::getNetwork();
$ecAdapter = Bitcoin::getEcAdapter();
$host = '127.0.0.1';
$port = '18332';
$user = getenv('BITCOINLIB_RPC_USER') ?: 'bitcoinrpc';
$pass = getenv('BITCOINLIB_RPC_PASSWORD') ?: 'BBpsLqmCCx7Vp8sRd5ygDxFkHZBgWLTTi55QwWgN6Ng6';
Bitcoin::setNetwork(\BitWasp\Bitcoin\Network\NetworkFactory::bitcoinTestnet());
$bitcoind = RpcFactory::bitcoind($host, $port, $user, $pass);
// Address to fund this test
$fundsKey = PrivateKeyFactory::fromWif('cQTqzY1hhC8u4aeFmqodENTnJvxgSk316PakYVgcFaHqAa4aCpwW');
$address = $fundsKey->getAddress()->getAddress();
// Txid / spendable output of funding transaction, funds will be moved from here -> multisig
$myTx = $bitcoind->getrawtransaction('e76ef5659124d5cacb0fa2536f8af8e279aea016be1408caa492295a6f85a214', true);
$spendOutput = 0;
// Funds will be send from Multisig -> this
$recipient = \BitWasp\Bitcoin\Address\AddressFactory::fromString('n1b2a9rFvuU9wBgBaoWngNvvMxRV94ke3x');
// Begin
$privateKey1 = PrivateKeyFactory::fromHex('17a2209250b59f07a25b560aa09cb395a183eb260797c0396b82904f918518d5', true);
$privateKey2 = PrivateKeyFactory::fromHex('17a2209250b59f07a25b560aa09cb395a183eb260797c0396b82904f918518d6', true);
$redeemScript = ScriptFactory::multisig(2, array($privateKey1->getPublicKey(), $privateKey2->getPublicKey()));
// First, move money from fundsKey to the multisig address
$new = new \BitWasp\Bitcoin\Transaction\TransactionBuilder($ecAdapter);
$new->spendOutput($myTx, $spendOutput)->payToAddress($redeemScript->getAddress(), 200000);
echo "[Fund this address: {$address}]\n";
echo "[P2SH address: " . $redeemScript->getAddress() . " ]\n";
 public static function addressFromWIF($wif)
 {
     $private_key_instance = PrivateKeyFactory::fromWif($wif);
     $address_instance = $private_key_instance->getAddress();
     return $address_instance->getAddress();
 }
 protected function signTx($private_key_wif, $tx_builder, $transaction_outputs)
 {
     $private_key = PrivateKeyFactory::fromWif($private_key_wif);
     $transaction = $tx_builder->get();
     $signer = new Signer($transaction, Bitcoin::getEcAdapter());
     // sign each input script
     foreach ($transaction_outputs as $offset => $transaction_output) {
         $signer->sign($offset, $private_key, $transaction_output);
     }
     $signed_transaction = $signer->get();
     return $signed_transaction;
 }
Exemplo n.º 5
0
 /**
  * @param string $wifPrivateKey
  * @return PrivateKeyInterface
  * @throws \Exception
  */
 public static function importPrivateKeyFromWif($wifPrivateKey)
 {
     $privateKey = PrivateKeyFactory::fromWif($wifPrivateKey);
     return $privateKey;
 }