<?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"; }
<?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();
<?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;
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); } else {