def get_verifying_key(serialized_msg, serialized_sig): """Attempts to recover a public key from a message and a signature. Args: serialized_msg (str): A serialized message. serialized_sig (str): A serialized signature. Returns: str: a public key. """ v, r, s = pybitcointools.decode_sig(serialized_sig) msghash = pybitcointools.electrum_sig_hash(serialized_msg) z = pybitcointools.hash_to_int(msghash) compress = True if v >= 31 else False if compress: rec = v - 31 else: rec = v - 27 try: pubkey = nativeECDSA.recover_pubkey( str(z), str(r), str(s), int(rec)) except Exception as ex: logger.warn('Unable to extract public key from signature' + ex.args[0]) return "" pubkey = pubkey.translate(None, 'h') # strip out hex indicators from opencpp pubkey = '04' + pubkey # indicate uncompressed pubkey if compress: pubkey = pybitcointools.compress(pubkey) return pubkey
def recover_pubkey(message, signature): v, r, s = pybitcointools.decode_sig(signature) msghash = pybitcointools.electrum_sig_hash(message) z = pybitcointools.hash_to_int(msghash) compress = True if v >= 31 else False if compress: rec = v - 31 else: rec = v - 27 try: pubkey = nativeECDSA.recover_pubkey(str(z), str(r), str(s), int(rec)) except ValueError as ex: LOGGER.warning('Unable to extract public key from signature' + ex.args[0]) return "" try: # pybitcointools package pubkey = pubkey.translate(None, 'h') except TypeError: # bitcoin package pubkey = pubkey.translate('h') pubkey = '04' + pubkey if compress: pubkey = pybitcointools.compress(pubkey) return pubkey
def address(self): if self._type == PubkeyType.compressed: bin_hash160 = get_bin_hash160(compress(self.to_bin())) return bin_hash160_to_address( bin_hash160, version_byte=self._version_byte) return bin_hash160_to_address(self.bin_hash160(), version_byte=self._version_byte)
def address(self): if self._type == PubkeyType.compressed: bin_hash160 = get_bin_hash160(compress(self.to_bin())) return bin_hash160_to_address(bin_hash160, version_byte=self._version_byte) return bin_hash160_to_address(self.bin_hash160(), version_byte=self._version_byte)
def test_compressed_keys(self): """ Tests compressed key """ msg = 'foo' priv = pbt.encode_privkey(pbt.random_key(), 'hex_compressed') sig = pbt.ecdsa_sign(msg, priv) # Force old pybitcointools to behave v, r, s = pbt.decode_sig(sig) if v < 31: v += 4 sig = pbt.encode_sig(v, r, s) pub = pbt.compress(pbt.privtopub(priv)) native_recovered = pbct_nativerecover.recover_pubkey(msg, sig) self.assertEquals(native_recovered, pub, "Priv Key that failed: {}".format(priv))
def test_compressed_keys(self): """ Tests compressed key """ msg = 'foo' self.longMessage = True priv = pbt.encode_privkey(pbt.random_key(), 'hex_compressed') sig = pbt.ecdsa_sign(msg, priv) # Force old pybitcointools to behave v, r, s = pbt.decode_sig(sig) if v < 31: v += 4 sig = pbt.encode_sig(v, r, s) pub = pbt.compress(pbt.privtopub(priv)) native_recovered = gossip.signed_object.get_verifying_key(msg, sig) self.assertEquals(native_recovered, pub, "Priv Key that failed: {}".format(priv))
def public_key(self, compressed=False): # lazily calculate and set the public key if not hasattr(self, '_public_key'): ecdsa_public_key = self._ecdsa_private_key.get_verifying_key() # compress the public key string if a compressed public key is # desired bin_public_key_string = PUBKEY_MAGIC_BYTE + \ ecdsa_public_key.to_string() if compressed: bin_public_key_string = compress(bin_public_key_string) # create the public key object from the public key string self._public_key = BitcoinPublicKey( bin_public_key_string, version_byte=self._pubkeyhash_version_byte) # return the public key object return self._public_key
def public_key(self): # lazily calculate and set the public key if not hasattr(self, '_public_key'): ecdsa_public_key = self._ecdsa_private_key.get_verifying_key() bin_public_key_string = PUBKEY_MAGIC_BYTE + \ ecdsa_public_key.to_string() if self._compressed: bin_public_key_string = compress(bin_public_key_string) # create the public key object from the public key string self._public_key = BitcoinPublicKey( bin_public_key_string, version_byte=self._pubkeyhash_version_byte) # return the public key object return self._public_key
# This will generate the hashed uncompressed public key, # that is, the "bitcoin address" that you share with others. # Again, the uncompressed format is not recommended anymore. # # The sharable bitcoin address: public_address_uncompressed = utils.public_address(public_key_version) # THE COMPRESSED KEY # Compressed keys only specify the x coordinate plus an 1 byte # flag indicating which side of the symmetrical curve the point is on, which allows y to be derived. # In order to indicate that key is compressed, "02" or "03" prefix is added to the encoded hex value. # The "02" and "03" is determined based on the the elliptic curve equation result. # # Public key with compression (32 bytes + 1 byte prefix = 33 bytes), in hex encoding public_key_version_comp = pybitcointools.compress(public_key_version) # This will generate the compressed public key, # that is, the "bitcoin address" that you share with others. # This is the modern and recommended format. # # The sharable bitcoin address (hashed public key for P2PKH script format, starts with 1). # There is a newer P2SH format, so addresses for transactions using this script format start # with number 3. public_address_compressed = utils.public_address(public_key_version_comp) # WE ARE DONE. # Let's now print the WIF's and their corresponding Bitcoin public addresses. # We are also going to verify that our public keys were correctly generated, comparing # our public addresses with those generated by pybitcoin library: # https://github.com/blockstack/pybitcoin