def test_recover_key_v3_and_create_address_with_key(self): signature: bytes = base64.b64decode(self.tx_v3['signature']) self.assertIsInstance(signature, bytes) self.assertTrue(len(signature) > 0) msg_hash: bytes = create_msg_hash(self.tx_v3, ('txHash', 'signature')) self.assertEqual(msg_hash, bytes.fromhex(self.tx_v3['txHash'])) uncompressed_public_key: bytes = _recover_key(msg_hash, signature, compressed=False) self.assertIsInstance(uncompressed_public_key, bytes) self.assertEqual(65, len(uncompressed_public_key)) self.assertEqual(0x04, uncompressed_public_key[0]) address: Address = _create_address_with_key(uncompressed_public_key) self.assertEqual(self.tx_v3['from'], str(address)) compressed_public_key: bytes = _recover_key(msg_hash, signature, compressed=True) self.assertIsInstance(compressed_public_key, bytes) self.assertEqual(33, len(compressed_public_key)) self.assertIn(compressed_public_key[0], (0x02, 0x03)) address: Address = _create_address_with_key(compressed_public_key) self.assertEqual(self.tx_v3['from'], str(address))
def test_recover_key_and_create_address_with_key(self, tx, tx_hash_key, compressed, expected_pubkey_len, expected_pubkey_prefix): # TEST: '_recover_key' method should return proper public key according to the compression flag signature: bytes = _base64_decode_signature(tx['signature']) msg_hash: bytes = create_msg_hash(tx, (tx_hash_key, 'signature')) public_key: bytes = _recover_key(msg_hash, signature, compressed=compressed) assert isinstance(public_key, bytes) assert len(public_key) == expected_pubkey_len assert public_key[0] in expected_pubkey_prefix # TEST: '_create_address_with_key' method should make same address no matter what public key format address: Address = _create_address_with_key(public_key) assert str(address) == tx['from']
def recover_to_addr(msg, sig): signature: bytes = base64.b64decode(sig) msg_hash: bytes = bytes.fromhex(msg) pubkey = _recover_key(msg_hash, signature, compressed=True) address = str(_create_address_with_key(pubkey)) return address