// File: contracts/wrapper/NameWrapper.sol



//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@ensdomains/ens-contracts/contracts/wrapper/ERC1155Fuse.sol";
import "@ensdomains/ens-contracts/contracts/wrapper/Controllable.sol";
import "@ensdomains/ens-contracts/contracts/wrapper/INameWrapper.sol";
import "@ensdomains/ens-contracts/contracts/wrapper/INameWrapperUpgrade.sol";
import "@ensdomains/ens-contracts/contracts/wrapper/IMetadataService.sol";
import "@ensdomains/ens-contracts/contracts/registry/ENS.sol";
import "@ensdomains/ens-contracts/contracts/ethregistrar/IBaseRegistrar.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@ensdomains/ens-contracts/contracts/wrapper/BytesUtil.sol";

error Unauthorised(bytes32 node, address addr);
error NameNotFound();
error IncompatibleParent();
error IncompatibleName(bytes name);
error IncorrectTokenType();
error LabelMismatch(bytes32 labelHash, bytes32 expectedLabelhash);
error LabelTooShort();
error LabelTooLong(string label);
error IncorrectTargetOwner(address owner);
error CannotUpgrade();
error InvalidExpiry(bytes32 node, uint64 expiry);

contract NameWrapper is
  Ownable,
  ERC1155Fuse,
  INameWrapper,
  Controllable,
  IERC721Receiver
{
  using BytesUtils for bytes;
  ENS public immutable override ens;
  IBaseRegistrar public immutable override registrar;
  IMetadataService public override metadataService;
  mapping(bytes32 => bytes) public override names;
  mapping(address => bool) public authorized;
  string public constant name = ".earth Nomads";
  string public constant symbol = "NOMAD";
  bytes32 private constant ETH_NODE =
    0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae;
  bytes32 private constant ROOT_NODE =
    0x0000000000000000000000000000000000000000000000000000000000000000;

  //A contract address to a new upgraded contract if any
  INameWrapperUpgrade public upgradeContract;
  uint64 private constant MAX_EXPIRY = type(uint64).max;

  constructor(
    ENS _ens,
    IBaseRegistrar _registrar,
    IMetadataService _metadataService
  ) {
    ens = _ens;
    registrar = _registrar;
    metadataService = _metadataService;

    /* Burn PARENT_CANNOT_CONTROL and CANNOT_UNWRAP fuses for ROOT_NODE and ETH_NODE */

    _setData(
      uint256(ETH_NODE),
      address(0),
      uint32(PARENT_CANNOT_CONTROL | CANNOT_UNWRAP),
      MAX_EXPIRY
    );
    _setData(
      uint256(ROOT_NODE),
      address(0),
      uint32(PARENT_CANNOT_CONTROL | CANNOT_UNWRAP),
      MAX_EXPIRY
    );
    names[ROOT_NODE] = "\x00";
    names[ETH_NODE] = "\x03eth\x00";
  }

  function supportsInterface(bytes4 interfaceId)
    public
    view
    virtual
    override(ERC1155Fuse, IERC165)
    returns (bool)
  {
    return
      interfaceId == type(INameWrapper).interfaceId ||
      super.supportsInterface(interfaceId);
  }

  /* ERC1155 */

  function ownerOf(uint256 id)
    public
    view
    override(ERC1155Fuse, INameWrapper)
    returns (address owner)
  {
    return super.ownerOf(id);
  }

  /* Metadata service */

  /**
   * @notice Set the metadata service. Only the owner can do this
   */

  function setMetadataService(IMetadataService _newMetadataService)
    public
    onlyOwner
  {
    metadataService = _newMetadataService;
  }

  /**
   * @notice Get the metadata uri
   * @return String uri of the metadata service
   */

  function uri(uint256 tokenId) public view override returns (string memory) {
    return metadataService.uri(tokenId);
  }

  /**
   * @notice Set the address of the upgradeContract of the contract. only admin can do this
   * @dev The default value of upgradeContract is the 0 address. Use the 0 address at any time
   * to make the contract not upgradable.
   * @param _upgradeAddress address of an upgraded contract
   */

  function setUpgradeContract(INameWrapperUpgrade _upgradeAddress)
    public
    onlyOwner
  {
    if (address(upgradeContract) != address(0)) {
      registrar.setApprovalForAll(address(upgradeContract), false);
      ens.setApprovalForAll(address(upgradeContract), false);
    }

    upgradeContract = _upgradeAddress;

    if (address(upgradeContract) != address(0)) {
      registrar.setApprovalForAll(address(upgradeContract), true);
      ens.setApprovalForAll(address(upgradeContract), true);
    }
  }

  /**
   * @notice Checks if msg.sender is the owner or approved by the owner of a name
   * @param node namehash of the name to check
   */

  modifier onlyTokenOwner(bytes32 node) {
    if (!isTokenOwnerOrApproved(node, msg.sender)) {
      revert Unauthorised(node, msg.sender);
    }

    _;
  }

  /**
   * @notice Checks if msg.sender is approved by the owner of the contract
   */

  modifier onlyAuthorized() {
    require(
      authorized[msg.sender] || msg.sender == owner(),
      "only authorized and owner"
    );
    _;
  }

  /**
   * @notice Checks if owner or approved by owner
   * @param node namehash of the name to check
   * @param addr which address to check permissions for
   * @return whether or not is owner or approved
   */

  function isTokenOwnerOrApproved(bytes32 node, address addr)
    public
    view
    override
    returns (bool)
  {
    address owner = ownerOf(uint256(node));
    return owner == addr || isApprovedForAll(owner, addr);
  }

  /**
   * @notice Gets fuse permissions for a specific name
   * @dev Fuses are represented by a uint32 where each permission is represented by 1 bit
   *      The interface has predefined fuses for all registry permissions, but additional
   *      fuses can be added for other use cases
   *      Also returns expiry, which is when the fuses are set to expire.
   * @param node namehash of the name to check
   * @return fuses A number that represents the permissions a name has. Returns 0 when expiry < block.timestamp
   * @return expiry Unix time of when the name expires and fuses are to expire
   */
  function getFuses(bytes32 node)
    public
    view
    override
    returns (uint32 fuses, uint64 expiry)
  {
    (, fuses, expiry) = getData(uint256(node));
    if (fuses == 0 && expiry == 0) {
      bytes memory name = names[node];
      if (name.length == 0) {
        revert NameNotFound();
      }
    }
  }

  /**
   * @notice Wraps a .eth domain, creating a new token and sending the original ERC721 token to this contract
   * @dev Can be called by the owner of the name on the .eth registrar or an authorised caller on the registrar
   * @param label label as a string of the .eth domain to wrap
   * @param wrappedOwner Owner of the name in this contract
   * @param fuses initial fuses to set
   * @param expiry when the fuses will expire
   * @param resolver resolver contract address
   */

  function wrapETH2LD(
    string calldata label,
    address wrappedOwner,
    uint32 fuses,
    uint64 expiry,
    address resolver
  ) public override onlyAuthorized returns (uint64) {
    uint256 tokenId = uint256(keccak256(bytes(label)));
    address registrant = registrar.ownerOf(tokenId);
    if (
      registrant != msg.sender &&
      !isApprovedForAll(registrant, msg.sender) &&
      !registrar.isApprovedForAll(registrant, msg.sender)
    ) {
      revert Unauthorised(_makeNode(ETH_NODE, bytes32(tokenId)), msg.sender);
    }

    // transfer the token from the user to this contract
    registrar.transferFrom(registrant, address(this), tokenId);

    // transfer the ens record back to the new owner (this contract)
    registrar.reclaim(tokenId, address(this));

    return _wrapETH2LD(label, wrappedOwner, fuses, expiry, resolver);
  }

  /**
   * @dev Registers a new .eth second-level domain and wraps it.
   *      Only callable by authorised controllers.
   * @param label The label to register (Eg, 'foo' for 'foo.eth').
   * @param wrappedOwner The owner of the wrapped name.
   * @param duration The duration, in seconds, to register the name for.
   * @param resolver The resolver address to set on the ENS registry (optional).
   * @param fuses initial fuses to set
   * @param expiry when the fuses will expire
   * @return registrarExpiry The expiry date of the new name on the .eth registrar, in seconds since the Unix epoch.
   */

  function registerAndWrapETH2LD(
    string calldata label,
    address wrappedOwner,
    uint256 duration,
    address resolver,
    uint32 fuses,
    uint64 expiry
  )
    external
    override
    onlyController
    onlyAuthorized
    returns (uint256 registrarExpiry)
  {
    uint256 tokenId = uint256(keccak256(bytes(label)));
    registrarExpiry = registrar.register(tokenId, address(this), duration);
    _wrapETH2LD(label, wrappedOwner, fuses, expiry, resolver);
  }

  /**
   * @dev Renews a .eth second-level domain.
   *      Only callable by authorised controllers.
   * @param tokenId The hash of the label to register (eg, `keccak256('foo')`, for 'foo.eth').
   * @param duration The number of seconds to renew the name for.
   * @return expires The expiry date of the name on the .eth registrar, in seconds since the Unix epoch.
   */
  function renew(
    uint256 tokenId,
    uint256 duration,
    uint64 expiry
  ) external override onlyController onlyAuthorized returns (uint256 expires) {
    bytes32 node = _makeNode(ETH_NODE, bytes32(tokenId));

    expires = registrar.renew(tokenId, duration);
    (address owner, uint32 fuses, uint64 oldExpiry) = getData(uint256(node));
    expiry = _normaliseExpiry(expiry, oldExpiry, uint64(expires));

    _setData(node, owner, fuses, expiry);
  }

  /**
   * @notice Wraps a non .eth domain, of any kind. Could be a DNSSEC name vitalik.xyz or a subdomain
   * @dev Can be called by the owner in the registry or an authorised caller in the registry
   * @param name The name to wrap, in DNS format
   * @param wrappedOwner Owner of the name in this contract
   * @param resolver resolver contract address
   */

  function wrap(
    bytes calldata name,
    address wrappedOwner,
    address resolver
  ) public override onlyAuthorized {
    (bytes32 labelhash, uint256 offset) = name.readLabel(0);
    bytes32 parentNode = name.namehash(offset);
    bytes32 node = _makeNode(parentNode, labelhash);

    if (parentNode == ETH_NODE) {
      revert IncompatibleParent();
    }

    address owner = ens.owner(node);

    if (
      owner != msg.sender &&
      !isApprovedForAll(owner, msg.sender) &&
      !ens.isApprovedForAll(owner, msg.sender)
    ) {
      revert Unauthorised(node, msg.sender);
    }

    if (resolver != address(0)) {
      ens.setResolver(node, resolver);
    }

    ens.setOwner(node, address(this));

    _wrap(node, name, wrappedOwner, 0, 0);
  }

  /**
   * @notice Unwraps a .eth domain. e.g. vitalik.eth
   * @dev Can be called by the owner in the wrapper or an authorised caller in the wrapper
   * @param labelhash labelhash of the .eth domain
   * @param newRegistrant sets the owner in the .eth registrar to this address
   * @param newController sets the owner in the registry to this address
   */

  function unwrapETH2LD(
    bytes32 labelhash,
    address newRegistrant,
    address newController
  )
    public
    override
    onlyAuthorized
    onlyTokenOwner(_makeNode(ETH_NODE, labelhash))
  {
    _unwrap(_makeNode(ETH_NODE, labelhash), newController);
    registrar.transferFrom(address(this), newRegistrant, uint256(labelhash));
  }

  /**
   * @notice Unwraps a non .eth domain, of any kind. Could be a DNSSEC name vitalik.xyz or a subdomain
   * @dev Can be called by the owner in the wrapper or an authorised caller in the wrapper
   * @param parentNode parent namehash of the name e.g. vitalik.xyz would be namehash('xyz')
   * @param labelhash labelhash of the name, e.g. vitalik.xyz would be keccak256('vitalik')
   * @param newController sets the owner in the registry to this address
   */

  function unwrap(
    bytes32 parentNode,
    bytes32 labelhash,
    address newController
  )
    public
    override
    onlyAuthorized
    onlyTokenOwner(_makeNode(parentNode, labelhash))
  {
    if (parentNode == ETH_NODE) {
      revert IncompatibleParent();
    }
    _unwrap(_makeNode(parentNode, labelhash), newController);
  }

  /**
   * @notice Sets fuses of a name
   * @param node namehash of the name
   * @param fuses fuses to burn (cannot burn PARENT_CANOT_CONTROL)
   */

  function setFuses(bytes32 node, uint32 fuses)
    public
    onlyTokenOwner(node)
    operationAllowed(node, CANNOT_BURN_FUSES)
    onlyAuthorized
    returns (uint32)
  {
    if (fuses & PARENT_CANNOT_CONTROL != 0) {
      // Only the parent can burn the PARENT_CANNOT_CONTROL fuse.
      revert Unauthorised(node, msg.sender);
    }

    (address owner, uint32 oldFuses, uint64 expiry) = getData(uint256(node));

    fuses |= oldFuses;
    _setFuses(node, owner, fuses, expiry);
    return fuses;
  }

  /**
   * @notice Add an operator that can interact with the contract
   * @param operator Address getting modified
   * @param authorization value of the authorization
   */

  function setAuthorization(address operator, bool authorization)
    public
    onlyOwner
  {
    authorized[operator] = authorization;
  }

  /**
   * @notice Upgrades a .eth wrapped domain by calling the wrapETH2LD function of the upgradeContract
   *     and burning the token of this contract
   * @dev Can be called by the owner of the name in this contract
   * @param label Label as a string of the .eth name to upgrade
   * @param wrappedOwner The owner of the wrapped name
   */

  function upgradeETH2LD(
    string calldata label,
    address wrappedOwner,
    address resolver
  ) public onlyAuthorized {
    bytes32 labelhash = keccak256(bytes(label));
    bytes32 node = _makeNode(ETH_NODE, labelhash);
    (uint32 fuses, uint64 expiry) = _prepareUpgrade(node);

    upgradeContract.wrapETH2LD(label, wrappedOwner, fuses, expiry, resolver);
  }

  /**
   * @notice Upgrades a non .eth domain of any kind. Could be a DNSSEC name vitalik.xyz or a subdomain
   * @dev Can be called by the owner or an authorised caller
   * Requires upgraded Namewrapper to permit old Namewrapper to call `setSubnodeRecord` for all names
   * @param parentNode namehash of the parent name
   * @param label Label as a string of the name to upgrade
   * @param wrappedOwner Owner of the name in this contract
   * @param resolver resolver contract for this name
   */

  function upgrade(
    bytes32 parentNode,
    string calldata label,
    address wrappedOwner,
    address resolver
  ) public onlyAuthorized {
    bytes32 labelhash = keccak256(bytes(label));
    bytes32 node = _makeNode(parentNode, labelhash);
    (uint32 fuses, uint64 expiry) = _prepareUpgrade(node);
    upgradeContract.setSubnodeRecord(
      parentNode,
      label,
      wrappedOwner,
      resolver,
      0,
      fuses,
      expiry
    );
  }

  /* @notice Sets fuses of a name that you own the parent of. Can also be called by the owner of a .eth name
   * @param parentNode parent namehash of the name e.g. vitalik.xyz would be namehash('xyz')
   * @param labelhash labelhash of the name, e.g. vitalik.xyz would be keccak256('vitalik')
   * @param fuses fuses to burn
   * @param expiry when the fuses will expire
   */

  function setChildFuses(
    bytes32 parentNode,
    bytes32 labelhash,
    uint32 fuses,
    uint64 expiry
  ) public onlyAuthorized {
    bytes32 node = _makeNode(parentNode, labelhash);
    (address owner, uint32 oldFuses, uint64 oldExpiry) = getData(uint256(node));
    uint64 maxExpiry;
    if (parentNode == ETH_NODE) {
      if (!isTokenOwnerOrApproved(node, msg.sender)) {
        revert Unauthorised(node, msg.sender);
      }
      // max expiry is set to the expiry on the registrar
      maxExpiry = uint64(registrar.nameExpires(uint256(labelhash)));
    } else {
      if (!isTokenOwnerOrApproved(parentNode, msg.sender)) {
        revert Unauthorised(node, msg.sender);
      }

      // max expiry is set to the expiry of the parent
      (, , maxExpiry) = getData(uint256(parentNode));
    }

    expiry = _normaliseExpiry(expiry, oldExpiry, maxExpiry);

    fuses |= oldFuses;
    _setFuses(node, owner, fuses, expiry);
  }

  /**
   * @notice Sets the subdomain owner in the registry and then wraps the subdomain
   * @param parentNode parent namehash of the subdomain
   * @param label label of the subdomain as a string
   * @param newOwner newOwner in the registry
   * @param fuses initial fuses for the wrapped subdomain
   * @param expiry when the fuses will expire
   */

  function setSubnodeOwner(
    bytes32 parentNode,
    string calldata label,
    address newOwner,
    uint32 fuses,
    uint64 expiry
  )
    public
    onlyTokenOwner(parentNode)
    canCallSetSubnodeOwner(parentNode, keccak256(bytes(label)))
    onlyAuthorized
    returns (bytes32 node)
  {
    bytes32 labelhash = keccak256(bytes(label));
    node = _makeNode(parentNode, labelhash);
    (, , expiry) = _getDataAndNormaliseExpiry(parentNode, node, expiry);

    if (ens.owner(node) != address(this)) {
      ens.setSubnodeOwner(parentNode, labelhash, address(this));
      _addLabelAndWrap(parentNode, node, label, newOwner, fuses, expiry);
    } else {
      _transferAndBurnFuses(node, newOwner, fuses, expiry);
    }
  }

  /**
   * @notice Sets the subdomain owner in the registry with records and then wraps the subdomain
   * @param parentNode parent namehash of the subdomain
   * @param label label of the subdomain as a string
   * @param newOwner newOwner in the registry
   * @param resolver resolver contract in the registry
   * @param ttl ttl in the regsitry
   * @param fuses initial fuses for the wrapped subdomain
   * @param expiry expiry date for the domain
   */

  function setSubnodeRecord(
    bytes32 parentNode,
    string memory label,
    address newOwner,
    address resolver,
    uint64 ttl,
    uint32 fuses,
    uint64 expiry
  )
    public
    onlyTokenOwner(parentNode)
    canCallSetSubnodeOwner(parentNode, keccak256(bytes(label)))
    onlyAuthorized
  {
    bytes32 labelhash = keccak256(bytes(label));
    bytes32 node = _makeNode(parentNode, labelhash);
    (, , expiry) = _getDataAndNormaliseExpiry(parentNode, node, expiry);
    if (ens.owner(node) != address(this)) {
      ens.setSubnodeRecord(parentNode, labelhash, address(this), resolver, ttl);
      _addLabelAndWrap(parentNode, node, label, newOwner, fuses, expiry);
    } else {
      ens.setSubnodeRecord(parentNode, labelhash, address(this), resolver, ttl);
      _transferAndBurnFuses(node, newOwner, fuses, expiry);
    }
  }

  /**
   * @notice Sets records for the name in the ENS Registry
   * @param node namehash of the name to set a record for
   * @param owner newOwner in the registry
   * @param resolver the resolver contract
   * @param ttl ttl in the registry
   */

  function setRecord(
    bytes32 node,
    address owner,
    address resolver,
    uint64 ttl
  )
    public
    override
    onlyTokenOwner(node)
    onlyAuthorized
    operationAllowed(
      node,
      CANNOT_TRANSFER | CANNOT_SET_RESOLVER | CANNOT_SET_TTL
    )
  {
    ens.setRecord(node, address(this), resolver, ttl);
    (address oldOwner, , ) = getData(uint256(node));
    _transfer(oldOwner, owner, uint256(node), 1, "");
  }

  /**
   * @notice Sets resolver contract in the registry
   * @param node namehash of the name
   * @param resolver the resolver contract
   */

  function setResolver(bytes32 node, address resolver)
    public
    override
    onlyTokenOwner(node)
    operationAllowed(node, CANNOT_SET_RESOLVER)
    onlyAuthorized
  {
    ens.setResolver(node, resolver);
  }

  /**
   * @notice Sets TTL in the registry
   * @param node namehash of the name
   * @param ttl TTL in the registry
   */

  function setTTL(bytes32 node, uint64 ttl)
    public
    override
    onlyTokenOwner(node)
    operationAllowed(node, CANNOT_SET_TTL)
    onlyAuthorized
  {
    ens.setTTL(node, ttl);
  }

  /**
   * @dev Allows an operation only if none of the specified fuses are burned.
   * @param node The namehash of the name to check fuses on.
   * @param fuseMask A bitmask of fuses that must not be burned.
   */

  modifier operationAllowed(bytes32 node, uint32 fuseMask) {
    (, uint32 fuses, ) = getData(uint256(node));
    if (fuses & fuseMask != 0) {
      revert OperationProhibited(node);
    }
    _;
  }

  /**
   * @notice Check whether a name can call setSubnodeOwner/setSubnodeRecord
   * @dev Checks both canCreateSubdomain and canReplaceSubdomain and whether not they have been burnt
   *      and checks whether the owner of the subdomain is 0x0 for creating or already exists for
   *      replacing a subdomain. If either conditions are true, then it is possible to call
   *      setSubnodeOwner
   * @param node namehash of the name to check
   * @param labelhash labelhash of the name to check
   */

  modifier canCallSetSubnodeOwner(bytes32 node, bytes32 labelhash) {
    bytes32 subnode = _makeNode(node, labelhash);
    address owner = ens.owner(subnode);

    if (owner == address(0)) {
      (, uint32 fuses, ) = getData(uint256(node));
      if (fuses & CANNOT_CREATE_SUBDOMAIN != 0) {
        revert OperationProhibited(node);
      }
    } else {
      (, uint32 subnodeFuses, ) = getData(uint256(subnode));
      if (subnodeFuses & PARENT_CANNOT_CONTROL != 0) {
        revert OperationProhibited(node);
      }
    }

    _;
  }

  /**
   * @notice Checks all Fuses in the mask are burned for the node
   * @param node namehash of the name
   * @param fuseMask the fuses you want to check
   * @return Boolean of whether or not all the selected fuses are burned
   */

  function allFusesBurned(bytes32 node, uint32 fuseMask)
    public
    view
    override
    returns (bool)
  {
    (, uint32 fuses, ) = getData(uint256(node));
    return fuses & fuseMask == fuseMask;
  }

  function onERC721Received(
    address to,
    address,
    uint256 tokenId,
    bytes calldata data
  ) public override returns (bytes4) {
    //check if it's the eth registrar ERC721
    if (msg.sender != address(registrar)) {
      revert IncorrectTokenType();
    }

    (
      string memory label,
      address owner,
      uint32 fuses,
      uint64 expiry,
      address resolver
    ) = abi.decode(data, (string, address, uint32, uint64, address));

    bytes32 labelhash = bytes32(tokenId);
    bytes32 labelhashFromData = keccak256(bytes(label));

    if (labelhashFromData != labelhash) {
      revert LabelMismatch(labelhashFromData, labelhash);
    }

    // transfer the ens record back to the new owner (this contract)
    registrar.reclaim(uint256(labelhash), address(this));

    _wrapETH2LD(label, owner, fuses, expiry, resolver);

    return IERC721Receiver(to).onERC721Received.selector;
  }

  /***** Internal functions */

  function _canTransfer(uint32 fuses) internal pure override returns (bool) {
    return fuses & CANNOT_TRANSFER == 0;
  }

  function _makeNode(bytes32 node, bytes32 labelhash)
    private
    pure
    returns (bytes32)
  {
    return keccak256(abi.encodePacked(node, labelhash));
  }

  function _addLabel(string memory label, bytes memory name)
    internal
    pure
    returns (bytes memory ret)
  {
    if (bytes(label).length < 1) {
      revert LabelTooShort();
    }
    if (bytes(label).length > 255) {
      revert LabelTooLong(label);
    }
    return abi.encodePacked(uint8(bytes(label).length), label, name);
  }

  function _mint(
    bytes32 node,
    address wrappedOwner,
    uint32 fuses,
    uint64 expiry
  ) internal override {
    address oldWrappedOwner = ownerOf(uint256(node));
    if (oldWrappedOwner != address(0)) {
      // burn and unwrap old token of old owner
      _burn(uint256(node));
      emit NameUnwrapped(node, address(0));
    }
    super._mint(node, wrappedOwner, fuses, expiry);
  }

  function _wrap(
    bytes32 node,
    bytes memory name,
    address wrappedOwner,
    uint32 fuses,
    uint64 expiry
  ) internal {
    names[node] = name;
    _canFusesBeBurned(node, fuses);
    _mint(node, wrappedOwner, fuses, expiry);
    emit NameWrapped(node, name, wrappedOwner, fuses, expiry);
  }

  function _addLabelAndWrap(
    bytes32 parentNode,
    bytes32 node,
    string memory label,
    address newOwner,
    uint32 fuses,
    uint64 expiry
  ) internal {
    bytes memory name = _addLabel(label, names[parentNode]);
    _wrap(node, name, newOwner, fuses, expiry);
  }

  function _prepareUpgrade(bytes32 node)
    private
    returns (uint32 fuses, uint64 expiry)
  {
    if (address(upgradeContract) == address(0)) {
      revert CannotUpgrade();
    }

    if (!isTokenOwnerOrApproved(node, msg.sender)) {
      revert Unauthorised(node, msg.sender);
    }

    (fuses, expiry) = getFuses(node);

    // burn token and fuse data
    _burn(uint256(node));
  }

  function _transferAndBurnFuses(
    bytes32 node,
    address newOwner,
    uint32 fuses,
    uint64 expiry
  ) internal {
    (address owner, , ) = getData(uint256(node));
    _transfer(owner, newOwner, uint256(node), 1, "");
    _setFuses(node, newOwner, fuses, expiry);
  }

  // wrapper function for stack limit
  function _getDataAndNormaliseExpiry(
    bytes32 parentNode,
    bytes32 node,
    uint64 expiry
  )
    internal
    view
    returns (
      address owner,
      uint32 fuses,
      uint64
    )
  {
    uint64 oldExpiry;
    (owner, fuses, oldExpiry) = getData(uint256(node));
    (, , uint64 maxExpiry) = getData(uint256(parentNode));

    expiry = _normaliseExpiry(expiry, oldExpiry, maxExpiry);
    return (owner, fuses, expiry);
  }

  function _getETH2LDDataAndNormaliseExpiry(
    bytes32 node,
    bytes32 labelhash,
    uint64 expiry
  )
    internal
    view
    returns (
      address owner,
      uint32 fuses,
      uint64
    )
  {
    uint64 oldExpiry;
    (owner, fuses, oldExpiry) = getData(uint256(node));
    uint64 maxExpiry = uint64(registrar.nameExpires(uint256(labelhash)));

    expiry = _normaliseExpiry(expiry, oldExpiry, maxExpiry);
    return (owner, fuses, expiry);
  }

  function _normaliseExpiry(
    uint64 expiry,
    uint64 oldExpiry,
    uint64 maxExpiry
  ) internal pure returns (uint64) {
    // Expiry cannot be more than maximum allowed
    // .eth names will check registrar, non .eth check parent
    if (expiry > maxExpiry) {
      expiry = maxExpiry;
    }
    // Expiry cannot be less than old expiry
    if (expiry < oldExpiry) {
      expiry = oldExpiry;
    }

    return expiry;
  }

  function _wrapETH2LD(
    string memory label,
    address wrappedOwner,
    uint32 fuses,
    uint64 expiry,
    address resolver
  ) private returns (uint64) {
    // Mint a new ERC1155 token with fuses
    // Set PARENT_CANNOT_REPLACE to reflect wrapper + registrar control over the 2LD
    bytes32 labelhash = keccak256(bytes(label));
    bytes32 node = _makeNode(ETH_NODE, labelhash);

    (, , expiry) = _getETH2LDDataAndNormaliseExpiry(node, labelhash, expiry);

    _addLabelAndWrap(
      ETH_NODE,
      node,
      label,
      wrappedOwner,
      fuses | PARENT_CANNOT_CONTROL,
      expiry
    );
    if (resolver != address(0)) {
      ens.setResolver(node, resolver);
    }

    return expiry;
  }

  function _unwrap(bytes32 node, address newOwner) private {
    if (newOwner == address(0x0) || newOwner == address(this)) {
      revert IncorrectTargetOwner(newOwner);
    }

    if (allFusesBurned(node, CANNOT_UNWRAP)) {
      revert OperationProhibited(node);
    }

    // Burn token and fuse data
    _burn(uint256(node));
    ens.setOwner(node, newOwner);

    emit NameUnwrapped(node, newOwner);
  }

  function _setFuses(
    bytes32 node,
    address owner,
    uint32 fuses,
    uint64 expiry
  ) internal {
    _setData(node, owner, fuses, expiry);
    emit FusesSet(node, fuses, expiry);
  }

  function _setData(
    bytes32 node,
    address owner,
    uint32 fuses,
    uint64 expiry
  ) internal {
    _canFusesBeBurned(node, fuses);
    super._setData(uint256(node), owner, fuses, expiry);
  }

  function _canFusesBeBurned(bytes32 node, uint32 fuses) internal pure {
    if (
      fuses & ~PARENT_CANNOT_CONTROL != 0 &&
      fuses & (PARENT_CANNOT_CONTROL | CANNOT_UNWRAP) !=
      (PARENT_CANNOT_CONTROL | CANNOT_UNWRAP)
    ) {
      revert OperationProhibited(node);
    }
  }
}

