/** * @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; }
public function testMerkleBlock() { $factory = new Factory(NetworkFactory::bitcoin(), new Random()); $hex = '0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d630101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08044c86041b020a02ffffffff0100f2052a01000000434104ecd3229b0571c3be876feaac0442a9f13c5a572742927af1dc623353ecf8c202225f64868137a18cdd85cbbb4c74fbccfd4f49639cf1bdc94a5672bb15ad5d4cac00000000'; $block = BlockFactory::fromHex($hex); $math = new Math(); $filter = BloomFilter::create($math, 10, 1.0E-6, 0, new Flags(BloomFilter::UPDATE_ALL)); $filter->insertHash('63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5'); // Check that Merkleblock message is serialized correctly $filtered = $block->filter($filter); $this->assertEquals($block->getHeader(), $filtered->getHeader()); $merkle = $factory->merkleblock($filtered); $serialized = $merkle->getNetworkMessage()->getBuffer(); $parsed = $factory->parse(new Parser($serialized))->getPayload(); $this->assertEquals($merkle, $parsed); }
<?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"; }
public static function getDefaultNetwork() { return NetworkFactory::bitcoin(); }
<?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;
<?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\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())"; $block_id = $db->insert($bsql, $bvals);
/** * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure * @expectedException \RuntimeException * @expectedExceptionMessage */ public function testInvalidBytes() { $v = '60002'; $services = Buffer::hex('0000000000000001'); $time = '123456789'; $recipient = new NetworkAddress(Buffer::hex('0000000000000001'), '10.0.0.1', '8332'); $sender = new NetworkAddress(Buffer::hex('0000000000000001'), '10.0.0.2', '8332'); $userAgent = new Buffer("/Satoshi:0.7.2/"); $lastBlock = '212672'; $random = new Random(); $nonce = $random->bytes(8)->getInt(); $version = new Version($v, $services, $time, $recipient, $sender, $nonce, $userAgent, $lastBlock, true); $bitcoin = new NetworkMessageSerializer(NetworkFactory::bitcoin()); $serialized = $version->getNetworkMessage(NetworkFactory::viacoinTestnet())->getBuffer(); $bitcoin->parse($serialized); }