Registrar services

The frontier chain comes with some very basic baselayer services, most of all the registrar. The registrar is composed of 3 components.

  • GlobalRegistrar to associate names (strings) to accounts (addresses).
  • HashReg to associate hashes to hashes (map any object to a 'content' hash.
  • UrlHint to associate content hashes to a hint for the location of the content. This is needed only if content storage is not content addressed, otherwise content hash is already the content address. If it is used, content fetched from the url should hash to content hash. In order to check authenticity of content one can check if this verifies.

Create and deploy GlobalRegistrar, HashReg and UrlHint

If the registrar contracts are not hardcoded in the blockchain (they are not at the time of writing), the registrars need to be deployed at least once on every chain.

If you are on the main live chain, the address of the main global registrar is hardcoded in the latest clients and therefore you do not need to do anything. If you want to change this or you are on a private chain you need to deploy these contracts at least once:

primary = eth.accounts[0];

globalRegistrarAddr = admin.setGlobalRegistrar("", primary);
hashRegAddr = admin.setHashReg("", primary);
urlHintAddr = admin.setUrlHint("", primary);

You need to mine or wait till the txs are all picked up. Initialise the registrar on the new address and check if the other registrars' names resolve to the correct addresses:

registrar = GlobalRegistrar.at(globalRegistrarAddr);
primary == registrar.owner("HashReg");
primary == registrar.owner("UrlHint");
hashRegAddr == registrar.addr("HashReg");
urlHintAddr registrar.addr("UrlHint");

and the following ones return correct code:

eth.getCode(registrar.address);
eth.getCode(registrar.addr("HashReg"));
eth.getCode(registrar.addr("UrlHint"));

From the second time onwards on the same chain as well as on other nodes, you simply seed with the GlobalRegistrars address, the rest is handled through it.

primary = eth.accounts[0];
globalRegistrarAddr = "0x225178b4829bbe7c9f8a6d2e3d9d87b66ed57d4f"

// set the global registrar address
admin.setGlobalRegistrar(globalRegistrarAddr)
// set HashReg address via globalRegistrar
hashRegAddr = admin.setHashReg()
// set UrlHint address via globalRegistrar
urlHintAddr = admin.setUrlHint()

// (re)sets the registrar variable to a GlobalRegistrar contract instance 
registrar = GlobalRegistrar.at(globalRegistrarAddr);

If this is successful, you should be able to check with the following commands if the registrar returns addresses:

registrar.owner("HashReg");
registrar.owner("UrlHint");
registrar.addr("HashReg");
registrar.addr("UrlHint");

and the following ones return correct code:

eth.getCode(registrar.address);
eth.getCode(registrar.addr("HashReg"));
eth.getCode(registrar.addr("UrlHint"));

Using the registrar services

Can provide useful interfaces between contracts and dapps.

Global registrar

To reserve a name register an account address with it, you need the following:

registrar.reserve.sendTransaction(name, {from:primary})
registrar.setAddress.sendTransaction (name, address, true, {from: primary})

You need to wait for the transactions to be picked up (or force mine them if you are on a private chain). To check you query the registrar:

registrar.owner(name)
registrar.addr(name)

HashReg and UrlHint

HashReg and UrlHint can be used with the following abis:

hashRegAbi = '[{"constant":false,"inputs":[],"name":"setowner","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"_key","type":"uint256"},{"name":"_content","type":"uint256"}],"name":"register","outputs":[],"type":"function"}]'
urlHintAbi = '[{"constant":false,"inputs":[{"name":"_hash","type":"uint256"},{"name":"idx","type":"uint8"},{"name":"_url","type":"uint256"}],"name":"register","outputs":[],"type":"function"}]'

setting up the contract instances:

hashReg = eth.contract(hashRegAbi).at(registrar.addr("HashReg")));
urlHint = eth.contract(UrlHintAbi).at(registrar.addr("UrlHint")));

Associate a content hash to a key hash:

hashReg.register.sendTransaction(keyhash, contenthash, {from:primary})

Associate a url to a content hash:

urlHint.register.sendTransaction(contenthash, url, {from:primary})

To check resolution:

contenthash = hashReg._hash(keyhash);
url = urlHint._url(contenthash);