def test_vectors4(): sk1 = PrivateKey.from_seed(bytes([1, 2, 3, 4, 5])) sk2 = PrivateKey.from_seed(bytes([1, 2, 3, 4, 5, 6])) pk1 = sk1.get_public_key() pk2 = sk2.get_public_key() m1 = bytes([7, 8, 9]) m2 = bytes([10, 11, 12]) sig9 = sk1.sign_prepend(m1) sig10 = sk2.sign_prepend(m2) assert (sig9.serialize() == bytes.fromhex( "d2135ad358405d9f2d4e68dc253d64b6049a821797817cffa5aa804086a8fb7b135175bb7183750e3aa19513db1552180f0b0ffd513c322f1c0c30a0a9c179f6e275e0109d4db7fa3e09694190947b17d890f3d58fe0b1866ec4d4f5a59b16ed" )) assert (sig10.serialize() == bytes.fromhex( "cc58c982f9ee5817d4fbf22d529cfc6792b0fdcf2d2a8001686755868e10eb32b40e464e7fbfe30175a962f1972026f2087f0495ba6e293ac3cf271762cd6979b9413adc0ba7df153cf1f3faab6b893404c2e6d63351e48cd54e06e449965f08" )) agg_sig = PrependSignature.aggregate([sig9, sig9, sig10]) message_hashes = [Util.hash256(m1), Util.hash256(m1), Util.hash256(m2)] pks = [pk1, pk1, pk2] assert (agg_sig.serialize() == bytes.fromhex( "c37077684e735e62e3f1fd17772a236b4115d4b581387733d3b97cab08b90918c7e91c23380c93e54be345544026f93505d41e6000392b82ab3c8af1b2e3954b0ef3f62c52fc89f99e646ff546881120396c449856428e672178e5e0e14ec894" )) assert (agg_sig.verify(message_hashes, pks))
def additional_python_methods(): private_key = PrivateKey.from_seed(b'123') s1 = private_key.sign(b'message') s2 = private_key.sign_prepend(b'message') assert s1.get_insecure_sig().verify([Util.hash256(b'message')], [private_key.get_public_key()]) assert s2.get_insecure_sig().verify([ Util.hash256(private_key.get_public_key().serialize() + Util.hash256(b'message')) ], [private_key.get_public_key()]) s1_b = Signature.from_insecure_sig(s1.get_insecure_sig()) s2_b = PrependSignature.from_insecure_sig(s2.get_insecure_sig()) assert s1 == s1_b and s2 == s2_b s3 = private_key.sign_insecure_prehashed(Util.hash256(b'456')) assert s3.verify([Util.hash256(b'456')], [private_key.get_public_key()]) esk = ExtendedPrivateKey.from_seed(b'789') epk = esk.get_public_key() s3 = private_key.sign(b'message3') s4 = private_key.sign_insecure(b'message3') assert bytes(private_key) == private_key.serialize() assert deepcopy(private_key) == private_key assert deepcopy(s1) == s1 assert deepcopy(s2) == s2 assert deepcopy(s3) == s3 assert deepcopy(s4) == s4 assert deepcopy( private_key.get_public_key()) == private_key.get_public_key() assert deepcopy(esk) == esk assert deepcopy(epk) == epk assert deepcopy(esk.get_chain_code()) == esk.get_chain_code()
def throw_wrong_type(): private_key = ExtendedPrivateKey.from_seed(b"foo").get_private_key() message_hash = bytes([10] * 32) sig_prepend = private_key.sign_prepend_prehashed(message_hash).serialize() sig_secure = private_key.sign_prehashed(message_hash).serialize() try: Signature.from_bytes(sig_prepend) except ValueError: try: PrependSignature.from_bytes(sig_secure) except ValueError: return assert False assert False
def no_throw_bad_sig(): private_key = ExtendedPrivateKey.from_seed(b"foo").get_private_key() message_hash = bytes([9] * 32) sig = private_key.sign_prepend_prehashed(message_hash).serialize() sig = sig[:-1] + bytes([0]) public_key = private_key.get_public_key() try: bad_signature = PrependSignature.from_bytes(sig) except ValueError: return assert (False)
def additional_python_methods(): private_key = PrivateKey.from_seed(b'123') s1 = private_key.sign(b'message') s2 = private_key.sign_prepend(b'message') assert s1.get_insecure_sig().verify([Util.hash256(b'message')], [private_key.get_public_key()]) assert s2.get_insecure_sig().verify([ Util.hash256(private_key.get_public_key().serialize() + Util.hash256(b'message')) ], [private_key.get_public_key()]) s1_b = Signature.from_insecure_sig(s1.get_insecure_sig()) s2_b = PrependSignature.from_insecure_sig(s2.get_insecure_sig()) assert s1 == s1_b and s2 == s2_b s3 = private_key.sign_insecure_prehashed(Util.hash256(b'456')) assert s3.verify([Util.hash256(b'456')], [private_key.get_public_key()])
def signature_from_string(signature): try: sig = PrependSignature.from_bytes(bytes.fromhex(signature)) except Exception: raise Exception return sig