Exemplo n.º 1
0
 /**
  * @param \BitWasp\Buffertools\Buffer|string $string
  * @param EcAdapterInterface $ecAdapter
  * @return SignatureInterface
  */
 public static function fromHex($string, EcAdapterInterface $ecAdapter = null)
 {
     $ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
     $serializer = EcSerializer::getSerializer($ecAdapter, 'BitWasp\\Bitcoin\\Crypto\\EcAdapter\\Serializer\\Signature\\DerSignatureSerializerInterface');
     /** @var DerSignatureSerializerInterface $serializer */
     return $serializer->parse($string);
 }
Exemplo n.º 2
0
 /**
  * @param Parser $parser
  * @return Alert
  */
 public function fromParser(Parser $parser)
 {
     $detail = $this->detail->fromParser($parser);
     list($sigBuffer) = $this->getSigBuf()->parse($parser);
     $adapter = Bitcoin::getEcAdapter();
     $serializer = EcSerializer::getSerializer('BitWasp\\Bitcoin\\Crypto\\EcAdapter\\Serializer\\Signature\\DerSignatureSerializerInterface', true, $adapter);
     $sig = $serializer->parse($sigBuffer);
     return new Alert($detail, $sig);
 }
Exemplo n.º 3
0
 /**
  * @param \BitWasp\Buffertools\BufferInterface|string $hex
  * @param bool $compressed
  * @param EcAdapterInterface|null $ecAdapter
  * @return PrivateKey
  */
 public static function fromHex($hex, $compressed = false, EcAdapterInterface $ecAdapter = null)
 {
     $ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
     /** @var PrivateKeySerializerInterface $serializer */
     $serializer = EcSerializer::getSerializer($ecAdapter, 'BitWasp\\Bitcoin\\Crypto\\EcAdapter\\Serializer\\Key\\PrivateKeySerializerInterface');
     $parsed = $serializer->parse($hex);
     if ($compressed) {
         $parsed = $ecAdapter->getPrivateKey($parsed->getSecretMultiplier(), $compressed);
     }
     return $parsed;
 }
Exemplo n.º 4
0
 /**
  * @param Parser $parser
  * @return Alert
  */
 public function fromParser(Parser &$parser)
 {
     $detail = $this->detail->fromParser($parser);
     list($sigR, $sigS) = $this->getSigTemplate()->parse($parser);
     $adapter = Bitcoin::getEcAdapter();
     if (!$adapter instanceof \BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Adapter\EcAdapter) {
         // We need to serialize this as DER, and deserialize it using the correct serializer...
         $temporary = new \BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Adapter\EcAdapter($adapter->getMath(), $adapter->getGenerator());
         $sig = new \BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Signature\Signature($temporary, $sigR, $sigS);
         $serializer = new \BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Serializer\Signature\DerSignatureSerializer($temporary);
         $serialized = $serializer->serialize($sig);
         // Parse using native EcAdapter
         /** @var \BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Signature\DerSignatureSerializerInterface $serializer */
         $serializer = EcSerializer::getSerializer($adapter, 'BitWasp\\Bitcoin\\Crypto\\EcAdapter\\Serializer\\Signature\\DerSignatureSerializerInterface');
         $sig = $serializer->parse($serialized);
     } else {
         $sig = new \BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Signature\Signature($adapter, $sigR, $sigS);
     }
     return new Alert($detail, $sig);
 }
Exemplo n.º 5
0
 /**
  * @param EcAdapterInterface $ecAdapter
  * @return PublicKeySerializerInterface
  */
 public static function getSerializer(EcAdapterInterface $ecAdapter = null)
 {
     return EcSerializer::getSerializer($ecAdapter ?: Bitcoin::getEcAdapter(), 'BitWasp\\Bitcoin\\Crypto\\EcAdapter\\Serializer\\Key\\PublicKeySerializerInterface');
 }
Exemplo n.º 6
0
 /**
  * @return BufferInterface
  */
 public function getBuffer()
 {
     $txSigSerializer = new TransactionSignatureSerializer(EcSerializer::getSerializer($this->ecAdapter, 'BitWasp\\Bitcoin\\Crypto\\EcAdapter\\Serializer\\Signature\\DerSignatureSerializerInterface'));
     return $txSigSerializer->serialize($this);
 }
Exemplo n.º 7
0
 /**
  * @return \BitWasp\Buffertools\Buffer
  */
 public function getBuffer()
 {
     $serializer = new SignedMessageSerializer(EcSerializer::getSerializer(Bitcoin::getEcAdapter(), 'BitWasp\\Bitcoin\\Crypto\\EcAdapter\\Serializer\\Signature\\CompactSignatureSerializerInterface'));
     return $serializer->serialize($this);
 }
Exemplo n.º 8
0
<?php

require "../vendor/autoload.php";
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\MessageSigner\MessageSigner;
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Signature\CompactSignatureSerializerInterface;
Bitcoin::setNetwork(\BitWasp\Bitcoin\Network\NetworkFactory::bitcoinTestnet());
$address = 'n2Z2DFCxG6vktyX1MFkKAQPQFsrmniGKj5';
$sig = '-----BEGIN BITCOIN SIGNED MESSAGE-----
hi
-----BEGIN SIGNATURE-----
IBpGR29vEbbl4kmpK0fcDsT75GPeH2dg5O199D3iIkS3VcDoQahJMGJEDozXot8JGULWjN9Llq79aF+FogOoz/M=
-----END BITCOIN SIGNED MESSAGE-----';
$ec = Bitcoin::getEcAdapter();
/** @var PayToPubKeyHashAddress $addr */
$addr = \BitWasp\Bitcoin\Address\AddressFactory::fromString($address);
/** @var CompactSignatureSerializerInterface $cs */
$cs = \BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer::getSerializer($ec, CompactSignatureSerializerInterface::class);
$serializer = new \BitWasp\Bitcoin\Serializer\MessageSigner\SignedMessageSerializer($cs);
$signedMessage = $serializer->parse($sig);
$signer = new MessageSigner($ec);
if ($signer->verify($signedMessage, $addr)) {
    echo "Signature verified!\n";
} else {
    echo "Failed to verify signature!\n";
}