예제 #1
0
 /**
  * @return Signature
  */
 public function convert()
 {
     $sig_t = '';
     /** @var resource $sig_t */
     secp256k1_ecdsa_recoverable_signature_convert($this->ecAdapter->getContext(), $this->resource, $sig_t);
     return new Signature($this->ecAdapter, $this->r, $this->s, $sig_t);
 }
예제 #2
0
 /**
  * @param \BitWasp\Buffertools\BufferInterface|string $data
  * @return PublicKey
  */
 public function parse($data)
 {
     $buffer = (new Parser($data))->getBuffer();
     $binary = $buffer->getBinary();
     $pubkey_t = '';
     /** @var resource $pubkey_t */
     if (!secp256k1_ec_pubkey_parse($this->ecAdapter->getContext(), $binary, $pubkey_t)) {
         throw new \RuntimeException('Secp256k1 failed to parse public key');
     }
     return new PublicKey($this->ecAdapter, $pubkey_t, $buffer->getSize() === 33);
 }
 /**
  * @param $data
  * @return SignatureInterface
  */
 public function parse($data)
 {
     $buffer = (new Parser($data))->getBuffer();
     $binary = $buffer->getBinary();
     $sig_t = '';
     /** @var resource $sig_t */
     if (!secp256k1_ecdsa_signature_parse_der($this->ecAdapter->getContext(), $binary, $sig_t)) {
         throw new \RuntimeException('Secp256k1: parse der failure');
     }
     // Unfortunately, we need to use the Parser here to get r and s :/
     list(, $inner) = $this->getOuterTemplate()->parse(new Parser($buffer));
     list(, $r, , $s) = $this->getInnerTemplate()->parse(new Parser($inner));
     /** @var Buffer $r */
     /** @var Buffer $s */
     return new Signature($this->ecAdapter, $r->getInt(), $s->getInt(), $sig_t);
 }
예제 #4
0
 /**
  * @param int $tweak
  * @return PublicKey
  * @throws \Exception
  */
 public function tweakMul($tweak)
 {
     $context = $this->ecAdapter->getContext();
     $math = $this->ecAdapter->getMath();
     $bin = pack('H*', str_pad($math->decHex($tweak), 64, '0', STR_PAD_LEFT));
     $clone = $this->clonePubkey();
     if (1 !== secp256k1_ec_pubkey_tweak_mul($context, $clone, $bin)) {
         throw new \RuntimeException('Secp256k1: tweak mul failed.');
     }
     return new PublicKey($this->ecAdapter, $clone, $this->compressed);
 }
예제 #5
0
 /**
  * @return PublicKey
  */
 public function getPublicKey()
 {
     if (null === $this->publicKey) {
         $context = $this->ecAdapter->getContext();
         $publicKey_t = '';
         /** @var resource $publicKey_t */
         if (1 !== secp256k1_ec_pubkey_create($context, $this->getBinary(), $publicKey_t)) {
             throw new \RuntimeException('Failed to create public key');
         }
         $this->publicKey = new PublicKey($this->ecAdapter, $publicKey_t, $this->compressed);
     }
     return $this->publicKey;
 }
 /**
  * @param string|BufferInterface $data
  * @return CompactSignature
  */
 public function parse($data)
 {
     $math = $this->ecAdapter->getMath();
     $buffer = (new Parser($data, $math))->getBuffer();
     if ($buffer->getSize() !== 65) {
         throw new \RuntimeException('Compact Sig must be 65 bytes');
     }
     $byte = $buffer->slice(0, 1)->getInt();
     $sig = $buffer->slice(1, 64);
     $recoveryFlags = $math->sub($byte, 27);
     if ($math->cmp($recoveryFlags, 7) > 0) {
         throw new \RuntimeException('Invalid signature type');
     }
     $isCompressed = $math->cmp($math->bitwiseAnd($recoveryFlags, 4), 0) !== 0;
     $recoveryId = $math->sub($recoveryFlags, $isCompressed ? 4 : 0);
     $sig_t = '';
     /** @var resource $sig_t */
     if (!secp256k1_ecdsa_recoverable_signature_parse_compact($this->ecAdapter->getContext(), $sig->getBinary(), $recoveryId, $sig_t)) {
         throw new \RuntimeException('Unable to parse compact signature');
     }
     return new CompactSignature($this->ecAdapter, $sig_t, $recoveryId, $isCompressed);
 }