Skip to content

Instantly share code, notes, and snippets.

@oneleo
Last active January 5, 2023 01:48
Show Gist options
  • Save oneleo/e0486259b091309586396c276aecd78c to your computer and use it in GitHub Desktop.
Save oneleo/e0486259b091309586396c276aecd78c to your computer and use it in GitHub Desktop.
ens-offchain-resolver

ENS Offchain Resolver

假設您已從 ens.domains 獲得了一組 ENS(在這裡我們以 token.eth 為例),並且在這之前我們已架設完成了一個 off-chain gateway server。

下面的說明將解釋如何將 off-chain gateway server 與 on-chain token.eth ENS 進行關聯,以便 ethers.js 可以從 off-chain gateway server 成功查詢到對應的 ENS Ethereum 地址。

在本文中,我們假設您已架設好的 off-chain gateway server 資訊及儲存的資料如下:

A、環境準備

  • 下載此 repo 並切換到 mainnet_resolver_feature 分支,接著開始編譯程式碼。
% git clone [email protected]:consenlabs/ens-offchain-resolver.git
% code ens-offchain-resolver
% git checkout mainnet_resolver_feature
% yarn clean && yarn install && yarn build

B、模擬

  • 切換到 packages/contracts/ 目錄並編輯 .env 檔。

注意:請將 DEPLOYER_PRIVATE_KEY 變數置換成您用來部署 OffchainResolver 合約的 deployer 私鑰。

% cd packages/contracts/
% cp .env.example .env && code .env

### 編輯 .env 檔
### ------------------------------
ENS_REGISTRY_OWNER_PRIVATE_KEY="0x0000000000000000000000000000000000000000000000000000000000000000"
ENS_DOMAIN_OWNER_PRIVATE_KEY="0x0000000000000000000000000000000000000000000000000000000000000001"
DEPLOYER_PRIVATE_KEY="0x0000000000000000000000000000000000000000000000000000000000000002"
ETHERSCAN_API_KEY="Created_by_https://etherscan.io/"
ALCHEMY_TOKEN="Created_by_https://www.alchemy.com/"
### ------------------------------
  1. 在 fork mainnet 中模擬完整的部署及解析行為
  • 完整的流程包含部署 OffchainResolver 合約、設置 token.eth ENS 的 OffchainResolver 地址、透過 ethers.js 進行 neal12.token.eth 子 ENS 查詢。
% npx hardhat run scripts/operating/DeployOffchainResolverAndSetResolver.ts --network hardhat

輸出範例(Deployer address 會與實際不同):

✔ DomainOwner address: 0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf, is this correct? … yes
✔ Deployer address: 0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF, is this correct? … yes
The new owner of the domain: 0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf
✔ Expected new contract address : 0xfdDE5A7823A0b6c094E73D15FF4A03Ec34e2cd11, is this correct? … yes
The gateway url from the OffchainResolver contract: http://imkeyserver.com:9003/{sender}/{data}.json
The new OffchainRresolver of the domain: 0xfdDE5A7823A0b6c094E73D15FF4A03Ec34e2cd11
Resolve the ETH address associated with the domain: 0x67F7E7dD168Af3D009E4C34Ef7312da6D23289Dd
Resolve the email associated with the domain: [email protected]

C、開始在主網上部署 OffchainResolver 合約

注意:請確認您 deployer 的 ETH 餘額充足。

  1. 請檢查 deployments/mainnet/AddressRecord.json 檔中列出的 Signer 是否與您已架設 off-chain gateway server 的 Signer 相同。
% code deployments/mainnet/AddressRecord.json

### 確認 signer address
### ------------------------------
{
  "Signer": "0x3B7D34d0E7e807A9D7aD74F094C5379aca61460D",
...
}
### ------------------------------
  1. 開始部署 OffchainResolver 合約。
% npx hardhat run scripts/deploy/OffchainResolver.ts --network mainnet

輸出範例(合約地址會與實際不同):

✔ Deployer address: 0x3B7D34d0E7e807A9D7aD74F094C5379aca61460D, is this correct? … yes
✔ Expected new contract address : 0xbF975Ba9ad5c242730435c9C133AedAE4B942dfa, is this correct? … yes
✔ Verify contract on etherscan (If fails, please execute this cmd again)
cmd : npx hardhat verify --network mainnet --contract contracts/OffchainResolver.sol:OffchainResolver 0xbF975Ba9ad5c242730435c9C133AedAE4B942dfa --constructor-args ./scripts/deploy/OffchainResolverVerifyArguments.ts ? … yes
  1. 如果上述合約驗證執行結果顯示失敗,請再次執行上述的相同指令,直到完成在 Etherscan 上的合約驗證(將顯示:Reason: Already Verified)。

請將合約地址調整成符合您實際情況的地址。

% npx hardhat verify --network mainnet --contract contracts/OffchainResolver.sol:OffchainResolver 0xbF975Ba9ad5c242730435c9C133AedAE4B942dfa --constructor-args ./scripts/deploy/OffchainResolverVerifyArguments.ts

D、設置 token.eth ENS 的 OffchainResolver 地址

  • 請到 ens.domains 網站設置 token.eth ENS 的 OffchainResolver 地址

E. 解析子 ENS

  • 嘗試使用 ethers.js 來解析 token.eth 的子 ENS:neal12.token.eth。
% npx hardhat run scripts/operating/ResolveSubdomain.ts --network mainnet

輸出範例(以下為 ethers.js 可正確從 off-chain gateway server 取得正確資訊的結果):

Resolve the ETH address associated with the domain: 0x67F7E7dD168Af3D009E4C34Ef7312da6D23289Dd
Resolve the email associated with the domain: [email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment