Skip to content

Commit

Permalink
Add hexToAddress to extract address
Browse files Browse the repository at this point in the history
  • Loading branch information
makoto committed Feb 21, 2024
1 parent 0629aea commit ebb7bee
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 30 deletions.
39 changes: 26 additions & 13 deletions crosschain-reverse-resolver/contracts/L1ReverseResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import "@ensdomains/ens-contracts/contracts/resolvers/profiles/ITextResolver.sol
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";
// import "@ensdomains/ens-contracts/contracts/reverseRegistrar/IDefaultReverseResolver.sol";
import "@ensdomains/ens-contracts/contracts/utils/HexUtils.sol";
// import "@ensdomains/ens-contracts/contracts/wrapper/BytesUtils.sol";
import "@ensdomains/ens-contracts/contracts/dnssec-oracle/BytesUtils.sol";
import "hardhat/console.sol";

interface IDefaultReverseResolver {
Expand All @@ -28,7 +30,7 @@ interface IDefaultReverseResolver {
) external view returns (string memory);
}

contract L1ReverseResolver is EVMFetchTarget, INameResolver, ITextResolver, ERC165 {
contract L1ReverseResolver is EVMFetchTarget, ERC165 {
using EVMFetcher for EVMFetcher.EVMFetchRequest;
IEVMVerifier immutable verifier;
address immutable target;
Expand All @@ -44,36 +46,49 @@ contract L1ReverseResolver is EVMFetchTarget, INameResolver, ITextResolver, ERC1
defaultReverseResolver = _defaultReverseResolver;
}

function resolve(bytes calldata name, bytes calldata data) external view returns (bytes memory result) {
bytes4 selector = bytes4(data);
if (selector == INameResolver.name.selector) {
(bytes32 node) = abi.decode(data[4:], (bytes32));
(address addr,) = HexUtils.hexToAddress(name, 0, BytesUtils.readUint8(name,0));
return bytes(_name(node, addr));
}
if (selector == ITextResolver.text.selector) {
(bytes32 node, string memory key) = abi.decode(data[4:], (bytes32, string));
return bytes(text(node, key));
}
}

/**
* Returns the address associated with an ENS node.
* @param node The ENS node to query.
* @return The associated name.
*/
// return versionable_names[recordVersions[node]][node];
function name(bytes32 node) public view returns (string memory) {
// function name(bytes32 node) public view returns (string memory) {
function _name(bytes32 node, address addr) private view returns (string memory) {
EVMFetcher.newFetchRequest(verifier, target)
.getStatic(RECORD_VERSIONS_SLOT)
.element(node)
.getDynamic(VERSIONABLE_NAME_SLOT)
.ref(0)
.element(node)
.fetch(this.nameCallback.selector, msg.data); // recordVersions
.fetch(this.nameCallback.selector, abi.encodePacked(addr)); // recordVersions
}

function nameCallback(
bytes[] memory values,
bytes memory callbackdata
) public view returns (bytes memory) {
// ) public view returns (string memory) {
return callbackdata;

) public view returns (string memory) {
// ) public view returns (string memory) {
if(values[1].length == 0 ){
(address addr, ) = callbackdata.hexToAddress(0, callbackdata.length);
// This returns 0x0000000000000000000000000000000000000000
return string(abi.encodePacked(addr));
// return addr;
// // return defaultReverseResolver.name(addr);
// return defaultReverseResolver.name(addr);
}else{
// return "foo2";
// return string(values[1]);
return string(values[1]);
}
}

Expand All @@ -85,7 +100,7 @@ contract L1ReverseResolver is EVMFetchTarget, INameResolver, ITextResolver, ERC1
*/
function text(
bytes32 node,
string calldata key
string memory key
) public view returns (string memory) {
EVMFetcher.newFetchRequest(verifier, target)
.getStatic(RECORD_VERSIONS_SLOT)
Expand All @@ -108,8 +123,6 @@ contract L1ReverseResolver is EVMFetchTarget, INameResolver, ITextResolver, ERC1
bytes4 interfaceId
) public override view returns (bool) {
return
interfaceId == type(ITextResolver).interfaceId ||
interfaceId == type(INameResolver).interfaceId ||
super.supportsInterface(interfaceId);
}
}
60 changes: 43 additions & 17 deletions crosschain-reverse-resolver/test/testReverseResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import { FetchRequest } from 'ethers';
import { ethers } from 'hardhat';
import { EthereumProvider } from 'hardhat/types';
import request from 'supertest';
import packet from 'dns-packet';
const NAMESPACE = 2147483658 // OP
const encodeName = (name) => '0x' + packet.name.encode(name).toString('hex')
const labelhash = (label) => ethers.keccak256(ethers.toUtf8Bytes(label))

type ethersObj = typeof ethersT &
Omit<HardhatEthersHelpers, 'provider'> & {
provider: Omit<HardhatEthersProvider, '_hardhatProvider'> & {
Expand Down Expand Up @@ -96,35 +100,53 @@ describe('Crosschain Reverse Resolver', () => {
await provider.send('evm_mine', []);
});

it("should test name", async() => {
it.only("should test name", async() => {
const name = 'vitalik.eth'
const encodedname = encodeName(name)
const node = await l2contract.node(
await signer.getAddress(),
)
await l2contract.clearRecords(await signer.getAddress())
console.log({address:await signer.getAddress()})
await l2contract.clearRecords(await signer.getAddress())
await l2contract.setName(name)
await provider.send("evm_mine", []);
const result2 = await target.name(node, { enableCcipRead: true })
expect(result2).to.equal(name);
const i = new ethers.Interface(["function name(bytes32) returns(string)"])
const calldata = i.encodeFunctionData("name", [node])
const result2 = await target.resolve(encodedname, calldata, { enableCcipRead: true })
// throws Error: invalid length for result data
// const decoded = i.decodeFunctionResult("name", result2)
expect(ethers.toUtf8String(result2)).to.equal(name);
})

it.only("should test fallback name", async() => {
const testSigner = new ethers.Wallet('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80');
const testAddress = testSigner.address
console.log(1, {testSigner})
const name = 'myname.eth'
console.log(2)
const node = await defaultReverseResolver.node(testAddress)
console.log(3)
const reverseLabel = testAddress.substring(2).toLowerCase()
const reverseLabelHash = labelhash(reverseLabel)

const defaultReverseName = `${reverseLabel}.default.reverse`
const defaultReverseNode = ethers.namehash(defaultReverseName)
const encodedDefaultReverseName = encodeName(defaultReverseName)

const l2ReverseName = `${reverseLabel}.${NAMESPACE}.reverse`
const l2ReverseNode = ethers.namehash(l2ReverseName)
const encodedL2ReverseName = encodeName(l2ReverseName)

console.log(2,{
reverseLabel,reverseLabelHash,
defaultReverseName, defaultReverseNode, encodedDefaultReverseName,
l2ReverseName, l2ReverseNode, encodedL2ReverseName,
})
const funcId = ethers
.id('setNameForAddrWithSignature(address,string,uint256,bytes)')
.substring(0, 10)
console.log(4)

console.log(4)
const block = await provider.getBlock('latest')
console.log(5, {signer})
const inceptionDate = block?.timestamp
const account = await signer.getAddress()
console.log(6, {funcId, name, inceptionDate, account})
console.log(6, {funcId, testAddress, inceptionDate})
const message = ethers.solidityPackedKeccak256(
['bytes32', 'address', 'uint256', 'uint256'],
[ethers.solidityPackedKeccak256(['bytes4', 'string'], [funcId, name]), testAddress, inceptionDate, 0],
Expand All @@ -140,14 +162,18 @@ describe('Crosschain Reverse Resolver', () => {
signature,
)
console.log(8)
// await l2contract.clearRecords(await signer.getAddress())
await l2contract.setName(name)

await provider.send("evm_mine", []);
console.log(10)
const result2 = await target.name(node, { enableCcipRead: true })
expect(await defaultReverseResolver.name(testAddress)).to.equal(name)
// const result2 = await target.name(node, { enableCcipRead: true })
const i = new ethers.Interface(["function name(bytes32) returns(string)"])
const calldata = i.encodeFunctionData("name", [l2ReverseNode])
console.log(10, {
l2ReverseNode,
encodedL2ReverseName, calldata
})
const result2 = await target.resolve(encodedL2ReverseName, calldata, { enableCcipRead: true })
console.log(11, {result2})
expect(result2).to.equal(name);
expect(ethers.toUtf8String(result2)).to.equal(name);
// const name = 'vitalik.eth'
// const node = await l2contract.node(
// await signer.getAddress(),
Expand Down

0 comments on commit ebb7bee

Please sign in to comment.