// File: @ensdomains/ens-contracts/contracts/wrapper/ERC1155Fuse.sol



//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "@openzeppelin/contracts/token/ERC1155/extensions/IERC1155MetadataURI.sol";
import "@openzeppelin/contracts/utils/Address.sol";

/* This contract is a variation on ERC1155 with the additions of _setData, getData and _canTransfer and ownerOf. _setData and getData allows the use of the other 96 bits next to the address of the owner for extra data. We use this to store 'fuses' that control permissions that can be burnt. 32 bits are used for the fuses themselves and 64 bits are used for the expiry of the name. When a name has expired, its fuses will be be set back to 0 */

error OperationProhibited(bytes32 node);

abstract contract ERC1155Fuse is ERC165, IERC1155, IERC1155MetadataURI {
    using Address for address;
    mapping(uint256 => uint256) public _tokens;

    // Mapping from owner to operator approvals
    mapping(address => mapping(address => bool)) private _operatorApprovals;

    /**************************************************************************
     * ERC721 methods
     *************************************************************************/

    function ownerOf(uint256 id) public view virtual returns (address) {
        (address owner, , ) = getData(id);
        return owner;
    }

    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId)
        public
        view
        virtual
        override(ERC165, IERC165)
        returns (bool)
    {
        return
            interfaceId == type(IERC1155).interfaceId ||
            interfaceId == type(IERC1155MetadataURI).interfaceId ||
            super.supportsInterface(interfaceId);
    }

    /**
     * @dev See {IERC1155-balanceOf}.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     */
    function balanceOf(address account, uint256 id)
        public
        view
        virtual
        override
        returns (uint256)
    {
        require(
            account != address(0),
            "ERC1155: balance query for the zero address"
        );
        (address owner, , ) = getData(id);
        if (owner == account) {
            return 1;
        }
        return 0;
    }

    /**
     * @dev See {IERC1155-balanceOfBatch}.
     *
     * Requirements:
     *
     * - `accounts` and `ids` must have the same length.
     */
    function balanceOfBatch(address[] memory accounts, uint256[] memory ids)
        public
        view
        virtual
        override
        returns (uint256[] memory)
    {
        require(
            accounts.length == ids.length,
            "ERC1155: accounts and ids length mismatch"
        );

        uint256[] memory batchBalances = new uint256[](accounts.length);

        for (uint256 i = 0; i < accounts.length; ++i) {
            batchBalances[i] = balanceOf(accounts[i], ids[i]);
        }

        return batchBalances;
    }

    /**
     * @dev See {IERC1155-setApprovalForAll}.
     */
    function setApprovalForAll(address operator, bool approved)
        public
        virtual
        override
    {
        require(
            msg.sender != operator,
            "ERC1155: setting approval status for self"
        );

        _operatorApprovals[msg.sender][operator] = approved;
        emit ApprovalForAll(msg.sender, operator, approved);
    }

    /**
     * @dev See {IERC1155-isApprovedForAll}.
     */
    function isApprovedForAll(address account, address operator)
        public
        view
        virtual
        override
        returns (bool)
    {
        return _operatorApprovals[account][operator];
    }

    /**
     * @dev Returns the Name's owner address and fuses
     */
    function getData(uint256 tokenId)
        public
        view
        returns (
            address owner,
            uint32 fuses,
            uint64 expiry
        )
    {
        uint256 t = _tokens[tokenId];
        owner = address(uint160(t));
        expiry = uint64(t >> 192);
        if (block.timestamp > expiry) {
            fuses = 0;
        } else {
            fuses = uint32(t >> 160);
        }
    }

    /**
     * @dev Sets the Name's owner address and fuses
     */
    function _setData(
        uint256 tokenId,
        address owner,
        uint32 fuses,
        uint64 expiry
    ) internal virtual {
        _tokens[tokenId] =
            uint256(uint160(owner)) |
            (uint256(fuses) << 160) |
            (uint256(expiry) << 192);
    }

    /**
     * @dev See {IERC1155-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 id,
        uint256 amount,
        bytes memory data
    ) public virtual override {
        require(to != address(0), "ERC1155: transfer to the zero address");
        require(
            from == msg.sender || isApprovedForAll(from, msg.sender),
            "ERC1155: caller is not owner nor approved"
        );

        _transfer(from, to, id, amount, data);
    }

    /**
     * @dev See {IERC1155-safeBatchTransferFrom}.
     */
    function safeBatchTransferFrom(
        address from,
        address to,
        uint256[] memory ids,
        uint256[] memory amounts,
        bytes memory data
    ) public virtual override {
        require(
            ids.length == amounts.length,
            "ERC1155: ids and amounts length mismatch"
        );
        require(to != address(0), "ERC1155: transfer to the zero address");
        require(
            from == msg.sender || isApprovedForAll(from, msg.sender),
            "ERC1155: transfer caller is not owner nor approved"
        );

        for (uint256 i = 0; i < ids.length; ++i) {
            uint256 id = ids[i];
            uint256 amount = amounts[i];

            (address oldOwner, uint32 fuses, uint64 expiration) = getData(id);

            if (!_canTransfer(fuses)) {
                revert OperationProhibited(bytes32(id));
            }

            require(
                amount == 1 && oldOwner == from,
                "ERC1155: insufficient balance for transfer"
            );
            _setData(id, to, fuses, expiration);
        }

        emit TransferBatch(msg.sender, from, to, ids, amounts);

        _doSafeBatchTransferAcceptanceCheck(
            msg.sender,
            from,
            to,
            ids,
            amounts,
            data
        );
    }

    /**************************************************************************
     * Internal/private methods
     *************************************************************************/

    function _canTransfer(uint32 fuses) internal virtual returns (bool);

    function _mint(
        bytes32 node,
        address newOwner,
        uint32 fuses,
        uint64 expiry
    ) internal virtual {
        uint256 tokenId = uint256(node);
        address owner = ownerOf(tokenId);
        require(owner == address(0), "ERC1155: mint of existing token");
        require(newOwner != address(0), "ERC1155: mint to the zero address");
        require(
            newOwner != address(this),
            "ERC1155: newOwner cannot be the NameWrapper contract"
        );

        _setData(tokenId, newOwner, fuses, expiry);
        emit TransferSingle(msg.sender, address(0x0), newOwner, tokenId, 1);
        _doSafeTransferAcceptanceCheck(
            msg.sender,
            address(0),
            newOwner,
            tokenId,
            1,
            ""
        );
    }

    function _burn(uint256 tokenId) internal virtual {
        address owner = ownerOf(tokenId);
        // Clear fuses and set owner to 0
        _setData(tokenId, address(0x0), 0, 0);
        emit TransferSingle(msg.sender, owner, address(0x0), tokenId, 1);
    }

    function _transfer(
        address from,
        address to,
        uint256 id,
        uint256 amount,
        bytes memory data
    ) internal {
        (address oldOwner, uint32 fuses, uint64 expiry) = getData(id);
        if (oldOwner == to) {
            return;
        }

        if (!_canTransfer(fuses)) {
            revert OperationProhibited(bytes32(id));
        }

        require(
            amount == 1 && oldOwner == from,
            "ERC1155: insufficient balance for transfer"
        );
        _setData(id, to, fuses, expiry);

        emit TransferSingle(msg.sender, from, to, id, amount);

        _doSafeTransferAcceptanceCheck(msg.sender, from, to, id, amount, data);
    }

    function _doSafeTransferAcceptanceCheck(
        address operator,
        address from,
        address to,
        uint256 id,
        uint256 amount,
        bytes memory data
    ) private {
        if (to.isContract()) {
            try
                IERC1155Receiver(to).onERC1155Received(
                    operator,
                    from,
                    id,
                    amount,
                    data
                )
            returns (bytes4 response) {
                if (
                    response != IERC1155Receiver(to).onERC1155Received.selector
                ) {
                    revert("ERC1155: ERC1155Receiver rejected tokens");
                }
            } catch Error(string memory reason) {
                revert(reason);
            } catch {
                revert("ERC1155: transfer to non ERC1155Receiver implementer");
            }
        }
    }

    function _doSafeBatchTransferAcceptanceCheck(
        address operator,
        address from,
        address to,
        uint256[] memory ids,
        uint256[] memory amounts,
        bytes memory data
    ) private {
        if (to.isContract()) {
            try
                IERC1155Receiver(to).onERC1155BatchReceived(
                    operator,
                    from,
                    ids,
                    amounts,
                    data
                )
            returns (bytes4 response) {
                if (
                    response !=
                    IERC1155Receiver(to).onERC1155BatchReceived.selector
                ) {
                    revert("ERC1155: ERC1155Receiver rejected tokens");
                }
            } catch Error(string memory reason) {
                revert(reason);
            } catch {
                revert("ERC1155: transfer to non ERC1155Receiver implementer");
            }
        }
    }
}

