$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";
 /**
  * send the transaction using the API
  *
  * @param string    $identifier             the identifier of the wallet
  * @param string    $rawTransaction         raw hex of the transaction (should be partially signed)
  * @param array     $paths                  list of the paths that were used for the UTXO
  * @param bool      $checkFee               let the server verify the fee after signing
  * @return string                           the complete raw transaction
  * @throws \Exception
  */
 public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false)
 {
     $data = ['raw_transaction' => $rawTransaction, 'paths' => $paths];
     // dynamic TTL for when we're signing really big transactions
     $ttl = max(5.0, count($paths) * 0.25) + 4.0;
     $response = $this->client->post("wallet/{$identifier}/send", ['check_fee' => (int) (!!$checkFee)], $data, RestClient::AUTH_HTTP_SIG, $ttl);
     $signed = self::jsonDecode($response->body(), true);
     if (!$signed['complete'] || $signed['complete'] == 'false') {
         throw new \Exception("Failed to completely sign transaction");
     }
     // create TX hash from the raw signed hex
     return RawTransaction::txid_from_raw($signed['hex']);
 }
Ejemplo n.º 3
0
 public function testTxDecodeNonStandard()
 {
     $txId = "e0c354620324422f59c9d62464a62240879cfb63856ebc60601da051522865ed";
     $raw = "0000000001e58a2b3e662984ec5a26c9d3bfbeab17c8bcb144d7b841ca2779b43f57424e23010000006b48304502210097166be10ed2660070929b68ea975e60ba4701bd5dbc2eb53290ce9ab6e5f9c502200222033604f63a4a7f97a29cd2039e97b4f9407544ced5ea6ec6371a223f2e98012103619344fbff8e9e203909cf9912c699f9d7ede4f3aa18b7545bfae16dd4c6df2effffffff020000000000000000216a1f4343484e01017b226d223a22746c222c2264223a7b2263223a223f63227d7d4847f50200000000226d784b4e7a3667583675627253457872326b66387556614539396d7a43536d78756900000000";
     $tx = RawTransaction::decode($raw);
     $this->assertEquals($raw, RawTransaction::encode($tx));
     $this->assertEquals($txId, RawTransaction::txid_from_raw(RawTransaction::encode($tx)));
 }