Skip to content

Instantly share code, notes, and snippets.

@kvhnuke
Last active May 23, 2017 06:53
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 kvhnuke/ddbf1bfc8dab01550243be123cc5f2cc to your computer and use it in GitHub Desktop.
Save kvhnuke/ddbf1bfc8dab01550243be123cc5f2cc to your computer and use it in GitHub Desktop.
/**
* SimpleRegistrar lets you claim a subdomain name for yourself and configure it
* all in one step. This one is deployed at xxx.xxx.xxx.
*
* To use it, simply call register() with the name you want and the appropriate
* fee.
* SimpleRegistrar will take care of everything: registering your subdomain,
* setting up a resolver, and pointing that resolver at the account that called
* it.
*
* Funds received from running this service are reinvested into building new
* features at MEW.
*
* Original Author: Nick Johnson <nick@arachnidlabs.com>
* Modified by: kvhnuke <kvhnuke@aol.com>
* Licensed under the Apache Public License, version 2.0.
*/
pragma solidity ^0.4.10;
contract AbstractENS {
function owner(bytes32 node) constant returns(address);
function resolver(bytes32 node) constant returns(address);
function ttl(bytes32 node) constant returns(uint64);
function setOwner(bytes32 node, address owner);
function setSubnodeOwner(bytes32 node, bytes32 label, address owner);
function setResolver(bytes32 node, address resolver);
function setTTL(bytes32 node, uint64 ttl);
}
contract owned {
address public owner;
function owned() {
owner = msg.sender;
}
modifier owner_only() {
if(msg.sender != owner) throw;
_;
}
function setOwner(address _owner) owner_only {
owner = _owner;
}
}
contract Resolver {
function setAddr(bytes32 node, address addr);
}
contract ReverseRegistrar {
function claim(address owner) returns (bytes32 node);
}
contract RegistrationFees is owned{
address public owner;
uint[] charLen;
uint[] charVal;
function RegistrationFees(uint[] _charLen, uint[] _charVal) {
owner = msg.sender;
charLen = _charLen;
charVal = _charVal;
}
function setFees(uint[] _charLen, uint[] _charVal) owner_only {
charLen = _charLen;
charVal = _charVal;
}
function strlen(string s) internal constant returns (uint) {
s;
uint ptr;
uint end;
assembly {
ptr := add(s, 1)
end := add(mload(s), ptr)
}
for (uint len = 0; ptr < end; len++) {
uint8 b;
assembly { b := and(mload(ptr), 0xFF) }
if (b < 0x80) {
ptr += 1;
} else if (b < 0xE0) {
ptr += 2;
} else if (b < 0xF0) {
ptr += 3;
} else if (b < 0xF8) {
ptr += 4;
} else if (b < 0xFC) {
ptr += 5;
} else {
ptr += 6;
}
}
return len;
}
function getFee(string _name) constant returns (uint){
uint len = strlen(_name);
for(uint i=0;i<charLen.length;i++){
if(len <= charLen[i]) return charVal[i];
}
return charVal[charVal.length-1];
}
}
contract SimpleRegistrar is owned {
// namehash('addr.reverse')
bytes32 constant RR_NODE = 0x91d1777781884d03a6757a803996e38de2a42967fb37eeaca72729271025a9e2;
// namehash('resolver.eth')
bytes32 constant PR_NODE = 0xfdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5;
event HashRegistered(bytes32 indexed hash, address indexed owner);
AbstractENS public ens;
bytes32 public rootNode;
Resolver public resolver;
RegistrationFees public rFees;
function SimpleRegistrar(AbstractENS _ens, bytes32 _rootNode, RegistrationFees _rFees) {
ens = _ens;
rootNode = _rootNode;
resolver = Resolver(ens.resolver(PR_NODE));
rFees = _rFees;
// Assign reverse record to sender
ReverseRegistrar(ens.owner(RR_NODE)).claim(msg.sender);
}
function withdraw() owner_only {
if(!msg.sender.send(this.balance)) throw;
}
function setResolver(Resolver _resolver) owner_only {
resolver = _resolver;
}
modifier can_register(string name) {
if(ens.owner(sha3(rootNode, sha3(name))) != 0 || msg.value < rFees.getFee(name)) throw;
_;
}
function transferOwnership (address _newOwner) owner_only { //take this out when you are confident
ens.setOwner(rootNode, _newOwner);
}
function isNameAvailable (string name) constant returns (bool) {
ens.owner(sha3(rootNode, sha3(name))) == 0;
}
function register(string name) payable can_register(name) {
var label = sha3(name);
// First register the name to ourselves
ens.setSubnodeOwner(rootNode, label, this);
// Now set a resolver up
var node = sha3(rootNode, label);
ens.setResolver(node, resolver);
resolver.setAddr(node, msg.sender);
// Now transfer ownership to the user
ens.setOwner(node, msg.sender);
HashRegistered(label, msg.sender);
// Send any excess ether back
uint fee = rFees.getFee(name);
if(msg.value > fee) {
if(!msg.sender.send(msg.value - fee)) throw;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment