ens resolve

This commit is contained in:
Subramanian Venkatesan 2018-10-05 14:49:10 +05:30 committed by Pascal Precht
parent 0f6e84bca3
commit f686fec74a
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
14 changed files with 116 additions and 19 deletions

View File

@ -133,7 +133,6 @@ class EmbarkController {
engine.startService("deployment"); engine.startService("deployment");
engine.startService("storage"); engine.startService("storage");
engine.startService("codeGenerator"); engine.startService("codeGenerator");
engine.startService("namingSystem");
engine.startService("console"); engine.startService("console");
engine.startService("pluginCommand"); engine.startService("pluginCommand");
@ -286,7 +285,6 @@ class EmbarkController {
engine.startService("deployment"); engine.startService("deployment");
engine.startService("storage"); engine.startService("storage");
engine.startService("codeGenerator"); engine.startService("codeGenerator");
engine.startService("namingSystem");
engine.startService("console"); engine.startService("console");
engine.startService("pluginCommand"); engine.startService("pluginCommand");
engine.events.once('check:backOnline:Ethereum', () => callback()); engine.events.once('check:backOnline:Ethereum', () => callback());
@ -478,7 +476,6 @@ class EmbarkController {
engine.startService("deployment"); engine.startService("deployment");
engine.startService("storage"); engine.startService("storage");
engine.startService("codeGenerator"); engine.startService("codeGenerator");
engine.startService("namingSystem");
callback(); callback();
}, },
function listLoadedPlugin(callback) { function listLoadedPlugin(callback) {

View File

@ -193,6 +193,7 @@ class Engine {
this.registerModule('profiler'); this.registerModule('profiler');
this.registerModule('deploytracker', {trackContracts: options.trackContracts}); this.registerModule('deploytracker', {trackContracts: options.trackContracts});
this.registerModule('specialconfigs'); this.registerModule('specialconfigs');
this.registerModule('ens');
this.registerModule('console_listener', {ipc: self.ipc}); this.registerModule('console_listener', {ipc: self.ipc});
this.registerModule('deployment', {plugins: this.plugins, onlyCompile: options.onlyCompile}); this.registerModule('deployment', {plugins: this.plugins, onlyCompile: options.onlyCompile});

View File

@ -5,7 +5,6 @@ let utils = require('../../utils/utils.js');
class ContractDeployer { class ContractDeployer {
constructor(options) { constructor(options) {
const self = this; const self = this;
this.logger = options.logger; this.logger = options.logger;
this.events = options.events; this.events = options.events;
this.plugins = options.plugins; this.plugins = options.plugins;
@ -57,12 +56,26 @@ class ContractDeployer {
async.map(arg, (sub_arg, nextSubEachCb) => { async.map(arg, (sub_arg, nextSubEachCb) => {
if (sub_arg[0] === "$") { if (sub_arg[0] === "$") {
parseArg(sub_arg, nextSubEachCb); parseArg(sub_arg, nextSubEachCb);
} else if(typeof sub_arg === 'string' && sub_arg.indexOf('.eth') === sub_arg.length - 4) {
self.events.request("ens:resolve", sub_arg, (err, name) => {
if(err) {
return nextSubEachCb(err);
}
return nextSubEachCb(err, name);
});
} else { } else {
nextSubEachCb(null, sub_arg); nextSubEachCb(null, sub_arg);
} }
}, (err, subRealArgs) => { }, (err, subRealArgs) => {
nextEachCb(null, subRealArgs); nextEachCb(null, subRealArgs);
}); });
} else if(typeof arg === 'string' && arg.indexOf('.eth') === arg.length - 4) {
self.events.request("ens:resolve", arg, (err, name) => {
if(err) {
return nextEachCb(err);
}
return nextEachCb(err, name);
});
} else { } else {
nextEachCb(null, arg); nextEachCb(null, arg);
} }

View File

@ -56,23 +56,27 @@ class ENS {
this.logger = embark.logger; this.logger = embark.logger;
this.events = embark.events; this.events = embark.events;
this.namesConfig = embark.config.namesystemConfig; this.namesConfig = embark.config.namesystemConfig;
this.enabled = false;
this.registration = this.namesConfig.register || {}; this.registration = this.namesConfig.register || {};
this.embark = embark; this.embark = embark;
this.ensConfig = require('./ensContractConfigs'); this.ensConfig = require('./ensContractConfigs');
this.configured = false; this.configured = false;
this.events.setCommandHandler("ens:resolve", this.ensResolve.bind(this));
if (this.namesConfig === {} || if (this.namesConfig === {} ||
this.namesConfig.enabled !== true || this.namesConfig.enabled !== true ||
this.namesConfig.available_providers.indexOf('ens') < 0) { this.namesConfig.available_providers.indexOf('ens') < 0) {
return; return;
} }
this.enabled = true;
this.doSetENSProvider = this.namesConfig.provider === 'ens'; this.doSetENSProvider = this.namesConfig.provider === 'ens';
this.addENSToEmbarkJS(); this.addENSToEmbarkJS();
this.registerEvents(); this.registerEvents();
this.registerConsoleCommands(); this.registerConsoleCommands();
} }
registerConsoleCommands() { registerConsoleCommands() {
this.embark.registerConsoleCommand((cmd, _options) => { this.embark.registerConsoleCommand((cmd, _options) => {
@ -102,7 +106,6 @@ class ENS {
registerEvents() { registerEvents() {
this.embark.registerActionForEvent("deploy:beforeAll", this.configureContractsAndRegister.bind(this)); this.embark.registerActionForEvent("deploy:beforeAll", this.configureContractsAndRegister.bind(this));
this.events.setCommandHandler("storage:ens:associate", this.associateStorageToEns.bind(this)); this.events.setCommandHandler("storage:ens:associate", this.associateStorageToEns.bind(this));
} }
@ -150,7 +153,6 @@ class ENS {
} catch (e) { } catch (e) {
return cb('Invalid IPFS hash'); return cb('Invalid IPFS hash');
} }
// Set content // Set content
async.waterfall([ async.waterfall([
function getRegistryABI(next) { function getRegistryABI(next) {
@ -361,6 +363,40 @@ class ENS {
this.embark.addProviderInit('names', code, shouldInit); this.embark.addProviderInit('names', code, shouldInit);
this.embark.addConsoleProviderInit('names', code, shouldInit); this.embark.addConsoleProviderInit('names', code, shouldInit);
} }
ensResolve(name, cb) {
const self = this;
if (!self.enabled) {
return cb('ENS not enabled');
}
if(!self.configured) {
return cb('ENS not configured');
}
const hashedName = namehash.hash(name);
async.waterfall([
function getResolverAddress(next) {
self.ensContract.methods.resolver(hashedName).call((err, resolverAddress) => {
if (err) {
next(err);
} else if(resolverAddress === '0x0000000000000000000000000000000000000000') {
next('Name not yet registered');
} else {
next(null, resolverAddress);
}
});
},
function createResolverContract(resolverAddress, next) {
self.events.request("blockchain:contract:create",
{abi: self.ensConfig.Resolver.abiDefinition, address: resolverAddress},
(resolver) => {
next(null, resolver);
});
},
function resolveName(resolverContract, next) {
resolverContract.methods.addr(hashedName).call(next);
}
], cb);
}
} }
module.exports = ENS; module.exports = ENS;

View File

@ -15,8 +15,28 @@ class SpecialConfigs {
this.registerDeployIfAction(); this.registerDeployIfAction();
} }
replaceWithENSAddress(cmd, cb) {
const self = this;
let regex = /\'[a-zA-Z0-9.]+\.eth\'/g;
return stringReplaceAsync.seq(cmd, regex, (ensDomain) => {
ensDomain = ensDomain.slice(1, ensDomain.length - 1);
return (new Promise((resolve, reject) => {
self.events.request("ens:resolve", ensDomain, (err, address) => {
if(err) {
return reject(new Error(err));
}
address = `'${address}'`;
return resolve(address);
});
}));
}).then((address) => {
cb(null, address);
}).catch(cb);
}
replaceWithAddresses(cmd, cb) { replaceWithAddresses(cmd, cb) {
const self = this; const self = this;
let regex = /\$\w+/g; let regex = /\$\w+/g;
stringReplaceAsync.seq(cmd, regex, (match) => { stringReplaceAsync.seq(cmd, regex, (match) => {
return (new Promise((resolve, reject) => { return (new Promise((resolve, reject) => {
@ -40,8 +60,8 @@ class SpecialConfigs {
return resolve(referedContract.deployedAddress); return resolve(referedContract.deployedAddress);
}); });
})); }));
}).then((result) => { }).then((address) => {
cb(null, result); cb(null, address);
}).catch(cb); }).catch(cb);
} }
@ -50,9 +70,13 @@ class SpecialConfigs {
this.embark.registerActionForEvent("contracts:deploy:afterAll", (cb) => { this.embark.registerActionForEvent("contracts:deploy:afterAll", (cb) => {
let afterDeployCmds = self.contractsConfig.afterDeploy || []; let afterDeployCmds = self.contractsConfig.afterDeploy || [];
async.mapLimit(afterDeployCmds, 1, (cmd, nextMapCb) => { async.mapLimit(afterDeployCmds, 1, (cmd, nextMapCb) => {
self.replaceWithAddresses(cmd, nextMapCb); async.waterfall([
function replaceWithAddresses(next) {
self.replaceWithAddresses(cmd, next);
},
self.replaceWithENSAddress.bind(self)
], nextMapCb);
}, (err, onDeployCode) => { }, (err, onDeployCode) => {
if (err) { if (err) {
self.logger.trace(err); self.logger.trace(err);
@ -87,14 +111,19 @@ class SpecialConfigs {
if (!contract.onDeploy || contract.deploy === false) { if (!contract.onDeploy || contract.deploy === false) {
return cb(); return cb();
} }
if (!contract.silent) { if (!contract.silent) {
self.logger.info(__('executing onDeploy commands')); self.logger.info(__('executing onDeploy commands'));
} }
let onDeployCmds = contract.onDeploy; let onDeployCmds = contract.onDeploy;
async.mapLimit(onDeployCmds, 1, (cmd, nextMapCb) => { async.mapLimit(onDeployCmds, 1, (cmd, nextMapCb) => {
self.replaceWithAddresses(cmd, nextMapCb); async.waterfall([
function replaceWithAddresses(next) {
self.replaceWithAddresses(cmd, next);
},
self.replaceWithENSAddress.bind(self)
], nextMapCb);
}, (err, onDeployCode) => { }, (err, onDeployCode) => {
if (err) { if (err) {
return cb(new Error("error running onDeploy for " + contract.className.cyan)); return cb(new Error("error running onDeploy for " + contract.className.cyan));

View File

@ -2,9 +2,11 @@ pragma solidity ^0.4.24;
contract AnotherStorage { contract AnotherStorage {
address public simpleStorageAddress; address public simpleStorageAddress;
address simpleStorageAddress2; address simpleStorageAddress2;
address ens;
constructor(address addr) public { constructor(address addr, address _ens) public {
simpleStorageAddress = addr; simpleStorageAddress = addr;
ens = _ens;
} }
} }

View File

@ -19,4 +19,8 @@ contract Test {
addr = _addr; addr = _addr;
} }
function changeAddress2(address _addr) public {
}
} }

View File

@ -6,7 +6,8 @@
"contracts": "contracts.json", "contracts": "contracts.json",
"storage": false, "storage": false,
"communication": false, "communication": false,
"webserver": false "webserver": false,
"namesystem": "ens.json"
}, },
"versions": { "versions": {
"web3": "1.0.0-beta", "web3": "1.0.0-beta",

View File

@ -0,0 +1,9 @@
{
"default" : {
"enabled": true,
"available_providers": "ens",
"register": {
"rootDomain": "embark.eth"
}
}
}

View File

@ -9,7 +9,7 @@ config({
args: [100] args: [100]
}, },
"AnotherStorage": { "AnotherStorage": {
args: ["$SimpleStorage"] args: ["$SimpleStorage", "embark.eth"]
} }
} }
}); });

View File

@ -13,7 +13,7 @@ config({
args: [100] args: [100]
}, },
AnotherStorage: { AnotherStorage: {
args: ["$SimpleStorage"] args: ["$SimpleStorage", "0xCAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE"]
}, },
Token: { Token: {
deploy: false, deploy: false,

View File

@ -10,6 +10,7 @@ library ZAMyLib {
contract Test { contract Test {
address public addr; address public addr;
address public ens;
function testAdd() public pure returns (uint _result) { function testAdd() public pure returns (uint _result) {
return ZAMyLib.add(1, 2); return ZAMyLib.add(1, 2);
@ -19,4 +20,8 @@ contract Test {
addr = _addr; addr = _addr;
} }
function changeENS(address _ens) public {
ens = _ens;
}
} }

View File

@ -30,7 +30,7 @@ module.exports = {
args: [1000] args: [1000]
}, },
Test: { Test: {
onDeploy: ["Test.methods.changeAddress('$MyToken')"] onDeploy: ["Test.methods.changeAddress('$MyToken')", "Test.methods.changeENS('embark.eth')"]
}, },
MyToken: { MyToken: {
instanceOf: "Token" instanceOf: "Token"

View File

@ -32,7 +32,7 @@ config({
instanceOf: "Token" instanceOf: "Token"
}, },
Test: { Test: {
onDeploy: ["Test.methods.changeAddress('$MyToken').send()"] onDeploy: ["Test.methods.changeAddress('$MyToken').send()", "Test.methods.changeENS('embark.eth').send()"]
}, },
ContractArgs: { ContractArgs: {
args: { args: {