Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Add ENS library #288

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions contracts/TestENSLib.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
pragma solidity ^0.4.22;

import './libs/ENSLib.sol';


/**
* A contract to test the ENSLibrary with a on chain implementation
*/
contract TestENSLib {
using ENSLib for bytes32;

address public ensAddr;


/**
* Constructor.
* @param _ensAddr The address of the ENS registry.
*/
function TestENSLib(address _ensAddr) {
ensAddr = _ensAddr;
}

/**
* @dev Returns the address that owns the specified node.
* @param node The specified node.
* @return address of the owner.
*/
function owner(bytes32 node) public view returns (address) {
return node.owner(ensAddr);
}

/**
* @dev Returns the address of the resolver for the specified node.
* @param node The specified node.
* @return address of the resolver.
*/
function resolver(bytes32 node) public view returns (address) {
return node.resolver(ensAddr);
}

/**
* @dev Returns the TTL of a node, and any records associated with it.
* @param node The specified node.
* @return ttl of the node.
*/
function ttl(bytes32 node) public view returns (uint64) {
return node.ttl(ensAddr);
}

}
42 changes: 42 additions & 0 deletions contracts/libs/ENSLib.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
pragma solidity ^0.4.18;

import '../ENS.sol';

/**
* @title ENS utility library for Solidity contracts.
* @author Augusto Lemble <me@augustolemble.com>
*
* @dev This library allows the access to ens information by their name.
* Once the namehash of the ens is available it only need the address of
* the ENS registry to get the ens information.
*/
library ENSLib {

/**
* @dev Returns the address that owns the specified node.
* @param node The specified node.
* @return address of the owner.
*/
function owner(bytes32 node, address ensAddress) public view returns (address) {
return ENS(ensAddress).owner(node);
}

/**
* @dev Returns the address of the resolver for the specified node.
* @param node The specified node.
* @return address of the resolver.
*/
function resolver(bytes32 node, address ensAddress) public view returns (address) {
return ENS(ensAddress).resolver(node);
}

/**
* @dev Returns the TTL of a node, and any records associated with it.
* @param node The specified node.
* @return ttl of the node.
*/
function ttl(bytes32 node, address ensAddress) public view returns (uint64) {
return ENS(ensAddress).ttl(node);
}

}
47 changes: 47 additions & 0 deletions test/ENSLib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const FIFSRegistrar = artifacts.require('FIFSRegistrar.sol');
const ENS = artifacts.require('ENSRegistry.sol');
const ENSLib = artifacts.require('ENSLib.sol');
const TestENSLib = artifacts.require('TestENSLib.sol');
const PublicResolver = artifacts.require('PublicResolver.sol');

const utils = require('./helpers/Utils.js');
const web3Utils = require('web3-utils');
const namehash = require('eth-ens-namehash');

contract('ENSLib', function (accounts) {

let resolver, registrar, ens;

beforeEach(async () => {
ens = await ENS.new();
const ensLib = await ENSLib.new();
TestENSLib.link('ENSLib', ensLib.address);
testENSLib = await TestENSLib.new(ens.address);

await ens.setSubnodeOwner(0, web3.sha3('eth'), accounts[0], {from: accounts[0]});
registrar = await FIFSRegistrar.new(ens.address, namehash('eth'));
await ens.setOwner(namehash('eth'), registrar.address, {from: accounts[0]});

await registrar.register(web3Utils.sha3('testdomain'), accounts[1], {from: accounts[0]});
await ens.setResolver(namehash('testdomain.eth'), accounts[2], {from: accounts[1]});
await ens.setTTL(namehash('testdomain.eth'), 10, {from: accounts[1]});
});

it('should get the right owner of subnode', async () => {
assert.equal(await ens.owner(namehash('eth')), registrar.address);
assert.equal(await ens.owner(namehash('testdomain.eth')), accounts[1]);
});

it('should get the right info of ens node by namehash', async () => {
assert.equal(await testENSLib.owner(namehash('eth')), registrar.address);
assert.equal(await testENSLib.resolver(namehash('eth')), '0x0000000000000000000000000000000000000000');
assert.equal(await testENSLib.ttl(namehash('eth')), 0);
});

it('should get the right info of ens subnode by namehash', async () => {
assert.equal(await testENSLib.owner(namehash('testdomain.eth')), accounts[1]);
assert.equal(await testENSLib.resolver(namehash('testdomain.eth')), accounts[2]);
assert.equal(await testENSLib.ttl(namehash('testdomain.eth')), 10);
});

});