// File: @ensdomains/ens-contracts/contracts/wrapper/Controllable.sol



//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts/access/Ownable.sol";

contract Controllable is Ownable {
    mapping(address=>bool) public controllers;

    event ControllerChanged(address indexed controller, bool active);

    function setController(address controller, bool active) onlyOwner() public {
        controllers[controller] = active;
        emit ControllerChanged(controller, active);
    }

    modifier onlyController() {
        require(controllers[msg.sender], "Controllable: Caller is not a controller");
        _;
    }
}

// File: @ensdomains/ens-contracts/contracts/wrapper/INameWrapper.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "../registry/ENS.sol";
import "../ethregistrar/IBaseRegistrar.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "./IMetadataService.sol";

uint32 constant CANNOT_UNWRAP = 1;
uint32 constant CANNOT_BURN_FUSES = 2;
uint32 constant CANNOT_TRANSFER = 4;
uint32 constant CANNOT_SET_RESOLVER = 8;
uint32 constant CANNOT_SET_TTL = 16;
uint32 constant CANNOT_CREATE_SUBDOMAIN = 32;
uint32 constant PARENT_CANNOT_CONTROL = 64;
uint32 constant CAN_DO_EVERYTHING = 0;

interface INameWrapper is IERC1155 {
    event NameWrapped(
        bytes32 indexed node,
        bytes name,
        address owner,
        uint32 fuses,
        uint64 expiry
    );

