API for ens

This commit is contained in:
Anthony Laibe 2018-08-13 11:36:48 +01:00
parent 60125c6278
commit 72d0e7c71a
1 changed files with 107 additions and 36 deletions

View File

@ -16,10 +16,10 @@ class ENS {
this.addENSToEmbarkJS();
this.configureContracts();
this.registerEvents();
this.registerActionForEvents();
}
registerEvents() {
registerActionForEvents() {
const self = this;
self.embark.registerActionForEvent("contracts:deploy:afterAll", (cb) => {
async.parallel([
@ -51,12 +51,12 @@ class ENS {
resolverAddress: results[2].deployedAddress
};
self.addSetProvider(config);
self.registerAPI(config);
if (!self.env === 'development' || !self.registration || !self.registration.subdomains || !Object.keys(self.registration.subdomains).length) {
return cb();
}
self.registerConfigDomains(config, cb);
self.registerAPI(config);
self.registerConfigSubdomains(config, cb);
});
});
}
@ -65,56 +65,127 @@ class ENS {
let self = this;
self.embark.registerAPICall(
'get',
'/embark-api/ens/registry',
'/embark-api/ens/resolve',
(req, res) => {
self.resolveName(req.query.name, config, (error, address) => {
if (error) {
return res.send({error: error.message});
}
res.send({address});
});
}
);
self.embark.registerAPICall(
'get',
'/embark-api/ens/lookup',
(req, res) => {
self.lookupAddress(req.query.address, config, (error, name) => {
if (error) {
return res.send({error: error.message});
}
res.send({name});
});
}
);
self.embark.registerAPICall(
'post',
'/embark-api/ens/register',
(req, res) => {
self.registerSubdomain({[req.body.subdomain]: req.body.address}, config, (error, transaction) => {
if (error) {
return res.send({error: error.message});
}
res.send({transaction});
});
}
);
}
registerConfigDomains(config, cb) {
registerConfigSubdomains(config, callback) {
this.registerSubdomains(this.registration.subdomains, config, callback);
}
lookupAddress(address, config, callback) {
let self = this;
self.createResolverContract(config, (err, resolver) => {
if (err) {
return callback(err);
}
if (address.startsWith("0x")) {
address = address.slice(2);
}
let node = utils.soliditySha3(address.toLowerCase() + reverseAddrSuffix);
resolver.methods.name(node).call(callback);
});
}
resolveName(name, config, callback) {
let self = this;
self.createResolverContract(config, (err, resolver) => {
if (err) {
return callback(err);
}
let node = namehash.hash(name);
resolver.methods.name(node).call(callback);
});
}
registerSubdomains(subdomains, config, callback) {
const self = this;
const register = require('./register');
self.events.request("blockchain:defaultAccount:get", (defaultAccount) => {
async.parallel([
function createRegistryContract(paraCb) {
self.events.request("blockchain:contract:create",
{abi: config.registryAbi, address: config.registryAddress},
(registry) => {
paraCb(null, registry);
});
},
function createRegistrarContract(paraCb) {
self.events.request("blockchain:contract:create",
{abi: config.registrarAbi, address: config.registrarAddress},
(registrar) => {
paraCb(null, registrar);
});
},
function createResolverContract(paraCb) {
self.events.request("blockchain:contract:create",
{abi: config.resolverAbi, address: config.resolverAddress},
(resolver) => {
paraCb(null, resolver);
});
}
], function (err, contracts) {
async.parallel({
ens: self.createRegistryContract.bind(this, config),
registrar: self.createRegistrarContract.bind(this, config),
resolver: self.createResolverContract.bind(this, config)
}, function (err, contracts) {
if (err) {
return cb(err);
return callback(err);
}
const [ens, registrar, resolver] = contracts;
const {ens, registrar, resolver} = contracts;
async.each(Object.keys(self.registration.subdomains), (subDomainName, eachCb) => {
const address = self.registration.subdomains[subDomainName];
async.each(Object.keys(subdomains), (subdomainName, eachCb) => {
const address = subdomains[subdomainName];
const reverseNode = utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix);
register(ens, registrar, resolver, defaultAccount, subDomainName, self.registration.rootDomain,
register(ens, registrar, resolver, defaultAccount, subdomainName, self.registration.rootDomain,
reverseNode, address, self.logger, eachCb);
}, cb);
}, callback);
});
});
}
createRegistryContract(config, callback) {
this.events.request("blockchain:contract:create", {
abi: config.registryAbi,
address: config.registryAddress
}, (registry) => {
callback(null, registry);
});
}
createRegistrarContract(config, callback) {
this.events.request("blockchain:contract:create", {
abi: config.registrarAbi,
address: config.registrarAddress
}, (registrar) => {
callback(null, registrar);
});
}
createResolverContract(config, callback) {
this.events.request("blockchain:contract:create", {
abi: config.resolverAbi,
address: config.resolverAddress
}, (resolver) => {
callback(null, resolver);
});
}
addENSToEmbarkJS() {
const self = this;
// TODO: make this a shouldAdd condition