本文整理汇总了PHP中BitWasp\Buffertools\Buffer类的典型用法代码示例。如果您正苦于以下问题:PHP Buffer类的具体用法?PHP Buffer怎么用?PHP Buffer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Buffer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* @param int $type
* @param Buffer $hash
*/
public function __construct($type, Buffer $hash)
{
if (false === $this->checkType($type)) {
throw new \InvalidArgumentException('Invalid type in InventoryVector');
}
if (false === (32 === $hash->getSize())) {
throw new \InvalidArgumentException('Hash size must be 32 bytes');
}
$this->type = $type;
$this->hash = $hash;
}
示例2: __construct
/**
* @param int|string $version
* @param Buffer $prevBlock
* @param Buffer $merkleRoot
* @param int|string $timestamp
* @param Buffer $bits
* @param int|string $nonce
*/
public function __construct($version, Buffer $prevBlock, Buffer $merkleRoot, $timestamp, Buffer $bits, $nonce)
{
if (!is_numeric($version)) {
throw new \InvalidArgumentException('Block header version must be numeric');
}
if ($prevBlock->getSize() !== 32) {
throw new \InvalidArgumentException('Block header prevBlock must be a 32-byte Buffer');
}
if ($merkleRoot->getSize() !== 32) {
throw new \InvalidArgumentException('Block header prevBlock must be a 32-byte Buffer');
}
if (!is_numeric($timestamp)) {
throw new \InvalidArgumentException('Block header timestamp must be numeric');
}
if (!is_numeric($nonce)) {
throw new \InvalidArgumentException('Block header nonce must be numeric');
}
$this->version = $version;
$this->prevBlock = $prevBlock;
$this->merkleRoot = $merkleRoot;
$this->timestamp = $timestamp;
$this->bits = $bits;
$this->nonce = $nonce;
$this->initFunctionAlias('version', 'getVersion')->initFunctionAlias('prevBlock', 'getPrevBlock')->initFunctionAlias('merkleRoot', 'getMerkleRoot')->initFunctionAlias('timestamp', 'getTimestamp')->initFunctionAlias('bits', 'getBits')->initFunctionAlias('nonce', 'getNonce');
}
示例3: parseIpBuffer
/**
* @param Buffer $ip
* @return string
* @throws \Exception
*/
private function parseIpBuffer(Buffer $ip)
{
$end = $ip->slice(12, 4);
return implode(".", array_map(function ($int) {
return unpack("C", $int)[1];
}, str_split($end->getBinary(), 1)));
}
示例4: unpackCompact
/**
* @param Buffer $bits
* @return array
*/
public function unpackCompact(Buffer $bits)
{
$bitStr = $bits->getBinary();
// Unpack and decode
$sci = array_map(function ($value) {
return $this->hexDec($value);
}, unpack('H2exp/H6mul', $bitStr));
return $sci;
}
示例5: __construct
/**
* OutPoint constructor.
* @param Buffer $hashPrevOutput
* @param int $nPrevOutput
*/
public function __construct(Buffer $hashPrevOutput, $nPrevOutput)
{
if ($hashPrevOutput->getSize() !== 32) {
throw new \InvalidArgumentException('OutPoint: hashPrevOut must be a 32-byte Buffer');
}
if (!is_numeric($nPrevOutput)) {
throw new \InvalidArgumentException('OutPoint: nPrevOut must be numeric');
}
$this->hashPrevOutput = $hashPrevOutput;
$this->nPrevOutput = $nPrevOutput;
$this->initFunctionAlias('txid', 'getTxId')->initFunctionAlias('vout', 'getVout');
}
示例6: check
/**
* @param Buffer $hash
* @param int|string $nBits
* @return bool
*/
public function check(Buffer $hash, $nBits)
{
$negative = false;
$overflow = false;
$target = $this->math->writeCompact($nBits, $negative, $overflow);
if ($negative || $overflow || $this->math->cmp($target, 0) === 0 || $this->math->cmp($target, $this->getMaxTarget()) > 0) {
throw new \RuntimeException('nBits below minimum work');
}
if ($this->math->cmp($hash->getInt(), $target) > 0) {
throw new \RuntimeException("Hash doesn't match nBits");
}
return true;
}
示例7: generateMasterKey
/**
* Generate a master private key given a
* @param Buffer $seed
* @param EcAdapterInterface $ecAdapter
* @return ElectrumKey
*/
public static function generateMasterKey(Buffer $seed, EcAdapterInterface $ecAdapter = null)
{
// Really weird, did electrum actually hash hex string seeds?
$seed = $oldseed = $seed->getHex();
// Perform sha256 hash 5 times per iteration
for ($i = 0; $i < 5 * 20000; $i++) {
// Hash should return binary data
$seed = hash('sha256', $seed . $oldseed, true);
}
// Convert binary data to hex.
$str = new Buffer($seed);
return self::fromSecretExponent($str->getInt(), $ecAdapter ?: Bitcoin::getEcAdapter());
}
示例8: entropyToWords
/**
* @param Buffer $entropy
* @return array
*/
public function entropyToWords(Buffer $entropy)
{
$math = $this->ecAdapter->getMath();
$ENT = $entropy->getSize() * 8;
$CS = $ENT / 32;
$entBits = $math->baseConvert($entropy->getHex(), 16, 2);
$csBits = $this->calculateChecksum($entropy, $CS);
$bits = str_pad($entBits . $csBits, $ENT + $CS, '0', STR_PAD_LEFT);
$result = [];
foreach (str_split($bits, 11) as $bit) {
$idx = $math->baseConvert($bit, 2, 10);
$result[] = $this->wordList->getWord($idx);
}
return $result;
}
示例9: 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);
}
示例10: 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);
}
示例11: 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);
}
示例12: __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);
});
}
示例13: 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;
});
}
示例14: 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);
}
示例15: isDERSignature
/**
* @param \BitWasp\Buffertools\Buffer $sig
* @return bool
* @throws SignatureNotCanonical
*/
public static function isDERSignature(Buffer $sig)
{
$checkVal = function ($fieldName, $start, $length, $binaryString) {
if ($length == 0) {
throw new SignatureNotCanonical('Signature ' . $fieldName . ' length is zero');
}
$typePrefix = ord(substr($binaryString, $start - 2, 1));
if ($typePrefix !== 0x2) {
throw new SignatureNotCanonical('Signature ' . $fieldName . ' value type mismatch');
}
$val = substr($binaryString, $start, $length);
$vAnd = $val[0] & pack("H*", '80');
if (ord($vAnd) === 128) {
throw new SignatureNotCanonical('Signature ' . $fieldName . ' value is negative');
}
if ($length > 1 && ord($val[0]) == 0x0 && !ord($val[1] & pack('H*', '80'))) {
throw new SignatureNotCanonical('Signature ' . $fieldName . ' value excessively padded');
}
};
$bin = $sig->getBinary();
$size = $sig->getSize();
if ($size < 9) {
throw new SignatureNotCanonical('Signature too short');
}
if ($size > 73) {
throw new SignatureNotCanonical('Signature too long');
}
if (ord($bin[0]) !== 0x30) {
throw new SignatureNotCanonical('Signature has wrong type');
}
if (ord($bin[1]) !== $size - 3) {
throw new SignatureNotCanonical('Signature has wrong length marker');
}
$lenR = ord($bin[3]);
$startR = 4;
if (5 + $lenR >= $size) {
throw new SignatureNotCanonical('Signature S length misplaced');
}
$lenS = ord($bin[5 + $lenR]);
$startS = 4 + $lenR + 2;
if ($lenR + $lenS + 7 !== $size) {
throw new SignatureNotCanonical('Signature R+S length mismatch');
}
$checkVal('R', $startR, $lenR, $bin);
$checkVal('S', $startS, $lenS, $bin);
return true;
}