diff --git a/src/fds/contracts/ENSRegistry.py b/src/fds/contracts/ENSRegistry.py index da77c9e..f993f7f 100644 --- a/src/fds/contracts/ENSRegistry.py +++ b/src/fds/contracts/ENSRegistry.py @@ -15,10 +15,59 @@ You should have received a copy of the GNU Lesser General Public License along with the FairDataSociety library. If not, see . -handles crypto +handles EnsRegistry contract """ +from typing import Union -class ENSRegistry: - def __init__(self): - ... +from ape import networks, project +from ape.api.accounts import AccountAPI +from ape.api.transactions import ReceiptAPI +from ape.types import AddressType + + +class EnsRegistry: + def __init__(self, account: AccountAPI, contract_address: Union[AddressType, None] = None): + self.account = account + self.contract_address = contract_address + self.contract = None + self.web3 = networks.provider._web3 + + if contract_address: + self.contract = project.ENSRegistry.at(self.contract_address) + + # ? interacting with the contract methods + # * returns the address of the owner + def owner(self, node: int) -> str: + node = node.to_bytes(32, byteorder="big") # type: ignore + self.node = f"0x{node.hex()}" # type: ignore + return self.contract.owner(self.node) # type: ignore + + def setResolver(self, node: int, address: AddressType) -> ReceiptAPI: + node = node.to_bytes(32, byteorder="big") # type: ignore + self.node = f"0x{node.hex()}" # type: ignore + return self.contract.setResolver(self.node, address, sender=self.account) # type: ignore + + def getResolver(self, node: int) -> str: + node = node.to_bytes(32, byteorder="big") # type: ignore + self.node = f"0x{node.hex()}" # type: ignore + return self.contract.resolver(self.node) # type: ignore + + def setOwner(self, node: int, address: AddressType) -> ReceiptAPI: + node = node.to_bytes(32, byteorder="big") # type: ignore + self.node = f"0x{node.hex()}" # type: ignore + return self.contract.setOwner(self.node, address, sender=self.account) # type: ignore + + # * Returns the TTL of a node, and any records associated with it. + def getTTL(self, node: int) -> int: + node = node.to_bytes(32, byteorder="big") # type: ignore + self.node = f"0x{node.hex()}" # type: ignore + return self.contract.ttl(self.node) # type: ignore + + def setTTL(self, node: int, ttl: int) -> ReceiptAPI: + node = node.to_bytes(32, byteorder="big") # type: ignore + self.node = f"0x{node.hex()}" # type: ignore + self.ttl = ttl + return self.contract.setTTL(self.node, self.ttl, sender=self.account) # type: ignore + + # TODO: Add all other methods if required. diff --git a/src/fds/fds_ens2.py b/src/fds/fds_ens2.py index 26ab4aa..16f9e00 100644 --- a/src/fds/fds_ens2.py +++ b/src/fds/fds_ens2.py @@ -17,3 +17,20 @@ mailbox smart contracts """ + + +from typing import Dict + +from ape.api.accounts import AccountAPI + +from fds.contracts.ENSRegistry import EnsRegistry + +KEY_STRING = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" # noqa: 501 + + +class FDSENS2: + def __init__(self, account: AccountAPI, config: Dict): + self.account = account + self.config = config + + self.ensRegistry = EnsRegistry(self.account, self.config.get("subdomainRegistrarAddress")) diff --git a/src/fds/tests/conftest.py b/src/fds/tests/conftest.py index 8fe5709..3821973 100644 --- a/src/fds/tests/conftest.py +++ b/src/fds/tests/conftest.py @@ -2,9 +2,11 @@ import ape # type: ignore import pytest +from ape import project from ape.contracts.base import ContractContainer, ContractInstance from ethpm_types import ContractType +from fds.contracts.ENSRegistry import EnsRegistry from fds.fds_contract import FDSContract from fds.fds_crypto import Crypto from fds.fds_wallet import Wallet @@ -229,3 +231,19 @@ def fdsContractDeploy(owner): contract = fdscontract.deploy(ape.project.SolidityTestContract) return contract + + +@pytest.fixture +def ensContract(owner): + contract = project.ENSRegistry.deploy(sender=owner) + + return contract + + +@pytest.fixture +def ENS(owner, ensContract): + contract = ensContract + + ENS = EnsRegistry(owner, contract.address) + + return ENS diff --git a/src/fds/tests/unit_tests/test_ensregistry.py b/src/fds/tests/unit_tests/test_ensregistry.py new file mode 100644 index 0000000..cebd686 --- /dev/null +++ b/src/fds/tests/unit_tests/test_ensregistry.py @@ -0,0 +1,51 @@ +import pytest +from ape.exceptions import ContractLogicError + +from fds.contracts.ENSRegistry import EnsRegistry + + +def test_load_ensContract(owner, ENS): + assert ENS.owner(0) == owner.address + + +def test_set_resolver(owner, ENS): + ENS.setResolver(0, owner.address) + + assert ENS.getResolver(0) == owner.address + + +def test_fail_setting_resolver(owner, ENS): + with pytest.raises(ContractLogicError): + ENS.setResolver(1, owner.address) + + +def test_set_owner(ENS, receiver): + ENS.setOwner(0, receiver.address) + + assert ENS.owner(0) == receiver.address + + +def test_fail_setting_owner(owner, ensContract, receiver): + contract = ensContract + + ENS = EnsRegistry(receiver, contract.address) + + with pytest.raises(ContractLogicError): + ENS.setOwner(0, owner.address) + + +def test_set_n_get_ttl(ENS): + assert ENS.getTTL(0) == 0 + + ENS.setTTL(0, 420) + + assert ENS.getTTL(0) == 420 + + +def test_fail_setting_ttl(owner, receiver, ensContract): + contract = ensContract + + ENS = EnsRegistry(owner, contract.address) + + with pytest.raises(ContractLogicError): + ENS.setTTL(12, 1111) diff --git a/src/fds/tests/unit_tests/test_fds_contract.py b/src/fds/tests/unit_tests/test_fds_contract.py index bf0338d..d8d9545 100644 --- a/src/fds/tests/unit_tests/test_fds_contract.py +++ b/src/fds/tests/unit_tests/test_fds_contract.py @@ -26,7 +26,7 @@ def test_deploy_contract(owner, fdsContractDeploy): # fdscontract = FDSContract(owner) # contract = fdscontract.deploy(project.SolidityTestContract) contract = fdsContractDeploy - assert contract.address == "0xF2Df0b975c0C9eFa2f8CA0491C2d1685104d2488" + assert contract.address != "0x0000000000000000000000000000000000000000" assert contract.balance == 0 assert contract.owner() == owner