From 850a80c90c23f529e8d50c7856a4fcc86078243b Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Fri, 28 Sep 2018 11:09:23 +0100 Subject: [PATCH] ENS config use the actual network ID Instead of relying on the name of network. Fetch the real network id and configure ENS based on that value --- cmd/cmd_controller.js | 6 +- lib/modules/blockchain_connector/index.js | 10 +- lib/modules/ens/index.js | 183 +++++++++++----------- 3 files changed, 107 insertions(+), 92 deletions(-) diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 41ff8ce76..315eec006 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -286,9 +286,11 @@ class EmbarkController { engine.startService("namingSystem"); engine.startService("console"); engine.startService("pluginCommand"); - callback(); + engine.events.on('check:backOnline:Ethereum', function () { + callback(); + }); }, - function web3IPC(callback) { + function ipcConnect(callback) { // Do specific work in case we are connected to a socket: // - Setup Web3 // - Apply history diff --git a/lib/modules/blockchain_connector/index.js b/lib/modules/blockchain_connector/index.js index eda9d2463..08dcf59dd 100644 --- a/lib/modules/blockchain_connector/index.js +++ b/lib/modules/blockchain_connector/index.js @@ -76,7 +76,7 @@ class BlockchainConnector { self.events.request("processes:launch", "blockchain", () => { self.provider.startWeb3Provider(() => { - this.web3.eth.net.getId() + this.getNetworkId() .then(id => { let networkId = self.blockchainConfig.networkId; if (!networkId && constants.blockchain.networkIds[self.blockchainConfig.networkType]) { @@ -185,6 +185,10 @@ class BlockchainConnector { self.getGasPrice(cb); }); + this.events.setCommandHandler("blockchain:networkId", function(cb) { + self.getNetworkId().then(cb); + }); + this.events.setCommandHandler("blockchain:contract:create", function(params, cb) { cb(self.ContractObject(params)); }); @@ -217,6 +221,10 @@ class BlockchainConnector { }); } + getNetworkId() { + return this.web3.eth.net.getId(); + } + ContractObject(params) { return new this.web3.eth.Contract(params.abi, params.address); } diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index eb5a67319..3931e7817 100644 --- a/lib/modules/ens/index.js +++ b/lib/modules/ens/index.js @@ -5,10 +5,78 @@ const async = require('async'); const embarkJsUtils = require('embarkjs').Utils; const reverseAddrSuffix = '.addr.reverse'; +const DEFAULT_ENS_CONTRACTS_CONFIG = { + "default": { + "contracts": { + "ENS": { + "deploy": false, + "silent": true + }, + "ENSRegistry": { + "deploy": true, + "silent": true, + "args": [] + }, + "Resolver": { + "deploy": true, + "silent": true, + "args": ["$ENSRegistry"] + }, + "FIFSRegistrar": { + "deploy": false + } + } + } +}; + +const ENS_CONTRACTS_CONFIG = { + "1": { + "contracts": { + "ENSRegistry": { + "address": "0x314159265dd8dbb310642f98f50c066173c1259b", + "silent": true + }, + "Resolver": { + "deploy": false + }, + "FIFSRegistrar": { + "deploy": false + } + } + }, + "3": { + "contracts": { + "ENSRegistry": { + "address": "0x112234455c3a32fd11230c42e7bccd4a84e02010", + "silent": true + }, + "Resolver": { + "deploy": false + }, + "FIFSRegistrar": { + "deploy": false + } + } + }, + "4": { + "contracts": { + "ENSRegistry": { + "address": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A", + "silent": true + }, + "Resolver": { + "deploy": false + }, + "FIFSRegistrar": { + "deploy": false + } + } + } +}; + class ENS { constructor(embark, _options) { this.env = embark.env; - this.isDev = embark.config.blockchainConfig.isDev; this.logger = embark.logger; this.events = embark.events; this.namesConfig = embark.config.namesystemConfig; @@ -23,12 +91,12 @@ class ENS { this.doSetENSProvider = this.namesConfig.provider === 'ens'; this.addENSToEmbarkJS(); - this.configureContracts(); this.registerEvents(); } registerEvents() { this.events.once("contracts:deploy:afterAll", this.setProviderAndRegisterDomains.bind(this)); + this.events.once("web3Ready", this.configureContracts.bind(this)); this.events.setCommandHandler("storage:ens:associate", this.associateStorageToEns.bind(this)); } @@ -74,10 +142,14 @@ class ENS { self.addSetProvider(config); } - if ((!self.isDev && self.env !== 'privatenet') || !self.registration || !self.registration.subdomains || !Object.keys(self.registration.subdomains).length) { - return cb(); - } - self.registerConfigDomains(config, cb); + self.events.request('blockchain:networkId', (networkId) => { + const isKnownNetworks = Object.keys(ENS_CONTRACTS_CONFIG).includes(networkId); + const shouldRegisterSubdomain = self.registration && self.registration.subdomains && Object.keys(self.registration.subdomains).length; + if (isKnownNetworks || !shouldRegisterSubdomain) { + return cb(); + } + self.registerConfigDomains(config, cb); + }); }); } @@ -216,100 +288,33 @@ class ENS { } configureContracts() { - const config = { - "default": { - "gas": "auto", - "contracts": { - "ENS": { - "deploy": false, - "silent": true - }, - "ENSRegistry": { - "deploy": true, - "silent": true, - "args": [] - }, - "Resolver": { - "deploy": true, - "silent": true, - "args": ["$ENSRegistry"] - }, - "FIFSRegistrar": { - "deploy": false - } - } - }, - "ropsten": { - "contracts": { - "ENSRegistry": { - "address": "0x112234455c3a32fd11230c42e7bccd4a84e02010", - "silent": true - }, - "Resolver": { - "deploy": false - }, - "FIFSRegistrar": { - "deploy": false - } - } - }, - "rinkeby": { - "contracts": { - "ENSRegistry": { - "address": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A", - "silent": true - }, - "Resolver": { - "deploy": false - }, - "FIFSRegistrar": { - "deploy": false - } - } - }, - "livenet": { - "contracts": { - "ENSRegistry": { - "address": "0x314159265dd8dbb310642f98f50c066173c1259b", - "silent": true - }, - "Resolver": { - "deploy": false - }, - "FIFSRegistrar": { - "deploy": false - } - } - } - }; - config.testnet = config.ropsten; + this.events.request('blockchain:networkId', (networkId) => { + const config = Object.assign({}, DEFAULT_ENS_CONTRACTS_CONFIG, {[this.env]: ENS_CONTRACTS_CONFIG[networkId]}); - if (this.registration && this.registration.rootDomain) { - // Register root domain if it is defined - const rootNode = namehash.hash(this.registration.rootDomain); - config.default.contracts['FIFSRegistrar'] = { - "deploy": true, - "silent": true, - "args": ["$ENSRegistry", rootNode], - "onDeploy": [ - `ENSRegistry.methods.setOwner('${rootNode}', web3.eth.defaultAccount).send({from: web3.eth.defaultAccount}).then(() => { + if (this.registration && this.registration.rootDomain) { + // Register root domain if it is defined + const rootNode = namehash.hash(this.registration.rootDomain); + config.default.contracts['FIFSRegistrar'] = { + "deploy": true, + "silent": true, + "args": ["$ENSRegistry", rootNode], + "onDeploy": [ + `ENSRegistry.methods.setOwner('${rootNode}', web3.eth.defaultAccount).send({from: web3.eth.defaultAccount}).then(() => { ENSRegistry.methods.setResolver('${rootNode}', "$Resolver").send({from: web3.eth.defaultAccount}); var reverseNode = web3.utils.soliditySha3(web3.eth.defaultAccount.toLowerCase().substr(2) + '${reverseAddrSuffix}'); ENSRegistry.methods.setResolver(reverseNode, "$Resolver").send({from: web3.eth.defaultAccount}); Resolver.methods.setAddr('${rootNode}', web3.eth.defaultAccount).send({from: web3.eth.defaultAccount}); Resolver.methods.setName(reverseNode, '${this.registration.rootDomain}').send({from: web3.eth.defaultAccount}); })` - ] - }; - } - config.privatenet = config.development; - this.embark.registerContractConfiguration(config); + ] + }; + } + this.embark.registerContractConfiguration(config); - //if (this.isDev || this.env === 'privatenet') { this.embark.events.request("config:contractsFiles:add", this.embark.pathToFile('./contracts/ENSRegistry.sol')); this.embark.events.request("config:contractsFiles:add", this.embark.pathToFile('./contracts/FIFSRegistrar.sol')); this.embark.events.request("config:contractsFiles:add", this.embark.pathToFile('./contracts/Resolver.sol')); - //} + }); } addSetProvider(config) {