From f686fec74a4a9b8568d39b88e3600773f8cf5f27 Mon Sep 17 00:00:00 2001 From: Subramanian Venkatesan Date: Fri, 5 Oct 2018 14:49:10 +0530 Subject: [PATCH] ens resolve --- cmd/cmd_controller.js | 3 -- lib/core/engine.js | 1 + lib/modules/deployment/contract_deployer.js | 15 ++++++- lib/modules/ens/index.js | 42 +++++++++++++++++-- lib/modules/specialconfigs/index.js | 41 +++++++++++++++--- .../contracts/another_storage.sol | 4 +- test_apps/contracts_app/contracts/test.sol | 4 ++ test_apps/contracts_app/embark.json | 3 +- test_apps/contracts_app/ens.json | 9 ++++ .../test/another_storage_spec.js | 2 +- test_apps/contracts_app/test/token_spec.js | 2 +- test_apps/test_app/app/contracts/test.sol | 5 +++ test_apps/test_app/config/contracts.js | 2 +- test_apps/test_app/test/token_spec.js | 2 +- 14 files changed, 116 insertions(+), 19 deletions(-) create mode 100644 test_apps/contracts_app/ens.json diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 697730341..80884543d 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -133,7 +133,6 @@ class EmbarkController { engine.startService("deployment"); engine.startService("storage"); engine.startService("codeGenerator"); - engine.startService("namingSystem"); engine.startService("console"); engine.startService("pluginCommand"); @@ -286,7 +285,6 @@ class EmbarkController { engine.startService("deployment"); engine.startService("storage"); engine.startService("codeGenerator"); - engine.startService("namingSystem"); engine.startService("console"); engine.startService("pluginCommand"); engine.events.once('check:backOnline:Ethereum', () => callback()); @@ -478,7 +476,6 @@ class EmbarkController { engine.startService("deployment"); engine.startService("storage"); engine.startService("codeGenerator"); - engine.startService("namingSystem"); callback(); }, function listLoadedPlugin(callback) { diff --git a/lib/core/engine.js b/lib/core/engine.js index 70e67e1f4..17aff1866 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -193,6 +193,7 @@ class Engine { this.registerModule('profiler'); this.registerModule('deploytracker', {trackContracts: options.trackContracts}); this.registerModule('specialconfigs'); + this.registerModule('ens'); this.registerModule('console_listener', {ipc: self.ipc}); this.registerModule('deployment', {plugins: this.plugins, onlyCompile: options.onlyCompile}); diff --git a/lib/modules/deployment/contract_deployer.js b/lib/modules/deployment/contract_deployer.js index 6084808e9..9966f9df2 100644 --- a/lib/modules/deployment/contract_deployer.js +++ b/lib/modules/deployment/contract_deployer.js @@ -5,7 +5,6 @@ let utils = require('../../utils/utils.js'); class ContractDeployer { constructor(options) { const self = this; - this.logger = options.logger; this.events = options.events; this.plugins = options.plugins; @@ -57,12 +56,26 @@ class ContractDeployer { async.map(arg, (sub_arg, nextSubEachCb) => { if (sub_arg[0] === "$") { 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 { nextSubEachCb(null, sub_arg); } }, (err, 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 { nextEachCb(null, arg); } diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index d48dffebf..3ac1d3156 100644 --- a/lib/modules/ens/index.js +++ b/lib/modules/ens/index.js @@ -56,23 +56,27 @@ class ENS { this.logger = embark.logger; this.events = embark.events; this.namesConfig = embark.config.namesystemConfig; + this.enabled = false; this.registration = this.namesConfig.register || {}; this.embark = embark; this.ensConfig = require('./ensContractConfigs'); this.configured = false; + this.events.setCommandHandler("ens:resolve", this.ensResolve.bind(this)); + if (this.namesConfig === {} || this.namesConfig.enabled !== true || this.namesConfig.available_providers.indexOf('ens') < 0) { return; } + this.enabled = true; this.doSetENSProvider = this.namesConfig.provider === 'ens'; this.addENSToEmbarkJS(); this.registerEvents(); this.registerConsoleCommands(); } - + registerConsoleCommands() { this.embark.registerConsoleCommand((cmd, _options) => { @@ -102,7 +106,6 @@ class ENS { registerEvents() { this.embark.registerActionForEvent("deploy:beforeAll", this.configureContractsAndRegister.bind(this)); - this.events.setCommandHandler("storage:ens:associate", this.associateStorageToEns.bind(this)); } @@ -150,7 +153,6 @@ class ENS { } catch (e) { return cb('Invalid IPFS hash'); } - // Set content async.waterfall([ function getRegistryABI(next) { @@ -361,6 +363,40 @@ class ENS { this.embark.addProviderInit('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; diff --git a/lib/modules/specialconfigs/index.js b/lib/modules/specialconfigs/index.js index 93e528305..21900d990 100644 --- a/lib/modules/specialconfigs/index.js +++ b/lib/modules/specialconfigs/index.js @@ -15,8 +15,28 @@ class SpecialConfigs { 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) { const self = this; + let regex = /\$\w+/g; stringReplaceAsync.seq(cmd, regex, (match) => { return (new Promise((resolve, reject) => { @@ -40,8 +60,8 @@ class SpecialConfigs { return resolve(referedContract.deployedAddress); }); })); - }).then((result) => { - cb(null, result); + }).then((address) => { + cb(null, address); }).catch(cb); } @@ -50,9 +70,13 @@ class SpecialConfigs { this.embark.registerActionForEvent("contracts:deploy:afterAll", (cb) => { let afterDeployCmds = self.contractsConfig.afterDeploy || []; - 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) => { if (err) { self.logger.trace(err); @@ -87,14 +111,19 @@ class SpecialConfigs { if (!contract.onDeploy || contract.deploy === false) { return cb(); } + if (!contract.silent) { self.logger.info(__('executing onDeploy commands')); } let onDeployCmds = contract.onDeploy; - 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) => { if (err) { return cb(new Error("error running onDeploy for " + contract.className.cyan)); diff --git a/test_apps/contracts_app/contracts/another_storage.sol b/test_apps/contracts_app/contracts/another_storage.sol index 2bf83d1da..c3140a92d 100644 --- a/test_apps/contracts_app/contracts/another_storage.sol +++ b/test_apps/contracts_app/contracts/another_storage.sol @@ -2,9 +2,11 @@ pragma solidity ^0.4.24; contract AnotherStorage { address public simpleStorageAddress; address simpleStorageAddress2; + address ens; - constructor(address addr) public { + constructor(address addr, address _ens) public { simpleStorageAddress = addr; + ens = _ens; } } diff --git a/test_apps/contracts_app/contracts/test.sol b/test_apps/contracts_app/contracts/test.sol index a463f5f18..6951d1bab 100644 --- a/test_apps/contracts_app/contracts/test.sol +++ b/test_apps/contracts_app/contracts/test.sol @@ -19,4 +19,8 @@ contract Test { addr = _addr; } + function changeAddress2(address _addr) public { + + } + } diff --git a/test_apps/contracts_app/embark.json b/test_apps/contracts_app/embark.json index cf27cae37..58aff9197 100644 --- a/test_apps/contracts_app/embark.json +++ b/test_apps/contracts_app/embark.json @@ -6,7 +6,8 @@ "contracts": "contracts.json", "storage": false, "communication": false, - "webserver": false + "webserver": false, + "namesystem": "ens.json" }, "versions": { "web3": "1.0.0-beta", diff --git a/test_apps/contracts_app/ens.json b/test_apps/contracts_app/ens.json new file mode 100644 index 000000000..fe46ba5eb --- /dev/null +++ b/test_apps/contracts_app/ens.json @@ -0,0 +1,9 @@ +{ + "default" : { + "enabled": true, + "available_providers": "ens", + "register": { + "rootDomain": "embark.eth" + } + } +} diff --git a/test_apps/contracts_app/test/another_storage_spec.js b/test_apps/contracts_app/test/another_storage_spec.js index e304e8a1e..cc3241750 100644 --- a/test_apps/contracts_app/test/another_storage_spec.js +++ b/test_apps/contracts_app/test/another_storage_spec.js @@ -9,7 +9,7 @@ config({ args: [100] }, "AnotherStorage": { - args: ["$SimpleStorage"] + args: ["$SimpleStorage", "embark.eth"] } } }); diff --git a/test_apps/contracts_app/test/token_spec.js b/test_apps/contracts_app/test/token_spec.js index a6e542100..71aab2f24 100644 --- a/test_apps/contracts_app/test/token_spec.js +++ b/test_apps/contracts_app/test/token_spec.js @@ -13,7 +13,7 @@ config({ args: [100] }, AnotherStorage: { - args: ["$SimpleStorage"] + args: ["$SimpleStorage", "0xCAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE"] }, Token: { deploy: false, diff --git a/test_apps/test_app/app/contracts/test.sol b/test_apps/test_app/app/contracts/test.sol index a463f5f18..f2b7e26f8 100644 --- a/test_apps/test_app/app/contracts/test.sol +++ b/test_apps/test_app/app/contracts/test.sol @@ -10,6 +10,7 @@ library ZAMyLib { contract Test { address public addr; + address public ens; function testAdd() public pure returns (uint _result) { return ZAMyLib.add(1, 2); @@ -19,4 +20,8 @@ contract Test { addr = _addr; } + function changeENS(address _ens) public { + ens = _ens; + } + } diff --git a/test_apps/test_app/config/contracts.js b/test_apps/test_app/config/contracts.js index c68c002ef..fadb638ad 100644 --- a/test_apps/test_app/config/contracts.js +++ b/test_apps/test_app/config/contracts.js @@ -30,7 +30,7 @@ module.exports = { args: [1000] }, Test: { - onDeploy: ["Test.methods.changeAddress('$MyToken')"] + onDeploy: ["Test.methods.changeAddress('$MyToken')", "Test.methods.changeENS('embark.eth')"] }, MyToken: { instanceOf: "Token" diff --git a/test_apps/test_app/test/token_spec.js b/test_apps/test_app/test/token_spec.js index 6dbdcd1f9..adfdce6fd 100644 --- a/test_apps/test_app/test/token_spec.js +++ b/test_apps/test_app/test/token_spec.js @@ -32,7 +32,7 @@ config({ instanceOf: "Token" }, Test: { - onDeploy: ["Test.methods.changeAddress('$MyToken').send()"] + onDeploy: ["Test.methods.changeAddress('$MyToken').send()", "Test.methods.changeENS('embark.eth').send()"] }, ContractArgs: { args: {