    event NameUnwrapped(bytes32 indexed node, address owner);

    event FusesSet(bytes32 indexed node, uint32 fuses, uint64 expiry);

    function ens() external view returns (ENS);

    function registrar() external view returns (IBaseRegistrar);

    function metadataService() external view returns (IMetadataService);

    function names(bytes32) external view returns (bytes memory);

    function wrap(
        bytes calldata name,
        address wrappedOwner,
        address resolver
    ) external;

    function wrapETH2LD(
        string calldata label,
        address wrappedOwner,
        uint32 fuses,
        uint64 _expiry,
        address resolver
    ) external returns (uint64 expiry);

    function registerAndWrapETH2LD(
        string calldata label,
        address wrappedOwner,
        uint256 duration,
        address resolver,
        uint32 fuses,
        uint64 expiry
    ) external returns (uint256 registrarExpiry);

    function renew(
        uint256 labelHash,
        uint256 duration,
        uint64 expiry
    ) external returns (uint256 expires);

    function unwrap(
        bytes32 node,
        bytes32 label,
        address owner
    ) external;

    function unwrapETH2LD(
        bytes32 label,
        address newRegistrant,
        address newController
    ) external;

    function setFuses(bytes32 node, uint32 fuses)
        external
        returns (uint32 newFuses);

