本文整理汇总了PHP中BitWasp\Buffertools\Buffer::hex方法的典型用法代码示例。如果您正苦于以下问题:PHP Buffer::hex方法的具体用法?PHP Buffer::hex怎么用?PHP Buffer::hex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitWasp\Buffertools\Buffer
的用法示例。
在下文中一共展示了Buffer::hex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testMethods
public function testMethods()
{
$loop = new \React\EventLoop\StreamSelectLoop();
$dns = (new \BitWasp\Bitcoin\Networking\Dns\Factory())->create('8.8.8.8', $loop);
$network = Bitcoin::getDefaultNetwork();
$random = new Random();
$messages = new \BitWasp\Bitcoin\Networking\Messages\Factory($network, $random);
$factory = new Factory($dns, $messages, $loop);
$this->assertInstanceOf($this->peerType, $factory->getPeer());
$services = Buffer::hex('00', 8);
$address = $factory->getAddress('127.0.0.1', 8332, $services);
$this->assertInstanceOf($this->addrType, $address);
$connector = $factory->getConnector();
$this->assertInstanceOf($this->connType, $connector);
$server = $factory->getServer();
$this->assertInstanceOf($this->serverType, $server);
$locator = $factory->getLocator();
$this->assertInstanceOf($this->locatorType, $locator);
$listener = $factory->getListener($server);
$this->assertInstanceOf($this->listenerType, $listener);
$handler = $factory->getPacketHandler();
$this->assertInstanceOf($this->handlerType, $handler);
$manager = $factory->getManager();
$this->assertInstanceOf($this->managerType, $manager);
}
示例2: __construct
public function __construct()
{
parent::__construct(function () {
$consensus = new ConsensusFactory(Bitcoin::getEcAdapter());
$loop = LoopFactory::create();
$context = new ZmqContext($loop);
$control = $context->getSocket(\ZMQ::SOCKET_SUB);
$control->connect('tcp://127.0.0.1:5594');
$control->subscribe('control');
$control->on('messages', function ($msg) use($loop) {
if ($msg[1] == 'shutdown') {
$loop->stop();
}
});
$results = $context->getSocket(\ZMQ::SOCKET_PUSH);
$results->connect("tcp://127.0.0.1:5593");
$workers = $context->getSocket(\ZMQ::SOCKET_PULL);
$workers->connect('tcp://127.0.0.1:5592');
$workers->on('message', function ($message) use($consensus, $results) {
$details = json_decode($message, true);
$txid = $details['txid'];
$flags = $details['flags'];
$vin = $details['vin'];
$scriptPubKey = new Script(Buffer::hex($details['scriptPubKey']));
$tx = TransactionFactory::fromHex($details['tx']);
$results->send(json_encode(['txid' => $txid, 'vin' => $vin, 'result' => $consensus->getConsensus(new Flags($flags))->verify($tx, $scriptPubKey, $vin)]));
});
$loop->run();
exit(0);
});
}
示例3: queryDnsSeeds
/**
* Connect to $numSeeds DNS seeds
*
* @param int $numSeeds
* @return \React\Promise\Promise|\React\Promise\PromiseInterface
*/
public function queryDnsSeeds($numSeeds = 1)
{
$peerList = new Deferred();
// Take $numSeeds
$seedHosts = self::dnsSeedHosts();
$seeds = array_slice($seedHosts, 0, min($numSeeds, count($seedHosts)));
// Connect to $numSeeds peers
/** @var Peer[] $vNetAddr */
$vNetAddr = [];
foreach ($seeds as $seed) {
echo " [ query DNS seed: " . $seed . " ] \n";
$this->dns->resolve($seed)->then(function ($ipList) use(&$vNetAddr, $peerList, &$numSeeds) {
$vNetAddr[] = $ipList;
if (count($vNetAddr) == $numSeeds) {
$peerList->resolve($vNetAddr);
}
});
}
// Compile the list of lists of peers into $this->knownAddresses
return $peerList->promise()->then(function (array $vPeerVAddrs) {
shuffle($vPeerVAddrs);
/** @var NetworkAddressInterface[] $addresses */
$addresses = [];
array_map(function (array $value) use(&$addresses) {
foreach ($value as $ip) {
$addresses[] = new NetworkAddress(Buffer::hex('01', 8), $ip, 8333);
}
}, $vPeerVAddrs);
$this->knownAddresses = array_merge($this->knownAddresses, $addresses);
return $this;
});
}
示例4: testPeer
public function testPeer()
{
$localhost = '127.0.0.1';
$localport = '8333';
$remotehost = '127.0.0.1';
$remoteport = '9999';
$loop = new StreamSelectLoop();
$dnsResolverFactory = new \React\Dns\Resolver\Factory();
$dns = $dnsResolverFactory->createCached('8.8.8.8', $loop);
$reactServer = new Server($loop);
$network = Bitcoin::getDefaultNetwork();
$client = new NetworkAddress(Buffer::hex('0000000000000001'), $localhost, $localport);
$server = new NetworkAddress(Buffer::hex('0000000000000001'), $remotehost, $remoteport);
$msgs = new Factory($network, new Random());
$serverReceivedConnection = false;
$serverListener = new Listener($server, $msgs, $reactServer, $loop);
$serverListener->on('connection', function (Peer $peer) use(&$serverReceivedConnection, &$serverListener) {
$peer->close();
$serverReceivedConnection = true;
});
$serverListener->listen($server->getPort());
$connector = new Connector($loop, $dns);
$clientConnection = new Peer($client, $msgs, $loop);
$clientConnection->connect($connector, $server)->then(function (Peer $peer) use($serverListener, &$loop) {
$peer->close();
$serverListener->close();
});
$loop->run();
$this->assertTrue($serverReceivedConnection);
}
示例5: composeSend
/**
* Composes a send transaction
* @param string $asset A counterparty asset name or BTC
* @param mixed $quantity Quantity of asset to send. Accepts a float or a Tokenly\CounterpartyTransactionComposer\Quantity or Tokenly\CryptoQuantity\CryptoQuantity object. Use a Quantity object for indivisible assets.
* @param mixed $destination A single destination bitcoin address. For BTC sends an array of [[address, amount], [address, amount]] is also allowed. Amounts should be float values.
* @param string $private_key_wif The private key in ASCII WIF format. This can be null to compose an unsigned transaction.
* @param array $utxos An array of UTXOs. Each UTXO should be ['txid' => txid, 'n' => n, 'amount' => amount (in satoshis), 'script' => script hexadecimal string]
* @param mixed $change_address_collection a single address string to receive all change. Or an array of [[address, amount], [address, amount], [address]]. Amounts should be float values. An address with no amount for the last entry will send the remaining change to that address.
* @param float $fee A fee
* @param float $btc_dust Amount of BTC dust to send with the Counterparty transaction.
* @return Array returns a ComposedTransaction object
*/
public function composeSend($asset, $quantity, $destination, $private_key_wif, $utxos, $change_address_collection = null, $fee = null, $btc_dust = null)
{
if ($asset == 'BTC') {
return $this->composeBTCSend($quantity, $destination, $private_key_wif, $utxos, $change_address_collection, $fee);
}
$fee_satoshis = $fee === null ? self::DEFAULT_FEE : intval(round($fee * self::SATOSHI));
$btc_dust_satoshis = $btc_dust === null ? self::DEFAULT_BTC_DUST : intval(round($btc_dust * self::SATOSHI));
// get total and change amount
$change_amounts = $this->calculateAndValidateChange($utxos, $btc_dust_satoshis, $fee_satoshis, $change_address_collection);
$tx_builder = TransactionFactory::build();
// add the UTXO inputs
$transaction_outputs = $this->addInputsAndReturnPreviousOutputs($utxos, $tx_builder);
// pay the btc_dust to the destination
if (is_array($destination)) {
throw new Exception("Multiple destinations are not supported for counterparty sends", 1);
}
$tx_builder->payToAddress($btc_dust_satoshis, AddressFactory::fromString($destination));
// build the OP_RETURN script
$op_return_builder = new OpReturnBuilder();
$op_return = $op_return_builder->buildOpReturn($quantity, $asset, $utxos[0]['txid']);
$script = ScriptFactory::create()->op('OP_RETURN')->push(Buffer::hex($op_return, 28))->getScript();
$tx_builder->output(0, $script);
// pay the change to self
$this->payChange($change_amounts, $tx_builder);
// sign
if ($private_key_wif !== null) {
$signed_transaction = $this->signTx($private_key_wif, $tx_builder, $transaction_outputs);
return $this->buildReturnValuesFromTransactionAndInputs($signed_transaction, $utxos, true);
}
return $this->buildReturnValuesFromTransactionAndInputs($tx_builder->get(), $utxos, false);
}
示例6: fromHex
/**
* @param string $hex
* @param bool $compressed
* @param EcAdapterInterface|null $ecAdapter
* @return PrivateKey
*/
public static function fromHex($hex, $compressed = false, EcAdapterInterface $ecAdapter = null)
{
$hex = Buffer::hex($hex);
$ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
$hexSerializer = new HexPrivateKeySerializer($ecAdapter);
return $hexSerializer->parse($hex)->setCompressed($compressed);
}
示例7: getGenesisBlock
/**
* @return \BitWasp\Bitcoin\Block\BlockInterface
*/
public function getGenesisBlock()
{
$timestamp = new Buffer('The Times 03/Jan/2009 Chancellor on brink of second bailout for banks', null, $this->math);
$publicKey = Buffer::hex('04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f', null, $this->math);
$inputScript = ScriptFactory::sequence([Buffer::int('486604799', 4, $this->math)->flip(), Buffer::int('4', null, $this->math), $timestamp]);
$outputScript = ScriptFactory::sequence([$publicKey, Opcodes::OP_CHECKSIG]);
return new Block($this->math, $this->getGenesisBlockHeader(), new TransactionCollection([(new TxBuilder())->version('1')->input(new Buffer('', 32), 4294967295.0, $inputScript)->output(5000000000.0, $outputScript)->locktime(0)->get()]));
}
示例8: testHashIsSerializedInReverseOrder
public function testHashIsSerializedInReverseOrder()
{
$buffer = Buffer::hex('0001020300010203000102030001020300010203000102030001020300010203');
$inv = Inventory::block($buffer);
$results = unpack("Vtype/H64hash", $inv->getBinary());
$parsedBuffer = Buffer::hex($results['hash']);
$this->assertEquals($buffer->getHex(), Buffertools::flipBytes($parsedBuffer)->getHex());
}
示例9: serialize
/**
* @param BlockInterface $block
* @return \BitWasp\Buffertools\BufferInterface
*/
public function serialize(BlockInterface $block)
{
$buffer = $block->getBuffer();
$size = $buffer->getSize();
$data = new Parser($this->getHeaderTemplate()->write([Buffer::hex($this->network->getNetMagicBytes()), $size]));
$data->writeBytes($size, $buffer);
return $data->getBuffer();
}
示例10: execute
/**
* @param NodeInterface $node
* @param array $params
* @return array
*/
public function execute(NodeInterface $node, array $params)
{
if (strlen($params[self::PARAM_HASH]) !== 64) {
throw new \RuntimeException('Invalid hash');
}
$index = $node->chain()->fetchIndex(Buffer::hex($params[self::PARAM_HASH]));
return ['header' => $this->convertIndexToArray($index)];
}
示例11: getIpBuffer
/**
* @param string $ip
* @return Buffer
*/
private function getIpBuffer($ip)
{
$hex = (string) dechex(ip2long($ip));
$hex = strlen($hex) % 2 == 1 ? '0' . $hex : $hex;
$hex = '00000000000000000000' . 'ffff' . $hex;
$buffer = Buffer::hex($hex);
return $buffer;
}
示例12: parse
/**
* @param $hex
* @return PublicKey
* @throws \Exception
*/
public function parse($hex)
{
$hex = Buffer::hex($hex);
if (!in_array($hex->getSize(), [PublicKey::LENGTH_COMPRESSED, PublicKey::LENGTH_UNCOMPRESSED])) {
throw new \Exception('Invalid hex string, must match size of compressed or uncompressed public key');
}
return $this->ecAdapter->publicKeyFromBuffer($hex);
}
示例13: read
/**
* @param Parser $parser
* @return Buffer
* @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
*/
public function read(Parser &$parser)
{
$bits = $this->readBits($parser->readBytes($this->length));
if (!$this->isBigEndian()) {
$bits = $this->flipBits($bits);
}
return Buffer::hex(str_pad($this->getMath()->baseConvert($bits, 2, 16), $this->length * 2, '0', STR_PAD_LEFT), $this->length, $this->getMath());
}
示例14: serialize
/**
* @param NetworkInterface $network
* @param PrivateKeyInterface $privateKey
* @return string
*/
public function serialize(NetworkInterface $network, PrivateKeyInterface $privateKey)
{
$serialized = Buffertools::concat(Buffer::hex($network->getPrivByte()), $this->hexSerializer->serialize($privateKey));
if ($privateKey->isCompressed()) {
$serialized = Buffertools::concat($serialized, new Buffer("", 1));
}
return Base58::encodeCheck($serialized);
}
示例15: getAddress
/**
* @param NetworkInterface|null $network
* @return string
*/
public function getAddress(NetworkInterface $network = null)
{
$network = $network ?: Bitcoin::getNetwork();
$witnessByte = dechex($this->witnessVersion);
$witnessByte = strlen($witnessByte) % 2 == 0 ? $witnessByte : '0' . $witnessByte;
$payload = Buffer::hex($this->getPrefixByte($network) . $witnessByte . "00" . $this->getHash());
return Base58::encodeCheck($payload);
}