def test_signing(self): message = Message(urandom(32)) private, public = generate_keypair() sig = message.sign(private) fake_sig = Signature(sig.r + 1, sig.s - 1) _, fake_public = generate_keypair() fake_message = Message.from_int(message.int() + 1) self.assertTrue(message.verify(sig, public)) self.assertFalse(message.verify(sig, fake_public)) self.assertFalse(message.verify(fake_sig, public)) self.assertFalse(fake_message.verify(sig, public))
def test_bech32_decode(self): private, public = generate_keypair() witprog = hash160(public.encode(compressed=True)) address = bech32.encode(self.hrp, self.witver, witprog) wv, decoded = bech32.decode(self.hrp, address) self.assertEqual(wv, self.witver) self.assertEqual(bytes(decoded), bytes(witprog))
def test_compression(self): prv, pub = generate_keypair() encoded = pub.encode(compressed=True) self.assertEqual(PublicKey.decode(encoded), pub) encoded = pub.encode(compressed=False) self.assertEqual(PublicKey.decode(encoded), pub)
def test_random_msg(self): for i in range(25): aux = secrets.token_bytes(32).ljust(32, b'\x00') msg = Message(secrets.token_bytes(32).ljust(32, b'\x00')) prv, pub = generate_keypair() sig = msg.sign_schnorr(prv, aux) result = msg.verify(sig, pub) self.assertTrue(result)
def vanity(prefix: str) -> Tuple[str, str, str]: """Generate a vanity address starting with the input (excluding the version byte)""" not_in_alphabet = {i for i in prefix if i not in base58.ALPHABET} assert not not_in_alphabet, f"Characters {not_in_alphabet} are not in alphabet" start = time() counter = 0 while True: counter += 1 private, public = generate_keypair() address = pubkey_to_address(public) if address[1:].startswith(prefix): duration = timedelta(seconds=round(time() - start)) print(f"Found address starting with {prefix} in {duration} after {counter:,} tries") return private.hex(), public.hex(), address