feat(@mbark/ens): enable the use of $accounts in registrations

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
This commit is contained in:
Jonathan Rainville 2020-01-31 15:48:08 -05:00 committed by Iuri Matias
parent c0042844a3
commit de0102223d
2 changed files with 34 additions and 11 deletions

View File

@ -4,6 +4,8 @@ const MyToken = require('Embark/contracts/MyToken');
const MyToken2 = require('Embark/contracts/MyToken2');
const EmbarkJS = require('Embark/EmbarkJS');
let accounts;
config({
namesystem: {
enabled: true,
@ -11,7 +13,8 @@ config({
"rootDomain": "embark.eth",
"subdomains": {
"mytoken": "$MyToken",
"MyToken2": "$MyToken2"
"MyToken2": "$MyToken2",
"account": "$accounts[0]"
}
}
},
@ -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');

View File

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