2022-06-28 21:51:19 +00:00
|
|
|
import json
|
|
|
|
|
2022-08-10 17:38:39 +00:00
|
|
|
from nucypher.policy.conditions.evm import ContractCondition
|
2023-06-09 20:28:24 +00:00
|
|
|
from nucypher.policy.conditions.lingo import ConditionLingo
|
2022-06-28 21:51:19 +00:00
|
|
|
|
|
|
|
|
2022-11-17 11:20:53 +00:00
|
|
|
def test_simple_lingo_serialization(custom_abi_with_multiple_parameters, erc1155_balance_condition_data):
|
|
|
|
original_data = custom_abi_with_multiple_parameters
|
2022-11-16 15:56:36 +00:00
|
|
|
condition = ContractCondition.from_json(original_data)
|
|
|
|
serialized_data = condition.to_json()
|
|
|
|
deserialized_data = json.loads(serialized_data)
|
|
|
|
assert json.loads(original_data) == deserialized_data
|
|
|
|
|
2022-11-17 11:20:53 +00:00
|
|
|
original_data = erc1155_balance_condition_data
|
|
|
|
condition = ContractCondition.from_json(original_data)
|
|
|
|
serialized_data = condition.to_json()
|
|
|
|
deserialized_data = json.loads(serialized_data)
|
|
|
|
assert json.loads(original_data) == deserialized_data
|
2022-11-16 15:56:36 +00:00
|
|
|
|
2022-11-17 11:20:53 +00:00
|
|
|
|
|
|
|
def test_repeating_lingo_serializations(custom_abi_with_multiple_parameters):
|
|
|
|
"""
|
|
|
|
Porter deserializes conditions, then serializes again for sending to Ursula.
|
2022-11-16 15:56:36 +00:00
|
|
|
Here we check that the end condition are identical to the original condition.
|
|
|
|
"""
|
2022-11-17 11:20:53 +00:00
|
|
|
original_data = custom_abi_with_multiple_parameters
|
2022-11-16 15:56:36 +00:00
|
|
|
first_condition = ContractCondition.from_json(original_data)
|
|
|
|
serialized_data = first_condition.to_json()
|
|
|
|
second_condition = ContractCondition.from_json(serialized_data)
|
|
|
|
final_data = second_condition.to_json()
|
2022-11-17 11:20:53 +00:00
|
|
|
assert json.loads(original_data) == json.loads(final_data)
|
|
|
|
|
2022-11-16 15:56:36 +00:00
|
|
|
|
2022-11-10 02:53:33 +00:00
|
|
|
def test_evm_condition_function_abi(t_staking_data):
|
|
|
|
original_data = t_staking_data
|
2022-11-07 15:55:28 +00:00
|
|
|
condition = ContractCondition.from_json(original_data)
|
|
|
|
serialized_data = condition.to_json()
|
|
|
|
deserialized_data = json.loads(serialized_data)
|
|
|
|
assert deserialized_data["functionAbi"] == condition.function_abi
|
|
|
|
|
|
|
|
|
2022-09-23 14:42:13 +00:00
|
|
|
def test_type_resolution_from_json(
|
2023-06-08 14:11:30 +00:00
|
|
|
time_condition, rpc_condition, erc20_evm_condition, erc721_evm_condition
|
2022-09-23 14:42:13 +00:00
|
|
|
):
|
2023-06-08 14:11:30 +00:00
|
|
|
conditions = (time_condition, rpc_condition, erc20_evm_condition)
|
2022-07-28 01:08:41 +00:00
|
|
|
for condition in conditions:
|
2023-06-13 20:27:31 +00:00
|
|
|
clingo = ConditionLingo(condition=condition)
|
|
|
|
condition_json = clingo.to_json()
|
|
|
|
resolved_condition_lingo = ConditionLingo.from_json(condition_json)
|
|
|
|
assert isinstance(resolved_condition_lingo.condition, type(condition))
|
2022-07-28 01:08:41 +00:00
|
|
|
|
|
|
|
|
2022-11-17 11:20:53 +00:00
|
|
|
def test_conditions_lingo_serialization(compound_lingo):
|
2023-06-13 20:27:31 +00:00
|
|
|
lingo_dict = {
|
|
|
|
"version": ConditionLingo.VERSION,
|
|
|
|
"condition": compound_lingo.condition.to_dict(),
|
|
|
|
}
|
|
|
|
json_serialized_lingo = json.dumps(lingo_dict)
|
2022-11-17 11:20:53 +00:00
|
|
|
lingo_json = compound_lingo.to_json()
|
2022-07-28 01:08:41 +00:00
|
|
|
restored_lingo = ConditionLingo.from_json(data=lingo_json)
|
|
|
|
assert lingo_json == json_serialized_lingo
|
|
|
|
restored_lingo_json = restored_lingo.to_json()
|
|
|
|
assert restored_lingo_json == json_serialized_lingo
|
|
|
|
|
2022-11-16 01:57:08 +00:00
|
|
|
# base64
|
2022-07-28 01:08:41 +00:00
|
|
|
lingo_b64 = restored_lingo.to_base64()
|
|
|
|
restored_lingo = ConditionLingo.from_base64(lingo_b64)
|
|
|
|
|
|
|
|
# after all the serialization and transformation the content must remain identical
|
|
|
|
assert restored_lingo.to_json() == lingo_json
|
2022-11-16 01:57:08 +00:00
|
|
|
|
|
|
|
|
2023-06-07 20:36:07 +00:00
|
|
|
def test_access_control_condition_to_from_bytes(compound_lingo):
|
2022-11-16 01:57:08 +00:00
|
|
|
# bytes
|
2023-06-09 20:28:24 +00:00
|
|
|
condition_bytes = bytes(compound_lingo.condition)
|
|
|
|
condition = compound_lingo.condition.__class__.from_bytes(condition_bytes)
|
|
|
|
assert condition.to_json() == compound_lingo.condition.to_json()
|
2022-11-16 01:57:08 +00:00
|
|
|
|
|
|
|
|
2023-06-07 20:36:07 +00:00
|
|
|
def test_access_control_condition_to_from_dict(compound_lingo):
|
2023-06-09 20:28:24 +00:00
|
|
|
# dict
|
|
|
|
condition_dict = compound_lingo.condition.to_dict()
|
|
|
|
condition = compound_lingo.condition.__class__.from_dict(condition_dict)
|
|
|
|
assert condition.to_json() == compound_lingo.condition.to_json()
|