/**
  * @param $coinSymbol
  * @return \BitWasp\Bitcoin\Network\Network
  * @throws \Exception
  */
 public static function getNetwork($coinSymbol)
 {
     CoinSymbolValidator::validate($coinSymbol, 'coinSymbol');
     $network = null;
     switch ($coinSymbol) {
         case 'btc':
             $network = NetworkFactory::bitcoin();
             break;
         case 'btc-testnet':
             $network = NetworkFactory::bitcoinTestnet();
             break;
         case 'ltc':
             $network = NetworkFactory::litecoin();
             break;
         case 'doge':
             $network = NetworkFactory::create('1e', '16', '9e')->setHDPubByte('02fd3929')->setHDPrivByte('02fd3955')->setNetMagicBytes('c0c0c0c0');
             break;
         case 'uro':
             // NetMagicBytes: https://github.com/urocoin/uro/blob/319de97bbd56a10a3b2dca5b36be0c7a9c6603ae/src/main.cpp#L3233
             throw new \Exception("Unsupported coin symbol: {$coinSymbol}");
             break;
         case 'bcy':
             // TODO: check ef, 043587cf, 04358394, d9b4bef9 values
             // not used for the time being
             $network = NetworkFactory::create('1b', '1f', 'ef', true)->setHDPubByte('043587cf')->setHDPrivByte('04358394')->setNetMagicBytes('d9b4bef9');
             break;
         default:
             throw new \Exception("Unsupported coin symbol: {$coinSymbol} by php-client");
     }
     return $network;
 }
<?php

require "../vendor/autoload.php";
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\MessageSigner\MessageSigner;
use BitWasp\Bitcoin\Serializer\MessageSigner\SignedMessageSerializer;
use BitWasp\Bitcoin\Serializer\Signature\CompactSignatureSerializer;
Bitcoin::setNetwork(\BitWasp\Bitcoin\Network\NetworkFactory::bitcoinTestnet());
$address = 'n2Z2DFCxG6vktyX1MFkKAQPQFsrmniGKj5';
$sig = '-----BEGIN BITCOIN SIGNED MESSAGE-----
hi
-----BEGIN SIGNATURE-----
IBpGR29vEbbl4kmpK0fcDsT75GPeH2dg5O199D3iIkS3VcDoQahJMGJEDozXot8JGULWjN9Llq79aF+FogOoz/M=
-----END BITCOIN SIGNED MESSAGE-----';
$ec = Bitcoin::getEcAdapter();
$addr = \BitWasp\Bitcoin\Address\AddressFactory::fromString($address);
$serializer = new SignedMessageSerializer(new CompactSignatureSerializer(Bitcoin::getMath()));
$signedMessage = $serializer->parse($sig);
$signer = new MessageSigner($ec);
if ($signer->verify($signedMessage, $addr)) {
    echo "Signature verified!\n";
} else {
    echo "Failed to verify signature!\n";
}
示例#3
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();