async def verify_signature(message): """Verifies a signature of a message, return True if verified, false if not""" loop = asyncio.get_event_loop() sig_raw = bytes(bytearray.fromhex(message["signature"])) sig = NulsSignature(sig_raw) sender_hash = hash_from_address(message["sender"]) (sender_chain_id, ) = struct.unpack("h", sender_hash[:2]) hash = public_key_to_hash(sig.pub_key, sender_chain_id) address = address_from_hash(hash) if address != message["sender"]: LOGGER.warning("Received bad signature from %s for %s" % (address, message["sender"])) return False verification = await get_verification_buffer(message) try: result = await run_in_executor(None, sig.verify, verification) except Exception: LOGGER.exception("NULS Signature verification error") result = False # result = sig.verify(verification) return result
async def test_sign_data(): private_key = PrivateKey(SECRET) sign: NulsSignature = NulsSignature.sign_data(pri_key=private_key.secret, digest_bytes=b"x" * (256 // 8)) assert sign assert type(sign.pub_key) == bytes assert type(sign.digest_bytes) == bytes assert type(sign.sig_ser) == bytes assert sign.ecc_type == None
def test_sign_data_deprecated(): """Test the data signature""" data = None signature = NulsSignature(data=data) delete_private_key_file() private_key = get_fallback_private_key() assert signature sign_deprecated: NulsSignatureSecp256k1 = ( NulsSignatureSecp256k1.sign_data_deprecated(pri_key=private_key, digest_bytes=b"x" * (256 // 8))) assert sign_deprecated
async def test_compare_sign_data(): private_key = PrivateKey(SECRET) sign: NulsSignature = NulsSignature.sign_data(pri_key=private_key.secret, digest_bytes=b"x" * (256 // 8)) sign_deprecated: NulsSignatureSecp256k1 = ( NulsSignatureSecp256k1.sign_data_deprecated(pri_key=private_key.secret, digest_bytes=b"x" * (256 // 8))) assert len(sign.sig_ser) == len(sign_deprecated.sig_ser) assert sign.sig_ser == sign_deprecated.sig_ser assert sign == sign_deprecated