Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# graphenebase/ecdsa.py stripped of non-secp256k1 methods
print(it('red',"verify_message...return phex"))
# require message and signature to be bytes
if not isinstance(message, bytes):
message = bytes(message, "utf-8")
if not isinstance(signature, bytes):
signature = bytes(signature, "utf-8")
digest = hashfn(message).digest()
sig = signature[1:]
# recover parameter only
recoverParameter = bytearray(signature)[0] - 4 - 27
# "bitwise or"; each bit of the output is 0
# if the corresponding bit of x AND of y is 0, otherwise it's 1
ALL_FLAGS = (
secp256k1_lib.SECP256K1_CONTEXT_VERIFY
| secp256k1_lib.SECP256K1_CONTEXT_SIGN
)
# ecdsa.PublicKey with additional functions to serialize
# in uncompressed and compressed formats
pub = secp256k1_PublicKey(flags=ALL_FLAGS)
# recover raw signature
sig = pub.ecdsa_recoverable_deserialize(sig, recoverParameter)
# recover public key
verifyPub = secp256k1_PublicKey(pub.ecdsa_recover(message, sig))
# convert recoverable sig to normal sig
normalSig = verifyPub.ecdsa_recoverable_convert(sig)
# verify
verifyPub.ecdsa_verify(message, normalSig)
phex = verifyPub.serialize(compressed=True)
return phex
def verify(self):
try:
pub_key = secp256k1.PublicKey()
pub_key.deserialize(self.get_public_key())
assert pub_key.public_key, "No public key defined"
if pub_key.flags & lib.SECP256K1_CONTEXT_VERIFY != \
lib.SECP256K1_CONTEXT_VERIFY:
raise Exception("instance not configured for sig verification")
msg_digest = self.get_hashing_algorithm()(
self._entity_hash).digest()
raw_sig = pub_key.ecdsa_deserialize_compact(
self._entity_hash_signature)
verified = lib.secp256k1_ecdsa_verify(
pub_key.ctx, raw_sig, msg_digest, pub_key.public_key)
except Exception as e:
LOGGER.exception(e)
return False
else:
return bool(verified)
signature = bytes(signature, "utf-8")
if not isinstance(message, bytes):
raise AssertionError()
if not isinstance(signature, bytes):
raise AssertionError()
digest = hashfn(message).digest()
sig = signature[1:]
# TODO: 4 means we use compressed keys.
# Grapehen uses compressed keys by default even though it would still allow
# uncompressed keys to be used. This library so far expects compressed keys
# due to this line:
recoverParameter = bytearray(signature)[0] - 4 - 27 # recover parameter only
if SECP256K1_MODULE == "secp256k1":
ALL_FLAGS = (
secp256k1.lib.SECP256K1_CONTEXT_VERIFY
| secp256k1.lib.SECP256K1_CONTEXT_SIGN
)
# Placeholder
pub = secp256k1.PublicKey(flags=ALL_FLAGS)
# Recover raw signature
sig = pub.ecdsa_recoverable_deserialize(sig, recoverParameter)
# Recover PublicKey
verifyPub = secp256k1.PublicKey(pub.ecdsa_recover(message, sig))
# Convert recoverable sig to normal sig
normalSig = verifyPub.ecdsa_recoverable_convert(sig)
# Verify
verifyPub.ecdsa_verify(message, normalSig)
phex = verifyPub.serialize(compressed=True)
elif SECP256K1_MODULE == "cryptography":
p = recover_public_key(digest, sig, recoverParameter, message)
order = ecdsa.SECP256k1.order
def verify(self, pubkeys=[], chain=None):
if not chain:
raise ValueError("Chain needs to be provided!")
chain_params = self.getChainParams(chain)
self.deriveDigest(chain)
signatures = self.data["signatures"].data
pubKeysFound = []
for signature in signatures:
sig = future_bytes(signature)[1:]
recoverParameter = (
future_bytes(signature)[0]) - 4 - 27 # recover parameter only
if USE_SECP256K1:
ALL_FLAGS = secp256k1.lib.SECP256K1_CONTEXT_VERIFY | \
secp256k1.lib.SECP256K1_CONTEXT_SIGN
# Placeholder
pub = secp256k1.PublicKey(flags=ALL_FLAGS)
# Recover raw signature
sig = pub.ecdsa_recoverable_deserialize(sig, recoverParameter)
# Recover PublicKey
verifyPub = secp256k1.PublicKey(
pub.ecdsa_recover(future_bytes(self.message), sig))
# Convert recoverable sig to normal sig
normalSig = verifyPub.ecdsa_recoverable_convert(sig)
# Verify
verifyPub.ecdsa_verify(future_bytes(self.message), normalSig)
phex = hexlify(
verifyPub.serialize(compressed=True)).decode('ascii')
pubKeysFound.append(phex)
else: