Hello, I am reading the source code of the ENS,when I register a name,the method “function register(string name, address owner, uint duration, bytes32 secret) public payable” of contract ETHRegistrarController is called, I want to know more about the parameter “secret”, including how does it been generated and what is it used for?
Your answers will be highly appreciated!
2 Likes
secret is just a random string. You call makeCommitment
with the node name and secret which returns commtitment
, then pass it to commit
function on step 1.
These lines will probably help understanding how they all fit together.
for (const label in checkLabels) { assert.equal(await controller.valid(label), checkLabels[label], label); } }); it('should report unused names as available', async () => { assert.equal(await controller.available(sha3('available')), true); }); it('should permit new registrations', async () => { var commitment = await controller.makeCommitment("newname", registrantAccount, secret); var tx = await controller.commit(commitment); assert.equal(await controller.commitments(commitment), (await web3.eth.getBlock(tx.receipt.blockNumber)).timestamp); await evm.advanceTime((await controller.minCommitmentAge()).toNumber()); var balanceBefore = await web3.eth.getBalance(controller.address); var tx = await controller.register("newname", registrantAccount, 28 * DAYS, secret, {value: 28 * DAYS + 1, gasPrice: 0}); assert.equal(tx.logs.length, 1); assert.equal(tx.logs[0].event, "NameRegistered"); assert.equal(tx.logs[0].args.name, "newname"); assert.equal(tx.logs[0].args.owner, registrantAccount);
function makeCommitment(string memory name, address owner, bytes32 secret) pure public returns(bytes32) { return makeCommitmentWithConfig(name, owner, secret, address(0), address(0)); } function makeCommitmentWithConfig(string memory name, address owner, bytes32 secret, address resolver, address addr) pure public returns(bytes32) { bytes32 label = keccak256(bytes(name)); if (resolver == address(0) && addr == address(0)) { return keccak256(abi.encodePacked(label, owner, secret)); } require(resolver != address(0)); return keccak256(abi.encodePacked(label, owner, resolver, addr, secret)); } function commit(bytes32 commitment) public { require(commitments[commitment] + maxCommitmentAge < now); commitments[commitment] = now; } function register(string calldata name, address owner, uint duration, bytes32 secret) external payable { registerWithConfig(name, owner, duration, secret, address(0), address(0)); }
import crypto from 'crypto' import moment from 'moment' function randomSecret() { return '0x' + crypto.randomBytes(32).toString('hex') } const Store = { get: label => { return window.localStorage.getItem('progress') ? JSON.parse(window.localStorage.getItem('progress'))[label] : null }, set: (label, obj) => { let data = {}
1 Like