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

Support directives in ENS configurations, such that subdomains can be registered to addresses of deployed tokens.

The following directives are supported:
```
"register": {
      "rootDomain": "embark.eth",
      "subdomains": {
        "status": "0x4a17f35f0a9927fb4141aa91cbbc72c1b31598de",
        "mytoken": "$MyToken",
        "MyToken2": "$MyToken2"
      }
    }
```

Add unit test for these directives.
This commit is contained in:
emizzle 2018-12-05 15:41:05 +11:00 committed by Iuri Matias
parent c24536d8a6
commit 75111569a2
3 changed files with 70 additions and 13 deletions

View File

@ -223,6 +223,7 @@ class ENS {
this.events.request("blockchain:defaultAccount:get", (defaultAccount) => { this.events.request("blockchain:defaultAccount:get", (defaultAccount) => {
async.each(Object.keys(this.registration.subdomains), (subDomainName, eachCb) => { async.each(Object.keys(this.registration.subdomains), (subDomainName, eachCb) => {
const address = this.registration.subdomains[subDomainName]; const address = this.registration.subdomains[subDomainName];
const directivesRegExp = new RegExp(/\$(\w+\[?\d?\]?)/g);
this.ensResolve(`${subDomainName}.${this.registration.rootDomain}`, (error, currentAddress) => { this.ensResolve(`${subDomainName}.${this.registration.rootDomain}`, (error, currentAddress) => {
if (currentAddress && currentAddress.toLowerCase() === address.toLowerCase()) { if (currentAddress && currentAddress.toLowerCase() === address.toLowerCase()) {
return eachCb(); return eachCb();
@ -233,14 +234,30 @@ class ENS {
return eachCb(error); 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
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); const reverseNode = utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix);
ENSFunctions.registerSubDomain(this.ensContract, this.registrarContract, this.resolverContract, defaultAccount, this.registerSubDomain(defaultAccount, subDomainName, reverseNode, address, secureSend, eachCb);
subDomainName, this.registration.rootDomain, reverseNode, address, this.logger, secureSend, eachCb);
}); });
}, cb); }, 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) { createResolverContract(config, callback) {
this.events.request("blockchain:contract:create", { this.events.request("blockchain:contract:create", {
abi: config.resolverAbi, abi: config.resolverAbi,

View File

@ -8,7 +8,9 @@
"register": { "register": {
"rootDomain": "embark.eth", "rootDomain": "embark.eth",
"subdomains": { "subdomains": {
"status": "0x4a17f35f0a9927fb4141aa91cbbc72c1b31598de" "status": "0x4a17f35f0a9927fb4141aa91cbbc72c1b31598de",
"mytoken": "$MyToken",
"MyToken2": "$MyToken2"
} }
} }
} }

View File

@ -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');
});
});