From d5f6da35993bc92d5c4c691af1ae7922bcf67ce5 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 21 Dec 2018 11:28:10 -0500 Subject: [PATCH] fix(ens/web3): use blockchain connector for ens and fix global web3 Use the blockchain connector for the ENS module to remove direct web3 usages Fix global web3 being overidden by the code generator --- .../modules/blockchain_connector/embarkjs.js | 16 ++++-- src/lib/modules/code_generator/index.js | 1 - src/lib/modules/ens/ENSFunctions.js | 3 +- src/lib/modules/ens/embarkjs.js | 51 ++++++++++++++----- src/lib/modules/ens/index.js | 25 +++++---- src/lib/modules/ens/register.js | 50 ------------------ 6 files changed, 61 insertions(+), 85 deletions(-) delete mode 100644 src/lib/modules/ens/register.js diff --git a/src/lib/modules/blockchain_connector/embarkjs.js b/src/lib/modules/blockchain_connector/embarkjs.js index a64f2cb81..225d6a727 100644 --- a/src/lib/modules/blockchain_connector/embarkjs.js +++ b/src/lib/modules/blockchain_connector/embarkjs.js @@ -2,11 +2,13 @@ const __embarkWeb3 = {}; -__embarkWeb3.init = function (_config) { - this.web3 = new Web3(); - if (!global.web3) { - global.web3 = this.web3; - } +__embarkWeb3.init = function(_config) { + this.web3 = global.web3 || new Web3(); + global.web3 = global.web3 || this.web3; +}; + +__embarkWeb3.getInstance = function () { + return this.web3; }; __embarkWeb3.getAccounts = function () { @@ -45,4 +47,8 @@ __embarkWeb3.toWei = function () { return this.web3.toWei(...arguments); }; +__embarkWeb3.getNetworkId = function () { + return this.web3.eth.net.getId(); +}; + diff --git a/src/lib/modules/code_generator/index.js b/src/lib/modules/code_generator/index.js index fb79d89d1..c25db16f2 100644 --- a/src/lib/modules/code_generator/index.js +++ b/src/lib/modules/code_generator/index.js @@ -371,7 +371,6 @@ class CodeGenerator { code += "\n if (typeof web3 === 'undefined') {"; code += "\n var web3 = new Web3();\n"; code += "\n }"; - code += "\nglobal.web3 = web3;\n"; let providerCode = self.generateProvider(false); code += providerCode; diff --git a/src/lib/modules/ens/ENSFunctions.js b/src/lib/modules/ens/ENSFunctions.js index 1a3ef05f8..ce0c497b0 100644 --- a/src/lib/modules/ens/ENSFunctions.js +++ b/src/lib/modules/ens/ENSFunctions.js @@ -1,4 +1,3 @@ -/*global web3*/ const namehash = require('eth-ens-namehash'); // Price of ENS registration contract functions const ENS_GAS_PRICE = 700000; @@ -8,7 +7,7 @@ const reverseAddressSuffix = '.addr.reverse'; const NoDecodeAddrErr = 'Error: Couldn\'t decode address from ABI: 0x'; const NoDecodeStringErr = 'ERROR: The returned value is not a convertible string: 0x0'; -function registerSubDomain(ens, registrar, resolver, defaultAccount, subdomain, rootDomain, reverseNode, address, logger, secureSend, callback) { +function registerSubDomain(web3, ens, registrar, resolver, defaultAccount, subdomain, rootDomain, reverseNode, address, logger, secureSend, callback) { const subnode = namehash.hash(subdomain); const rootNode = namehash.hash(rootDomain); const node = namehash.hash(`${subdomain}.${rootDomain}`); diff --git a/src/lib/modules/ens/embarkjs.js b/src/lib/modules/ens/embarkjs.js index 36c18b3fe..00b7bfddd 100644 --- a/src/lib/modules/ens/embarkjs.js +++ b/src/lib/modules/ens/embarkjs.js @@ -1,4 +1,4 @@ -/*global EmbarkJS, web3, registerSubDomain, namehash*/ +/*global EmbarkJS, Web3, registerSubDomain, namehash*/ let __embarkENS = {}; @@ -153,13 +153,25 @@ __embarkENS.setProvider = function (config) { self.registration = config.registration; self.env = config.env; EmbarkJS.onReady(() => { - web3.eth.net.getId() + EmbarkJS.Blockchain.blockchainConnector.getNetworkId() .then((id) => { const registryAddress = self.registryAddresses[id] || config.registryAddress; self._isAvailable = true; - self.ens = new EmbarkJS.Blockchain.Contract({abi: config.registryAbi, address: registryAddress, web3: web3}); - self.registrar = new EmbarkJS.Blockchain.Contract({abi: config.registrarAbi, address: config.registrarAddress, web3: web3}); - self.resolver = new EmbarkJS.Blockchain.Contract({abi: config.resolverAbi, address: config.resolverAddress, web3: web3}); + self.ens = new EmbarkJS.Blockchain.Contract({ + abi: config.registryAbi, + address: registryAddress, + web3: EmbarkJS.Blockchain.blockchainConnector.getInstance() + }); + self.registrar = new EmbarkJS.Blockchain.Contract({ + abi: config.registrarAbi, + address: config.registrarAddress, + web3: EmbarkJS.Blockchain.blockchainConnector.getInstance() + }); + self.resolver = new EmbarkJS.Blockchain.Contract({ + abi: config.resolverAbi, + address: config.resolverAddress, + web3: EmbarkJS.Blockchain.blockchainConnector.getInstance() + }); }) .catch(err => { if (err.message.indexOf('Provider not set or invalid') > -1) { @@ -188,7 +200,7 @@ __embarkENS.resolve = function (name, callback) { if (!this.ens) { resolveOrReject(providerNotSetError); } - if (!web3.eth.defaultAccount) { + if (!EmbarkJS.Blockchain.blockchainConnector.getDefaultAccount()) { resolveOrReject(defaultAccountNotSetError); } @@ -201,7 +213,7 @@ __embarkENS.resolve = function (name, callback) { let resolverContract = new EmbarkJS.Blockchain.Contract({ abi: this.resolverInterface, address: resolvedAddress, - web3: web3 + web3: EmbarkJS.Blockchain.blockchainConnector.getInstance() }); resolverContract.methods.addr(node).call(resolveOrReject); }).catch(resolveOrReject); @@ -225,7 +237,7 @@ __embarkENS.lookup = function (address, callback) { return resolveOrReject(providerNotSetError); } - if (!web3.eth.defaultAccount) { + if (!EmbarkJS.Blockchain.blockchainConnector.getDefaultAccount()) { return resolveOrReject(defaultAccountNotSetError); } @@ -233,7 +245,7 @@ __embarkENS.lookup = function (address, callback) { address = address.slice(2); } - let node = web3.utils.soliditySha3(address.toLowerCase() + reverseAddrSuffix); + let node = Web3.utils.soliditySha3(address.toLowerCase() + reverseAddrSuffix); this.ens.methods.resolver(node).call().then(resolverAddress => { if (resolverAddress === voidAddress) { @@ -242,7 +254,7 @@ __embarkENS.lookup = function (address, callback) { const resolverContract = new EmbarkJS.Blockchain.Contract({ abi: this.resolverInterface, address: resolverAddress, - web3: web3 + web3: EmbarkJS.Blockchain.blockchainConnector.getInstance() }); resolverContract.methods.name(node).call(resolveOrReject); }).catch(resolveOrReject); @@ -252,7 +264,7 @@ __embarkENS.lookup = function (address, callback) { __embarkENS.registerSubDomain = function (name, address, callback) { callback = callback || function () {}; - if (!web3.eth.defaultAccount) { + if (!EmbarkJS.Blockchain.blockchainConnector.getDefaultAccount()) { return callback(defaultAccountNotSetError); } @@ -262,13 +274,24 @@ __embarkENS.registerSubDomain = function (name, address, callback) { if (!this.registration || !this.registration.rootDomain) { return callback('No rootDomain is declared in config/namesystem.js (register.rootDomain). Unable to register a subdomain until then.'); } - if (!address || !web3.utils.isAddress(address)) { + if (!address || !Web3.utils.isAddress(address)) { return callback('You need to specify a valid address for the subdomain'); } // Register function generated by the index - registerSubDomain(this.ens, this.registrar, this.resolver, web3.eth.defaultAccount, name, this.registration.rootDomain, - web3.utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix), address, console, EmbarkJS.Utils.secureSend, (err, result) => { + registerSubDomain( + EmbarkJS.Blockchain.blockchainConnector.getInstance(), + this.ens, + this.registrar, + this.resolver, + EmbarkJS.Blockchain.blockchainConnector.getDefaultAccount(), + name, + this.registration.rootDomain, + Web3.utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix), + address, + console, + EmbarkJS.Utils.secureSend, + (err, result) => { if (err && err.indexOf('Transaction has been reverted by the EVM') > -1) { return callback('Registration was rejected. Are you the owner of the root domain?'); } diff --git a/src/lib/modules/ens/index.js b/src/lib/modules/ens/index.js index 46d126736..fccaa1661 100644 --- a/src/lib/modules/ens/index.js +++ b/src/lib/modules/ens/index.js @@ -281,8 +281,10 @@ class ENS { } registerSubDomain(defaultAccount, subDomainName, reverseNode, address, secureSend, cb) { - ENSFunctions.registerSubDomain(this.ensContract, this.registrarContract, this.resolverContract, defaultAccount, - subDomainName, this.registration.rootDomain, reverseNode, address, this.logger, secureSend, cb); + this.events.request("blockchain:get", (web3) => { + ENSFunctions.registerSubDomain(web3, this.ensContract, this.registrarContract, this.resolverContract, defaultAccount, + subDomainName, this.registration.rootDomain, reverseNode, address, this.logger, secureSend, cb); + }); } createResolverContract(config, callback) { @@ -311,7 +313,7 @@ class ENS { } ], function (error, address) { if (error) { - return res.send({error: error.message}); + return res.send({error: error.message || error}); } res.send({address}); }); @@ -328,7 +330,7 @@ class ENS { } ], function (error, name) { if (error) { - return res.send({error: error || error.message}); + return res.send({error: error.message || error}); } res.send({name}); }); @@ -340,16 +342,13 @@ class ENS { '/embark-api/ens/register', (req, res) => { self.events.request("blockchain:defaultAccount:get", (defaultAccount) => { - const secureSend = embarkJsUtils.secureSend; const {subdomain, address} = req.body; - const reverseNode = utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix); - ENSFunctions.registerSubDomain(self.ensContract, self.registrarContract, self.resolverContract, defaultAccount, - subdomain, self.registration.rootDomain, reverseNode, address, self.logger, secureSend, (error) => { - if (error) { - return res.send({error: error || error.message}); - } - res.send({name: `${req.body.subdomain}.${self.registration.rootDomain}`, address: req.body.address}); - }); + this.safeRegisterSubDomain(subdomain, address, defaultAccount, (error) => { + if (error) { + return res.send({error: error.message || error}); + } + res.send({name: `${req.body.subdomain}.${self.registration.rootDomain}`, address: req.body.address}); + }); }); } ); diff --git a/src/lib/modules/ens/register.js b/src/lib/modules/ens/register.js deleted file mode 100644 index ab344403a..000000000 --- a/src/lib/modules/ens/register.js +++ /dev/null @@ -1,50 +0,0 @@ -/*global web3*/ -const namehash = require('eth-ens-namehash'); -// Price of ENS registration contract functions -const ENS_GAS_PRICE = 700000; - -function registerSubDomain(ens, registrar, resolver, defaultAccount, subdomain, rootDomain, reverseNode, address, logger, secureSend, callback) { - const subnode = namehash.hash(subdomain); - const rootNode = namehash.hash(rootDomain); - const node = namehash.hash(`${subdomain}.${rootDomain}`); - // FIXME Registrar calls a function in ENS and in privatenet it doesn't work for soem reason - // const toSend = registrar.methods.register(subnode, defaultAccount); - const toSend = ens.methods.setSubnodeOwner(rootNode, subnode, defaultAccount); - let transaction; - - secureSend(web3, toSend, {from: defaultAccount, gas: ENS_GAS_PRICE}, false) - // Set resolver for the node - .then(transac => { - if (transac.status !== "0x1" && transac.status !== "0x01" && transac.status !== true) { - logger.warn('Failed transaction', transac); - return callback('Failed to register. Check gas cost.'); - } - transaction = transac; - return secureSend(web3, ens.methods.setResolver(node, resolver.options.address), {from: defaultAccount, gas: ENS_GAS_PRICE}, false); - }) - // Set address for node - .then(_result => { - return secureSend(web3, resolver.methods.setAddr(node, address), {from: defaultAccount, gas: ENS_GAS_PRICE}, false); - }) - // Set resolver for the reverse node - .then(_result => { - return secureSend(web3, ens.methods.setResolver(reverseNode, resolver.options.address), {from: defaultAccount, gas: ENS_GAS_PRICE}, false); - }) - // Set name for reverse node - .then(_result => { - return secureSend(web3, resolver.methods.setName(reverseNode, `${subdomain}.${rootDomain}`), {from: defaultAccount, gas: ENS_GAS_PRICE}, false); - }) - .then(_result => { - callback(null, transaction); - }) - .catch(err => { - logger.error(err.message || err); - callback('Failed to register with error: ' + (err.message || err)); - }); -} - -if (typeof module !== 'undefined' && module.exports) { - module.exports = registerSubDomain; -} - -