def from_pem(cls, pem): """ :param pem: A private key previously encoded as PEM. :type pem: ``bytes`` :rtype: :class:`~bitcoinpython.PrivateKeyTestnet` """ return PrivateKeyTestnet(ECPrivateKey.from_pem(pem))
def from_int(cls, num): """ :param num: A private key in raw integer form. :type num: ``int`` :rtype: :class:`~bitcoinpython.PrivateKeyTestnet` """ return PrivateKeyTestnet(ECPrivateKey.from_int(num))
def from_der(cls, der): """ :param der: A private key previously encoded as DER. :type der: ``bytes`` :rtype: :class:`~bitcoinpython.PrivateKeyTestnet` """ return PrivateKeyTestnet(ECPrivateKey.from_der(der))
def from_bytes(cls, bytestr): """ :param bytestr: A private key previously encoded as hex. :type bytestr: ``bytes`` :rtype: :class:`~bitcoinpython.PrivateKeyTestnet` """ return PrivateKeyTestnet(ECPrivateKey(bytestr))
def from_hex(cls, hexed): """ :param hexed: A private key previously encoded as hex. :type hexed: ``str`` :rtype: :class:`~bitcoinpython.PrivateKeyTestnet` """ return PrivateKeyTestnet(ECPrivateKey.from_hex(hexed))
def __init__(self, wif=None): if wif: if isinstance(wif, str): private_key_bytes, compressed, version = wif_to_bytes(wif) self._pk = ECPrivateKey(private_key_bytes) elif isinstance(wif, ECPrivateKey): self._pk = wif compressed = True else: raise TypeError('Wallet Import Format must be a string.') else: self._pk = ECPrivateKey() compressed = True self._public_point = None self._public_key = self._pk.public_key.format(compressed=compressed)
def generate_key_address_pairs(prefix, counter, match, queue): # pragma: no cover context = Context() while True: if match.is_set(): return with counter.get_lock(): counter.value += 1 private_key = ECPrivateKey(context=context) address = b58encode_check( b'\x00' + ripemd160_sha256(private_key.public_key.format()) ) if address.startswith(prefix): match.set() queue.put_nowait((private_key.secret, address)) return
def generate_key_address_pair(): # pragma: no cover private_key = ECPrivateKey() address = public_key_to_address(private_key.public_key.format()) return bytes_to_wif(private_key.secret), address
def test_init_from_key(self): pk = ECPrivateKey() base_key = BaseKey(pk) assert base_key._pk == pk
class BaseKey: """This class represents a point on the elliptic curve secp256k1 and provides all necessary cryptographic functionality. You shouldn't use this class directly. :param wif: A private key serialized to the Wallet Import Format. If the argument is not supplied, a new private key will be created. The WIF compression flag will be adhered to, but the version byte is disregarded. Compression will be used by all new keys. :type wif: ``str`` :raises TypeError: If ``wif`` is not a ``str``. """ def __init__(self, wif=None): if wif: if isinstance(wif, str): private_key_bytes, compressed, version = wif_to_bytes(wif) self._pk = ECPrivateKey(private_key_bytes) elif isinstance(wif, ECPrivateKey): self._pk = wif compressed = True else: raise TypeError('Wallet Import Format must be a string.') else: self._pk = ECPrivateKey() compressed = True self._public_point = None self._public_key = self._pk.public_key.format(compressed=compressed) @property def public_key(self): """The public point serialized to bytes.""" return self._public_key @property def public_point(self): """The public point (x, y).""" if self._public_point is None: self._public_point = Point(*public_key_to_coords(self._public_key)) return self._public_point def sign(self, data): """Signs some data which can be verified later by others using the public key. :param data: The message to sign. :type data: ``bytes`` :returns: A signature compliant with BIP-62. :rtype: ``bytes`` """ return self._pk.sign(data) def verify(self, signature, data): """Verifies some data was signed by this private key. :param signature: The signature to verify. :type signature: ``bytes`` :param data: The data that was supposedly signed. :type data: ``bytes`` :rtype: ``bool`` """ return self._pk.public_key.verify(signature, data) def to_hex(self): """:rtype: ``str``""" return self._pk.to_hex() def to_bytes(self): """:rtype: ``bytes``""" return self._pk.secret def to_der(self): """:rtype: ``bytes``""" return self._pk.to_der() def to_pem(self): """:rtype: ``bytes``""" return self._pk.to_pem() def to_int(self): """:rtype: ``int``""" return self._pk.to_int() def is_compressed(self): """Returns whether or not this private key corresponds to a compressed public key. :rtype: ``bool`` """ return True if len(self.public_key) == 33 else False def __eq__(self, other): return self.to_int() == other.to_int()