    function setChildFuses(
        bytes32 parentNode,
        bytes32 labelhash,
        uint32 fuses,
        uint64 expiry
    ) external;

    function setSubnodeRecord(
        bytes32 node,
        string calldata label,
        address owner,
        address resolver,
        uint64 ttl,
        uint32 fuses,
        uint64 expiry
    ) external;

    function setRecord(
        bytes32 node,
        address owner,
        address resolver,
        uint64 ttl
    ) external;

    function setSubnodeOwner(
        bytes32 node,
        string calldata label,
        address newOwner,
        uint32 fuses,
        uint64 expiry
    ) external returns (bytes32);

    function isTokenOwnerOrApproved(bytes32 node, address addr)
        external
        returns (bool);

    function setResolver(bytes32 node, address resolver) external;

    function setTTL(bytes32 node, uint64 ttl) external;

    function ownerOf(uint256 id) external returns (address owner);

    function getFuses(bytes32 node)
        external
        returns (uint32 fuses, uint64 expiry);

    function allFusesBurned(bytes32 node, uint32 fuseMask)
        external
        view
        returns (bool);
}

// File: @ensdomains/ens-contracts/contracts/wrapper/INameWrapperUpgrade.sol



//SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

interface INameWrapperUpgrade {
    function setSubnodeRecord(
        bytes32 node,
        string calldata label,
        address owner,
        address resolver,
        uint64 ttl,
        uint32 fuses,
        uint64 expiry
    ) external;

