Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@embark/core): Support directives in ENS config #1165

Merged
merged 1 commit into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/modules/blockchain_process/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,15 @@ Blockchain.prototype.initChainAndGetAddress = function (callback) {
function listAccounts(next) {
self.runCommand(self.client.listAccountsCommand(), {}, (err, stdout, _stderr) => {
if (err || stdout === undefined || stdout.indexOf("Fatal") >= 0) {
this.logger.info(__("no accounts found").green);
self.logger.info(__("no accounts found").green);
return next();
}
let firstAccountFound = self.client.parseListAccountsCommandResultToAddress(stdout);
if (firstAccountFound === undefined || firstAccountFound === "") {
console.log(__("no accounts found").green);
return next();
}
this.logger.info(__("already initialized").green);
self.logger.info(__("already initialized").green);
address = firstAccountFound;
next(ALREADY_INITIALIZED);
});
Expand Down
41 changes: 29 additions & 12 deletions src/lib/modules/ens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const async = require('async');
const embarkJsUtils = require('embarkjs').Utils;
const reverseAddrSuffix = '.addr.reverse';
const ENSFunctions = require('./ENSFunctions');
import { ZERO_ADDRESS } from '../../utils/addressUtils';
import {ZERO_ADDRESS} from '../../utils/addressUtils';
import {ens} from '../../constants';

const ENS_WHITELIST = ens.whitelist;
Expand Down Expand Up @@ -223,6 +223,7 @@ class ENS {
this.events.request("blockchain:defaultAccount:get", (defaultAccount) => {
async.each(Object.keys(this.registration.subdomains), (subDomainName, eachCb) => {
const address = this.registration.subdomains[subDomainName];
const directivesRegExp = new RegExp(/\$(\w+\[?\d?\]?)/g);
this.ensResolve(`${subDomainName}.${this.registration.rootDomain}`, (error, currentAddress) => {
if (currentAddress && currentAddress.toLowerCase() === address.toLowerCase()) {
return eachCb();
Expand All @@ -233,14 +234,30 @@ class ENS {
return eachCb(error);
}

const directives = directivesRegExp.exec(address);
if (directives && directives.length) {
this.embark.registerActionForEvent("contracts:deploy:afterAll", async (deployActionCb) => {
this.events.request("contracts:contract", directives[1], (contract) => {
if(!contract) return deployActionCb(); // if the contract is not registered in the config, it will be undefined here
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should print a warning so that the user knows that he tried to register something to an address we don't know about.

const reverseNode = utils.soliditySha3(contract.deployedAddress.toLowerCase().substr(2) + reverseAddrSuffix);
this.registerSubDomain(defaultAccount, subDomainName, reverseNode, contract.deployedAddress, secureSend, deployActionCb);
});
});
return eachCb();
}

const reverseNode = utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix);
ENSFunctions.registerSubDomain(this.ensContract, this.registrarContract, this.resolverContract, defaultAccount,
subDomainName, this.registration.rootDomain, reverseNode, address, this.logger, secureSend, eachCb);
this.registerSubDomain(defaultAccount, subDomainName, reverseNode, address, secureSend, eachCb);
});
}, cb);
});
}

registerSubDomain(defaultAccount, subDomainName, reverseNode, address, secureSend, cb) {
ENSFunctions.registerSubDomain(this.ensContract, this.registrarContract, this.resolverContract, defaultAccount,
subDomainName, this.registration.rootDomain, reverseNode, address, this.logger, secureSend, cb);
}

createResolverContract(config, callback) {
this.events.request("blockchain:contract:create", {
abi: config.resolverAbi,
Expand All @@ -253,7 +270,7 @@ class ENS {
registerAPI() {
let self = this;

const createInternalResolverContract = function(resolverAddress, callback) {
const createInternalResolverContract = function (resolverAddress, callback) {
self.createResolverContract({resolverAbi: self.ensConfig.Resolver.abiDefinition, resolverAddress}, callback);
};

Expand All @@ -262,10 +279,10 @@ class ENS {
'/embark-api/ens/resolve',
(req, res) => {
async.waterfall([
function(callback) {
function (callback) {
ENSFunctions.resolveName(req.query.name, self.ensContract, createInternalResolverContract.bind(self), callback);
}
], function(error, address) {
], function (error, address) {
if (error) {
return res.send({error: error.message});
}
Expand All @@ -279,10 +296,10 @@ class ENS {
'/embark-api/ens/lookup',
(req, res) => {
async.waterfall([
function(callback) {
function (callback) {
ENSFunctions.lookupAddress(req.query.address, self.ensContract, utils, createInternalResolverContract.bind(self), callback);
}
], function(error, name) {
], function (error, name) {
if (error) {
return res.send({error: error || error.message});
}
Expand Down Expand Up @@ -315,10 +332,10 @@ class ENS {
const self = this;

// get namehash, import it into file
self.events.request("version:get:eth-ens-namehash", function(EnsNamehashVersion) {
self.events.request("version:get:eth-ens-namehash", function (EnsNamehashVersion) {
let currentEnsNamehashVersion = require('../../../../package.json').dependencies["eth-ens-namehash"];
if (EnsNamehashVersion !== currentEnsNamehashVersion) {
self.events.request("version:getPackageLocation", "eth-ens-namehash", EnsNamehashVersion, function(err, location) {
self.events.request("version:getPackageLocation", "eth-ens-namehash", EnsNamehashVersion, function (err, location) {
self.embark.registerImportFile("eth-ens-namehash", fs.dappPath(location));
});
}
Expand Down Expand Up @@ -475,7 +492,7 @@ class ENS {
if (!self.enabled) {
return cb('ENS not enabled');
}
if(!self.configured) {
if (!self.configured) {
return cb('ENS not configured');
}
const hashedName = namehash.hash(name);
Expand All @@ -485,7 +502,7 @@ class ENS {
if (err) {
return next(err);
}
if(resolverAddress === ZERO_ADDRESS) {
if (resolverAddress === ZERO_ADDRESS) {
return next(NOT_REGISTERED_ERROR);
}
next(null, resolverAddress);
Expand Down
4 changes: 3 additions & 1 deletion test_apps/test_app/config/namesystem.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"register": {
"rootDomain": "embark.eth",
"subdomains": {
"status": "0x4a17f35f0a9927fb4141aa91cbbc72c1b31598de"
"status": "0x4a17f35f0a9927fb4141aa91cbbc72c1b31598de",
"mytoken": "$MyToken",
"MyToken2": "$MyToken2"
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions test_apps/test_app/test/namesystem_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*global describe, it, web3, config*/
const assert = require('assert');
const MyToken = require('Embark/contracts/MyToken');
const MyToken2 = require('Embark/contracts/MyToken2');
const EmbarkJS = require('Embark/EmbarkJS');

config({
contracts: {
"Token": {
deploy: false,
args: [1000]
},
"MyToken": {
instanceOf: "Token"
},
"MyToken2": {
instanceOf: "Token",
args: [2000]
}
}
});

describe("ENS functions", function() {
it('should allow directives in ENS subdomains', async function() {
const myTokenAddress = await EmbarkJS.Names.resolve('mytoken.embark.eth');
assert.strictEqual(MyToken.options.address, myTokenAddress);

const myToken2Address = await EmbarkJS.Names.resolve('MyToken2.embark.eth');
assert.strictEqual(MyToken2.options.address, myToken2Address);

const myTokenName = await EmbarkJS.Names.lookup(MyToken.options.address.toLowerCase());
assert.strictEqual(myTokenName, 'mytoken.embark.eth');

const myToken2Name = await EmbarkJS.Names.lookup(MyToken2.options.address.toLowerCase());
assert.strictEqual(myToken2Name, 'MyToken2.embark.eth');
});
});