From 75373f7d07badc551af354d4112388961a639fcd Mon Sep 17 00:00:00 2001 From: Makoto Inoue <2630+makoto@users.noreply.github.com> Date: Mon, 13 Nov 2023 18:13:45 +0000 Subject: [PATCH] Add getaddr --- crosschain-resolver/README.md | 15 ++++++++- crosschain-resolver/package.json | 1 + crosschain-resolver/scripts/getaddr.ts | 46 ++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 crosschain-resolver/scripts/getaddr.ts diff --git a/crosschain-resolver/README.md b/crosschain-resolver/README.md index ff132c63..a51473a8 100644 --- a/crosschain-resolver/README.md +++ b/crosschain-resolver/README.md @@ -108,9 +108,18 @@ const l2resolverAddress = await DelegatableResolverFactory.predictAddress(OWNER_ await DelegatableResolverFactory.create(OWNER_ADDRESS) await DelegatableResolverFactory['setAddr(bytes32,address)'](node, OWNER_ADDRESS) // On L1 -await L1Resolver['addr(bytes32)'](node, {enableCcipRead:true}) +const abi = [ + "function addr(bytes32) view returns (address)", + "function resolve(bytes,bytes) view returns (bytes)", +] +const i = new ethers.Interface(abi) +const calldata = i.encodeFunctionData("addr", [node]) +const result2 = await l1resolver.resolve(encodedname, calldata, { enableCcipRead: true }) +const address = i.decodeFunctionResult("addr", result2)[0] ``` +NOTE: The l1 resolver must be queried through `resolve` function to handle subnames + Or run the script ``` @@ -121,4 +130,8 @@ DEPLOYER_PRIVATE_KEY=$DEPLOYER_PRIVATE_KEY L1_PROVIDER_URL=$L1_PROVIDER_URL L2_P DEPLOYER_PRIVATE_KEY=$DEPLOYER_PRIVATE_KEY L1_PROVIDER_URL=$L1_PROVIDER_URL L2_PROVIDER_URL=$L2_PROVIDER_URL L1_ETHERSCAN_API_KEY=$L1_ETHERSCAN_API_KEY L2_ETHERSCAN_API_KEY=$L2_ETHERSCAN_API_KEY L2_PROVIDER_URL=$L2_PROVIDER_URL L2_RESOLVER_FACTORY_ADDRESS=$L2_RESOLVER_FACTORY_ADDRESS ENS_NAME=$ENS_NAME yarn setupl2 ``` +``` +L1_PROVIDER_URL=$L1_PROVIDER_URL L1_ETHERSCAN_API_KEY=$L1_ETHERSCAN_API_KEY L2_ETHERSCAN_API_KEY=$L2_ETHERSCAN_API_KEY L2_PROVIDER_URL=$L2_PROVIDER_URL ENS_NAME=$ENS_NAME yarn getaddr +``` + ### Issue subname to L2 \ No newline at end of file diff --git a/crosschain-resolver/package.json b/crosschain-resolver/package.json index 12b48103..758f94aa 100644 --- a/crosschain-resolver/package.json +++ b/crosschain-resolver/package.json @@ -9,6 +9,7 @@ "clean": "rm -fr artifacts cache node_modules typechain-types", "setupl1": "hardhat run scripts/setupl1.ts --network goerli", "setupl2": "hardhat run scripts/setupl2.ts --network optimismGoerli", + "getaddr": "hardhat run scripts/getaddr.ts --network goerli", "lint": "exit 0" }, "devDependencies": { diff --git a/crosschain-resolver/scripts/getaddr.ts b/crosschain-resolver/scripts/getaddr.ts new file mode 100644 index 00000000..a768c768 --- /dev/null +++ b/crosschain-resolver/scripts/getaddr.ts @@ -0,0 +1,46 @@ +import hre from 'hardhat'; +import packet from 'dns-packet'; +const abi = ['function name(bytes32) view returns(string)']; +const encodeName = (name) => '0x' + packet.name.encode(name).toString('hex') +const l1abi = [ + "function getTarget(bytes,uint256) view returns (bytes32, address)", + "function addr(bytes32) view returns (address)", + "function resolve(bytes,bytes) view returns (bytes)", +] + +const l2abi = [ + "function addr(bytes32) view returns (address)" +] + +import {ethers} from 'ethers'; +export const main = async () => { + if (!process.env.L1_PROVIDER_URL || !process.env.ENS_NAME) + throw 'Set L1_PROVIDER_URL and ENS_NAME'; + + const L1_PROVIDER_URL = process.env.L1_PROVIDER_URL; + const L2_PROVIDER_URL = process.env.L2_PROVIDER_URL; + const ENS_NAME = process.env.ENS_NAME; + const encodedname = encodeName(ENS_NAME) + const node = ethers.namehash(ENS_NAME) + + const l1provider = new ethers.JsonRpcProvider(L1_PROVIDER_URL); + const l2provider = new ethers.JsonRpcProvider(L2_PROVIDER_URL); + const resolver = await l1provider.getResolver(ENS_NAME) + const l1resolver = new ethers.Contract(resolver.address, l1abi, l1provider); + const target = await l1resolver.getTarget(encodedname, 0) + const l2resolverAddress = target[1] + console.log('Target is set to ' + l2resolverAddress); + const l2resolver = new ethers.Contract(l2resolverAddress, l2abi, l2provider); + const l2address = await l2resolver['addr(bytes32)'](node) + console.log('L2 query result ' + l2address); + const i = new ethers.Interface(l1abi) + const calldata = i.encodeFunctionData("addr", [node]) + const result2 = await l1resolver.resolve(encodedname, calldata, { enableCcipRead: true }) + const decoded = i.decodeFunctionResult("addr", result2) + console.log('L1 query result ' + decoded[0]); + // These should also work but somehow not working. Mabye some inconsistent resolver interface? + console.log(await l1provider.resolveName(ENS_NAME)); + console.log(await resolver.getAddress()); +}; + +main(); \ No newline at end of file