Skip to content

Commit

Permalink
feat: works with current L2 resolver (without ERC721)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julink-eth committed Apr 11, 2023
1 parent 0c967d0 commit 01fdc66
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 55 deletions.
1 change: 1 addition & 0 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const provider = new ethers.providers.JsonRpcProvider(l1_provider_url, {
const name = program.args[0];
console.log("name", name);
const resolverFound = await provider.getResolver(name);
console.log("resolverFound address", resolverFound?.address);
const node = namehash.hash(name);
console.log("node", node);

Expand Down
27 changes: 15 additions & 12 deletions packages/contracts/contracts/l1/LineaResolverStub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import { Lib_SecureMerkleTrie } from "@eth-optimism/contracts/libraries/trie/Lib
import { Lib_RLPReader } from "@eth-optimism/contracts/libraries/rlp/Lib_RLPReader.sol";
import { Lib_BytesUtils } from "@eth-optimism/contracts/libraries/utils/Lib_BytesUtils.sol";

import "hardhat/console.sol";

struct L2StateProof {
uint64 nodeIndex;
bytes32 blockHash;
bytes sendRoot;
bytes encodedBlockArray;
bytes stateTrieWitness;
bytes32 stateRoot;
bytes storageTrieWitness;
bytes node;
bytes result;
}

interface IResolverService {
Expand Down Expand Up @@ -84,7 +82,7 @@ contract LineaResolverStub is IExtendedResolver, SupportsInterface {
gateways,
callData,
LineaResolverStub.resolveWithProof.selector,
abi.encode(name)
data
);
}

Expand All @@ -95,18 +93,28 @@ contract LineaResolverStub is IExtendedResolver, SupportsInterface {
bytes calldata response,
bytes calldata extraData
) external view returns (bytes memory) {
// We only resolve if the addr(bytes32) is called otherwise we simply return an empty response
bytes4 signature = bytes4(extraData[0:4]);
if (signature != bytes4(0x3b3b57de)) {
return abi.encode("");
}

// This is the hash name of the domain name
bytes32 node = abi.decode(extraData[4:], (bytes32));

L2StateProof memory proof = abi.decode(response, (L2StateProof));
// bytes32 node = abi.decode(extraData, (bytes32));
// step 2: check blockHash against encoded block array
require(
proof.blockHash == keccak256(proof.encodedBlockArray),
"blockHash encodedBlockArray mismatch"
);

// step 3: check storage value from derived value
// Here the node used should be in extra data but we need to find a way
// to convert extra data to an ens hashname in solidity, in the meantime we use
// the node sent by the gateway
bytes32 slot = keccak256(abi.encodePacked(proof.node, uint256(1)));
bytes32 slot = keccak256(abi.encodePacked(node, uint256(1)));
bytes32 value = getStorageValue(
l2resolver,
slot,
Expand All @@ -115,12 +123,7 @@ contract LineaResolverStub is IExtendedResolver, SupportsInterface {
proof.storageTrieWitness
);

require(
keccak256(proof.result) == keccak256(abi.encode(value)),
"LineaResolverStub: value different from expected result"
);

return proof.result;
return abi.encode(value);
}

function getl2Resolver() external view returns (address) {
Expand Down
45 changes: 2 additions & 43 deletions packages/gateway/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { Server } from "@chainlink/ccip-read-server";
import { Command } from "commander";
import { ethers, BytesLike } from "ethers";
import { ethers } from "ethers";
import { Result } from "ethers/lib/utils";

const IResolverAbi = require("../../contracts/artifacts/contracts/l1/LineaResolverStub.sol/IResolverService.json")
.abi;
const IResolverL2Abi = require("../../contracts/artifacts/contracts/l2/LineaResolver.sol/LineaResolver.json")
.abi;
import { abi as Resolver_abi } from "@ensdomains/ens-contracts/artifacts/contracts/resolvers/Resolver.sol/Resolver.json";
const Resolver = new ethers.utils.Interface(Resolver_abi);
const rollupAbi = require("../abi/rollup.json");
const { BigNumber } = ethers;
const program = new Command();
Expand Down Expand Up @@ -59,9 +55,9 @@ server.add(IResolverAbi, [
console.log("name", name);
const node = ethers.utils.namehash(name);
console.log("node", node);
const addrSlot = ethers.utils.keccak256(node + "00".repeat(31) + "01");

if (debug) {
const addrSlot = ethers.utils.keccak256(node + "00".repeat(31) + "01");
const to = request?.to;
console.log(1, {
node,
Expand All @@ -88,7 +84,6 @@ server.add(IResolverAbi, [
}

const lastBlockFinalized = await rollup.lastFinalizedBatchHeight();
const stateRootHash = await rollup.stateRootHash();
const blockNumber = lastBlockFinalized.toNumber();
console.log(`Last block number finalized on L2 : ${blockNumber}`);
const block = await l2provider.getBlock(blockNumber);
Expand Down Expand Up @@ -130,19 +125,12 @@ server.add(IResolverAbi, [
(proof.storageProof as any[]).filter((x) => x.key === slot)[0].proof
);

// Result that will returned to the client after verification of the proof
const { result } = await getResult(name, data);

const finalProof = {
nodeIndex: blockNumber,
blockHash,
sendRoot: stateRootHash,
encodedBlockArray,
stateTrieWitness: accountProof,
stateRoot,
storageTrieWitness: storageProof,
node,
result,
};
console.log(7, { finalProof });
return [finalProof];
Expand All @@ -163,32 +151,3 @@ function decodeDnsName(dnsname: Buffer) {
}
return labels.join(".");
}

async function getResult(
name: string,
data: string
): Promise<{ result: BytesLike }> {
// Parse the data nested inside the second argument to `resolve`
const { signature, args } = Resolver.parseTransaction({ data });
console.log("signature", signature);

if (ethers.utils.nameprep(name) !== name) {
throw new Error("Name must be normalised");
}

if (ethers.utils.namehash(name) !== args[0]) {
throw new Error("Name does not match namehash");
}

const resolverL2 = await new ethers.Contract(
l2_resolver_address,
IResolverL2Abi,
l2provider
);
const node = ethers.utils.namehash(name);
const result = await resolverL2.addr(node);

return {
result: Resolver.encodeFunctionResult(signature, [result]),
};
}

0 comments on commit 01fdc66

Please sign in to comment.