/** * @param PublicKeyInterface $publicKey * @return Buffer */ public function serialize(PublicKeyInterface $publicKey) { $point = $publicKey->getPoint(); $parser = new Parser(); $parser->writeBytes(1, $this->getPrefix($publicKey->isCompressed(), $point)); $math = $this->ecAdapter->getMath(); $publicKey->isCompressed() ? $parser->writeBytes(32, $math->decHex($point->getX())) : $parser->writeBytes(32, $math->decHex($point->getX()))->writeBytes(32, $math->decHex($point->getY())); return $parser->getBuffer(); }
/** * @param PublicKey $publicKey * @return Buffer */ private function doSerialize(PublicKey $publicKey) { $math = $this->ecAdapter->getMath(); $point = $publicKey->getPoint(); $compressed = $publicKey->isCompressed(); $parser = new Parser('', $math); $parser->writeBytes(1, $this->getPrefix($compressed, $point)); $compressed ? $parser->writeBytes(32, Buffer::int($point->getX(), null, $math)) : $parser->writeBytes(32, Buffer::int($point->getX(), null, $math))->writeBytes(32, Buffer::int($point->getY(), null, $math)); return $parser->getBuffer(); }
/** * @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(); }
/** * @param TransactionSignature $txSig * @return \BitWasp\Buffertools\Buffer */ public function serialize(TransactionSignature $txSig) { $sig = $this->sigSerializer->serialize($txSig->getSignature()); $parser = new Parser($sig->getHex()); $parser->writeBytes(1, Buffer::int($txSig->getHashType(), 1)); $buffer = $parser->getBuffer(); return $buffer; }
/** * @param Headers $msg * @return \BitWasp\Buffertools\Buffer */ public function serialize(Headers $msg) { $headers = []; $null = new Buffer(""); foreach ($msg->getHeaders() as $header) { $temp = new Parser($header->getBuffer()); $temp->writeBytes(1, $null); $headers[] = $temp->getBuffer(); } return $this->getTemplate()->write([$headers]); }
/** * @param AlertDetail $detail * @return \BitWasp\Buffertools\Buffer */ public function serialize(AlertDetail $detail) { $setCancels = []; foreach ($detail->getSetCancel() as $toCancel) { $t = new Parser(); $setCancels[] = $t->writeBytes(4, Buffer::int($toCancel), true)->getBuffer(); } $setSubVers = []; foreach ($detail->getSetSubVer() as $subVer) { $t = new Parser(); $setSubVers[] = $t->writeBytes(4, Buffer::int($subVer), true)->getBuffer(); } return $this->getTemplate()->write([$detail->getVersion(), $detail->getRelayUntil(), $detail->getExpiration(), $detail->getId(), $detail->getCancel(), $setCancels, $detail->getMinVer(), $detail->getMaxVer(), $setSubVers, $detail->getPriority(), $detail->getComment(), $detail->getStatusBar()]); }
/** * Push data into the stack. * * @param $data * @return $this * @throws \Exception */ public function push(Buffer $data) { $length = $data->getSize(); $parsed = new Parser('', $this->math); /** Note that larger integers are serialized without flipping bits - Big endian */ if ($length < $this->opcodes->getOpByName('OP_PUSHDATA1')) { $varInt = Buffertools::numToVarInt($length); $data = new Buffer($varInt->getBinary() . $data->getBinary(), null, $this->math); $parsed->writeBytes($data->getSize(), $data); } else { if ($length <= 0xff) { $lengthSize = 1; } elseif ($length <= 0xffff) { $lengthSize = 2; } else { $lengthSize = 4; } $op = $this->opcodes->getOpByName('OP_PUSHDATA' . $lengthSize); $parsed->writeBytes(1, Buffer::int($op))->writeBytes($lengthSize, Buffer::int($length), true)->writeBytes($length, $data); } $this->script .= $parsed->getBuffer()->getBinary(); return $this; }
/** * {@inheritdoc} * @see \BitWasp\Bitcoin\Block\BlockHeaderInterface::getBlockHash() */ public function getBlockHash() { $parser = new Parser(); return $parser->writeBytes(32, Hash::sha256d($this->getBuffer()), true)->getBuffer()->getHex(); }
/** * Create a buffer containing data to be hashed hashed to yield the child offset * * @param integer|string $sequence * @return Buffer * @throws \Exception */ public function getHmacSeed($sequence) { $parser = new Parser(); $hardened = $this->ecAdapter->getMath()->getBinaryMath()->isNegative($sequence, 32); if ($hardened) { if ($this->isPrivate() === false) { throw new \Exception("Can't derive a hardened key without the private key"); } $parser->writeBytes(1, '00')->writeBytes(32, $this->getPrivateKey()->getBuffer()); } else { $parser->writeBytes(33, $this->getPublicKey()->getBuffer()); } return $parser->writeInt(4, $sequence)->getBuffer(); }
/** * Take an array containing serializable objects. * @param SerializableInterface []|Buffer[] * @return $this */ public function writeArray($serializable) { $parser = new Parser(Buffertools::numToVarInt(count($serializable)), $this->math); foreach ($serializable as $object) { if ($object instanceof SerializableInterface) { $object = $object->getBuffer(); } if ($object instanceof Buffer) { $parser->writeBytes($object->getSize(), $object); } else { throw new \RuntimeException('Input to writeArray must be Buffer[], or SerializableInterface[]'); } } $this->string .= $parser->getBuffer()->getBinary(); return $this; }
public function testWriteBytesFlipPadded() { $parser = new Parser(); $parser->writeBytes(4, Buffer::hex('34'), true); $this->assertEquals("34000000", $parser->getBuffer()->getHex()); }