Skip to content

Instantly share code, notes, and snippets.

@rfikki
Created March 24, 2017 14:38
Show Gist options
  • Save rfikki/2959c9a52fecd19638514509eb10ab27 to your computer and use it in GitHub Desktop.
Save rfikki/2959c9a52fecd19638514509eb10ab27 to your computer and use it in GitHub Desktop.
function namehash(name) {
var node = '0x0000000000000000000000000000000000000000000000000000000000000000';
if(name != '') {
var labels = name.split(".");
for(var i = labels.length - 1; i >= 0; i--) {
node = web3.sha3(node + web3.sha3(labels[i]).slice(2), {encoding: 'hex'});
}
}
return node.toString();
}
var ensContract = web3.eth.contract([
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "resolver",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "owner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "label",
"type": "bytes32"
},
{
"name": "owner",
"type": "address"
}
],
"name": "setSubnodeOwner",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "ttl",
"type": "uint64"
}
],
"name": "setTTL",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "ttl",
"outputs": [
{
"name": "",
"type": "uint64"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "resolver",
"type": "address"
}
],
"name": "setResolver",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "owner",
"type": "address"
}
],
"name": "setOwner",
"outputs": [],
"payable": false,
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "node",
"type": "bytes32"
},
{
"indexed": false,
"name": "owner",
"type": "address"
}
],
"name": "Transfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "node",
"type": "bytes32"
},
{
"indexed": true,
"name": "label",
"type": "bytes32"
},
{
"indexed": false,
"name": "owner",
"type": "address"
}
],
"name": "NewOwner",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "node",
"type": "bytes32"
},
{
"indexed": false,
"name": "resolver",
"type": "address"
}
],
"name": "NewResolver",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "node",
"type": "bytes32"
},
{
"indexed": false,
"name": "ttl",
"type": "uint64"
}
],
"name": "NewTTL",
"type": "event"
}
]);
var ens = ensContract.at('0x112234455c3a32fd11230c42e7bccd4a84e02010');
var auctionRegistrarContract = web3.eth.contract([
{
"constant": false,
"inputs": [
{
"name": "_hash",
"type": "bytes32"
}
],
"name": "releaseDeed",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "unhashedName",
"type": "string"
}
],
"name": "invalidateName",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "hash",
"type": "bytes32"
},
{
"name": "owner",
"type": "address"
},
{
"name": "value",
"type": "uint256"
},
{
"name": "salt",
"type": "bytes32"
}
],
"name": "shaBid",
"outputs": [
{
"name": "sealedBid",
"type": "bytes32"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "bytes32"
}
],
"name": "entries",
"outputs": [
{
"name": "status",
"type": "uint8"
},
{
"name": "deed",
"type": "address"
},
{
"name": "registrationDate",
"type": "uint256"
},
{
"name": "value",
"type": "uint256"
},
{
"name": "highestBid",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "ens",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_hash",
"type": "bytes32"
}
],
"name": "transferRegistrars",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "bytes32"
}
],
"name": "sealedBids",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_hash",
"type": "bytes32"
},
{
"name": "newOwner",
"type": "address"
}
],
"name": "transfer",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_hash",
"type": "bytes32"
}
],
"name": "finalizeAuction",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_hash",
"type": "bytes32"
},
{
"name": "_owner",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
},
{
"name": "_salt",
"type": "bytes32"
}
],
"name": "unsealBid",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "registryCreated",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "sealedBid",
"type": "bytes32"
}
],
"name": "newBid",
"outputs": [],
"payable": true,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "seal",
"type": "bytes32"
}
],
"name": "cancelBid",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_hashes",
"type": "bytes32[]"
}
],
"name": "startAuctions",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_hash",
"type": "bytes32"
}
],
"name": "startAuction",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "rootNode",
"outputs": [
{
"name": "",
"type": "bytes32"
}
],
"payable": false,
"type": "function"
},
{
"inputs": [
{
"name": "_ens",
"type": "address"
},
{
"name": "_rootNode",
"type": "bytes32"
}
],
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "hash",
"type": "bytes32"
},
{
"indexed": false,
"name": "auctionExpiryDate",
"type": "uint256"
}
],
"name": "AuctionStarted",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "hash",
"type": "bytes32"
},
{
"indexed": false,
"name": "deposit",
"type": "uint256"
}
],
"name": "NewBid",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "hash",
"type": "bytes32"
},
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
},
{
"indexed": false,
"name": "status",
"type": "uint8"
}
],
"name": "BidRevealed",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "hash",
"type": "bytes32"
},
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
},
{
"indexed": false,
"name": "now",
"type": "uint256"
}
],
"name": "HashRegistered",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "hash",
"type": "bytes32"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "HashReleased",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "hash",
"type": "bytes32"
},
{
"indexed": true,
"name": "name",
"type": "string"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
},
{
"indexed": false,
"name": "now",
"type": "uint256"
}
],
"name": "HashInvalidated",
"type": "event"
}
]);
var ethRegistrar = auctionRegistrarContract.at(ens.owner(namehash('eth')));
var deedContract = web3.eth.contract([
{
"constant": true,
"inputs": [],
"name": "creationDate",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "destroyDeed",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "newOwner",
"type": "address"
}
],
"name": "setOwner",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "registrar",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "refundRatio",
"type": "uint256"
}
],
"name": "closeDeed",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "newRegistrar",
"type": "address"
}
],
"name": "setRegistrar",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "newValue",
"type": "uint256"
}
],
"name": "setBalance",
"outputs": [],
"payable": true,
"type": "function"
},
{
"inputs": [],
"type": "constructor"
},
{
"payable": true,
"type": "fallback"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"name": "newOwner",
"type": "address"
}
],
"name": "OwnerChanged",
"type": "event"
},
{
"anonymous": false,
"inputs": [],
"name": "DeedClosed",
"type": "event"
}
]);
var fifsRegistrarContract = web3.eth.contract([
{
"constant": true,
"inputs": [],
"name": "ens",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "bytes32"
}
],
"name": "expiryTimes",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "subnode",
"type": "bytes32"
},
{
"name": "owner",
"type": "address"
}
],
"name": "register",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "rootNode",
"outputs": [
{
"name": "",
"type": "bytes32"
}
],
"payable": false,
"type": "function"
},
{
"inputs": [
{
"name": "ensAddr",
"type": "address"
},
{
"name": "node",
"type": "bytes32"
}
],
"type": "constructor"
}
]);
var testRegistrar = fifsRegistrarContract.at(ens.owner(namehash('test')));
var resolverContract = web3.eth.contract([
{
"constant": true,
"inputs": [
{
"name": "interfaceID",
"type": "bytes4"
}
],
"name": "supportsInterface",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "addr",
"outputs": [
{
"name": "ret",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "kind",
"type": "bytes32"
}
],
"name": "has",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "addr",
"type": "address"
}
],
"name": "setAddr",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "content",
"outputs": [
{
"name": "ret",
"type": "bytes32"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "hash",
"type": "bytes32"
}
],
"name": "setContent",
"outputs": [],
"payable": false,
"type": "function"
},
{
"inputs": [
{
"name": "ensAddr",
"type": "address"
}
],
"type": "constructor"
},
{
"payable": false,
"type": "fallback"
}
]);
var publicResolver = resolverContract.at('0x4c641fb9bad9b60ef180c31f56051ce826d21a9a');
var reverseRegistrarContract = web3.eth.contract([
{
"constant": false,
"inputs": [
{
"name": "owner",
"type": "address"
}
],
"name": "claim",
"outputs": [
{
"name": "node",
"type": "bytes32"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "ens",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "addr",
"type": "address"
}
],
"name": "node",
"outputs": [
{
"name": "ret",
"type": "bytes32"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "rootNode",
"outputs": [
{
"name": "",
"type": "bytes32"
}
],
"payable": false,
"type": "function"
},
{
"inputs": [
{
"name": "ensAddr",
"type": "address"
},
{
"name": "node",
"type": "bytes32"
}
],
"payable": false,
"type": "constructor"
}
]);
var reverseRegistrar = reverseRegistrarContract.at(ens.owner(namehash('addr.reverse')));
function getAddr(name) {
var node = namehash(name)
var resolverAddress = ens.resolver(node);
if(resolverAddress == '0x0000000000000000000000000000000000000000') {
return resolverAddress;
}
return resolverContract.at(resolverAddress).addr(node);
}
function getContent(name) {
var node = namehash(name)
var resolverAddress = ens.resolver(node);
if(resolverAddress == '0x0000000000000000000000000000000000000000') {
return "0x0000000000000000000000000000000000000000000000000000000000000000";
}
return resolverContract.at(resolverAddress).content(node);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment