Created
January 18, 2023 19:06
-
-
Save alextcn/e73fad61c4b9b9dc795026cb57dfa678 to your computer and use it in GitHub Desktop.
Commit and regsiter ENS domain on Goerli network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ethers, Wallet } from 'ethers' | |
// const ALCHEMY_URL = `https://eth-goerli.g.alchemy.com/v2/${process.env.NEXT_PUBLIC_ALCHEMY_API_KEY!}` | |
const ETH_REGISTRAR_ADDRESS = '0x283Af0B28c62C092C9727F1Ee09c02CA627EB7F5' // goerli | |
const ETH_REGISTRAR_ABI = [ | |
'function available(string) view returns (bool)', | |
'function makeCommitment(string,address,bytes32) pure returns (bytes32)', | |
'function commit(bytes32)', | |
'function register(string,address,uint256,bytes32) payable', | |
] | |
const randomSecret = () => { | |
const bytes = Buffer.allocUnsafe(32) | |
return `0x${crypto.getRandomValues(bytes).toString('hex')}` | |
} | |
async function commitAndRegister() { | |
// init ethers | |
// const provider = new ethers.providers.JsonRpcProvider(ALCHEMY_URL) | |
const provider = new ethers.providers.Web3Provider(window.ethereum!) | |
const signer = provider.getSigner() | |
const _contract = new ethers.Contract(ETH_REGISTRAR_ADDRESS, ETH_REGISTRAR_ABI, provider) | |
const contract = _contract.connect(signer) | |
// params | |
const name = 'dowjones' | |
const owner = await signer.getAddress() | |
const secret = randomSecret() | |
const duration = 60 * 60 * 24 * 365 // 31536000 seconds = 1 year | |
const valueEth = ethers.utils.parseEther('1.0') // extra ether gets returned | |
// check name available | |
const available = await contract.available(name) | |
console.log(`${name}.eth is ${available ? 'available' : 'taken'}`) | |
if (!available) return | |
// 1. generate a commitment hash from the name they want to register and a secret value | |
const commitmentHash = await contract.makeCommitment(name, owner, secret) | |
console.log(`commitment created:\n name = ${name}\n owner = ${owner}\n secret = ${secret}\n commitmentHash = ${commitmentHash}`) | |
// 2. submit the commitment hash from #1 to the controller | |
let tx = await contract.commit(commitmentHash) | |
let txHash = (await tx.wait()).transactionHash | |
console.log(`commitment sent: tx = ${txHash}`) | |
// 3. wait for at least 1 minute | |
console.log(`waiting for 1 sec...`) | |
await (new Promise((resolve) => setTimeout(resolve, 65 * 1000))) | |
// 4. submit a registration request for the name, along with the secret value from #1 | |
tx = await contract.register(name, owner, duration, secret, { | |
value: valueEth | |
}) | |
txHash = (await tx.wait()).transactionHash | |
console.log(`registration sent: tx = ${txHash}`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment