Skip to content

Instantly share code, notes, and snippets.

@lucaford
Created September 23, 2022 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucaford/e1d8d9793eb5ac2710be75a785b92168 to your computer and use it in GitHub Desktop.
Save lucaford/e1d8d9793eb5ac2710be75a785b92168 to your computer and use it in GitHub Desktop.
Register an ENS domain with nodejs
const ethers = require("ethers");
const crypto = require("crypto").webcrypto;
const { getReabableEtherNumber } = require("./helpers/getReadableEtherNumber");
const ERC_721_ABI = [
"function owner(bytes32 node) public view returns (address)",
"function maxCommitmentAge() view returns (uint256)",
"function minCommitmentAge() view returns (uint256)",
"function MIN_REGISTRATION_DURATION() view returns (uint256)",
"function commit(bytes32)",
"function makeCommitment(string name, address owner, bytes32 secret) view returns (bytes32)",
"function rentPrice(string name, uint256 duration) view returns (uint256)",
"function registerWithConfig(string name, address owner, uint256 duration, bytes32 secret, address resolver, address addr)",
];
const ETH_NAME_TO_REGISTER = "";
const ETH_REGISTRAR_CONTROLLER = "0x283Af0B28c62C092C9727F1Ee09c02CA627EB7F5";
const privateKey = "";
const MY_ADDRESS = "";
const provider = new ethers.providers.getDefaultProvider("goerli");
const wallet = new ethers.Wallet(privateKey, provider);
const registerEns = async () => {
const contract = new ethers.Contract(
ETH_REGISTRAR_CONTROLLER,
ERC_721_ABI,
provider
);
const contractWithWallet = await contract.connect(wallet);
const random = new Uint8Array(32);
crypto.getRandomValues(random);
const salt =
"0x" +
Array.from(random)
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
const price =
getReabableEtherNumber(
await contractWithWallet.rentPrice(ETH_NAME_TO_REGISTER, 31556952)
) * 1.1;
const commitment = await contractWithWallet.makeCommitment(
ETH_NAME_TO_REGISTER,
MY_ADDRESS,
salt
);
const tx = await contractWithWallet.commit(commitment);
await tx.wait();
console.log("Waiting 60 sec... ");
setTimeout(async () => {
const registerWithConfigTx = await contractWithWallet.registerWithConfig(
ETH_NAME_TO_REGISTER,
MY_ADDRESS,
31556952,
salt,
"0x4976fb03C32e5B8cfe2b6cCB31c09Ba78EBaBa41",
MY_ADDRESS,
{ gasLimit: 31000 }
);
await registerWithConfigTx.wait();
}, 60000);
};
registerEns();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment