API for ens
This commit is contained in:
parent
60125c6278
commit
72d0e7c71a
|
@ -16,10 +16,10 @@ class ENS {
|
||||||
|
|
||||||
this.addENSToEmbarkJS();
|
this.addENSToEmbarkJS();
|
||||||
this.configureContracts();
|
this.configureContracts();
|
||||||
this.registerEvents();
|
this.registerActionForEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
registerEvents() {
|
registerActionForEvents() {
|
||||||
const self = this;
|
const self = this;
|
||||||
self.embark.registerActionForEvent("contracts:deploy:afterAll", (cb) => {
|
self.embark.registerActionForEvent("contracts:deploy:afterAll", (cb) => {
|
||||||
async.parallel([
|
async.parallel([
|
||||||
|
@ -51,12 +51,12 @@ class ENS {
|
||||||
resolverAddress: results[2].deployedAddress
|
resolverAddress: results[2].deployedAddress
|
||||||
};
|
};
|
||||||
self.addSetProvider(config);
|
self.addSetProvider(config);
|
||||||
|
self.registerAPI(config);
|
||||||
|
|
||||||
if (!self.env === 'development' || !self.registration || !self.registration.subdomains || !Object.keys(self.registration.subdomains).length) {
|
if (!self.env === 'development' || !self.registration || !self.registration.subdomains || !Object.keys(self.registration.subdomains).length) {
|
||||||
return cb();
|
return cb();
|
||||||
}
|
}
|
||||||
self.registerConfigDomains(config, cb);
|
self.registerConfigSubdomains(config, cb);
|
||||||
self.registerAPI(config);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -65,56 +65,127 @@ class ENS {
|
||||||
let self = this;
|
let self = this;
|
||||||
self.embark.registerAPICall(
|
self.embark.registerAPICall(
|
||||||
'get',
|
'get',
|
||||||
'/embark-api/ens/registry',
|
'/embark-api/ens/resolve',
|
||||||
(req, res) => {
|
(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 self = this;
|
||||||
const register = require('./register');
|
const register = require('./register');
|
||||||
|
|
||||||
self.events.request("blockchain:defaultAccount:get", (defaultAccount) => {
|
self.events.request("blockchain:defaultAccount:get", (defaultAccount) => {
|
||||||
async.parallel([
|
async.parallel({
|
||||||
function createRegistryContract(paraCb) {
|
ens: self.createRegistryContract.bind(this, config),
|
||||||
self.events.request("blockchain:contract:create",
|
registrar: self.createRegistrarContract.bind(this, config),
|
||||||
{abi: config.registryAbi, address: config.registryAddress},
|
resolver: self.createResolverContract.bind(this, config)
|
||||||
(registry) => {
|
}, function (err, contracts) {
|
||||||
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) {
|
|
||||||
if (err) {
|
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) => {
|
async.each(Object.keys(subdomains), (subdomainName, eachCb) => {
|
||||||
const address = self.registration.subdomains[subDomainName];
|
const address = subdomains[subdomainName];
|
||||||
const reverseNode = utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix);
|
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);
|
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() {
|
addENSToEmbarkJS() {
|
||||||
const self = this;
|
const self = this;
|
||||||
// TODO: make this a shouldAdd condition
|
// TODO: make this a shouldAdd condition
|
||||||
|
|
Loading…
Reference in New Issue