    function wrapETH2LD(
        string calldata label,
        address wrappedOwner,
        uint32 fuses,
        uint64 expiry,
        address resolver
    ) external;
}

// File: @ensdomains/ens-contracts/contracts/wrapper/IMetadataService.sol



pragma solidity >=0.8.4;

interface IMetadataService {
    function uri(uint256) external view returns (string memory);
}

// File: @ensdomains/ens-contracts/contracts/registry/ENS.sol



pragma solidity >=0.8.4;

interface ENS {
    // Logged when the owner of a node assigns a new owner to a subnode.
    event NewOwner(bytes32 indexed node, bytes32 indexed label, address owner);

    // Logged when the owner of a node transfers ownership to a new account.
    event Transfer(bytes32 indexed node, address owner);

    // Logged when the resolver for a node changes.
    event NewResolver(bytes32 indexed node, address resolver);

    // Logged when the TTL of a node changes
    event NewTTL(bytes32 indexed node, uint64 ttl);

    // Logged when an operator is added or removed.
    event ApprovalForAll(
        address indexed owner,
        address indexed operator,
        bool approved
    );

    function setRecord(
        bytes32 node,
        address owner,
        address resolver,
        uint64 ttl
    ) external;

    function setSubnodeRecord(
        bytes32 node,
        bytes32 label,
        address owner,
        address resolver,
        uint64 ttl
    ) external;

