Skip to content

Commit

Permalink
Merge pull request #342 from ensdomains/fix/unify-utility-contracts
Browse files Browse the repository at this point in the history
unify utility contracts
  • Loading branch information
mdtanrikulu authored Apr 4, 2024
2 parents 63ca27e + 8dd3f11 commit b479cd2
Show file tree
Hide file tree
Showing 34 changed files with 217 additions and 121 deletions.
2 changes: 1 addition & 1 deletion contracts/dnsregistrar/DNSClaimChecker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
pragma solidity ^0.8.4;

import "../dnssec-oracle/DNSSEC.sol";
import "../dnssec-oracle/BytesUtils.sol";
import "../dnssec-oracle/RRUtils.sol";
import "../utils/BytesUtils.sol";
import "../utils/HexUtils.sol";
import "@ensdomains/buffer/contracts/Buffer.sol";

Expand Down
2 changes: 1 addition & 1 deletion contracts/dnsregistrar/DNSRegistrar.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ pragma solidity ^0.8.4;

import "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import "@ensdomains/buffer/contracts/Buffer.sol";
import "../dnssec-oracle/BytesUtils.sol";
import "../dnssec-oracle/DNSSEC.sol";
import "../dnssec-oracle/RRUtils.sol";
import "../registry/ENSRegistry.sol";
import "../root/Root.sol";
import "../resolvers/profiles/AddrResolver.sol";
import "../utils/BytesUtils.sol";
import "./DNSClaimChecker.sol";
import "./PublicSuffixList.sol";
import "./IDNSRegistrar.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/dnsregistrar/OffchainDNSResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import "../../contracts/resolvers/profiles/IAddrResolver.sol";
import "../../contracts/resolvers/profiles/IExtendedResolver.sol";
import "../../contracts/resolvers/profiles/IExtendedDNSResolver.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import "../dnssec-oracle/BytesUtils.sol";
import "../dnssec-oracle/DNSSEC.sol";
import "../dnssec-oracle/RRUtils.sol";
import "../registry/ENSRegistry.sol";
import "../utils/HexUtils.sol";
import "../utils/BytesUtils.sol";

import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {LowLevelCallUtils} from "../utils/LowLevelCallUtils.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/dnsregistrar/RecordParser.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

import "../dnssec-oracle/BytesUtils.sol";
import "../utils/BytesUtils.sol";

