From 2195475fe6988fb4bfaf4a8509bd24f79c43e58d Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Mon, 17 Dec 2018 15:06:25 +0100 Subject: [PATCH] fix(@embark/ens): make resolve() work with promises and callbacks Changes in https://github.com/embark-framework/embark/commit/c64c093a48626736ce9da64b603e1c3820ea3f22 resulted in a regression that ENS functions within console/dashboard didn't work properly anymore. This commit ensures that both APIs, `EmbarkJS.Names.resolve()` as well as `EmbarkJS.Names.lookup()` can be either used using `async/await` or promised based syntax within the console/dashboard. Example: ``` await EmbarkJS.Names.resolve('me.eth.eth'); EmbarkJS.Names.resolve('me.eth.eth').then(val => ..., err => ...) EmbarkJS.Names.resolve('me.eth.eth', (err, val) => ...) ``` Same with: ``` await EmbarkJS.Names.lookup('0x...'); EmbarkJS.Names.lookup('0x...').then(val => ..., err => ...) EmbarkJS.Names.lookup('0x...', (err, val) => ...) ``` --- src/lib/core/modules/coderunner/codeRunner.js | 2 +- src/lib/modules/ens/embarkjs.js | 98 ++++++++++--------- 2 files changed, 55 insertions(+), 45 deletions(-) diff --git a/src/lib/core/modules/coderunner/codeRunner.js b/src/lib/core/modules/coderunner/codeRunner.js index 8b0341757..fa1b2f2a4 100644 --- a/src/lib/core/modules/coderunner/codeRunner.js +++ b/src/lib/core/modules/coderunner/codeRunner.js @@ -96,7 +96,7 @@ class CodeRunner { cb(null, value); } catch (error) { // Improve error message when there's no connection to node - if (error.message.indexOf(WEB3_INVALID_RESPONSE_ERROR) !== -1) { + if (error.message && error.message.indexOf(WEB3_INVALID_RESPONSE_ERROR) !== -1) { error.message += '. Are you connected to an Ethereum node?'; } diff --git a/src/lib/modules/ens/embarkjs.js b/src/lib/modules/ens/embarkjs.js index 2a38fb12e..36c18b3fe 100644 --- a/src/lib/modules/ens/embarkjs.js +++ b/src/lib/modules/ens/embarkjs.js @@ -172,71 +172,81 @@ __embarkENS.setProvider = function (config) { }; __embarkENS.resolve = function (name, callback) { - callback = callback || function () {}; - if (!this.ens) { - return callback(providerNotSetError); - } - if (!web3.eth.defaultAccount) { - return callback(defaultAccountNotSetError); - } + return new Promise((resolve, reject) => { - let node = namehash.hash(name); - - function cb(err, addr) { - if (err === NoDecodeAddrError) { - return callback(name + " is not registered", "0x"); + function resolveOrReject(err, addr) { + if (err) { + if (err === NoDecodeAddrError) { + err = `${name} is not registered`; + addr = '0x'; + } + return !callback ? reject(err) : callback(err); + } + return !callback ? resolve(addr) : callback(err, addr); } - callback(err, addr); - } - return this.ens.methods.resolver(node).call() - .then(resolverAddress => { - if (resolverAddress === voidAddress) { - return cb('Name not yet registered'); + if (!this.ens) { + resolveOrReject(providerNotSetError); + } + if (!web3.eth.defaultAccount) { + resolveOrReject(defaultAccountNotSetError); + } + + let node = namehash.hash(name); + + this.ens.methods.resolver(node).call().then(resolvedAddress => { + if (resolvedAddress === voidAddress) { + return resolveOrReject('Name not yet registered'); } let resolverContract = new EmbarkJS.Blockchain.Contract({ abi: this.resolverInterface, - address: resolverAddress, + address: resolvedAddress, web3: web3 }); - return resolverContract.methods.addr(node).call(cb); - }) - .catch(cb); + resolverContract.methods.addr(node).call(resolveOrReject); + }).catch(resolveOrReject); + }); }; __embarkENS.lookup = function (address, callback) { - callback = callback || function () {}; - if (!this.ens) { - return callback(providerNotSetError); - } - if (!web3.eth.defaultAccount) { - return callback(defaultAccountNotSetError); - } - if (address.startsWith("0x")) { - address = address.slice(2); - } - let node = web3.utils.soliditySha3(address.toLowerCase() + reverseAddrSuffix); + return new Promise((resolve, reject) => { - function cb(err, name) { - if (err === NoDecodeStringError || err === NoDecodeAddrError) { - return callback('Address does not resolve to name. Try syncing chain.'); + function resolveOrReject(err, name) { + if (err) { + if (err === NoDecodeStringError || err === NoDecodeAddrError) { + err = 'Address does not resolve to name. Try syncing chain.'; + } + return !callback ? reject(err) : callback(err); + } + return !callback ? resolve(name) : callback(err, name); } - return callback(err, name); - } - return this.ens.methods.resolver(node).call() - .then(resolverAddress => { + if (!this.ens) { + return resolveOrReject(providerNotSetError); + } + + if (!web3.eth.defaultAccount) { + return resolveOrReject(defaultAccountNotSetError); + } + + if (address.startsWith("0x")) { + address = address.slice(2); + } + + let node = web3.utils.soliditySha3(address.toLowerCase() + reverseAddrSuffix); + + this.ens.methods.resolver(node).call().then(resolverAddress => { if (resolverAddress === voidAddress) { - return cb('Address not associated to a resolver'); + return resolveOrReject('Address not associated to a resolver'); } const resolverContract = new EmbarkJS.Blockchain.Contract({ abi: this.resolverInterface, address: resolverAddress, web3: web3 }); - return resolverContract.methods.name(node).call(cb); - }) - .catch(cb); + resolverContract.methods.name(node).call(resolveOrReject); + }).catch(resolveOrReject); + }); }; __embarkENS.registerSubDomain = function (name, address, callback) {