    function setSubnodeOwner(
        bytes32 node,
        bytes32 label,
        address owner
    ) external returns (bytes32);

    function setResolver(bytes32 node, address resolver) external;

    function setOwner(bytes32 node, address owner) external;

    function setTTL(bytes32 node, uint64 ttl) external;

    function setApprovalForAll(address operator, bool approved) external;

    function owner(bytes32 node) external view returns (address);

    function resolver(bytes32 node) external view returns (address);

    function ttl(bytes32 node) external view returns (uint64);

    function recordExists(bytes32 node) external view returns (bool);

    function isApprovedForAll(address owner, address operator)
        external
        view
        returns (bool);
}

// File: @ensdomains/ens-contracts/contracts/ethregistrar/IBaseRegistrar.sol



import "../registry/ENS.sol";
import "./IBaseRegistrar.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";

interface IBaseRegistrar is IERC721 {

    event ControllerAdded(address indexed controller);
    event ControllerRemoved(address indexed controller);
    event NameMigrated(
        uint256 indexed id,
        address indexed owner,
        uint256 expires
    );
    event NameRegistered(
        uint256 indexed id,
        address indexed owner,
        uint256 expires
    );
    event NameRenewed(uint256 indexed id, uint256 expires);
    // Authorises a controller, who can register and renew domains.
    function addController(address controller) external;

    // Revoke controller permission for an address.
    function removeController(address controller) external;

    // Set the resolver for the TLD this registrar manages.
    function setResolver(address resolver) external;

    // Returns the expiration timestamp of the specified label hash.
    function nameExpires(uint256 id) external view returns (uint256);

    // Returns true iff the specified name is available for registration.
    function available(uint256 id) external view returns (bool);

    /**
     * @dev Register a name.
     */
    function register(
        uint256 id,
        address owner,
        uint256 duration
    ) external returns (uint256);

    function renew(uint256 id, uint256 duration) external returns (uint256);

    /**
     * @dev Reclaim ownership of a name in ENS, if you own it in the registrar.
     */
    function reclaim(uint256 id, address owner) external;
}

// File: @openzeppelin/contracts/token/ERC721/IERC721Receiver.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC721/IERC721Receiver.sol)

pragma solidity ^0.8.0;

/**
 * @title ERC721 token receiver interface
 * @dev Interface for any contract that wants to support safeTransfers
 * from ERC721 asset contracts.
 */
interface IERC721Receiver {
    /**
     * @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
     * by `operator` from `from`, this function is called.
     *
     * It must return its Solidity selector to confirm the token transfer.
     * If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
     *
     * The selector can be obtained in Solidity with `IERC721Receiver.onERC721Received.selector`.
     */
    function onERC721Received(
        address operator,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) external returns (bytes4);
}

// File: @openzeppelin/contracts/access/Ownable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

// File: @ensdomains/ens-contracts/contracts/wrapper/BytesUtil.sol



//SPDX-License-Identifier: MIT
pragma solidity >=0.8.4;

library BytesUtils {
    /*
    * @dev Returns the keccak-256 hash of a byte range.
    * @param self The byte string to hash.
    * @param offset The position to start hashing at.
    * @param len The number of bytes to hash.
    * @return The hash of the byte range.
    */
    function keccak(bytes memory self, uint offset, uint len) internal pure returns (bytes32 ret) {
        require(offset + len <= self.length);
        assembly {
            ret := keccak256(add(add(self, 32), offset), len)
        }
    }

    /**
     * @dev Returns the ENS namehash of a DNS-encoded name.
     * @param self The DNS-encoded name to hash.
     * @param offset The offset at which to start hashing.
     * @return The namehash of the name.
     */
    function namehash(bytes memory self, uint offset) internal pure returns(bytes32) {
        (bytes32 labelhash, uint newOffset) = readLabel(self, offset);
        if(labelhash == bytes32(0)) {
            require(offset == self.length - 1, "namehash: Junk at end of name");
            return bytes32(0);
        }
        return keccak256(abi.encodePacked(namehash(self, newOffset), labelhash));
    }
    
    /**
     * @dev Returns the keccak-256 hash of a DNS-encoded label, and the offset to the start of the next label.
     * @param self The byte string to read a label from.
     * @param idx The index to read a label at.
     * @return labelhash The hash of the label at the specified index, or 0 if it is the last label.
     * @return newIdx The index of the start of the next label.
     */
    function readLabel(bytes memory self, uint256 idx) internal pure returns (bytes32 labelhash, uint newIdx) {
        require(idx < self.length, "readLabel: Index out of bounds");
        uint len = uint(uint8(self[idx]));
        if(len > 0) {
            labelhash = keccak(self, idx + 1, len);
        } else {
            labelhash = bytes32(0);
        }
        newIdx = idx + len + 1;
    }
}

// File: @openzeppelin/contracts/utils/introspection/ERC165.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/ERC165.sol)

pragma solidity ^0.8.0;

import "./IERC165.sol";

/**
 * @dev Implementation of the {IERC165} interface.
 *
 * Contracts that want to implement ERC165 should inherit from this contract and override {supportsInterface} to check
 * for the additional interface id that will be supported. For example:
 *
 * ```solidity
 * function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
 *     return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId);
 * }
 * ```
 *
 * Alternatively, {ERC165Storage} provides an easier to use but more expensive implementation.
 */
abstract contract ERC165 is IERC165 {
    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
        return interfaceId == type(IERC165).interfaceId;
    }
}

// File: @openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC1155/IERC1155Receiver.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev _Available since v3.1._
 */
interface IERC1155Receiver is IERC165 {
    /**
     * @dev Handles the receipt of a single ERC1155 token type. This function is
     * called at the end of a `safeTransferFrom` after the balance has been updated.
     *
     * NOTE: To accept the transfer, this must return
     * `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))`
     * (i.e. 0xf23a6e61, or its own function selector).
     *
     * @param operator The address which initiated the transfer (i.e. msg.sender)
     * @param from The address which previously owned the token
     * @param id The ID of the token being transferred
     * @param value The amount of tokens being transferred
     * @param data Additional data with no specified format
     * @return `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` if transfer is allowed
     */
    function onERC1155Received(
        address operator,
        address from,
        uint256 id,
        uint256 value,
        bytes calldata data
    ) external returns (bytes4);

