Skip to content

Commit

Permalink
refactor: register receiving name dns-encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
pikonha committed Oct 9, 2024
1 parent 6514a6d commit ad4c037
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 30 deletions.
12 changes: 3 additions & 9 deletions packages/client/src/l2.write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ import { privateKeyToAccount } from 'viem/accounts'

import { abi as uAbi } from '@blockful/contracts/out/UniversalResolver.sol/UniversalResolver.json'
import { abi as l1Abi } from '@blockful/contracts/out/L1Resolver.sol/L1Resolver.json'
import {
getRevertErrorData,
getChain,
extractLabelFromName,
extractParentFromName,
} from './client'
import { getRevertErrorData, getChain } from './client'

config({
path: process.env.ENV_FILE || '../.env',
Expand Down Expand Up @@ -58,7 +53,7 @@ const _ = (async () => {
throw new Error('RESOLVER_ADDRESS is required')
}

const name = normalize('gibi.blockful.eth')
const name = normalize('gibi.arb.eth')
const node = namehash(name)
const signer = privateKeyToAccount(privateKey as Hex)

Expand Down Expand Up @@ -112,8 +107,7 @@ const _ = (async () => {
functionName: 'register',
abi: l1Abi,
args: [
namehash(extractParentFromName(name)), // parent
extractLabelFromName(name), // label
toHex(name), // name
signer.address, // owner
duration,
`0x${'a'.repeat(64)}` as Hex, // secret
Expand Down
17 changes: 8 additions & 9 deletions packages/contracts/script/local/L2ArbitrumResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {StaticMetadataService} from
"@ens-contracts/wrapper/StaticMetadataService.sol";
import {IMetadataService} from "@ens-contracts/wrapper/IMetadataService.sol";
import {PublicResolver} from "@ens-contracts/resolvers/PublicResolver.sol";
import {NameEncoder} from "@ens-contracts/utils/NameEncoder.sol";

import {ENSHelper} from "../ENSHelper.sol";
import {SubdomainController} from "../../src/SubdomainController.sol";
Expand Down Expand Up @@ -81,9 +82,7 @@ contract L2ArbitrumResolver is Script, ENSHelper {
uint256 subdomainPrice = 0.001 ether;
uint256 commitTime = 0;
SubdomainController subdomainController = new SubdomainController(
address(nameWrapper),
subdomainPrice,
commitTime
address(nameWrapper), subdomainPrice, commitTime
);
nameWrapper.setApprovalForAll(address(subdomainController), true);

Expand All @@ -101,16 +100,16 @@ contract L2ArbitrumResolver is Script, ENSHelper {
"arb", msg.sender, 31556952000, address(arbResolver), 1
);

(bytes memory name, bytes32 node) =
NameEncoder.dnsEncodeName("blockful.arb.eth");

bytes[] memory data = new bytes[](1);
data[0] = abi.encodeWithSelector(
TextResolver.setText.selector,
namehash("blockful.arb.eth"),
"com.twitter",
"@blockful"
TextResolver.setText.selector, node, "com.twitter", "@blockful"
);

subdomainController.register{value: subdomainController.price()}(
namehash("arb.eth"),
"blockful",
name,
msg.sender,
31556952000,
keccak256("secret"),
Expand Down
6 changes: 2 additions & 4 deletions packages/contracts/src/L1Resolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ contract L1Resolver is

/**
* Forwards the registering of a subdomain to the L2 contracts
* @param -parentNode namehash of the parent node
* @param -label The name to be registered.
* @param -name The DNS-encoded name to be registered.
* @param -owner Owner of the domain
* @param -duration duration The duration in seconds of the registration.
* @param -secret The secret to be used for the registration based on commit/reveal
Expand All @@ -115,8 +114,7 @@ contract L1Resolver is
* @param -extraData any encoded additional data
*/
function register(
bytes32, /* parentNode */
string calldata, /* label */
bytes calldata, /* name */
address, /* owner */
uint256, /* duration */
bytes32, /* secret */
Expand Down
48 changes: 44 additions & 4 deletions packages/contracts/src/SubdomainController.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import "forge-std/console.sol";

import {INameWrapper} from "@ens-contracts/wrapper/INameWrapper.sol";
import {Resolver} from "@ens-contracts/resolvers/Resolver.sol";
import {BytesUtils} from "@ens-contracts/utils/BytesUtils.sol";

import {ENSHelper} from "../script/ENSHelper.sol";
import {OffchainRegister} from "./interfaces/OffchainResolver.sol";

contract SubdomainController is OffchainRegister, ENSHelper {

using BytesUtils for bytes;

uint256 public price;
uint256 public commitTime;
INameWrapper nameWrapper;
Expand All @@ -24,10 +29,9 @@ contract SubdomainController is OffchainRegister, ENSHelper {
}

function register(
bytes32 parentNode,
string calldata label,
bytes calldata name,
address owner,
uint256 duration,
uint256 duration,
bytes32, /* secret */
address resolver,
bytes[] calldata data,
Expand All @@ -39,7 +43,11 @@ contract SubdomainController is OffchainRegister, ENSHelper {
payable
override
{
bytes32 node = keccak256(abi.encodePacked(parentNode, labelhash(label)));
bytes32 node = _getNode(name);
string memory label = _getLabel(name);

(, uint256 offset) = name.readLabel(0);
bytes32 parentNode = name.namehash(offset);

require(
nameWrapper.ownerOf(uint256(node)) == address(0),
Expand All @@ -56,4 +64,36 @@ contract SubdomainController is OffchainRegister, ENSHelper {
}
}

function _getNode(bytes memory name) private pure returns (bytes32 node) {
return _getNode(name, 0);
}

function _getNode(
bytes memory name,
uint256 offset
)
private
pure
returns (bytes32 node)
{
uint256 len = name.readUint8(offset);
node = bytes32(0);
if (len > 0) {
bytes32 label = name.keccak(offset + 1, len);
bytes32 parentNode = _getNode(name, offset + len + 1);
node = keccak256(abi.encodePacked(parentNode, label));
}
return node;
}

function _getLabel(bytes calldata name)
private
pure
returns (string memory)
{
uint256 labelLength = uint256(uint8(name[0]));
if (labelLength == 0) return "";
return string(name[1:labelLength + 1]);
}

}
6 changes: 2 additions & 4 deletions packages/contracts/src/interfaces/OffchainResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ interface OffchainRegister {

/**
* Forwards the registering of a domain to the L2 contracts
* @param parentNode namehash of the parent node
* @param label The name to be registered.
* @param name DNS-encoded name to be registered.
* @param owner Owner of the domain
* @param duration duration The duration in seconds of the registration.
* @param secret The secret to be used for the registration based on commit/reveal
Expand All @@ -17,8 +16,7 @@ interface OffchainRegister {
* @param extraData any encoded additional data
*/
function register(
bytes32 parentNode,
string calldata label,
bytes calldata name,
address owner,
uint256 duration,
bytes32 secret,
Expand Down
Loading

0 comments on commit ad4c037

Please sign in to comment.