/** * 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; }
/** * @param string $message * @return \BitWasp\Buffertools\Buffer */ public function calculateMessageHash($message) { $content = new Buffer("Bitcoin Signed Message:\n" . Buffertools::numToVarInt(strlen($message))->getBinary() . $message); $hash = Hash::sha256d($content); return $hash; }
/** * @expectedException \Exception */ public function testNumToVarIntOutOfRange() { // Check that this is out of range (PHP's fault) $decimal = EccFactory::getAdapter()->pow(2, 32) + 1; Buffertools::numToVarInt($decimal); }
/** * 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; }
/** * @param string $message * @return BufferInterface * @throws \Exception */ private function calculateBody($message) { return new Buffer("Bitcoin Signed Message:\n" . Buffertools::numToVarInt(strlen($message))->getBinary() . $message, null, $this->ecAdapter->getMath()); }