Esempio n. 1
0
<?php

use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Network\NetworkFactory;
use BitWasp\Bitcoin\Script\ScriptFactory;
use BitWasp\Bitcoin\Key\PrivateKeyFactory;
use BitWasp\Bitcoin\Rpc\RpcFactory;
use BitWasp\Bitcoin\Miner\Miner;
require __DIR__ . "/../vendor/autoload.php";
// init network (TESTNET)
Bitcoin::setNetwork(NetworkFactory::bitcoinTestnet());
// generate a privatekey so we can received the BTC
$privKey = PrivateKeyFactory::create(true);
var_dump($privKey->toWif());
// get latest block from RPC
$rpc = RpcFactory::bitcoind(getenv('BITCOINLIB_RPC_HOST') ?: 'localhost', "18332", getenv('BITCOINLIB_RPC_USER') ?: 'bitcoin', getenv('BITCOINLIB_RPC_PASSWORD') ?: 'YOUR_PASSWORD');
$latest = $rpc->getblock($rpc->getbestblockhash());
// mining in the future \o/
$timestamp = time() + 3600 * 2;
// create script to pay ourselves
$script = ScriptFactory::scriptPubKey()->payToPubKey($privKey->getPublicKey());
// init miner
$miner = new Miner(Bitcoin::getMath(), $latest->getHeader(), $script, null, $timestamp, 2, true);
// let's GO!
var_dump("mining!");
$block = $miner->run();
// result
var_dump($block->getHeader()->getBlockHash());
echo $block->getBuffer();
use BitWasp\Bitcoin\Key\PublicKeyInterface;
use BitWasp\Bitcoin\Transaction\TransactionFactory;
use BitWasp\Bitcoin\Script\ScriptFactory;
use BitWasp\Bitcoin\Key\PrivateKeyFactory;
use BitWasp\Bitcoin\Key\PublicKeyFactory;
use BitWasp\Bitcoin\Address\AddressFactory;
use BitWasp\Bitcoin\Rpc\RpcFactory;
$network = Bitcoin::getNetwork();
$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());
$network = Bitcoin::getNetwork();
$ecAdapter = Bitcoin::getEcAdapter();
$bitcoind = RpcFactory::bitcoind($host, $port, $user, $pass);
$privateKey1 = PrivateKeyFactory::fromHex('17a2209250b59f07a25b560aa09cb395a183eb260797c0396b82904f918518d5', true);
$privateKey2 = PrivateKeyFactory::fromHex('17a2209250b59f07a25b560aa09cb395a183eb260797c0396b82904f918518d6', true);
$redeemScript = ScriptFactory::multisig(2, array($privateKey1->getPublicKey(), $privateKey2->getPublicKey()));
$multisig = $redeemScript->getAddress();
echo "[P2SH address: " . $multisig->getAddress($network) . " ]\n";
echo "[private key order: " . implode(", ", array_map(function (PublicKeyInterface $publicKey) use($privateKey1, $privateKey2) {
    if ($publicKey->getBinary() == $privateKey1->getPublicKey()->getBinary()) {
        return "1";
    } else {
        return "2";
    }
}, $redeemScript->getKeys())) . "]\n";
$myTx = $bitcoind->getrawtransaction('6d4f5d2cce43660c29e03a794497da3f204312358ca6a6e47035ef916ce19db9', true);
$spendOutput = 0;
$recipient = AddressFactory::fromString('n1b2a9rFvuU9wBgBaoWngNvvMxRV94ke3x');
Esempio n. 3
0
use BitWasp\Bitcoin\Math\Math;
use BitWasp\Bitcoin\Network\NetworkFactory;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Script\OutputScriptFactory;
use BitWasp\Bitcoin\Script\ScriptFactory;
use BitWasp\Buffertools\Buffer;
use willgriffin\MariaInterface\MariaInterface;
$configFile = count($argv) > 1 ? $argv[1] : false;
$x = count($argv) > 2 ? intval($argv[2]) : 1;
$math = new Math();
$difficulty = new Difficulty($math);
if (file_exists($configFile)) {
    $config = (object) parse_ini_file($configFile);
    //$currency = Main::getCurrency($currencyName);
    $db = new MariaInterface(["host" => $config->dbhost, "user" => $config->dbuser, "pass" => $config->dbpass, "port" => $config->dbport, "name" => $config->dbname]);
    $bitcoind = RpcFactory::bitcoind($config->rpchost, $config->rpcport, $config->rpcuser, $config->rpcpass);
    $network = NetworkFactory::create($config->magic_byte, $config->magic_p2sh_byte, $config->private_key_byte)->setHDPubByte($config->hd_pub_byte)->setHDPrivByte($config->hd_priv_byte)->setNetMagicBytes($config->net_magic_bytes);
    Bitcoin::setNetwork($network);
    $nextBlockHash = $bitcoind->getblockhash($x);
    do {
        echo "Block {$x}\n";
        $blockhash = $nextBlockHash;
        $block = $bitcoind->getblock($blockhash);
        $blockHeader = $block->getHeader();
        $blockBits = $blockHeader->getBits();
        $blockTime = $blockHeader->getTimestamp();
        $nextBlockHash = $blockHeader->getNextBlock();
        $bvals = ['isiidsisdss', $blockHeader->getTimestamp(), $blockHeader->getBlockHash(), $block->getBuffer()->getSize(), $x, $blockHeader->getVersion(), $blockHeader->getMerkleRoot(), $blockHeader->getNonce(), $math->getCompact($blockBits), $difficulty->getDifficulty($blockBits), $blockHeader->getPrevBlock(), $nextBlockHash];
        $block_id = $db->value('select block_id from blocks where hash = ?', ['s', $blockhash]);
        if (!$block_id) {
            $bsql = "insert into blocks " . "(time, " . "hash, " . "size, " . "height, " . "version, " . "merkleroot, " . "nonce, " . "bits, " . "difficulty, " . "previousblockhash, " . "nextblockhash, " . "last_updated " . ") values (from_unixtime(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now())";