/**
  * @return \BitWasp\Buffertools\Template
  */
 private function getTemplate()
 {
     return (new TemplateFactory())->uint32le()->vector(function (Parser &$parser) {
         return $this->inputSerializer->fromParser($parser);
     })->vector(function (Parser &$parser) {
         return $this->outputSerializer->fromParser($parser);
     })->uint32le()->getTemplate();
 }
Пример #2
0
 /**
  * @param Parser $parser
  * @return Transaction
  */
 public function fromParser(Parser $parser)
 {
     $math = Bitcoin::getMath();
     $int32le = new Int32($math, ByteOrder::LE);
     $uint32le = new Uint32($math, ByteOrder::LE);
     $varint = new VarInt($math, ByteOrder::LE);
     $version = $int32le->read($parser);
     $vin = [];
     $vinCount = $varint->read($parser);
     for ($i = 0; $i < $vinCount; $i++) {
         $vin[] = $this->inputSerializer->fromParser($parser);
     }
     $vout = [];
     $flags = 0;
     if (count($vin) == 0) {
         $flags = (int) $varint->read($parser);
         if ($flags != 0) {
             $vinCount = $varint->read($parser);
             for ($i = 0; $i < $vinCount; $i++) {
                 $vin[] = $this->inputSerializer->fromParser($parser);
             }
             $voutCount = $varint->read($parser);
             for ($i = 0; $i < $voutCount; $i++) {
                 $vout[] = $this->outputSerializer->fromParser($parser);
             }
         }
     } else {
         $voutCount = $varint->read($parser);
         for ($i = 0; $i < $voutCount; $i++) {
             $vout[] = $this->outputSerializer->fromParser($parser);
         }
     }
     $vwit = [];
     if ($flags & 1) {
         $flags ^= 1;
         $witCount = count($vin);
         for ($i = 0; $i < $witCount; $i++) {
             $vectorCount = $varint->read($parser);
             $vwit[] = $this->witnessSerializer->fromParser($parser, $vectorCount);
         }
     }
     if ($flags) {
         throw new \RuntimeException('Flags byte was 0');
     }
     $lockTime = $uint32le->read($parser);
     return new Transaction($version, new TransactionInputCollection($vin), new TransactionOutputCollection($vout), new TransactionWitnessCollection($vwit), $lockTime);
 }
Пример #3
0
 /**
  * @return Buffer
  */
 public function getBuffer()
 {
     $serializer = new TransactionInputSerializer();
     $out = $serializer->serialize($this);
     return $out;
 }