def test_from_string_various(self): uint160 = UInt160.from_string("11" * 20) expected_data_uint160 = bytearray([0x11] * 20) self.assertEqual(expected_data_uint160, uint160.to_array()) uint256 = UInt256.from_string("11" * 32) expected_data_uint256 = bytearray([0x11] * 32) self.assertEqual(expected_data_uint256, uint256.to_array()) uint160_from_bytes = UInt160.deserialize_from_bytes( expected_data_uint160) self.assertEqual(expected_data_uint160, uint160_from_bytes.to_array()) uint256_from_bytes = UInt256.deserialize_from_bytes( expected_data_uint256) self.assertEqual(expected_data_uint256, uint256_from_bytes.to_array()) # test deserialize with too much data data_uint160 = bytearray(21 * [0x11]) uint160_from_bytes = UInt160.deserialize_from_bytes(data_uint160) self.assertEqual(data_uint160[:20], uint160_from_bytes.to_array()) data_uint256 = bytearray(33 * [0x11]) uint256_from_bytes = UInt256.deserialize_from_bytes(data_uint256) self.assertEqual(expected_data_uint256[:32], uint256_from_bytes.to_array()) # test deserialize with too little data data_uint160 = bytearray(19 * [0x11]) data_uint256 = bytearray(31 * [0x11]) with self.assertRaises(ValueError) as ctx: UInt160.deserialize_from_bytes(data_uint160) self.assertEqual( "Insufficient data 19 bytes is less than the required 20", str(ctx.exception)) with self.assertRaises(ValueError) as ctx: UInt256.deserialize_from_bytes(data_uint256) self.assertEqual( "Insufficient data 31 bytes is less than the required 32", str(ctx.exception))
def bytes_to_UInt160(bytestring: bytes): return Hash160Str.from_UInt160( UInt160.deserialize_from_bytes(bytestring))