MCPcopy
hub / github.com/petertodd/python-bitcoinlib / CPubKey

Class CPubKey

bitcoin/core/key.py:565–624  ·  view source on GitHub ↗

An encapsulated public key Attributes: is_valid - Corresponds to CPubKey.IsValid() is_fullyvalid - Corresponds to CPubKey.IsFullyValid() is_compressed - Corresponds to CPubKey.IsCompressed()

Source from the content-addressed store, hash-verified

563 if Q: _ssl.EC_POINT_free(Q)
564
565class CPubKey(bytes):
566 """An encapsulated public key
567
568 Attributes:
569
570 is_valid - Corresponds to CPubKey.IsValid()
571
572 is_fullyvalid - Corresponds to CPubKey.IsFullyValid()
573
574 is_compressed - Corresponds to CPubKey.IsCompressed()
575 """
576
577 def __new__(cls, buf, _cec_key=None):
578 self = super(CPubKey, cls).__new__(cls, buf)
579 if _cec_key is None:
580 _cec_key = CECKey()
581 self._cec_key = _cec_key
582 self.is_fullyvalid = _cec_key.set_pubkey(self) is not None
583 return self
584
585 @classmethod
586 def recover_compact(cls, hash, sig): # pylint: disable=redefined-builtin
587 """Recover a public key from a compact signature."""
588 if len(sig) != 65:
589 raise ValueError("Signature should be 65 characters, not [%d]" % (len(sig), ))
590
591 recid = (sig[0] - 27) & 3
592 compressed = (sig[0] - 27) & 4 != 0
593
594 cec_key = CECKey()
595 cec_key.set_compressed(compressed)
596
597 sigR = sig[1:33]
598 sigS = sig[33:65]
599
600 result = cec_key.recover(sigR, sigS, hash, len(hash), recid, 0)
601
602 if result < 1:
603 return False
604
605 pubkey = cec_key.get_pubkey()
606
607 return CPubKey(pubkey, _cec_key=cec_key)
608
609 @property
610 def is_valid(self):
611 return len(self) > 0
612
613 @property
614 def is_compressed(self):
615 return len(self) == 33
616
617 def verify(self, hash, sig): # pylint: disable=redefined-builtin
618 return self._cec_key.verify(hash, sig)
619
620 def __str__(self):
621 return repr(self)
622

Callers 4

recover_compactMethod · 0.85
TMethod · 0.85

Calls

no outgoing calls

Tested by 3

TMethod · 0.68