Skip to content

Commit

Permalink
Review of #40
Browse files Browse the repository at this point in the history
  • Loading branch information
sshmatrix committed Aug 14, 2023
1 parent e8d969e commit 971afa6
Show file tree
Hide file tree
Showing 8 changed files with 494 additions and 567 deletions.
516 changes: 309 additions & 207 deletions src/CCIP2ETH.sol

Large diffs are not rendered by default.

51 changes: 38 additions & 13 deletions src/GatewayManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ contract GatewayManager is iERC173, iGatewayManager {

/// @dev - Errors
error ContenthashNotImplemented(bytes1 _type);
error ResolverFunctionNotImplemented(bytes4 func);
error InvalidRequest(string _message);
error UnimplementedFeature(bytes4 func);

/// @dev - Contract owner/multisig address
address public owner;
Expand Down Expand Up @@ -63,42 +64,56 @@ contract GatewayManager is iERC173, iGatewayManager {
* @return gateways - Pseudo-random list of gateway URLs for CCIP-Read
* Gateway URL e.g. https://gateway.tld/ipns/fe501017200...51735c/.well-known/eth/virgil/avatar.json?t=0x0123456789
*/
function randomGateways(bytes calldata _recordhash, string memory _path, uint256 seed)
function randomGateways(bytes memory _recordhash, string memory _path, uint256 seed)
public
view
returns (string[] memory gateways)
{
/// @dev Filter recordhash vs. web2 gateway
if (_recordhash.length == 32) {
// Short IPNS hash
_recordhash = abi.encodePacked(hex"e5010172002408011220", _recordhash);
} else if (iGatewayManager(this).isWeb2(_recordhash)) {
// Web2 fallback
gateways = new string[](1);
gateways[0] = string.concat(string(_recordhash), _path, ".json?t={data}");
return gateways;
}
unchecked {
uint256 gLen = Gateways.length;
uint256 len = (gLen / 2) + 2;
if (len > 4) len = 4;
gateways = new string[](len);
uint256 i;
if (bytes8(_recordhash[:8]) == bytes8("https://")) {
gateways[0] = string.concat(string(_recordhash), _path, ".json?t={data}");
return gateways;
}
if (bytes(PrimaryGateway).length > 0) {
gateways[i++] = string.concat(
"https://", formatSubdomain(_recordhash), ".", PrimaryGateway, _path, ".json?t={data}"
"https://",
iGatewayManager(this).formatSubdomain(_recordhash),
".",
PrimaryGateway,
_path,
".json?t={data}"
);
}
string memory _fullPath;
bytes1 _prefix = _recordhash[0];
if (_prefix == 0xe2) {
_fullPath = string.concat(
"/api/v0/dag/get?arg=f", bytesToHexString(_recordhash, 2), _path, ".json?t={data}&format=dag-cbor"
"/api/v0/dag/get?arg=f",
bytesToHexString(_recordhash, _recordhash[1] == 0xe5 ? 3 : 2),
_path,
".json?t={data}&format=dag-cbor"
);
} else if (_prefix == 0xe5) {
_fullPath = string.concat("/ipns/f", bytesToHexString(_recordhash, 2), _path, ".json?t={data}");
} else if (_prefix == 0xe3) {
_fullPath = string.concat("/ipfs/f", bytesToHexString(_recordhash, 2), _path, ".json?t={data}");
} else if (_prefix == bytes1("k")) {
_fullPath = string.concat("/ipns/", string(_recordhash), _path, ".json?t={data}");
} else if (bytes2(_recordhash[:2]) == bytes2("ba")) {
} else if (_prefix == bytes1("b")) {
_fullPath = string.concat("/ipfs/", string(_recordhash), _path, ".json?t={data}");
} else {
revert("UNSUPPORTED_RECORDHASH");
revert InvalidRequest("BAD_RECORDHASH");
}
while (i < len) {
seed = uint256(keccak256(abi.encodePacked(block.number * i, seed)));
Expand All @@ -110,10 +125,20 @@ contract GatewayManager is iERC173, iGatewayManager {
/**
* Note - Future Feature in CCIP2-v2
*/
function __fallback(bytes memory response, bytes memory extradata) external pure returns (bytes memory) {
function __fallback(bytes calldata response, bytes calldata extradata) external view returns (bytes memory) {
this;
response;
extradata;
revert("NOT_YET_IMPLEMENTED");
revert UnimplementedFeature(iGatewayManager.__fallback.selector);
}

/**
* @dev Checks if recordhash is a web2 gateway
* @param _recordhash - Recordhash to check
* @return - Bool
*/
function isWeb2(bytes calldata _recordhash) external pure returns (bool) {
return (bytes8(_recordhash[:8]) == bytes8("https://"));
}

/**
Expand Down Expand Up @@ -146,7 +171,7 @@ contract GatewayManager is iERC173, iGatewayManager {
}
_jsonPath = string.concat("dns/", uintToString(resource));
} else {
revert ResolverFunctionNotImplemented(func);
revert UnimplementedFeature(func);
}
}

Expand Down
16 changes: 13 additions & 3 deletions src/Interface.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,17 @@ interface iCCIP2ETH is iENSIP10 {
external
view
returns (address _signer);
function setRecordhash(bytes32 _node, bytes calldata _contenthash) external;
function setOwnerhash(bytes calldata _contenthash) external;
function setSubRecordhash(string[] memory _subdomains, bytes32 _node, bytes calldata _contenthash) external;
function setRecordhash(bytes32 _node, bytes calldata _recordhash) external payable;
function setShortRecordhash(bytes32 _node, bytes32 _recordhash) external payable;
function setOwnerhash(bytes calldata _recordhash) external payable;
function redirectService(bytes calldata _encoded, bytes calldata _requested)
external
view
returns (bytes4 _selector, bytes32 _namehash, bytes memory _redirectRequest, string memory _domain);
function setDeepSubRecordhash(bytes32 _node, string[] memory _subdomains, bytes calldata _recordhash)
external
payable;
function setSubRecordhash(bytes32 _node, string memory _subdomain, bytes calldata _recordhash) external payable;
}

interface iGatewayManager is iERC173 {
Expand All @@ -60,6 +68,8 @@ interface iGatewayManager is iERC173 {
function addGateway(string calldata _domain) external;
function removeGateway(uint256 _index) external;
function replaceGateway(uint256 _index, string calldata _domain) external;
function formatSubdomain(bytes calldata _recordhash) external pure returns (string memory result);
function isWeb2(bytes calldata _recordhash) external pure returns (bool);
}

interface iUtils {}
Expand Down
Loading

0 comments on commit 971afa6

Please sign in to comment.