Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
if not isinstance(signature, bytes_types):
signature = py23_bytes(signature, "utf-8")
if not isinstance(message, bytes_types):
raise AssertionError()
if not isinstance(signature, bytes_types):
raise AssertionError()
digest = hashfn(message).digest()
sig = signature[1:]
if recover_parameter is None:
recover_parameter = bytearray(signature)[0] - 4 - 27 # recover parameter only
if recover_parameter < 0:
log.info("Could not recover parameter")
return None
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, recover_parameter)
# 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, recover_parameter, message)
order = ecdsa.SECP256k1.order
r, s = ecdsa.util.sigdecode_string(sig, order)
sigder = encode_dss_signature(r, s)
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
r, s = ecdsa.util.sigdecode_string(sig, order)
sigder = encode_dss_signature(r, s)
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:
p = self.recover_public_key(self.digest, sig, recoverParameter)
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
r, s = ecdsa.util.sigdecode_string(sig, order)