コード例 #1
0
$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";
コード例 #2
0
// Create derived key from master key + derivation
$key = BIP32::build_key($master, $def);
// Display private extended key and the address that's derived from it.
echo "Generated key: note that all depth=1 keys are hardened. \n {$key[1]}        : {$key[0]}\n";
echo "             : " . BIP32::key_to_address($key[0]) . "\n";
// Convert the extended private key to the public key, and display the address that's derived from it.
$pub = BIP32::extended_private_to_public($key);
echo "Public key\n {$pub[1]}        : {$pub[0]}\n";
echo "             : " . BIP32::key_to_address($pub[0]) . "\n";
/////////////////////////////
// We're gonna spent the first txout from this tx:
//  https://www.blocktrail.com/BTC/tx/4a2231e13182cdb64fa2f9aae38fca46549891e9dc15e8aaf484d82fc6e0a1d8
// Set up inputs here
$inputs = array(array('txid' => '4a2231e13182cdb64fa2f9aae38fca46549891e9dc15e8aaf484d82fc6e0a1d8', 'vout' => 0));
// Set up outputs here
$outputs = array('1KuE17Fbcdsn3Ns5T9Wzi1epurRnKC9qVr' => BitcoinLib::toSatoshi(0.0004));
////////////////////////////
// Parameters for signing.
// Create JSON inputs parameter
// - These can come from bitcoind, or just knowledge of the txid/vout/scriptPubKey,
//   and redeemScript if needed.
$json_inputs = json_encode(array(array('txid' => '4a2231e13182cdb64fa2f9aae38fca46549891e9dc15e8aaf484d82fc6e0a1d8', 'vout' => 0, 'scriptPubKey' => '76a914' . 'bf012bde5bd12eb7f9a66de5697b241b65a9a3c9' . '88ac')));
// build wallet from private key(s)
$wallet = array();
BIP32::bip32_keys_to_wallet($wallet, array($key), '00');
// Create raw transaction
$raw_transaction = RawTransaction::create($inputs, $outputs);
// Sign the transaction
$signed = RawTransaction::sign($wallet, $raw_transaction, $json_inputs);
print_r($signed);
echo "\n";
コード例 #3
0
 public function testValidInput()
 {
     $inputs = [["txid" => "5a373fd13679fc55f479f08bef25d5e808031f97331a48f950ced89d7e99c269", "vout" => 31, "scriptPubKey" => "76a914d17e062579b71bfe199a80991a253d929f8bd35b88ac"]];
     RawTransaction::create($inputs, ['15XjXdS1qTBy3i8vCCriWSAbm1qx5JgJVz' => 10000]);
     RawTransaction::create($inputs, ['15XjXdS1qTBy3i8vCCriWSAbm1qx5JgJVz' => BitcoinLib::toSatoshi(10000.0)]);
     RawTransaction::create($inputs, ['15XjXdS1qTBy3i8vCCriWSAbm1qx5JgJVz' => BitcoinLib::toSatoshi(10000000000.0)]);
 }
コード例 #4
0
 public function testSatoshiConversion()
 {
     $toSatoshi = [["0.00000001", "1", 1], [1.0E-8, "1", 1], ["0.29560000", "29560000", 29560000], [0.2956, "29560000", 29560000], ["1.0000009", "100000090", 100000090], [1.0000009, "100000090", 100000090], ["1.00000009", "100000009", 100000009], [1.00000009, "100000009", 100000009], ["21000000.00000001", "2100000000000001", 2100000000000001], [21000000.00000001, "2100000000000001", 2100000000000001], ["21000000.0000009", "2100000000000090", 2100000000000090], [21000000.0000009, "2100000000000090", 2100000000000090], ["21000000.00000009", "2100000000000009", 2100000000000009], [21000000.00000009, "2100000000000009", 2100000000000009], ["210000000.00000009", "21000000000000009", 21000000000000009], [210000000.0000001, "21000000000000009", 21000000000000009]];
     $toBTC = [["1", "0.00000001"], [1, "0.00000001"], ["29560000", "0.29560000"], [29560000, "0.29560000"], ["100000090", "1.00000090"], [100000090, "1.00000090"], ["100000009", "1.00000009"], [100000009, "1.00000009"], ["2100000000000001", "21000000.00000001"], [2100000000000001, "21000000.00000001"], ["2100000000000090", "21000000.00000090"], [2100000000000090, "21000000.00000090"], ["2100000000000009", "21000000.00000009"], [2100000000000009, "21000000.00000009"], ["21000000000000009", "210000000.00000009"], [21000000000000009, "210000000.00000009"], ["210000000000000009", "2100000000.00000009"], [210000000000000009, "2100000000.00000009"], ["2100000000000000009", "21000000000.00000009"], [2100000000000000009, "21000000000.00000009"]];
     foreach ($toSatoshi as $i => $test) {
         $btc = $test[0];
         $satoshiString = $test[1];
         $satoshiInt = $test[2];
         $string = BitcoinLib::toSatoshiString($btc);
         $this->assertEquals($satoshiString, $string, "[{$i}] {$btc} => {$satoshiString} =? {$string}");
         $this->assertTrue($satoshiString === $string, "[{$i}] {$btc} => {$satoshiString} ==? {$string}");
         $int = BitcoinLib::toSatoshi($btc);
         $this->assertEquals($satoshiInt, $int, "[{$i}] {$btc} => {$satoshiInt} =? {$int}");
         $this->assertTrue($satoshiInt === $int, "[{$i}] {$btc} => {$satoshiInt} ==? {$int}");
     }
     foreach ($toBTC as $i => $test) {
         $satoshi = $test[0];
         $btc = $test[1];
         $this->assertEquals($btc, BitcoinLib::toBTC($satoshi), "[{$i}] {$satoshi} => {$btc}");
         $this->assertTrue($btc === BitcoinLib::toBTC($satoshi), "[{$i}] {$satoshi} => {$btc}");
     }
 }
コード例 #5
0
 * address: mhsywR248h21gCB8oSwse5tmFSPvo9d5ML
 * priv:    cMps8Dg4Z1ThcwvPiPpshR6cbosYoTrgUwgLcFasBSxsdLHwzoUK
 * pub:     02ab1fae8dacd465460ad8e0c08cb9c25871782aa539a58b65f9bf1264c355d098
 *
 * address: mh7gsCxi4pcuNyHU9aWD9pGogHNJJZcCta
 * priv:    cNn72iUvQhuzZCWg3TC31fvyNDYttL8emHgMcFJzhF4xnFo8LYCk
 * pub:     02dc43b58ee5313d1969b939718d2c8104a3365d45f12f91753bfc950d16d3e82e
 *
 * 2of3 address: 2N1zEScjXeBDX2Gy4c6ojLTfqjRjSvf7iEC
 * 2of3 redeem:  522103c0b1fd07752ebdd43c75c0a60d67958eeac8d4f5245884477eae094c4361418d2102ab1fae8dacd465460ad8e0c08cb9c25871782aa539a58b65f9bf1264c355d0982102dc43b58ee5313d1969b939718d2c8104a3365d45f12f91753bfc950d16d3e82e53ae
 *
 * funded in TX: 83c5c88e94d9c518f314e30ca0529ab3f8e5e4f14a8936db4a32070005e3b61f
 */
$redeem_script = "522103c0b1fd07752ebdd43c75c0a60d67958eeac8d4f5245884477eae094c4361418d2102ab1fae8dacd465460ad8e0c08cb9c25871782aa539a58b65f9bf1264c355d0982102dc43b58ee5313d1969b939718d2c8104a3365d45f12f91753bfc950d16d3e82e53ae";
$inputs = array(array("txid" => "83c5c88e94d9c518f314e30ca0529ab3f8e5e4f14a8936db4a32070005e3b61f", "vout" => 0, "scriptPubKey" => "a9145fe34588f475c5251ff994eafb691a5ce197d18b87", "redeemScript" => $redeem_script));
$outputs = array("n3P94USXs7LzfF4BKJVyGv2uCfBQRbvMZJ" => BitcoinLib::toSatoshi(0.0001));
$raw_transaction = RawTransaction::create($inputs, $outputs);
/*
 * sign with first key
 */
$wallet = array();
RawTransaction::private_keys_to_wallet($wallet, array("cV2BRcdtWoZMSovYCpoY9gyvjiVK5xufpAwdAFk1jdonhGZq1cCm"));
RawTransaction::redeem_scripts_to_wallet($wallet, array($redeem_script));
$sign = RawTransaction::sign($wallet, $raw_transaction, json_encode($inputs));
print_r($sign);
var_dump(2 == $sign['req_sigs'], 1 == $sign['sign_count'], 'false' === $sign['complete']);
/*
 * sign with second key
 */
$wallet = array();
RawTransaction::private_keys_to_wallet($wallet, array("cMps8Dg4Z1ThcwvPiPpshR6cbosYoTrgUwgLcFasBSxsdLHwzoUK"));
コード例 #6
0
<?php

use BitWasp\BitcoinLib\BitcoinLib;
use BitWasp\BitcoinLib\RawTransaction;
require_once __DIR__ . '/../vendor/autoload.php';
/////////////////////////////
// Parameters for creation..
// Set up inputs here
$inputs = array(array('txid' => '6737e1355be0566c583eecd48bf8a5e1fcdf2d9f51cc7be82d4393ac9555611c', 'vout' => 0));
// Set up outputs here.
$outputs = array('1PGa6cMAzzrBpTtfvQTzX5PmUxsDiFzKyW' => BitcoinLib::toSatoshi(0.00015));
////////////////////////////
// Parameters for signing.
// Create JSON inputs parameter
// - These can come from bitcoind, or just knowledge of the txid/vout/scriptPubKey,
//   and redeemScript if needed.
$json_inputs = json_encode(array(array('txid' => '6737e1355be0566c583eecd48bf8a5e1fcdf2d9f51cc7be82d4393ac9555611c', 'vout' => 0, 'scriptPubKey' => '76a914' . '7e3f939e8ded8c0d93695310d6d481ae5da39616' . '88ac')));
// Private Key
$wallet = array();
RawTransaction::private_keys_to_wallet($wallet, array('L2V4QgXVUyWVoMGejTj7PrRUUCEi9D9Y1AhUM8E6f5yJm7gemgN6'), '00');
// Create raw transaction
$raw_transaction = RawTransaction::create($inputs, $outputs);
// Sign the transaction
//  To broadcast you would send the $sign['hex'] on to the network
//  eg; with `bitcoind sendrawtransaction <hex>`
$sign = RawTransaction::sign($wallet, $raw_transaction, $json_inputs);
print_r($sign);
echo "\n";
// Get the transaction hash from the raw transaction
$txid = RawTransaction::txid_from_raw($sign['hex']);
print_r($txid);