Skip to content

Commit

Permalink
feat(@mbark/ens): enable the use of $accounts in registrations
Browse files Browse the repository at this point in the history
Enable putting `$accounts[i]` in subdomain registrations, where `i`
is the index of the `getAccounts` array.
This is the same behaviour we have for contract deployement
  • Loading branch information
jrainville authored and iurimatias committed Feb 6, 2020
1 parent c004284 commit de01022
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
14 changes: 11 additions & 3 deletions dapps/tests/app/test/namesystem_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ const MyToken = require('Embark/contracts/MyToken');
const MyToken2 = require('Embark/contracts/MyToken2');
const EmbarkJS = require('Embark/EmbarkJS');

let accounts;

config({
namesystem: {
enabled: true,
"register": {
"rootDomain": "embark.eth",
"subdomains": {
"mytoken": "$MyToken",
"MyToken2": "$MyToken2"
"MyToken2": "$MyToken2",
"account": "$accounts[0]"
}
}
},
Expand All @@ -30,15 +33,20 @@ config({
}
}
}
}, (_err, web3_accounts) => {
accounts = web3_accounts;
});

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);
assert.strictEqual(myTokenAddress, MyToken.options.address);

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

const accountAddress = await EmbarkJS.Names.resolve('account.embark.eth');
assert.strictEqual(accountAddress, accounts[0]);

const myTokenName = await EmbarkJS.Names.lookup(MyToken.options.address.toLowerCase());
assert.strictEqual(myTokenName, 'mytoken.embark.eth');
Expand Down
31 changes: 23 additions & 8 deletions packages/plugins/ens/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,35 @@ class ENS {
}

async registerConfigDomains(config, cb) {
const defaultAccount = await this.web3DefaultAccount;
if (!this.config.namesystemConfig.register) {
return cb();
}
const defaultAccount = await this.web3DefaultAccount;
const web3 = await this.web3;

async.each(Object.keys(this.config.namesystemConfig.register.subdomains), (subDomainName, eachCb) => {
const address = this.config.namesystemConfig.register.subdomains[subDomainName];
let address = this.config.namesystemConfig.register.subdomains[subDomainName];
const directivesRegExp = new RegExp(/\$(\w+\[?\d?\]?)/g);

const directives = directivesRegExp.exec(address);
if (directives && directives.length) {
return eachCb();
}
this.safeRegisterSubDomain(subDomainName, address, defaultAccount, eachCb);
// Using an anonymous function here because setting an async.js function as `async` creates issues
(async () => {
const directives = directivesRegExp.exec(address);
if (directives && directives.length) {
if (!directives[0].includes('accounts')) {
return eachCb();
}

const match = address.match(/\$accounts\[([0-9]+)]/);
const accountIndex = match[1];
const accounts = await web3.eth.getAccounts();

if (!accounts[accountIndex]) {
return eachCb(__('No corresponding account at index %d', match[1]));
}
address = accounts[accountIndex];
}
this.safeRegisterSubDomain(subDomainName, address, defaultAccount, eachCb);
})();
}, cb);
}

Expand All @@ -215,7 +230,7 @@ class ENS {
const directivesRegExp = new RegExp(/\$(\w+\[?\d?\]?)/g);

const directives = directivesRegExp.exec(address);
if (!directives || !directives.length) {
if (!directives || !directives.length || directives[0].includes('accounts')) {
return resolve();
}

Expand Down

0 comments on commit de01022

Please sign in to comment.