library RecordParser {
using BytesUtils for bytes;
Expand Down
2 changes: 1 addition & 1 deletion contracts/dnsregistrar/TLDPublicSuffixList.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pragma solidity ^0.8.4;

import "../dnssec-oracle/BytesUtils.sol";
import "../utils/BytesUtils.sol";
import "./PublicSuffixList.sol";

/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/dnsregistrar/mocks/DummyParser.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pragma solidity ^0.8.4;

import "../../dnssec-oracle/BytesUtils.sol";
import "../../utils/BytesUtils.sol";
import "../RecordParser.sol";

contract DummyParser {
Expand Down
2 changes: 1 addition & 1 deletion contracts/dnssec-oracle/DNSSECImpl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ pragma solidity ^0.8.4;
pragma experimental ABIEncoderV2;

import "./Owned.sol";
import "./BytesUtils.sol";
import "./RRUtils.sol";
import "./DNSSEC.sol";
import "./algorithms/Algorithm.sol";
import "./digests/Digest.sol";
import "../utils/BytesUtils.sol";
import "@ensdomains/buffer/contracts/Buffer.sol";

/*
Expand Down
2 changes: 1 addition & 1 deletion contracts/dnssec-oracle/RRUtils.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pragma solidity ^0.8.4;

import "./BytesUtils.sol";
import "../utils/BytesUtils.sol";
import "@ensdomains/buffer/contracts/Buffer.sol";

/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/dnssec-oracle/algorithms/P256SHA256Algorithm.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pragma solidity ^0.8.4;

import "./Algorithm.sol";
import "./EllipticCurve.sol";
import "../BytesUtils.sol";
import "../../utils/BytesUtils.sol";

contract P256SHA256Algorithm is Algorithm, EllipticCurve {
using BytesUtils for *;
Expand Down
2 changes: 1 addition & 1 deletion contracts/dnssec-oracle/algorithms/RSASHA1Algorithm.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pragma solidity ^0.8.4;

import "./Algorithm.sol";
import "../BytesUtils.sol";
import "./RSAVerify.sol";
import "../../utils/BytesUtils.sol";
import "@ensdomains/solsha1/contracts/SHA1.sol";

/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/dnssec-oracle/algorithms/RSASHA256Algorithm.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pragma solidity ^0.8.4;

import "./Algorithm.sol";
import "../BytesUtils.sol";
import "./RSAVerify.sol";
import "../../utils/BytesUtils.sol";

/**
* @dev Implements the DNSSEC RSASHA256 algorithm.
Expand Down
2 changes: 1 addition & 1 deletion contracts/dnssec-oracle/algorithms/RSAVerify.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pragma solidity ^0.8.4;

import "../BytesUtils.sol";
import "./ModexpPrecompile.sol";
import "../../utils/BytesUtils.sol";

library RSAVerify {
/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/dnssec-oracle/digests/SHA1Digest.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pragma solidity ^0.8.4;

import "./Digest.sol";
import "../BytesUtils.sol";
import "../../utils/BytesUtils.sol";
import "@ensdomains/solsha1/contracts/SHA1.sol";

/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/dnssec-oracle/digests/SHA256Digest.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pragma solidity ^0.8.4;

import "./Digest.sol";
import "../BytesUtils.sol";
import "../../utils/BytesUtils.sol";

/**
* @dev Implements the DNSSEC SHA256 digest.
Expand Down
2 changes: 1 addition & 1 deletion contracts/ethregistrar/ETHRegistrarController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ~0.8.17;

import {BaseRegistrarImplementation} from "./BaseRegistrarImplementation.sol";
import {StringUtils} from "./StringUtils.sol";
import {StringUtils} from "../utils/StringUtils.sol";
import {Resolver} from "../resolvers/Resolver.sol";
import {ENS} from "../registry/ENS.sol";
import {ReverseRegistrar} from "../reverseRegistrar/ReverseRegistrar.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/ethregistrar/StablePriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ~0.8.17;

import "./IPriceOracle.sol";
import "./StringUtils.sol";
import "../utils/StringUtils.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/introspection/IERC165.sol";

Expand Down
32 changes: 0 additions & 32 deletions contracts/ethregistrar/StringUtils.sol

This file was deleted.

2 changes: 1 addition & 1 deletion contracts/resolvers/profiles/ExtendedDNSResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "../../resolvers/profiles/IAddressResolver.sol";
import "../../resolvers/profiles/IAddrResolver.sol";
import "../../resolvers/profiles/ITextResolver.sol";
import "../../utils/HexUtils.sol";
import "../../dnssec-oracle/BytesUtils.sol";
import "../../utils/BytesUtils.sol";

/**
* @dev Resolves names on ENS by interpreting record data stored in a DNS TXT record.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,46 @@ library BytesUtils {
}
}

/**
* @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,
uint256 offset
) internal pure returns (bytes32) {
(bytes32 labelhash, uint256 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, uint256 newIdx) {
require(idx < self.length, "readLabel: Index out of bounds");
uint256 len = uint256(uint8(self[idx]));
if (len > 0) {
labelhash = keccak(self, idx + 1, len);
} else {
labelhash = bytes32(0);
}
newIdx = idx + len + 1;
}

/*
* @dev Returns a positive number if `other` comes lexicographically after
* `self`, a negative number if it comes before, or zero if the
Expand Down
25 changes: 25 additions & 0 deletions contracts/utils/HexUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,29 @@ library HexUtils {
(bytes32 r, bool valid) = hexStringToBytes32(str, idx, lastIdx);
return (address(uint160(uint256(r))), valid);
}

/**
* @dev Attempts to convert an address to a hex string
* @param addr The _addr to parse
*/
function addressToHex(address addr) internal pure returns (string memory) {
bytes memory hexString = new bytes(40);
for (uint i = 0; i < 20; i++) {
bytes1 byteValue = bytes1(uint8(uint160(addr) >> (8 * (19 - i))));
bytes1 highNibble = bytes1(uint8(byteValue) / 16);
bytes1 lowNibble = bytes1(
uint8(byteValue) - 16 * uint8(highNibble)
);
hexString[2 * i] = _nibbleToHexChar(highNibble);
hexString[2 * i + 1] = _nibbleToHexChar(lowNibble);
}
return string(hexString);
}

function _nibbleToHexChar(
bytes1 nibble
) internal pure returns (bytes1 hexChar) {
if (uint8(nibble) < 10) return bytes1(uint8(nibble) + 0x30);
else return bytes1(uint8(nibble) + 0x57);
}
}
2 changes: 1 addition & 1 deletion contracts/utils/NameEncoder.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import {BytesUtils} from "../wrapper/BytesUtils.sol";
import {BytesUtils} from "../utils/BytesUtils.sol";

library NameEncoder {
using BytesUtils for bytes;
Expand Down
85 changes: 85 additions & 0 deletions contracts/utils/StringUtils.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
pragma solidity >=0.8.4;

library StringUtils {
/**
* @dev Returns the length of a given string
*
* @param s The string to measure the length of
* @return The length of the input string
*/
function strlen(string memory s) internal pure returns (uint256) {
uint256 len;
uint256 i = 0;
uint256 bytelength = bytes(s).length;
for (len = 0; i < bytelength; len++) {
bytes1 b = bytes(s)[i];
if (b < 0x80) {
i += 1;
} else if (b < 0xE0) {
i += 2;
} else if (b < 0xF0) {
i += 3;
} else if (b < 0xF8) {
i += 4;
} else if (b < 0xFC) {
i += 5;
} else {
i += 6;
}
}
return len;
}

/**
* @dev Escapes special characters in a given string
*
* @param str The string to escape
* @return The escaped string
*/
function escape(string memory str) internal pure returns (string memory) {
bytes memory strBytes = bytes(str);
uint extraChars = 0;

// count extra space needed for escaping
for (uint i = 0; i < strBytes.length; i++) {
if (_needsEscaping(strBytes[i])) {
extraChars++;
}
}

// allocate buffer with the exact size needed
bytes memory buffer = new bytes(strBytes.length + extraChars);
uint index = 0;

// escape characters
for (uint i = 0; i < strBytes.length; i++) {
if (_needsEscaping(strBytes[i])) {
buffer[index++] = "\\";
buffer[index++] = _getEscapedChar(strBytes[i]);
} else {
buffer[index++] = strBytes[i];
}
}
return string(buffer);
}
// determine if a character needs escaping
function _needsEscaping(bytes1 char) private pure returns (bool) {
return
char == '"' ||
char == "/" ||
char == "\\" ||
char == "\n" ||
char == "\r" ||
char == "\t";
}
// get the escaped character
function _getEscapedChar(bytes1 char) private pure returns (bytes1) {
if (char == "\n") return "n";
if (char == "\r") return "r";
if (char == "\t") return "t";
return char;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//SPDX-License-Identifier: MIT
pragma solidity ~0.8.17;

import {BytesUtils} from "../BytesUtils.sol";
import {BytesUtils} from "./BytesUtils.sol";

contract TestBytesUtils {
using BytesUtils for *;
Expand Down
2 changes: 1 addition & 1 deletion contracts/utils/UniversalResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {LowLevelCallUtils} from "./LowLevelCallUtils.sol";
import {ENS} from "../registry/ENS.sol";
import {IExtendedResolver} from "../resolvers/profiles/IExtendedResolver.sol";
import {Resolver, INameResolver, IAddrResolver} from "../resolvers/Resolver.sol";
import {BytesUtils} from "../utils/BytesUtils.sol";
import {NameEncoder} from "./NameEncoder.sol";
import {BytesUtils} from "../wrapper/BytesUtils.sol";
import {HexUtils} from "./HexUtils.sol";

error OffchainLookup(
Expand Down
Loading

0 comments on commit b479cd2

Please sign in to comment.