2022-08-16 13:40:27 +00:00
|
|
|
"""
|
|
|
|
This file is part of nucypher.
|
|
|
|
|
|
|
|
nucypher is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
nucypher is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
2022-06-28 21:51:19 +00:00
|
|
|
import json
|
|
|
|
|
2022-07-28 01:08:41 +00:00
|
|
|
from nucypher.policy.conditions._utils import _deserialize_condition_lingo
|
2022-08-10 17:38:39 +00:00
|
|
|
from nucypher.policy.conditions.evm import ContractCondition
|
2022-07-28 01:08:41 +00:00
|
|
|
from nucypher.policy.conditions.lingo import ConditionLingo
|
2022-06-28 21:51:19 +00:00
|
|
|
|
|
|
|
|
2022-11-07 15:55:28 +00:00
|
|
|
def test_evm_condition_function_abi(TStaking_data):
|
|
|
|
original_data = TStaking_data
|
|
|
|
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-07-02 18:26:58 +00:00
|
|
|
def test_evm_condition_json_serializers(ERC1155_balance_condition_data):
|
|
|
|
original_data = ERC1155_balance_condition_data
|
2022-08-10 17:38:39 +00:00
|
|
|
condition = ContractCondition.from_json(original_data)
|
2022-06-28 21:51:19 +00:00
|
|
|
serialized_data = condition.to_json()
|
2022-09-13 14:29:29 +00:00
|
|
|
|
|
|
|
deserialized_data = json.loads(serialized_data)
|
|
|
|
deserialized_data.pop("functionAbi")
|
|
|
|
|
|
|
|
assert json.loads(original_data) == deserialized_data
|
2022-07-28 01:08:41 +00:00
|
|
|
|
|
|
|
|
2022-09-23 14:42:13 +00:00
|
|
|
def test_type_resolution_from_json(
|
|
|
|
timelock_condition, rpc_condition, erc20_evm_condition, erc721_evm_condition
|
|
|
|
):
|
2022-07-28 01:08:41 +00:00
|
|
|
conditions = (
|
|
|
|
timelock_condition,
|
|
|
|
rpc_condition,
|
2022-09-23 14:42:13 +00:00
|
|
|
erc20_evm_condition,
|
|
|
|
erc721_evm_condition,
|
2022-07-28 01:08:41 +00:00
|
|
|
)
|
|
|
|
for condition in conditions:
|
|
|
|
condition_json = condition.to_json()
|
|
|
|
resolved_condition = _deserialize_condition_lingo(condition_json)
|
|
|
|
assert isinstance(resolved_condition, type(condition))
|
|
|
|
|
|
|
|
|
2022-09-23 14:42:13 +00:00
|
|
|
def test_conditions_lingo_serialization(lingo):
|
2022-08-16 13:40:27 +00:00
|
|
|
json_serialized_lingo = json.dumps([l.to_dict() for l in lingo.conditions])
|
2022-07-28 01:08:41 +00:00
|
|
|
lingo_json = lingo.to_json()
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|