    /**
     * @dev Handles the receipt of a multiple ERC1155 token types. This function
     * is called at the end of a `safeBatchTransferFrom` after the balances have
     * been updated.
     *
     * NOTE: To accept the transfer(s), this must return
     * `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))`
     * (i.e. 0xbc197c81, or its own function selector).
     *
     * @param operator The address which initiated the batch transfer (i.e. msg.sender)
     * @param from The address which previously owned the token
     * @param ids An array containing ids of each token being transferred (order and length must match values array)
     * @param values An array containing amounts of each token being transferred (order and length must match ids array)
     * @param data Additional data with no specified format
     * @return `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))` if transfer is allowed
     */
    function onERC1155BatchReceived(
        address operator,
        address from,
        uint256[] calldata ids,
        uint256[] calldata values,
        bytes calldata data
    ) external returns (bytes4);
}

// File: @openzeppelin/contracts/token/ERC1155/IERC1155.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC1155/IERC1155.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC1155 compliant contract, as defined in the
 * https://eips.ethereum.org/EIPS/eip-1155[EIP].
 *
 * _Available since v3.1._
 */
interface IERC1155 is IERC165 {
    /**
     * @dev Emitted when `value` tokens of token type `id` are transferred from `from` to `to` by `operator`.
     */
    event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);

    /**
     * @dev Equivalent to multiple {TransferSingle} events, where `operator`, `from` and `to` are the same for all
     * transfers.
     */
    event TransferBatch(
        address indexed operator,
        address indexed from,
        address indexed to,
        uint256[] ids,
        uint256[] values
    );

    /**
     * @dev Emitted when `account` grants or revokes permission to `operator` to transfer their tokens, according to
     * `approved`.
     */
    event ApprovalForAll(address indexed account, address indexed operator, bool approved);

    /**
     * @dev Emitted when the URI for token type `id` changes to `value`, if it is a non-programmatic URI.
     *
     * If an {URI} event was emitted for `id`, the standard
     * https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[guarantees] that `value` will equal the value
     * returned by {IERC1155MetadataURI-uri}.
     */
    event URI(string value, uint256 indexed id);

    /**
     * @dev Returns the amount of tokens of token type `id` owned by `account`.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     */
    function balanceOf(address account, uint256 id) external view returns (uint256);

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {balanceOf}.
     *
     * Requirements:
     *
     * - `accounts` and `ids` must have the same length.
     */
    function balanceOfBatch(address[] calldata accounts, uint256[] calldata ids)
        external
        view
        returns (uint256[] memory);

    /**
     * @dev Grants or revokes permission to `operator` to transfer the caller's tokens, according to `approved`,
     *
     * Emits an {ApprovalForAll} event.
     *
     * Requirements:
     *
     * - `operator` cannot be the caller.
     */
    function setApprovalForAll(address operator, bool approved) external;

    /**
     * @dev Returns true if `operator` is approved to transfer ``account``'s tokens.
     *
     * See {setApprovalForAll}.
     */
    function isApprovedForAll(address account, address operator) external view returns (bool);

    /**
     * @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
     *
     * Emits a {TransferSingle} event.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - If the caller is not `from`, it must have been approved to spend ``from``'s tokens via {setApprovalForAll}.
     * - `from` must have a balance of tokens of type `id` of at least `amount`.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
     * acceptance magic value.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 id,
        uint256 amount,
        bytes calldata data
    ) external;

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {safeTransferFrom}.
     *
     * Emits a {TransferBatch} event.
     *
     * Requirements:
     *
     * - `ids` and `amounts` must have the same length.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
     * acceptance magic value.
     */
    function safeBatchTransferFrom(
        address from,
        address to,
        uint256[] calldata ids,
        uint256[] calldata amounts,
        bytes calldata data
    ) external;
}

// File: @openzeppelin/contracts/token/ERC1155/extensions/IERC1155MetadataURI.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC1155/extensions/IERC1155MetadataURI.sol)

pragma solidity ^0.8.0;

import "../IERC1155.sol";

/**
 * @dev Interface of the optional ERC1155MetadataExtension interface, as defined
 * in the https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[EIP].
 *
 * _Available since v3.1._
 */
interface IERC1155MetadataURI is IERC1155 {
    /**
     * @dev Returns the URI for token type `id`.
     *
     * If the `\{id\}` substring is present in the URI, it must be replaced by
     * clients with the actual token type ID.
     */
    function uri(uint256 id) external view returns (string memory);
}

// File: @openzeppelin/contracts/utils/Address.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (utils/Address.sol)

pragma solidity ^0.8.1;

/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     * ====
     *
     * [IMPORTANT]
     * ====
     * You shouldn't rely on `isContract` to protect against flash loan attacks!
     *
     * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
     * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
     * constructor.
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize/address.code.length, which returns 0
        // for contracts in construction, since the code is only stored at the end
        // of the constructor execution.

        return account.code.length > 0;
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

        (bool success, ) = recipient.call{value: amount}("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain `call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionCall(target, data, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        require(isContract(target), "Address: call to non-contract");

        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        require(isContract(target), "Address: static call to non-contract");

        (bool success, bytes memory returndata) = target.staticcall(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionDelegateCall(target, data, "Address: low-level delegate call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(isContract(target), "Address: delegate call to non-contract");

        (bool success, bytes memory returndata) = target.delegatecall(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason using the provided one.
     *
     * _Available since v4.3._
     */
    function verifyCallResult(
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal pure returns (bytes memory) {
        if (success) {
            return returndata;
        } else {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly
                /// @solidity memory-safe-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}

// File: @openzeppelin/contracts/utils/introspection/IERC165.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[EIP].
 *
 * Implementers can declare support of contract interfaces, which can then be
 * queried by others ({ERC165Checker}).
 *
 * For an implementation, see {ERC165}.
 */
interface IERC165 {
    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}

// File: @openzeppelin/contracts/utils/Context.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

// File: @openzeppelin/contracts/token/ERC721/IERC721.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC721/IERC721.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC721 compliant contract.
 */
interface IERC721 is IERC165 {
    /**
     * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
     */
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
     */
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
     */
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

    /**
     * @dev Returns the number of tokens in ``owner``'s account.
     */
    function balanceOf(address owner) external view returns (uint256 balance);

    /**
     * @dev Returns the owner of the `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function ownerOf(uint256 tokenId) external view returns (address owner);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes calldata data
    ) external;

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must have been allowed to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Transfers `tokenId` token from `from` to `to`.
     *
     * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Gives permission to `to` to transfer `tokenId` token to another account.
     * The approval is cleared when the token is transferred.
     *
     * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
     *
     * Requirements:
     *
     * - The caller must own the token or be an approved operator.
     * - `tokenId` must exist.
     *
     * Emits an {Approval} event.
     */
    function approve(address to, uint256 tokenId) external;

    /**
     * @dev Approve or remove `operator` as an operator for the caller.
     * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
     *
     * Requirements:
     *
     * - The `operator` cannot be the caller.
     *
     * Emits an {ApprovalForAll} event.
     */
    function setApprovalForAll(address operator, bool _approved) external;

    /**
     * @dev Returns the account approved for `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function getApproved(uint256 tokenId) external view returns (address operator);

    /**
     * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
     *
     * See {setApprovalForAll}
     */
    function isApprovedForAll(address owner, address operator) external view returns (bool);
}